Listing 1
public interface Host extends Remote
{
java.util.Hashtable signOn( SubscriberAccount newSubscriber)
throws RemoteException, Exception;
void signOff( String name)
throws RemoteException;
void sendMessage( String senderName, String message)
throws RemoteException;
}
public interface Subscriber extends Remote
{
void displayMessage( String senderName, String message)
throws RemoteException;
}
Listing 2
public class SubscriberAccount implements Serializable
{
private String name;
private String hobby;
private Subscriber subscriber;
public SubscriberAccount( String _name, String _hobby, Subscriber _subscriber)
{
name = _name;
hobby = _hobby;
subscriber = _subscriber;
}
public String queryName()
{
return name;
}
public String queryHobby()
{
return hobby;
}
public Subscriber queryInterface()
{
return subscriber;
}
}
Listing 3
namespace Chat
{
public class SubscriberAccountImpl: Chat.SubscriberAccount
{
public SubscriberAccountImpl()
{
}
public SubscriberAccountImpl( string _name, string _hobby, Subscriber _subscriber)
{
name = _name;
hobby = _hobby;
subscriber = _subscriber;
}
public override string queryName( )
{
return name;
}
public override string queryHobby( )
{
return hobby;
}
public override Chat.Subscriber queryInterface( )
{
return subscriber;
}
}
}
Listing 4
namespace DotNetClient
{
public class SubscriberImpl: Chat.SubscriberPOA
{
private AsyncUIAccess m_oUserInterface;
public SubscriberImpl( AsyncUIAccess a_oUserInterface)
{
m_oUserInterface = a_oUserInterface;
}
public override void displayMessage( string a_strName, string a_strMsg)
{
m_oUserInterface.writeLog( "<" + a_strName + "> '" + a_strMsg + "'");
}
}
}
Listing 5
public class Connection
{
private string[] m_strORBInit =
{"-ORBInitRef NameService=corbaloc:iiop:1.2@:1050/NameService",
"-ORBDebug",
"-AddAssembly DotNetClient.exe, JavaBuiltinTypesCF.dll, MinCorCF.dll"
};
private Chat.Host m_oChatHost;
private Chat.SubscriberAccount m_oAccount;
private Middsol.CORBA.ORB m_oOrb;
signOn() is called by the user interface when the client wants to connect to a server:
public void signOn( AsyncUIAccess a_oAsyncUIAccess, string a_strIpAddr,
string a_strName, string a_strHobby)
{
// Instantiate local ORB, find remote ORB and connect
initializeConnection( a_oAsyncUIAccess, a_strIpAddr);
// Instantiate local Subscriber interface
Chat.Subscriber oSubscriber = new SubscriberImpl( a_oAsyncUIAccess)._this();
// create local subscriber's account object
m_oAccount = new Chat.SubscriberAccountImpl( a_strName, a_strHobby, oSubscriber);
try
{
// sign on and display returned list of participants
displayListOfSubscribers( a_oAsyncUIAccess, m_oChatHost.signOn( m_oAccount));
}
catch( Middsol.java.lang.Ex ex)
{
a_oAsyncUIAccess.writeLog("+++ Sign-on failed. +++");
a_oAsyncUIAccess.writeLog("+++ Please try another name.+++");
deinitializeConnection();
}
}
signOff() calls this very method on the host to remove this instance from the list of subscribers:
public void signOff( )
{
if( m_oOrb != null)
{
m_oChatHost.signOff( m_oAccount.queryName());
deinitializeConnection();
}
}
sendMessage() calls this very method on the host:
public void sendMessage( string a_strMessage)
{
m_oChatHost.sendMessage( m_oAccount.queryName(), a_strMessage);
}
The initialize() method initializes the ORB:
private void initializeConnection( AsyncUIAccess a_oAsyncUIAccess, string a_strIpAddr)
{
try
{
//Initialize the CORBA framework
initCORBA( a_oAsyncUIAccess, a_strIpAddr);
connectToHost( a_oAsyncUIAccess);
}
catch(Middsol.CosNaming.NamingContextPackage.NotFound ex)
{
//Name service not running or not accessible
a_oAsyncUIAccess.writeLog( "NS Manager Error:" + ex.why);
throw new System.Exception();
}
catch( Middsol.CORBA.SystemException exSys)
{
a_oAsyncUIAccess.writeLog( "Server exception caught (Middsol.CORBA.SystemException).");
a_oAsyncUIAccess.writeLog( "ID:" + exSys.ID);
deinitializeConnection();
}
}
Listing 6
private void initCORBA( AsyncUIAccess a_oAsyncUIAccess, string a_strIpAddr)
{
m_strORBInit[0] = m_strORBInit[0].Replace("", a_strIpAddr);
m_oOrb = Middsol.CORBA._ORB.init( m_strORBInit, null);
Middsol.PortableServer.POA oRootPOA =
Middsol.PortableServer.POAHelper.narrow( m_oOrb.resolve_initial_references( "RootPOA" ));
oRootPOA.the_POAManager.activate();
}
deinitializeConnection() destroys the ORB.
private void deinitializeConnection()
{
if( m_oOrb != null)
{
m_oOrb.destroy();
}
m_oOrb = null;
}
Listing 7
private void connectToHost( AsyncUIAccess a_oAsyncUIAccess)
{
Middsol.CosNaming.NamingContextExt oNsCtx =
Middsol.CosNaming.NamingContextExtHelper.narrow(
m_oOrb.resolve_initial_references("NameService"));
m_oChatHost = Chat.HostHelper.narrow( oNsCtx.resolve_str("ChatHost"));
a_oAsyncUIAccess.writeLog( "Chat host connected");
}
Listing 8
private void displayListOfSubscribers( AsyncUIAccess a_oAsyncUIAccess,
System.Collections.Hashtable a_oSubscriberList)
{
System.Collections.IDictionaryEnumerator enumerator = a_oSubscriberList.GetEnumerator();
a_oAsyncUIAccess.writeLog("-- Subscribers --");
while( enumerator.MoveNext())
{
Chat.SubscriberInfo oInfo = (Chat.SubscriberInfo)enumerator.Value;
a_oAsyncUIAccess.writeLog(" " + oInfo.queryName() + ", " + oInfo.queryHobby());
}
a_oAsyncUIAccess.writeLog("-----------------");
}