- A Lightweight Java Middleware
<< Example 4a

Example 4b

is an alternative implementation of the same scenario as in example 4a, again it shows how to run an Agent on a remote server THERE and make it talk back to a Listener HERE.


How to do it

The implementation differs from that of example 4a in that the proxy Listener passed to the remote Agent is an interface proxy that implements the interface Listener.


package tw.net.ocs.example;

import tw.net.ocs.*;
import tw.net.ocs.proxy.*;

public class Example4b {

   public static void main(String[] args) {

      try {
      System.out.println("Running Example4b: Listener HERE, Agent THERE [using interface proxy]");

         // Step 1: Provide the connection from THERE to HERE

         // Create a server instance (to host the Listener
         // that the remote Agent will talk to)
      OCSServer serverHere=OCSServer.createInstance(
         23230,  // listen at this port
         null  // unnamed
         // Supply the server with a Listener instance (necessary,
         // since the interface proxy created below cannot create its subject
         // but only attach to it), and give the Listener the ID 'listenerHere'
      serverHere.addSubject(new ListenerImpl(),"listenerHere");
         // ... and start the server

         // Create the proxy Listener to be installed in the Agent THERE.
         // Let the OCSInterfaceProxyFactory supply an object that implements
         // interface Listener and make it talk to the real Listener
         // hosted in the server HERE
      Listener thereListener = (Listener)OCSInterfaceProxyFactory.generateProxyFor(
         Listener.class,  // the interface that the proxy should implement
         ListenerImpl.class,  // the class of the subject that it should associate with
         "listenerHere",  // the subject to talk to
         OCSClient.createInstance( // supply it a client
            serverHere,  // connecting to the server HERE,
            null,  // unnamed, and
            false  // not made the standard instance
         true  // check: does the subject class implement every method of the interface?

         // Step 2: Provide the connection from HERE to THERE

         // Create an OCSClient instance that
         "localhost",  // communicates with a server at this host
         23229,  // and this port,
         null,  // unnamed, and
         true  // made the standard client to use by subsequently created proxies

         // Allocate a proxy Agent and pass it the proxy Listener created above.
         // (This will transfer the proxy Listener to the remote Agent THERE,
         // turning every call of the Listener into a call of the Listener HERE.)
      Agent agent = new TwNetOcsExampleAgent(thereListener);
         // ... and let the Agent act
      catch (OCSException x) {


To run the demo,
start a server in a separate shell:

java -cp ocs.jar tw.net.ocs.OCSServer -start -port 23229

and run Example4b:

java -cp ocs.jar;ocs-examples.jar tw.net.ocs.example.Example4b
Last modified Jun 8, 2006