1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 using System;
6 using System.Collections.Generic;
7 using System.Threading.Tasks;
8 using System.Threading;
9 
10 using IceInternal;
11 
12 namespace Ice
13 {
14     sealed class CommunicatorI : Communicator
15     {
destroy()16         public void destroy()
17         {
18             _instance.destroy();
19         }
20 
shutdown()21         public void shutdown()
22         {
23             try
24             {
25                 _instance.objectAdapterFactory().shutdown();
26             }
27             catch(Ice.CommunicatorDestroyedException)
28             {
29                 // Ignore
30             }
31         }
32 
waitForShutdown()33         public void waitForShutdown()
34         {
35             try
36             {
37                 _instance.objectAdapterFactory().waitForShutdown();
38             }
39             catch(Ice.CommunicatorDestroyedException)
40             {
41                 // Ignore
42             }
43         }
44 
isShutdown()45         public bool isShutdown()
46         {
47             try
48             {
49                 return _instance.objectAdapterFactory().isShutdown();
50             }
51             catch(Ice.CommunicatorDestroyedException)
52             {
53                 return true;
54             }
55         }
56 
stringToProxy(string s)57         public ObjectPrx stringToProxy(string s)
58         {
59             return _instance.proxyFactory().stringToProxy(s);
60         }
61 
proxyToString(ObjectPrx proxy)62         public string proxyToString(ObjectPrx proxy)
63         {
64             return _instance.proxyFactory().proxyToString(proxy);
65         }
66 
propertyToProxy(string s)67         public ObjectPrx propertyToProxy(string s)
68         {
69             return _instance.proxyFactory().propertyToProxy(s);
70         }
71 
proxyToProperty(ObjectPrx proxy, string prefix)72         public Dictionary<string, string> proxyToProperty(ObjectPrx proxy, string prefix)
73         {
74             return _instance.proxyFactory().proxyToProperty(proxy, prefix);
75         }
76 
stringToIdentity(string s)77         public Identity stringToIdentity(string s)
78         {
79             return Util.stringToIdentity(s);
80         }
81 
identityToString(Identity ident)82         public string identityToString(Identity ident)
83         {
84             return Util.identityToString(ident, _instance.toStringMode());
85         }
86 
createObjectAdapter(string name)87         public ObjectAdapter createObjectAdapter(string name)
88         {
89             return _instance.objectAdapterFactory().createObjectAdapter(name, null);
90         }
91 
createObjectAdapterWithEndpoints(string name, string endpoints)92         public ObjectAdapter createObjectAdapterWithEndpoints(string name, string endpoints)
93         {
94             if(name.Length == 0)
95             {
96                 name = Guid.NewGuid().ToString();
97             }
98 
99             getProperties().setProperty(name + ".Endpoints", endpoints);
100             return _instance.objectAdapterFactory().createObjectAdapter(name, null);
101         }
102 
createObjectAdapterWithRouter(string name, RouterPrx router)103         public ObjectAdapter createObjectAdapterWithRouter(string name, RouterPrx router)
104         {
105             if(name.Length == 0)
106             {
107                 name = Guid.NewGuid().ToString();
108             }
109 
110             //
111             // We set the proxy properties here, although we still use the proxy supplied.
112             //
113             Dictionary<string, string> properties = proxyToProperty(router, name + ".Router");
114             foreach(KeyValuePair<string, string> entry in properties)
115             {
116                 getProperties().setProperty(entry.Key, entry.Value);
117             }
118 
119             return _instance.objectAdapterFactory().createObjectAdapter(name, router);
120         }
121 
addObjectFactory(ObjectFactory factory, string id)122         public void addObjectFactory(ObjectFactory factory, string id)
123         {
124             _instance.addObjectFactory(factory, id);
125         }
126 
findObjectFactory(string id)127         public ObjectFactory findObjectFactory(string id)
128         {
129             return _instance.findObjectFactory(id);
130         }
131 
getValueFactoryManager()132         public ValueFactoryManager getValueFactoryManager()
133         {
134             return _instance.initializationData().valueFactoryManager;
135         }
136 
getProperties()137         public Properties getProperties()
138         {
139             return _instance.initializationData().properties;
140         }
141 
getLogger()142         public Logger getLogger()
143         {
144             return _instance.initializationData().logger;
145         }
146 
getObserver()147         public Instrumentation.CommunicatorObserver getObserver()
148         {
149             return _instance.initializationData().observer;
150         }
151 
getDefaultRouter()152         public RouterPrx getDefaultRouter()
153         {
154             return _instance.referenceFactory().getDefaultRouter();
155         }
156 
setDefaultRouter(RouterPrx router)157         public void setDefaultRouter(RouterPrx router)
158         {
159             _instance.setDefaultRouter(router);
160         }
161 
getDefaultLocator()162         public LocatorPrx getDefaultLocator()
163         {
164             return _instance.referenceFactory().getDefaultLocator();
165         }
166 
setDefaultLocator(LocatorPrx locator)167         public void setDefaultLocator(LocatorPrx locator)
168         {
169             _instance.setDefaultLocator(locator);
170         }
171 
getImplicitContext()172         public ImplicitContext getImplicitContext()
173         {
174             return _instance.getImplicitContext();
175         }
176 
getPluginManager()177         public PluginManager getPluginManager()
178         {
179             return _instance.pluginManager();
180         }
181 
flushBatchRequests(Ice.CompressBatch compressBatch)182         public void flushBatchRequests(Ice.CompressBatch compressBatch)
183         {
184             try
185             {
186                 var completed = new FlushBatchTaskCompletionCallback();
187                 var outgoing = new CommunicatorFlushBatchAsync(_instance, completed);
188                 outgoing.invoke(_flushBatchRequests_name, compressBatch, true);
189                 completed.Task.Wait();
190             }
191             catch(AggregateException ex)
192             {
193                 throw ex.InnerException;
194             }
195         }
196 
flushBatchRequestsAsync(Ice.CompressBatch compressBatch, IProgress<bool> progress = null, CancellationToken cancel = new CancellationToken())197         public Task flushBatchRequestsAsync(Ice.CompressBatch compressBatch,
198                                             IProgress<bool> progress = null,
199                                             CancellationToken cancel = new CancellationToken())
200         {
201             var completed = new FlushBatchTaskCompletionCallback(progress, cancel);
202             var outgoing = new CommunicatorFlushBatchAsync(_instance, completed);
203             outgoing.invoke(_flushBatchRequests_name, compressBatch, false);
204             return completed.Task;
205         }
206 
begin_flushBatchRequests(Ice.CompressBatch compressBatch)207         public AsyncResult begin_flushBatchRequests(Ice.CompressBatch compressBatch)
208         {
209             return begin_flushBatchRequests(compressBatch, null, null);
210         }
211 
212         private const string _flushBatchRequests_name = "flushBatchRequests";
213 
214         private class CommunicatorFlushBatchCompletionCallback : AsyncResultCompletionCallback
215         {
CommunicatorFlushBatchCompletionCallback(Communicator communicator, Instance instance, string op, object cookie, AsyncCallback callback)216             public CommunicatorFlushBatchCompletionCallback(Communicator communicator,
217                                                             Instance instance,
218                                                             string op,
219                                                             object cookie,
220                                                             AsyncCallback callback)
221                 : base(communicator, instance, op, cookie, callback)
222             {
223             }
224 
getCompletedCallback()225             protected override AsyncCallback getCompletedCallback()
226             {
227                 return (AsyncResult result) =>
228                 {
229                     try
230                     {
231                         result.throwLocalException();
232                     }
233                     catch(Exception ex)
234                     {
235                         if(exceptionCallback_ != null)
236                         {
237                             exceptionCallback_.Invoke(ex);
238                         }
239                     }
240                 };
241             }
242         };
243 
begin_flushBatchRequests(Ice.CompressBatch compressBatch, AsyncCallback cb, object cookie)244         public AsyncResult begin_flushBatchRequests(Ice.CompressBatch compressBatch, AsyncCallback cb, object cookie)
245         {
246             var result = new CommunicatorFlushBatchCompletionCallback(this,
247                                                                       _instance,
248                                                                       _flushBatchRequests_name,
249                                                                       cookie,
250                                                                       cb);
251             var outgoing = new CommunicatorFlushBatchAsync(_instance, result);
252             outgoing.invoke(_flushBatchRequests_name, compressBatch, false);
253             return result;
254         }
255 
end_flushBatchRequests(AsyncResult result)256         public void end_flushBatchRequests(AsyncResult result)
257         {
258             if(result != null && result.getCommunicator() != this)
259             {
260                 const string msg = "Communicator for call to end_" + _flushBatchRequests_name +
261                                    " does not match communicator that was used to call corresponding begin_" +
262                                    _flushBatchRequests_name + " method";
263                 throw new ArgumentException(msg);
264             }
265             AsyncResultI.check(result, _flushBatchRequests_name).wait();
266         }
267 
createAdmin(ObjectAdapter adminAdapter, Identity adminIdentity)268         public ObjectPrx createAdmin(ObjectAdapter adminAdapter, Identity adminIdentity)
269         {
270             return _instance.createAdmin(adminAdapter, adminIdentity);
271         }
272 
getAdmin()273         public ObjectPrx getAdmin()
274         {
275             return _instance.getAdmin();
276         }
277 
addAdminFacet(Object servant, string facet)278         public void addAdminFacet(Object servant, string facet)
279         {
280             _instance.addAdminFacet(servant, facet);
281         }
282 
removeAdminFacet(string facet)283         public Object removeAdminFacet(string facet)
284         {
285             return _instance.removeAdminFacet(facet);
286         }
287 
findAdminFacet(string facet)288         public Object findAdminFacet(string facet)
289         {
290             return _instance.findAdminFacet(facet);
291         }
292 
findAllAdminFacets()293         public Dictionary<string, Object> findAllAdminFacets()
294         {
295             return _instance.findAllAdminFacets();
296         }
297 
Dispose()298         public void Dispose()
299         {
300             destroy();
301         }
302 
CommunicatorI(InitializationData initData)303         internal CommunicatorI(InitializationData initData)
304         {
305             _instance = new Instance(this, initData);
306         }
307 
308         /*
309         ~CommunicatorI()
310         {
311             if(!destroyed_)
312             {
313                 if(!System.Environment.HasShutdownStarted)
314                 {
315                     _instance.initializationData().logger.warning(
316                             "Ice::Communicator::destroy() has not been called");
317                 }
318                 else
319                 {
320                     System.Console.Error.WriteLine("Ice::Communicator::destroy() has not been called");
321                 }
322             }
323         }
324         */
325 
326         //
327         // Certain initialization tasks need to be completed after the
328         // constructor.
329         //
finishSetup(ref string[] args)330         internal void finishSetup(ref string[] args)
331         {
332             try
333             {
334                 _instance.finishSetup(ref args, this);
335             }
336             catch(System.Exception)
337             {
338                 _instance.destroy();
339                 throw;
340             }
341         }
342 
343         //
344         // For use by Util.getInstance()
345         //
getInstance()346         internal Instance getInstance()
347         {
348             return _instance;
349         }
350 
351         private Instance _instance;
352     }
353 }
354