//# //# ORB Helper Utilities //# $Revision: 1.1 $ //# Copyright 2004 by Eric Y. Theriault //# All Rights Reserved. //# http://www.eyt.ca/CORBA //# using System; using System.Runtime.Remoting; using Ch.Elca.Iiop; using Ch.Elca.Iiop.Services; using omg.org.CosNaming; namespace ca.eyt.corba { /// /// Summary description for ORBHelper. /// public class ORBHelper { /// /// Used for parsing the command line arguments. /// private enum State { /// /// This is the normal state of the algorithm. /// Parsing, /// /// When an -ORBInitialHost is seen, this state is enabled. /// Host, /// /// When an -ORBInitialPort is seen, this state is enabled. /// Port, /// /// When an -ORBInitRef is seen, this state is enabled. /// Reference }; /// /// Acquires a nameserver reference from the command line. /// IIOP.net does not appear to process command-line arguments like /// standard CORBA implementations do. This method is intended to make up for /// this lack of functionality. This assumes that you have registered the /// channels. /// Arguments from the program. This processes -ORBInitialHost, /// -ORBInitialPort, and -ORBInitRef parameters like Java, TAO, and Mico does. /// Initial CORBA Reference. /// public static NamingContext resolveNameserviceReference( CorbaInit orb, string [] args ) { String initialHost = "localhost"; String initialPort = "1050"; String initialReference = null; State state = State.Parsing; for ( int i = 0; i < args.Length; ++ i ) { // Check the argument; If it is a match, set the state accordingly. // If it is not a match, check the state, and copy in the parameters // if needed. if ( args[i].CompareTo( "-ORBInitialHost" ) == 0 ) { if ( state != State.Parsing ) { throw new Exception( "State should be Parsing, but it is " + state ); } state = State.Host; } else if ( args[i].CompareTo( "-ORBInitialPort" ) == 0 ) { if ( state != State.Parsing ) { throw new Exception( "State should be Parsing, but it is " + state ); } state = State.Port; } else if ( args[i].CompareTo( "-ORBInitRef" ) == 0 ) { if ( state != State.Parsing ) { throw new Exception( "State should be Parsing, but it is " + state ); } state = State.Reference; } else if ( state != State.Parsing ) { switch ( state ) { case State.Host: initialHost = args[i]; break; case State.Port: initialPort = args[i]; break; case State.Reference: const String ns = "NameService="; int index = args[i].IndexOf( ns ); if ( index == -1 ) { throw new Exception( "Initial Reference does not specify a Nameservice: " + args[i] ); } initialReference = args[i].Substring( index + ns.Length ); break; default: throw new Exception( "Invalid State: " + state ); } state = State.Parsing; } } // Initialize the initialReference if the other fields are not null. if ( initialReference == null && initialHost != null && initialPort != null ) { initialReference = String.Format( "corbaloc:iiop:{0}:{1}/NameService", initialHost, initialPort ); } // OK. Now let's process if ( initialReference != null ) { return RemotingServices.Connect( typeof( NamingContext ), initialReference ) as NamingContext; } else { // Currently not possible, but... throw new Exception( "No naming service specified." ); } } } }