Merge pull request #2323 from esdrubal/servicemodel
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Tcp / TcpChannel.cs
1 //
2 // System.Runtime.Remoting.Channels.Tcp.TcpChannel.cs
3 //
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Runtime.Remoting.Messaging;
33 using System.Text.RegularExpressions;
34
35 namespace System.Runtime.Remoting.Channels.Tcp
36 {
37         public class TcpChannel : IChannelReceiver, IChannel, IChannelSender
38         {
39                 private TcpClientChannel _clientChannel;
40                 private TcpServerChannel _serverChannel;
41                 private string _name = "tcp";
42                 private int _priority = 1;
43         
44                 public TcpChannel ()
45                 {
46                         Init (new Hashtable(), null, null);
47                 }
48
49                 public TcpChannel (int port)
50                 {
51                         Hashtable ht = new Hashtable();
52                         ht ["port"] = port.ToString();
53                         Init (ht, null, null);
54                 }
55
56                 void Init (IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)
57                 {
58                         _clientChannel = new TcpClientChannel (properties,clientSink);
59
60                         if (properties != null) {
61                                 if(properties["port"] != null)
62                                         _serverChannel = new TcpServerChannel(properties, serverSink);
63
64                                 object val = properties ["name"];
65                                 if (val != null)
66                                         _name = val as string;
67                         
68                                 val = properties ["priority"];
69                                 if (val != null)
70                                         _priority = Convert.ToInt32 (val);
71                         }
72                 }
73
74
75                 public TcpChannel (IDictionary properties,
76                                    IClientChannelSinkProvider clientSinkProvider,
77                                    IServerChannelSinkProvider serverSinkProvider)
78                 {
79                         Init (properties, clientSinkProvider, serverSinkProvider);
80                 }
81
82                 public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
83                 {
84                         return _clientChannel.CreateMessageSink(url,
85                                 remoteChannelData, out objectURI);
86                 }
87
88                 public string ChannelName {
89                         get { return _name; }
90                 }
91
92                 public int ChannelPriority {
93                         get { return _priority; }
94                 }
95
96                 public void StartListening (object data)
97                 {
98                         if (_serverChannel != null)
99                                 _serverChannel.StartListening (data);
100                 }
101                 
102                 public void StopListening (object data)
103                 {
104                         if (_serverChannel != null)
105                                 _serverChannel.StopListening(data);
106                         TcpConnectionPool.Shutdown ();
107                 }
108
109                 public string [] GetUrlsForUri (string objectURI)
110                 {
111                         if (_serverChannel != null)
112                                 return _serverChannel.GetUrlsForUri (objectURI);
113                         return null;
114                 }
115
116                 public object ChannelData {
117                         get {
118                                 if (_serverChannel != null)
119                                         return _serverChannel.ChannelData;
120                                 return null;
121                         }
122                 }
123
124                 public string Parse (string url, out string objectURI)
125                 {
126                         return TcpChannel.ParseChannelUrl (url, out objectURI);
127                 }
128
129                 internal static string ParseChannelUrl (string url, out string objectURI)
130                 {
131                         if (url == null)
132                                 throw new ArgumentNullException ("url");
133                         
134                         string host, port;
135                         
136                         return ParseTcpURL (url, out host, out port, out objectURI);
137                 }
138
139                 internal static string ParseTcpURL (string url, out string host, out string port, out string objectURI)
140                 {
141                         // format: "tcp://host:port/path/to/object"
142                         objectURI = null;
143                         host = null;
144                         port = null;
145                         
146                         // url needs to be at least "tcp:"
147                         if (url.Length < 4 || url[3] != ':' ||
148                             (url[0] != 'T' && url[0] != 't') ||
149                             (url[1] != 'C' && url[1] != 'c') ||
150                             (url[2] != 'P' && url[2] != 'p'))
151                                 return null;
152                         
153                         // "tcp:" is acceptable
154                         if (url.Length == 4)
155                                 return url;
156                         
157                         // must be of the form "tcp://"
158                         if (url.Length <= 5 || url[4] != '/' || url[5] != '/')
159                                 return null;
160                         
161                         // "tcp://" is acceptable
162                         if (url.Length == 6)
163                                 return url;
164                         
165                         int i;
166                         for (i = 6; i < url.Length; i++) {
167                                 if (url[i] == ':' || url[i] == '/')
168                                         break;
169                         }
170                         
171                         host = url.Substring (6, i - 6);
172                         
173                         if (i + 1 < url.Length && url[i] == ':') {
174                                 int start = i + 1;
175                                 
176                                 for (i++; i < url.Length; i++) {
177                                         if (url[i] == '/')
178                                                 break;
179                                 }
180                                 
181                                 if (i > start)
182                                         port = url.Substring (start, i - start);
183                         }
184                         
185                         if (i >= url.Length || url[i] != '/')
186                                 return url;
187                         
188                         objectURI = url.Substring (i);
189                         
190                         return url.Substring (0, i);
191                 }
192         }
193 }