ICE 3.4.2
[php5-ice-freebsdport.git] / cs / src / Ice / Util.cs
blob4e8e7588366224786d4a53d266df48368f461b45
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 using System;
11 using System.Threading;
13 namespace Ice
15 /// <summary>
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.
20 /// </summary>
21 public interface ThreadNotification
23 /// <summary>
24 /// The Ice run time calls start for each new
25 /// thread it creates. The call is made by the newly-started thread.
26 /// </summary>
27 void start();
29 /// <summary>
30 /// The Ice run time calls stop before it destroys
31 /// a thread. The call is made by thread that is about to be
32 /// destroyed.
33 /// </summary>
34 void stop();
37 #if COMPACT
38 /// <summary>
39 /// A delegate for an action taking no parameters.
40 /// </summary>
41 public delegate void VoidAction();
42 #endif
44 /// <summary>
45 /// A delegate for the dispatcher. The dispatcher is called by the Ice
46 /// runtime to dispatch servant calls and AMI callbacks.
47 /// </summary>
48 #if COMPACT
49 public delegate void Dispatcher(VoidAction call, Connection con);
50 #else
51 public delegate void Dispatcher(System.Action call, Connection con);
52 #endif
54 /// <summary>
55 /// A class that encpasulates data to initalize a communicator.
56 /// </summary>
57 public class InitializationData : ICloneable
59 /// <summary>
60 /// Creates and returns a copy of this object.
61 /// </summary>
62 public System.Object Clone()
65 // A member-wise copy is safe because the members are immutable.
67 return MemberwiseClone();
70 /// <summary>
71 /// The properties for the communicator.
72 /// </summary>
73 public Properties properties;
75 /// <summary>
76 /// The logger for the communicator.
77 /// </summary>
78 public Logger logger;
80 /// <summary>
81 /// The Stats instance for the communicator.
82 /// </summary>
83 public Stats stats;
85 /// <summary>
86 /// The thread hook for the communicator.
87 /// </summary>
88 public ThreadNotification threadHook;
90 /// <summary>
91 /// The dispatcher for the communicator.
92 /// </summary>
93 public Dispatcher dispatcher;
96 /// <summary>
97 /// Utility methods for the Ice run time.
98 /// </summary>
99 public sealed class Util
101 /// <summary>
102 /// Creates a new empty property set.
103 /// </summary>
104 /// <returns>A new empty property set.</returns>
105 public static Properties createProperties()
107 return new PropertiesI();
110 /// <summary>
111 /// Creates a property set initialized from an argument vector.
112 /// </summary>
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);
126 /// <summary>
127 /// Creates a property set initialized from an argument vector.
128 /// </summary>
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);
144 /// <summary>
145 /// Creates a communicator.
146 /// </summary>
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);
156 /// <summary>
157 /// Creates a communicator.
158 /// </summary>
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)
167 if(initData == null)
169 initData = new InitializationData();
171 else
173 initData = (InitializationData)initData.Clone();
176 initData.properties = createProperties(ref args, initData.properties);
178 CommunicatorI result = new CommunicatorI(initData);
179 result.finishSetup(ref args);
180 return result;
183 /// <summary>
184 /// Creates a communicator.
185 /// </summary>
186 /// <param name="initData">Additional intialization data.</param>
187 /// <returns>The initialized communicator.</returns>
188 public static Communicator initialize(InitializationData initData)
190 if(initData == null)
192 initData = new InitializationData();
194 else
196 initData = (InitializationData)initData.Clone();
199 CommunicatorI result = new CommunicatorI(initData);
200 string[] args = new string[0];
201 result.finishSetup(ref args);
202 return result;
205 /// <summary>
206 /// Creates a communicator using a default configuration.
207 /// </summary>
208 public static Communicator initialize()
210 return initialize(null);
213 /// <summary>
214 /// Converts a string to an object identity.
215 /// </summary>
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] != '\\')
230 if(slash == -1)
232 slash = pos;
234 else
237 // Extra unescaped slash found.
239 IdentityParseException ex = new IdentityParseException();
240 ex.str = "unescaped backslash in identity `" + s + "'";
241 throw ex;
244 pos++;
247 if(slash == -1)
249 ident.category = "";
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;
258 throw ex;
261 else
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;
271 throw ex;
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;
283 throw ex;
286 else
288 ident.name = "";
292 return ident;
295 /// <summary>
296 /// Converts an object identity to a string.
297 /// </summary>
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, "/");
306 else
308 return IceUtilInternal.StringUtil.escapeString(ident.category, "/") + '/' +
309 IceUtilInternal.StringUtil.escapeString(ident.name, "/");
313 /// <summary>
314 /// This method is deprecated. Use System.Guid instead.
315 /// </summary>
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);
322 /// <summary>
323 /// Compares the object identities of two proxies.
324 /// </summary>
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>
330 public static int
331 proxyIdentityCompare(ObjectPrx lhs, ObjectPrx rhs)
333 if(lhs == null && rhs == null)
335 return 0;
337 else if(lhs == null && rhs != null)
339 return -1;
341 else if(lhs != null && rhs == null)
343 return 1;
345 else
347 Identity lhsIdentity = lhs.ice_getIdentity();
348 Identity rhsIdentity = rhs.ice_getIdentity();
349 int n;
350 n = string.CompareOrdinal(lhsIdentity.name, rhsIdentity.name);
351 if(n != 0)
353 return n;
355 return string.CompareOrdinal(lhsIdentity.category, rhsIdentity.category);
359 /// <summary>
360 /// Compares the object identities and facets of two proxies.
361 /// </summary>
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)
371 return 0;
373 else if(lhs == null && rhs != null)
375 return -1;
377 else if(lhs != null && rhs == null)
379 return 1;
381 else
383 Identity lhsIdentity = lhs.ice_getIdentity();
384 Identity rhsIdentity = rhs.ice_getIdentity();
385 int n;
386 n = string.CompareOrdinal(lhsIdentity.name, rhsIdentity.name);
387 if(n != 0)
389 return n;
391 n = string.CompareOrdinal(lhsIdentity.category, rhsIdentity.category);
392 if(n != 0)
394 return n;
397 string lhsFacet = lhs.ice_getFacet();
398 string rhsFacet = rhs.ice_getFacet();
399 if(lhsFacet == null && rhsFacet == null)
401 return 0;
403 else if(lhsFacet == null)
405 return -1;
407 else if(rhsFacet == null)
409 return 1;
411 else
413 return string.CompareOrdinal(lhsFacet, rhsFacet);
418 /// <summary>
419 /// Creates an input stream for dynamic invocation and dispatch.
420 /// </summary>
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);
429 /// <summary>
430 /// Creates an output stream for dynamic invocation and dispatch.
431 /// </summary>
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);
439 /// <summary>
440 /// Returns the process-wide logger.
441 /// </summary>
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;
455 /// <summary>
456 /// Changes the process-wide logger.
457 /// </summary>
458 /// <param name="logger">The new process-wide logger.</param>
459 public static void setProcessLogger(Logger logger)
461 lock(_processLoggerMutex)
463 _processLogger = logger;
467 /// <summary>
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
470 /// patch level.
471 /// </summary>
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
478 /// <summary>
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.
482 /// </summary>
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;