d64ddb5cf70d8ace757d480fa9e42445804d0e60
[mono.git] / mcs / class / RabbitMQ.Client / src / client / api / AmqpTcpEndpoint.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007, 2008 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
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
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
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 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
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
30 //
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
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd.,
39 //   Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
40 //
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
44 //   Technologies Ltd.;
45 //
46 //   All Rights Reserved.
47 //
48 //   Contributor(s): ______________________________________.
49 //
50 //---------------------------------------------------------------------------
51 using System;
52 using System.Collections;
53 using RabbitMQ.Client.Impl;
54
55 namespace RabbitMQ.Client
56 {
57     ///<summary>Represents a TCP-addressable AMQP peer, including the
58     ///protocol variant to use, and a host name and port
59     ///number.</summary>
60     public class AmqpTcpEndpoint
61     {
62         private IProtocol m_protocol;
63         ///<summary>Retrieve or set the IProtocol of this AmqpTcpEndpoint.</summary>
64         public IProtocol Protocol
65         {
66             get { return m_protocol; }
67             set { m_protocol = value; }
68         }
69
70         private string m_hostName;
71         ///<summary>Retrieve or set the hostname of this AmqpTcpEndpoint.</summary>
72         public string HostName
73         {
74             get { return m_hostName; }
75             set { m_hostName = value; }
76         }
77
78         private int m_port;
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>
82         public int Port
83         {
84             get { return (m_port == -1) ? m_protocol.DefaultPort : m_port; }
85             set { m_port = value; }
86         }
87
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
91         ///used.</summary>
92         public AmqpTcpEndpoint(IProtocol protocolVariant, string hostname, int portOrMinusOne)
93         {
94             m_protocol = protocolVariant;
95             m_hostName = hostname;
96             m_port = portOrMinusOne;
97         }
98
99         ///<summary>Returns a URI-like string of the form
100         ///amqp-PROTOCOL://HOSTNAME:PORTNUMBER</summary>
101         ///<remarks>
102         /// This method is intended mainly for debugging and logging use.
103         ///</remarks>
104         public override string ToString()
105         {
106             return "amqp-" + Protocol + "://" + HostName + ":" + Port;
107         }
108
109         ///<summary>Compares this instance by value (protocol,
110         ///hostname, port) against another instance</summary>
111         public override bool Equals(object obj)
112         {
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;
118             return true;
119         }
120
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()
125         {
126             return
127                 Protocol.GetHashCode() ^
128                 HostName.GetHashCode() ^
129                 Port;
130         }
131
132         ///<summary>Construct an instance from a protocol and an
133         ///address in "hostname:port" format.</summary>
134         ///<remarks>
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).
140         ///</remarks>
141         public static AmqpTcpEndpoint Parse(IProtocol protocol, string address) {
142             int index = address.IndexOf(':');
143             if (index == -1) {
144                 return new AmqpTcpEndpoint(protocol, address, -1);
145             } else {
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),
150                                            portNum);
151             }
152         }
153
154         ///<summary>Splits the passed-in string on ",", and passes the
155         ///substrings to AmqpTcpEndpoint.Parse()</summary>
156         ///<remarks>
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.
161         ///</remarks>
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));
169                 }
170             }
171             return (AmqpTcpEndpoint[]) results.ToArray(typeof(AmqpTcpEndpoint));
172         }
173     }
174 }