**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.CORBA / CORBAServerChannel.cs
blobd8561ea1252188f38be5bb56c2f2b0796a9b3759
1 //
2 // System.Runtime.Remoting.Channels.CORBA.CORBAServerChannel.cs
3 //
4 // Author: Dietmar Maurer (dietmar@ximian.com)
5 //
6 // 2002 (C) Copyright, Ximian, Inc.
7 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Runtime.Remoting.Messaging;
32 using System.Text.RegularExpressions;
33 using System.Net.Sockets;
34 using System.Net;
35 using System.Threading;
36 using System.IO;
38 namespace System.Runtime.Remoting.Channels.CORBA
40 public class CORBAServerChannel : IChannelReceiver, IChannel
42 int port = 0;
43 string name = "simple";
44 string host;
45 int priority = 1;
46 Thread server_thread = null;
47 TcpListener listener;
48 CORBAServerTransportSink sink;
49 ChannelDataStore channel_data;
51 void Init (IServerChannelSinkProvider provider) {
52 if (provider == null) {
53 provider = new CORBAServerFormatterSinkProvider ();
56 IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (provider, this);
58 host = Dns.GetHostByName(Dns.GetHostName()).HostName;
60 string [] uris = null;
62 if (port != 0) {
63 uris = new String [1];
64 uris [0] = GetChannelUri ();
67 channel_data = new ChannelDataStore (uris);;
69 sink = new CORBAServerTransportSink (next_sink);
71 listener = new TcpListener (port);
72 StartListening (null);
75 public CORBAServerChannel (int port)
77 this.port = port;
78 Init (null);
81 public CORBAServerChannel (IDictionary properties,
82 IServerChannelSinkProvider serverSinkProvider)
84 port = (int)properties ["port"];
85 Init (serverSinkProvider);
88 public CORBAServerChannel (string name, int port,
89 IServerChannelSinkProvider serverSinkProvider)
91 name = name;
92 this.port = port;
93 Init (serverSinkProvider);
96 public CORBAServerChannel (string name, int port)
98 name = name;
99 this.port = port;
100 Init (null);
103 public object ChannelData
105 get {
106 return channel_data;
110 public string ChannelName
112 get {
113 return name;
117 public int ChannelPriority
119 get {
120 return priority;
124 string GetChannelUri ()
126 return "corba://" + host + ":" + port;
129 public string[] GetUrlsForUri (string uri)
131 string [] chnl_uris = channel_data.ChannelUris;
133 if (uri.IndexOf ('/') != 0)
134 uri = "/" + uri;
136 string [] result = new String [chnl_uris.Length];
138 for (int i = 0; i < chnl_uris.Length; i++) {
139 result [i] = chnl_uris [i] + uri;
142 return result;
145 public string Parse (string url, out string objectURI)
147 int port;
149 string host = CORBAChannel.ParseCORBAURL (url, out objectURI, out port);
151 return GetChannelUri ();
154 void WaitForConnections ()
156 while (true) {
157 TcpClient client = listener.AcceptTcpClient ();
159 sink.InternalProcessMessage (client.GetStream ());
161 client.Close ();
165 public void StartListening (object data)
167 if (server_thread == null) {
168 listener.Start ();
169 if (port == 0) {
170 port = ((IPEndPoint)listener.LocalEndpoint).Port;
171 channel_data.ChannelUris = new String [1];
172 channel_data.ChannelUris [0] = GetChannelUri ();
175 server_thread = new Thread (new ThreadStart (WaitForConnections));
176 server_thread.Start ();
180 public void StopListening (object data)
182 if (server_thread != null) {
183 server_thread.Abort ();
184 server_thread = null;
185 listener.Stop ();