Callback - Using Callbacks between CORBA Applications

Description:

Most of the examples use a dedicated server process to host the CORBA objects. Within this examples, CORBA object IORs will usually be published via flat files or the CORBA Naming Service.

Very often it is necessary to have peer-to-peer dialogs between CORBA objects where object references are passed as operation parameters between different processes. In this example we create such a dialog using a simple callback pattern.

Since we call the client back within a new, separate thread, the client is not blocked within its synchronous call to the server. Therefore we need to call the 'Orb.run()' method also on the client side to avoid shutting down the client process before the callbacks are done.

Source:

\Demo\CallBack

Goal:

Realize a CORBA peer-to-peer communication using a callback object


Example

Step 1. The IDL (CallBack.idl):

    module CallBack
    {
        // "client" side callback interface:
        interface IGreetings
        {
            string hello( in string strName);
        };
        
        // "server" side interface calling back:
        interface ISrvAdm
        {
            void regCallBack( in IGreetings oNameOfInterface);
        };
    };

(Will be compiled into CallBack.cs)

Step 2. "Client" Side Callback Object Implementation (within CbClt.cs):

Implement the callback class:

    public class Greetings: CallBack.IGreetingsPOA
    {
        override public string hello(  string a_strName  )
        {
            Console.WriteLine("Greetings from {0}", a_strName);
            return "Greeting from " + a_strName; 
        }
    }

Create a callback object:

    Greetings oGreetings = new Greetings();

Retrieve a server reference and send the callback objects servant reference (obtained by calling its '_this()' method) to the server:

    CallBack.ISrvAdm oSrvAdm= CallBack.ISrvAdmHelper.narrow( m_oOrb.string_to_object("file://c:\\CallBack.ior"));
    oSrvAdm.regCallBack( oGreetings._this() );

Do not forget to start the ORB event loop in the client also to avoid premature process shutdown:

    m_oOrb.run();

Step 3. "Server" Side Object Implementation calling back (within CbSrv.cs):

The server passes the callback object reference into a newly created background thread:

    public class SrvAdm: CallBack.ISrvAdmPOA
    {
        override public void regCallBack(  IGreetings a_oNameOfInterface  )
        {
            CallBackThd oCallBackThd = new CallBackThd( a_oNameOfInterface );
            
            Thread oThread = new Thread( new ThreadStart( oCallBackThd.run ) );
            oThread.IsBackground = true;
            oThread.Start();
        }
    }

Within this thread the callback object is called:

    public class CallBackThd
    {
        IGreetings m_oNameOfInterface;
        
        public CallBackThd ( IGreetings a_oNameOfInterface)
        {
            m_oNameOfInterface = a_oNameOfInterface;
        }
        
        public void run()
        {
            string strLine;
            
            while( true)
            {
                Console.Write("Enter a Name :");
                strLine = Console.ReadLine();
                
                m_oNameOfInterface.hello( strLine );
            }
        }
    }

Step 4. Run the example

a.) Start the Server.

b.) Start the Client.