no message
[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 (lsg@ctv.es)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 using System.Collections;
11 using System.Runtime.Remoting.Messaging;
12 using System.Text.RegularExpressions;
13
14 namespace System.Runtime.Remoting.Channels.Tcp
15 {
16         public class TcpChannel : IChannelReceiver, IChannel, IChannelSender
17         {
18                 private TcpClientChannel _clientChannel;\r
19                 private TcpServerChannel _serverChannel = null;\r
20                 private string _name;\r
21         
22                 public TcpChannel (): this (0)
23         {
24                 }
25
26                 public TcpChannel (int port)
27                 {
28                         Hashtable ht = new Hashtable();\r
29                         ht["port"] = port.ToString();\r
30                         Init(ht, null, null);\r
31                 }
32
33                 public void Init(IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)\r
34                 {\r
35                         _clientChannel = new TcpClientChannel(properties,clientSink);\r
36 \r
37                         string port = properties["port"] as string;\r
38                         if (port != null && port != string.Empty)\r
39                         {\r
40                                 _serverChannel = new TcpServerChannel(properties, serverSink);\r
41                         }\r
42 \r
43                         _name = properties["name"] as string;\r
44                 }\r
45 \r
46
47                 public TcpChannel (IDictionary properties,
48                                    IClientChannelSinkProvider clientSinkProvider,
49                                    IServerChannelSinkProvider serverSinkProvider)
50                 {
51                         Init (properties, clientSinkProvider, serverSinkProvider);
52                 }
53
54                 public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)\r
55                 {\r
56                         return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);\r
57                 }\r
58 \r
59                 public string ChannelName\r
60                 {\r
61                         get { return _name; }\r
62                 }\r
63 \r
64                 public int ChannelPriority\r
65                 {\r
66                         get { return 1; }\r
67                 }\r
68 \r
69                 public void StartListening (object data)\r
70                 {\r
71                         if (_serverChannel != null) _serverChannel.StartListening(data);\r
72                 }\r
73                 \r
74                 public void StopListening (object data)\r
75                 {\r
76                         if (_serverChannel != null) _serverChannel.StopListening(data);\r
77                 }\r
78 \r
79                 public string[] GetUrlsForUri (string uri)\r
80                 {\r
81                         if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);\r
82                         else return null;\r
83                 }\r
84 \r
85                 public object ChannelData\r
86                 {\r
87                         get \r
88                         {\r
89                                 if (_serverChannel != null) return _serverChannel.ChannelData;\r
90                                 else return null;\r
91                         }\r
92                 }\r
93
94                 public string Parse (string url, out string objectURI)
95                 {
96                         return TcpChannel.ParseChannelUrl (url, out objectURI);
97                 }
98
99                 internal static string ParseChannelUrl (string url, out string objectURI)
100                 {
101                         int port;
102                         
103                         string host = ParseTcpURL (url, out objectURI, out port);
104                         if (host != null)
105                                 return "tcp://" + host + ":" + port;
106                         else
107                                 return null;
108                 }
109
110                 internal static string ParseTcpURL (string url, out string objectURI, out int port)
111                 {
112                         // format: "tcp://host:port/path/to/object"
113                         
114                         objectURI = null;
115                         port = 0;
116                         
117                         Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)[/](.*)");
118
119                         if (!m.Success)
120                                 return null;
121                         
122                         string host = m.Groups[1].Value;
123                         string port_str = m.Groups[2].Value;
124                         objectURI = m.Groups[3].Value;
125                         port = Convert.ToInt32 (port_str);
126                                 
127                         return host;
128                 }
129         }
130 }