1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 namespace IceInternal
6 {
7     using System;
8     using System.Diagnostics;
9     using System.Collections.Generic;
10 
11     sealed class WSEndpoint : EndpointI
12     {
WSEndpoint(ProtocolInstance instance, EndpointI del, string res)13         internal WSEndpoint(ProtocolInstance instance, EndpointI del, string res)
14         {
15             _instance = instance;
16             _delegate = del;
17             _resource = res;
18         }
19 
WSEndpoint(ProtocolInstance instance, EndpointI del, List<string> args)20         internal WSEndpoint(ProtocolInstance instance, EndpointI del, List<string> args)
21         {
22             _instance = instance;
23             _delegate = del;
24 
25             initWithOptions(args);
26 
27             if(_resource == null)
28             {
29                 _resource = "/";
30             }
31         }
32 
WSEndpoint(ProtocolInstance instance, EndpointI del, Ice.InputStream s)33         internal WSEndpoint(ProtocolInstance instance, EndpointI del, Ice.InputStream s)
34         {
35             _instance = instance;
36             _delegate = del;
37 
38             _resource = s.readString();
39         }
40 
41         private sealed class InfoI : Ice.WSEndpointInfo
42         {
InfoI(EndpointI e)43             public InfoI(EndpointI e)
44             {
45                 _endpoint = e;
46             }
47 
type()48             override public short type()
49             {
50                 return _endpoint.type();
51             }
52 
datagram()53             override public bool datagram()
54             {
55                 return _endpoint.datagram();
56             }
57 
secure()58             override public bool secure()
59             {
60                 return _endpoint.secure();
61             }
62 
63             private EndpointI _endpoint;
64         }
65 
getInfo()66         public override Ice.EndpointInfo getInfo()
67         {
68             Ice.WSEndpointInfo info = new InfoI(this);
69             info.underlying = _delegate.getInfo();
70             info.resource = _resource;
71             info.compress = info.underlying.compress;
72             info.timeout = info.underlying.timeout;
73             return info;
74         }
75 
type()76         public override short type()
77         {
78             return _delegate.type();
79         }
80 
protocol()81         public override string protocol()
82         {
83             return _delegate.protocol();
84         }
85 
streamWriteImpl(Ice.OutputStream s)86         public override void streamWriteImpl(Ice.OutputStream s)
87         {
88             _delegate.streamWriteImpl(s);
89             s.writeString(_resource);
90         }
91 
timeout()92         public override int timeout()
93         {
94             return _delegate.timeout();
95         }
96 
timeout(int timeout)97         public override EndpointI timeout(int timeout)
98         {
99             if(timeout == _delegate.timeout())
100             {
101                 return this;
102             }
103             else
104             {
105                 return new WSEndpoint(_instance, _delegate.timeout(timeout), _resource);
106             }
107         }
108 
connectionId()109         public override string connectionId()
110         {
111             return _delegate.connectionId();
112         }
113 
connectionId(string connectionId)114         public override EndpointI connectionId(string connectionId)
115         {
116             if(connectionId.Equals(_delegate.connectionId()))
117             {
118                 return this;
119             }
120             else
121             {
122                 return new WSEndpoint(_instance, _delegate.connectionId(connectionId), _resource);
123             }
124         }
125 
compress()126         public override bool compress()
127         {
128             return _delegate.compress();
129         }
130 
compress(bool compress)131         public override EndpointI compress(bool compress)
132         {
133             if(compress == _delegate.compress())
134             {
135                 return this;
136             }
137             else
138             {
139                 return new WSEndpoint(_instance, _delegate.compress(compress), _resource);
140             }
141         }
142 
datagram()143         public override bool datagram()
144         {
145             return _delegate.datagram();
146         }
147 
secure()148         public override bool secure()
149         {
150             return _delegate.secure();
151         }
152 
transceiver()153         public override Transceiver transceiver()
154         {
155             return null;
156         }
157 
158         private sealed class EndpointI_connectorsI : EndpointI_connectors
159         {
EndpointI_connectorsI(ProtocolInstance instance, string host, string res, EndpointI_connectors cb)160             public EndpointI_connectorsI(ProtocolInstance instance, string host, string res, EndpointI_connectors cb)
161             {
162                 _instance = instance;
163                 _host = host;
164                 _resource = res;
165                 _callback = cb;
166             }
167 
connectors(List<Connector> connectors)168             public void connectors(List<Connector> connectors)
169             {
170                 List<Connector> l = new List<Connector>();
171                 foreach(Connector c in connectors)
172                 {
173                     l.Add(new WSConnector(_instance, c, _host, _resource));
174                 }
175                 _callback.connectors(l);
176             }
177 
exception(Ice.LocalException ex)178             public void exception(Ice.LocalException ex)
179             {
180                 _callback.exception(ex);
181             }
182 
183             private ProtocolInstance _instance;
184             private string _host;
185             private string _resource;
186             private EndpointI_connectors _callback;
187         }
188 
connectors_async(Ice.EndpointSelectionType selType, EndpointI_connectors callback)189         public override void connectors_async(Ice.EndpointSelectionType selType, EndpointI_connectors callback)
190         {
191             string host = "";
192             for(Ice.EndpointInfo p = _delegate.getInfo(); p != null; p = p.underlying)
193             {
194                 if(p is Ice.IPEndpointInfo)
195                 {
196                     Ice.IPEndpointInfo ipInfo = (Ice.IPEndpointInfo)p;
197                     host = ipInfo.host + ":" + ipInfo.port;
198                 }
199             }
200             _delegate.connectors_async(selType, new EndpointI_connectorsI(_instance, host, _resource, callback));
201         }
202 
acceptor(string adapterName)203         public override Acceptor acceptor(string adapterName)
204         {
205             return new WSAcceptor(this, _instance, _delegate.acceptor(adapterName));
206         }
207 
endpoint(EndpointI delEndp)208         public WSEndpoint endpoint(EndpointI delEndp)
209         {
210             if(delEndp == _delegate)
211             {
212                 return this;
213             }
214             else
215             {
216                 return new WSEndpoint(_instance, delEndp, _resource);
217             }
218         }
219 
expandIfWildcard()220         public override List<EndpointI> expandIfWildcard()
221         {
222             List<EndpointI> l = new List<EndpointI>();
223             foreach(EndpointI e in _delegate.expandIfWildcard())
224             {
225                 l.Add(e == _delegate ? this : new WSEndpoint(_instance, e, _resource));
226             }
227             return l;
228         }
229 
expandHost(out EndpointI publish)230         public override List<EndpointI> expandHost(out EndpointI publish)
231         {
232             List<EndpointI> l = new List<EndpointI>();
233             foreach(EndpointI e in _delegate.expandHost(out publish))
234             {
235                 l.Add(e == _delegate ? this : new WSEndpoint(_instance, e, _resource));
236             }
237             if(publish != null)
238             {
239                 publish = publish == _delegate ? this : new WSEndpoint(_instance, publish, _resource);
240             }
241             return l;
242         }
243 
equivalent(EndpointI endpoint)244         public override bool equivalent(EndpointI endpoint)
245         {
246             if(!(endpoint is WSEndpoint))
247             {
248                 return false;
249             }
250             WSEndpoint wsEndpointI = (WSEndpoint)endpoint;
251             return _delegate.equivalent(wsEndpointI._delegate);
252         }
253 
options()254         public override string options()
255         {
256             //
257             // WARNING: Certain features, such as proxy validation in Glacier2,
258             // depend on the format of proxy strings. Changes to toString() and
259             // methods called to generate parts of the reference string could break
260             // these features. Please review for all features that depend on the
261             // format of proxyToString() before changing this and related code.
262             //
263             string s = _delegate.options();
264 
265             if(_resource != null && _resource.Length > 0)
266             {
267                 s += " -r ";
268                 bool addQuote = _resource.IndexOf(':') != -1;
269                 if(addQuote)
270                 {
271                     s += "\"";
272                 }
273                 s += _resource;
274                 if(addQuote)
275                 {
276                     s += "\"";
277                 }
278             }
279 
280             return s;
281         }
282 
GetHashCode()283         public override int GetHashCode()
284         {
285             int h = _delegate.GetHashCode();
286             HashUtil.hashAdd(ref h, _resource);
287             return h;
288         }
289 
CompareTo(EndpointI obj)290         public override int CompareTo(EndpointI obj)
291         {
292             if(!(obj is WSEndpoint))
293             {
294                 return type() < obj.type() ? -1 : 1;
295             }
296 
297             WSEndpoint p = (WSEndpoint)obj;
298             if(this == p)
299             {
300                 return 0;
301             }
302 
303             int v = string.Compare(_resource, p._resource, StringComparison.Ordinal);
304             if(v != 0)
305             {
306                 return v;
307             }
308 
309             return _delegate.CompareTo(p._delegate);
310         }
311 
checkOption(string option, string argument, string endpoint)312         protected override bool checkOption(string option, string argument, string endpoint)
313         {
314             switch(option[1])
315             {
316             case 'r':
317             {
318                 if(argument == null)
319                 {
320                     Ice.EndpointParseException e = new Ice.EndpointParseException();
321                     e.str = "no argument provided for -r option in endpoint " + endpoint + _delegate.options();
322                     throw e;
323                 }
324                 _resource = argument;
325                 return true;
326             }
327 
328             default:
329             {
330                 return false;
331             }
332             }
333         }
334 
335         private ProtocolInstance _instance;
336         private EndpointI _delegate;
337         private string _resource;
338     }
339 
340     public class WSEndpointFactory : EndpointFactoryWithUnderlying
341     {
WSEndpointFactory(ProtocolInstance instance, short type)342         public WSEndpointFactory(ProtocolInstance instance, short type) : base(instance, type)
343         {
344         }
345 
cloneWithUnderlying(ProtocolInstance instance, short underlying)346         override public EndpointFactory cloneWithUnderlying(ProtocolInstance instance, short underlying)
347         {
348             return new WSEndpointFactory(instance, underlying);
349         }
350 
createWithUnderlying(EndpointI underlying, List<string> args, bool oaEndpoint)351         override protected EndpointI createWithUnderlying(EndpointI underlying, List<string> args, bool oaEndpoint)
352         {
353             return new WSEndpoint(instance_, underlying, args);
354         }
355 
readWithUnderlying(EndpointI underlying, Ice.InputStream s)356         override protected EndpointI readWithUnderlying(EndpointI underlying, Ice.InputStream s)
357         {
358             return new WSEndpoint(instance_, underlying, s);
359         }
360     }
361 }
362