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