1 // **********************************************************************
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
8 // **********************************************************************
11 using System
.Threading
;
16 /// Interface for thread notification hooks. Applications can derive
17 /// a class tat implements the start and stop
18 /// methods to intercept creation and destruction of threads created
19 /// by the Ice run time.
21 public interface ThreadNotification
24 /// The Ice run time calls start for each new
25 /// thread it creates. The call is made by the newly-started thread.
30 /// The Ice run time calls stop before it destroys
31 /// a thread. The call is made by thread that is about to be
39 /// A delegate for an action taking no parameters.
41 public delegate void VoidAction();
45 /// A delegate for the dispatcher. The dispatcher is called by the Ice
46 /// runtime to dispatch servant calls and AMI callbacks.
49 public delegate void Dispatcher(VoidAction call
, Connection con
);
51 public delegate void Dispatcher(System
.Action call
, Connection con
);
55 /// A class that encpasulates data to initalize a communicator.
57 public class InitializationData
: ICloneable
60 /// Creates and returns a copy of this object.
62 public System
.Object
Clone()
65 // A member-wise copy is safe because the members are immutable.
67 return MemberwiseClone();
71 /// The properties for the communicator.
73 public Properties properties
;
76 /// The logger for the communicator.
81 /// The Stats instance for the communicator.
86 /// The thread hook for the communicator.
88 public ThreadNotification threadHook
;
91 /// The dispatcher for the communicator.
93 public Dispatcher dispatcher
;
97 /// Utility methods for the Ice run time.
99 public sealed class Util
102 /// Creates a new empty property set.
104 /// <returns>A new empty property set.</returns>
105 public static Properties
createProperties()
107 return new PropertiesI();
111 /// Creates a property set initialized from an argument vector.
113 /// <param name="args">A command-line argument vector, possibly containing
114 /// options to set properties. If the command-line options include
115 /// a --Ice.Config option, the corresponding configuration
116 /// files are parsed. If the same property is set in a configuration
117 /// file and in the argument vector, the argument vector takes precedence.
118 /// This method modifies the argument vector by removing any Ice-related options.</param>
119 /// <returns>A property set initialized with the property settings
120 /// that were removed from args.</returns>
121 public static Properties
createProperties(ref string[] args
)
123 return new PropertiesI(ref args
, null);
127 /// Creates a property set initialized from an argument vector.
129 /// <param name="args">A command-line argument vector, possibly containing
130 /// options to set properties. If the command-line options include
131 /// a --Ice.Config option, the corresponding configuration
132 /// files are parsed. If the same property is set in a configuration
133 /// file and in the argument vector, the argument vector takes precedence.
134 /// This method modifies the argument vector by removing any Ice-related options.</param>
135 /// <param name="defaults">Default values for the property set. Settings in configuration
136 /// files and args override these defaults.</param>
137 /// <returns>A property set initialized with the property settings
138 /// that were removed from args.</returns>
139 public static Properties
createProperties(ref string[] args
, Properties defaults
)
141 return new PropertiesI(ref args
, defaults
);
145 /// Creates a communicator.
147 /// <param name="args">A command-line argument vector. Any Ice-related options
148 /// in this vector are used to intialize the communicator.
149 /// This method modifies the argument vector by removing any Ice-related options.</param>
150 /// <returns>The initialized communicator.</returns>
151 public static Communicator
initialize(ref string[] args
)
153 return initialize(ref args
, null);
157 /// Creates a communicator.
159 /// <param name="args">A command-line argument vector. Any Ice-related options
160 /// in this vector are used to intialize the communicator.
161 /// This method modifies the argument vector by removing any Ice-related options.</param>
162 /// <param name="initData">Additional intialization data. Property settings in args
163 /// override property settings in initData.</param>
164 /// <returns>The initialized communicator.</returns>
165 public static Communicator
initialize(ref string[] args
, InitializationData initData
)
169 initData
= new InitializationData();
173 initData
= (InitializationData
)initData
.Clone();
176 initData
.properties
= createProperties(ref args
, initData
.properties
);
178 CommunicatorI result
= new CommunicatorI(initData
);
179 result
.finishSetup(ref args
);
184 /// Creates a communicator.
186 /// <param name="initData">Additional intialization data.</param>
187 /// <returns>The initialized communicator.</returns>
188 public static Communicator
initialize(InitializationData initData
)
192 initData
= new InitializationData();
196 initData
= (InitializationData
)initData
.Clone();
199 CommunicatorI result
= new CommunicatorI(initData
);
200 string[] args
= new string[0];
201 result
.finishSetup(ref args
);
206 /// Creates a communicator using a default configuration.
208 public static Communicator
initialize()
210 return initialize(null);
214 /// Converts a string to an object identity.
216 /// <param name="s">The string to convert.</param>
217 /// <returns>The converted object identity.</returns>
218 public static Identity
stringToIdentity(string s
)
220 Identity ident
= new Identity();
223 // Find unescaped separator.
225 int slash
= -1, pos
= 0;
226 while((pos
= s
.IndexOf((System
.Char
) '/', pos
)) != -1)
228 if(pos
== 0 || s
[pos
- 1] != '\\')
237 // Extra unescaped slash found.
239 IdentityParseException ex
= new IdentityParseException();
240 ex
.str
= "unescaped backslash in identity `" + s
+ "'";
252 ident
.name
= IceUtilInternal
.StringUtil
.unescapeString(s
, 0, s
.Length
);
254 catch(System
.ArgumentException e
)
256 IdentityParseException ex
= new IdentityParseException();
257 ex
.str
= "invalid identity name `" + s
+ "': " + e
.Message
;
265 ident
.category
= IceUtilInternal
.StringUtil
.unescapeString(s
, 0, slash
);
267 catch(System
.ArgumentException e
)
269 IdentityParseException ex
= new IdentityParseException();
270 ex
.str
= "invalid category in identity `" + s
+ "': " + e
.Message
;
273 if(slash
+ 1 < s
.Length
)
277 ident
.name
= IceUtilInternal
.StringUtil
.unescapeString(s
, slash
+ 1, s
.Length
);
279 catch(System
.ArgumentException e
)
281 IdentityParseException ex
= new IdentityParseException();
282 ex
.str
= "invalid name in identity `" + s
+ "': " + e
.Message
;
296 /// Converts an object identity to a string.
298 /// <param name="ident">The object identity to convert.</param>
299 /// <returns>The string representation of the object identity.</returns>
300 public static string identityToString(Identity ident
)
302 if(ident
.category
== null || ident
.category
.Length
== 0)
304 return IceUtilInternal
.StringUtil
.escapeString(ident
.name
, "/");
308 return IceUtilInternal
.StringUtil
.escapeString(ident
.category
, "/") + '/' +
309 IceUtilInternal
.StringUtil
.escapeString(ident
.name
, "/");
314 /// This method is deprecated. Use System.Guid instead.
316 [Obsolete("This method is deprecated. Use System.Guid instead.")]
317 public static string generateUUID()
319 return Guid
.NewGuid().ToString().ToUpper(System
.Globalization
.CultureInfo
.InvariantCulture
);
323 /// Compares the object identities of two proxies.
325 /// <param name="lhs">A proxy.</param>
326 /// <param name="rhs">A proxy.</param>
327 /// <returns>-1 if the identity in lhs compares
328 /// less than the identity in rhs; 0 if the identities
329 /// compare equal; 1, otherwise.</returns>
331 proxyIdentityCompare(ObjectPrx lhs
, ObjectPrx rhs
)
333 if(lhs
== null && rhs
== null)
337 else if(lhs
== null && rhs
!= null)
341 else if(lhs
!= null && rhs
== null)
347 Identity lhsIdentity
= lhs
.ice_getIdentity();
348 Identity rhsIdentity
= rhs
.ice_getIdentity();
350 n
= string.CompareOrdinal(lhsIdentity
.name
, rhsIdentity
.name
);
355 return string.CompareOrdinal(lhsIdentity
.category
, rhsIdentity
.category
);
360 /// Compares the object identities and facets of two proxies.
362 /// <param name="lhs">A proxy.</param>
363 /// <param name="rhs">A proxy.</param>
364 /// <returns>-1 if the identity and facet in lhs compare
365 /// less than the identity and facet in rhs; 0 if the identities
366 /// and facets compare equal; 1, otherwise.</returns>
367 public static int proxyIdentityAndFacetCompare(ObjectPrx lhs
, ObjectPrx rhs
)
369 if(lhs
== null && rhs
== null)
373 else if(lhs
== null && rhs
!= null)
377 else if(lhs
!= null && rhs
== null)
383 Identity lhsIdentity
= lhs
.ice_getIdentity();
384 Identity rhsIdentity
= rhs
.ice_getIdentity();
386 n
= string.CompareOrdinal(lhsIdentity
.name
, rhsIdentity
.name
);
391 n
= string.CompareOrdinal(lhsIdentity
.category
, rhsIdentity
.category
);
397 string lhsFacet
= lhs
.ice_getFacet();
398 string rhsFacet
= rhs
.ice_getFacet();
399 if(lhsFacet
== null && rhsFacet
== null)
403 else if(lhsFacet
== null)
407 else if(rhsFacet
== null)
413 return string.CompareOrdinal(lhsFacet
, rhsFacet
);
419 /// Creates an input stream for dynamic invocation and dispatch.
421 /// <param name="communicator">The communicator for the stream.</param>
422 /// <param name="bytes">An encoded request or reply.</param>
423 /// <returns>The input stream.</returns>
424 public static InputStream
createInputStream(Communicator communicator
, byte[] bytes
)
426 return new InputStreamI(communicator
, bytes
);
430 /// Creates an output stream for dynamic invocation and dispatch.
432 /// <param name="communicator">The communicator for the stream.</param>
433 /// <returns>The output stream.</returns>
434 public static OutputStream
createOutputStream(Communicator communicator
)
436 return new OutputStreamI(communicator
);
440 /// Returns the process-wide logger.
442 /// <returns>The process-wide logger.</returns>
443 public static Logger
getProcessLogger()
445 lock(_processLoggerMutex
)
447 if(_processLogger
== null)
449 _processLogger
= new ConsoleLoggerI(System
.AppDomain
.CurrentDomain
.FriendlyName
);
451 return _processLogger
;
456 /// Changes the process-wide logger.
458 /// <param name="logger">The new process-wide logger.</param>
459 public static void setProcessLogger(Logger logger
)
461 lock(_processLoggerMutex
)
463 _processLogger
= logger
;
468 /// Returns the Ice version in the form A.B.C, where A indicates the
469 /// major version, B indicates the minor version, and C indicates the
472 /// <returns>The Ice version.</returns>
473 public static string stringVersion()
475 return "3.4.2"; // "A.B.C", with A=major, B=minor, C=patch
479 /// Returns the Ice version as an integer in the form A.BB.CC, where A
480 /// indicates the major version, BB indicates the minor version, and CC
481 /// indicates the patch level. For example, for Ice 3.3.1, the returned value is 30301.
483 /// <returns>The Ice version.</returns>
484 public static int intVersion()
486 return 30402; // AABBCC, with AA=major, BB=minor, CC=patch
489 private static object _processLoggerMutex
= new object();
490 private static Logger _processLogger
= null;
494 namespace IceInternal
496 public sealed class Util
498 public static Instance
getInstance(Ice
.Communicator communicator
)
500 Ice
.CommunicatorI p
= (Ice
.CommunicatorI
) communicator
;
501 return p
.getInstance();
504 public static ProtocolPluginFacade
getProtocolPluginFacade(Ice
.Communicator communicator
)
506 return new ProtocolPluginFacadeI(communicator
);
509 public static System
.Threading
.ThreadPriority
stringToThreadPriority(string s
)
511 if(String
.IsNullOrEmpty(s
))
513 return ThreadPriority
.Normal
;
515 if(s
.StartsWith("ThreadPriority.", StringComparison
.Ordinal
))
517 s
= s
.Substring("ThreadPriority.".Length
, s
.Length
);
519 if(s
.Equals("Lowest"))
521 return ThreadPriority
.Lowest
;
523 else if(s
.Equals("BelowNormal"))
525 return ThreadPriority
.BelowNormal
;
527 else if(s
.Equals("Normal"))
529 return ThreadPriority
.Normal
;
531 else if(s
.Equals("AboveNormal"))
533 return ThreadPriority
.AboveNormal
;
535 else if(s
.Equals("Highest"))
537 return ThreadPriority
.Highest
;
539 return ThreadPriority
.Normal
;