Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
6.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using UDDI;
  5. using UDDI.Replication;
  6. namespace UDDI.Tools
  7. {
  8. public class Monitor
  9. {
  10. public static int PollInterval = 5000;
  11. public static string Program = null;
  12. [STAThread]
  13. static void Main( string[] args )
  14. {
  15. Console.WriteLine( "Microsoft (R) UDDI Monitor Utility" );
  16. Console.WriteLine( "Copyright (C) Microsoft Corp. 2002. All rights reserved.\r\n" );
  17. try
  18. {
  19. ProcessCommandLine( args );
  20. ArrayList results = new ArrayList();
  21. ConnectionManager.Open( false, false );
  22. //
  23. // Get the list of known operatorNodes.
  24. //
  25. OperatorNodeCollection operatorNodes = new OperatorNodeCollection();
  26. operatorNodes.Get();
  27. //
  28. // Get the last notification message status for each operator.
  29. //
  30. foreach( OperatorNode operatorNode in operatorNodes )
  31. {
  32. ReplicationResult result = new ReplicationResult();
  33. result.GetLast( operatorNode.OperatorNodeID, true );
  34. results.Add( result );
  35. }
  36. //
  37. // Monitor changes to operator status every 5 seconds.
  38. //
  39. while( true )
  40. {
  41. Console.WriteLine( "Polling for new notifications: {0}. Press Ctrl+C to stop.", DateTime.Now );
  42. for( int i = 0; i < operatorNodes.Count; i ++ )
  43. {
  44. ReplicationResult lastResult = (ReplicationResult)results[ i ];
  45. ReplicationResult result = new ReplicationResult();
  46. result.GetLast( operatorNodes[ i ].OperatorNodeID, true );
  47. //
  48. // Check to see if a notification message has been received
  49. //
  50. if( result.LastChange > lastResult.LastChange )
  51. {
  52. DateTime time = new DateTime( result.LastChange );
  53. Console.WriteLine(
  54. "\r\n\tnotify_changeRecordsAvailable detected\r\n\t\tNode: {0}\r\n\t\tTime: {1}",
  55. result.OperatorNodeID,
  56. time );
  57. //
  58. // Execute the specified file.
  59. //
  60. Console.WriteLine(
  61. "\t\tStarting: {0} -o {1}",
  62. Program,
  63. result.OperatorNodeID );
  64. Process process = Process.Start( Program, "-o " + result.OperatorNodeID );
  65. process.WaitForExit();
  66. Console.WriteLine(
  67. "\t\tReturn code: {0}\r\n",
  68. process.ExitCode );
  69. //
  70. // Save the current notify result so that we don't
  71. // reprocess.
  72. //
  73. results[ i ] = result;
  74. }
  75. }
  76. System.Threading.Thread.Sleep( PollInterval );
  77. }
  78. }
  79. catch( CommandLineException e )
  80. {
  81. if( null != e.Message && e.Message.Length > 0 )
  82. {
  83. Console.WriteLine( e.Message );
  84. Console.WriteLine();
  85. }
  86. else
  87. {
  88. DisplayUsage();
  89. }
  90. }
  91. catch( Exception e )
  92. {
  93. Console.WriteLine( "Exception: {0}", e.ToString() );
  94. }
  95. finally
  96. {
  97. ConnectionManager.Close();
  98. }
  99. return;
  100. }
  101. public static void DisplayUsage()
  102. {
  103. Console.WriteLine( "Starts the specified program whenever a notify_changeRecordsAvailable\r\nmessage is received." );
  104. Console.WriteLine( "\r\nUsage:" );
  105. Console.WriteLine( "\tmonitor.exe [switches] <program>" );
  106. Console.WriteLine( "\r\nSwitches:" );
  107. Console.WriteLine( "\t-i <interval> Sets the poll interval (in milliseconds). The" );
  108. Console.WriteLine( "\t default is 5000." );
  109. Console.WriteLine( "\r\nExamples:" );
  110. Console.WriteLine( "\tmonitor.exe c:\\bin\\retrieve.exe" );
  111. Console.WriteLine( "\tmonitor.exe -t 10000 c:\\bin\\retrieve.exe" );
  112. }
  113. internal static void ProcessCommandLine( string[] args )
  114. {
  115. int i = 0;
  116. if( 0 == args.Length )
  117. throw new CommandLineException();
  118. while( i < args.Length )
  119. {
  120. if( "-" == args[ i ].Substring( 0, 1 ) || "/" == args[ i ].Substring( 0, 1 ) )
  121. {
  122. switch( args[ i ].Substring( 1 ).ToLower() )
  123. {
  124. case "i":
  125. if( i + 1 >= args.Length )
  126. throw new CommandLineException( "Missing required parameter 'interval'." );
  127. i ++;
  128. try
  129. {
  130. PollInterval = Convert.ToInt32( args[ i ] );
  131. }
  132. catch
  133. {
  134. throw new CommandLineException( "Parameter 'interval' must be numeric." );
  135. }
  136. break;
  137. case "?":
  138. throw new CommandLineException();
  139. default:
  140. throw new CommandLineException( "Invalid switch." );
  141. }
  142. }
  143. else
  144. {
  145. if( null == Program )
  146. Program = args[ i ];
  147. else
  148. throw new CommandLineException( "Too many command line parameters." );
  149. }
  150. i ++;
  151. }
  152. if( null == Program )
  153. throw new CommandLineException( "Missing required parameter 'Program'." );
  154. }
  155. }
  156. /// ****************************************************************
  157. /// public class CommandLineException
  158. /// ----------------------------------------------------------------
  159. /// <summary>
  160. /// Exception class for errors encountered while parsing the
  161. /// command-line.
  162. /// </summary>
  163. /// ****************************************************************
  164. ///
  165. public class CommandLineException : ApplicationException
  166. {
  167. /// ************************************************************
  168. /// public CommandLineException [constructor]
  169. /// ------------------------------------------------------------
  170. /// <summary>
  171. /// CommandLineException constructor.
  172. /// </summary>
  173. /// ************************************************************
  174. ///
  175. public CommandLineException()
  176. : base( "" )
  177. {
  178. }
  179. /// ************************************************************
  180. /// public CommandLineException [constructor]
  181. /// ------------------------------------------------------------
  182. /// <summary>
  183. /// CommandLineException constructor.
  184. /// </summary>
  185. /// ------------------------------------------------------------
  186. /// <param name="message">
  187. /// Exception message.
  188. /// </param>
  189. /// ************************************************************
  190. ///
  191. public CommandLineException( string message )
  192. : base( message )
  193. {
  194. }
  195. }
  196. }