2008-01-25 Zoltan Varga <vargaz@gmail.com>
[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 = null;
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) _name = val as string;
66                         
67                                 val = properties ["priority"];
68                                 if (val != null) _priority = Convert.ToInt32 (val);
69                         }
70                 }
71
72
73                 public TcpChannel (IDictionary properties,
74                                    IClientChannelSinkProvider clientSinkProvider,
75                                    IServerChannelSinkProvider serverSinkProvider)
76                 {
77                         Init (properties, clientSinkProvider, serverSinkProvider);
78                 }
79
80                 public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
81                 {
82                         return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
83                 }
84
85                 public string ChannelName
86                 {
87                         get { return _name; }
88                 }
89
90                 public int ChannelPriority
91                 {
92                         get { return _priority; }
93                 }
94
95                 public void StartListening (object data)
96                 {
97                         if (_serverChannel != null) _serverChannel.StartListening (data);
98                 }
99                 
100                 public void StopListening (object data)
101                 {
102                         if (_serverChannel != null) _serverChannel.StopListening(data);
103                         TcpConnectionPool.Shutdown ();
104                 }
105
106                 public string[] GetUrlsForUri (string uri)
107                 {
108                         if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
109                         else return null;
110                 }
111
112                 public object ChannelData
113                 {
114                         get 
115                         {
116                                 if (_serverChannel != null) return _serverChannel.ChannelData;
117                                 else return null;
118                         }
119                 }
120
121                 public string Parse (string url, out string objectURI)
122                 {
123                         return TcpChannel.ParseChannelUrl (url, out objectURI);
124                 }
125
126                 internal static string ParseChannelUrl (string url, out string objectURI)
127                 {
128                         if (url == null) throw new ArgumentNullException ("url");
129                         
130                         int port;
131                         
132                         string host = ParseTcpURL (url, out objectURI, out port);
133                         if (host != null)
134                                 return "tcp://" + host + ":" + port;
135                         else
136                                 return null;
137                 }
138
139                 internal static string ParseTcpURL (string url, out string objectURI, out int port)
140                 {
141                         // format: "tcp://host:port/path/to/object"
142                         
143                         objectURI = null;
144                         port = 0;
145                         
146                         if (!url.StartsWith ("tcp://")) return null;
147                         int colon = url.IndexOf (':', 6);
148                         if (colon == -1) return null;
149                         string host = url.Substring (6, colon - 6);
150
151                         int slash = url.IndexOf ('/', colon + 1);
152                         if (slash == -1) slash = url.Length;
153                         string port_str = url.Substring (colon + 1, slash - colon - 1);
154                         
155                         if (slash < url.Length)
156                                 objectURI = url.Substring (slash + 1);
157
158                         try {
159                                 port = Convert.ToInt32 (port_str);
160                         } catch {
161                                 return null;
162                         }
163
164                         if (objectURI == string.Empty)
165                                 objectURI = null;
166                                 
167                         return host;
168                 }
169         }
170 }