1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007, 2008 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd.,
39 // Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
41 // Portions created by LShift Ltd., Cohesive Financial Technologies
42 // LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007, 2008
43 // LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
46 // All Rights Reserved.
48 // Contributor(s): ______________________________________.
50 //---------------------------------------------------------------------------
52 using System.Collections;
53 using RabbitMQ.Client.Impl;
55 namespace RabbitMQ.Client
57 ///<summary>Represents a TCP-addressable AMQP peer, including the
58 ///protocol variant to use, and a host name and port
60 public class AmqpTcpEndpoint
62 private IProtocol m_protocol;
63 ///<summary>Retrieve or set the IProtocol of this AmqpTcpEndpoint.</summary>
64 public IProtocol Protocol
66 get { return m_protocol; }
67 set { m_protocol = value; }
70 private string m_hostName;
71 ///<summary>Retrieve or set the hostname of this AmqpTcpEndpoint.</summary>
72 public string HostName
74 get { return m_hostName; }
75 set { m_hostName = value; }
79 ///<summary>Retrieve or set the port number of this
80 ///AmqpTcpEndpoint. A port number of -1 causes the default
81 ///port number for the IProtocol to be used.</summary>
84 get { return (m_port == -1) ? m_protocol.DefaultPort : m_port; }
85 set { m_port = value; }
88 ///<summary>Construct an AmqpTcpEndpoint with the given
89 ///IProtocol, hostname, and port number. If the port number is
90 ///-1, the default port number for the IProtocol will be
92 public AmqpTcpEndpoint(IProtocol protocolVariant, string hostname, int portOrMinusOne)
94 m_protocol = protocolVariant;
95 m_hostName = hostname;
96 m_port = portOrMinusOne;
99 ///<summary>Returns a URI-like string of the form
100 ///amqp-PROTOCOL://HOSTNAME:PORTNUMBER</summary>
102 /// This method is intended mainly for debugging and logging use.
104 public override string ToString()
106 return "amqp-" + Protocol + "://" + HostName + ":" + Port;
109 ///<summary>Compares this instance by value (protocol,
110 ///hostname, port) against another instance</summary>
111 public override bool Equals(object obj)
113 AmqpTcpEndpoint other = obj as AmqpTcpEndpoint;
114 if (other == null) return false;
115 if (other.Protocol != Protocol) return false;
116 if (other.HostName != HostName) return false;
117 if (other.Port != Port) return false;
121 ///<summary>Implementation of hash code depending on protocol,
122 ///hostname and port, to line up with the implementation of
123 ///Equals()</summary>
124 public override int GetHashCode()
127 Protocol.GetHashCode() ^
128 HostName.GetHashCode() ^
132 ///<summary>Construct an instance from a protocol and an
133 ///address in "hostname:port" format.</summary>
135 /// If the address string passed in contains ":", it is split
136 /// into a hostname and a port-number part. Otherwise, the
137 /// entire string is used as the hostname, and the port-number
138 /// is set to -1 (meaning the default number for the protocol
139 /// variant specified).
141 public static AmqpTcpEndpoint Parse(IProtocol protocol, string address) {
142 int index = address.IndexOf(':');
144 return new AmqpTcpEndpoint(protocol, address, -1);
146 string portStr = address.Substring(index + 1).Trim();
147 int portNum = (portStr.Length == 0) ? -1 : int.Parse(portStr);
148 return new AmqpTcpEndpoint(protocol,
149 address.Substring(0, index),
154 ///<summary>Splits the passed-in string on ",", and passes the
155 ///substrings to AmqpTcpEndpoint.Parse()</summary>
157 /// Accepts a string of the form "hostname:port,
158 /// hostname:port, ...", where the ":port" pieces are
159 /// optional, and returns a corresponding array of
160 /// AmqpTcpEndpoints.
162 public static AmqpTcpEndpoint[] ParseMultiple(IProtocol protocol, string addresses) {
163 string[] partsArr = addresses.Split(new char[] { ',' });
164 ArrayList results = new ArrayList();
165 foreach (string partRaw in partsArr) {
166 string part = partRaw.Trim();
167 if (part.Length > 0) {
168 results.Add(Parse(protocol, part));
171 return (AmqpTcpEndpoint[]) results.ToArray(typeof(AmqpTcpEndpoint));