//#
//# Corba Time Server
//# $Revision: 1.1 $
//# Copyright 2004 by Eric Y. Theriault
//# All Rights Reserved.
//# http://www.eyt.ca/CORBA
//#
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using Ch.Elca.Iiop;
using Ch.Elca.Iiop.Services;
namespace ca.eyt.corba
{
///
/// Implements a CORBA server to provide the local time.
///
public class TimeServer
{
///
/// Server Port
///
const int ServerPort = 7832;
///
/// Main.
///
[System.STAThread]
public static void Main( string[] args )
{
try {
String iorFile = "time.ior";
// Create and Initialize the ORB
CorbaInit orb = CorbaInit.GetInit();
// Register server channel
IiopChannel channel = new IiopChannel( ServerPort );
ChannelServices.RegisterChannel( channel );
// Create the time Servant and register it.
ca.eyt.corba.TimeImpl timeImpl = new ca.eyt.corba.TimeImpl();
// Create the object reference.
string objectURI = "Time";
ObjRef objrefWellKnown = RemotingServices.Marshal( timeImpl, objectURI );
// Acquire the IOR for the object reference.
//TODO: There is probably a nicer way to do this.
omg.org.CORBA.OrbServices srv = omg.org.CORBA.OrbServices.GetSingleton();
String[] url = channel.GetUrlsForUri( objrefWellKnown.URI );
if ( url.Length != 1 ) {
throw new Exception( "Unexpected input: " + url.Length );
}
String ior = srv.object_to_string( srv.string_to_object( url[0] ) );
// Write out the file
try {
using ( System.IO.StreamWriter writer = new System.IO.StreamWriter( iorFile ) ) {
writer.WriteLine( ior );
}
} catch ( Exception e ) {
System.Console.Error.WriteLine( iorFile + " raised an exception: " + e.Message );
System.Environment.Exit( 1 );
}
// Server is now up and running
System.Console.Out.WriteLine( "TimeServer is ready and waiting..." );
while ( true ) {
//TODO: Probably not the best solution.
System.Threading.Thread.Sleep( 10000 );
}
} catch ( System.Exception e ) {
System.Console.WriteLine( "Exception: " + e.Message );
System.Console.WriteLine( e.StackTrace.ToString() );
System.Environment.Exit( 1 );
}
}
}
}