//#
//# Corba Time Client
//# $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;
using omg.org.CosNaming;
namespace ca.eyt.corba
{
///
/// Implements a class to acquire the Time from a CORBA server.
///
public class TimeClient
{
///
/// Port to multicast on. This number is random.
///
public const int MulticastPort = 7007;
///
/// Address to multicast to. The default is link-local, which is
/// generally OK, but in the presence of a segmented network with
/// routers, this address and the TTL will have to be changed.
///
public const String MulticastAddress = "224.0.0.1";
///
/// This is the message that is multicasted to discover the new
/// server.
///
public const String MulticastDiscoverServer = "DiscoverIOR";
///
/// Main.
///
[System.STAThread]
public static void Main( string[] args )
{
try {
// Create and Initialize the ORB
CorbaInit orb = CorbaInit.GetInit();
// Register client channel
IiopClientChannel clientChannel = new IiopClientChannel();
ChannelServices.RegisterChannel( clientChannel );
String ior = null;
// Please note that a real implementation would provide more
// options, and possibly share this with the server. This
// is only meant to show the functionality.
MulticastSocket mcast = null;
try {
mcast = new MulticastSocket( MulticastPort );
mcast.setSoTimeout( 1000 );
mcast.send( MulticastAddress, MulticastDiscoverServer );
while ( true ) {
// Receive the converted string.
System.Net.IPEndPoint source = null;
ior = mcast.receive( ref source );
if ( ior.StartsWith( MulticastDiscoverServer ) == false ) {
break;
}
}
} finally {
if ( mcast != null ) {
mcast.close();
}
}
// Get the root naming context
NamingContext nameService = RemotingServices.Connect( typeof( NamingContext ), ior ) as NamingContext;
String Time = "Time";
NameComponent[] name = new NameComponent[] { new NameComponent( Time, "" ) };
// Resolve the ior into an object.
TimeTools.Time timeImpl = (TimeTools.Time)nameService.resolve( name );
// OK. Let's call the Time interface's methods.
TimeTools.TimeOfDay time = timeImpl.getTime();
System.Console.Out.WriteLine( "Time server's time is: {0}:{1}:{2}", time.hour, time.minute, time.second );
} catch ( System.Exception e ) {
System.Console.WriteLine( "Exception: " + e.Message );
System.Console.WriteLine( e.StackTrace.ToString() );
System.Environment.Exit( 1 );
}
System.Environment.Exit( 0 );
}
}
}