OCSelot
- A Lightweight Java Middleware
<< Examples overview   Example 1 >>

Participating classes

The examples make use of two classes and an interface in package tw.net.ocs.example:

In addition, there are two proxy subclasses in package tw.net.ocs.proxy:

Proxy subclasses override every method of their base, implementing it with the machinery that routes the requests to the server-side subjects. The code of these classes is generated by the OCSClassProxyFactory and not reproduced here.

Finally, there is a proxy of the interface Listener. This is generated anonymously on the fly by the OCSInterfaceProxyFactory.

Agent

A simple agent that does a little work and keeps a listener informed about its activities.

public class Agent {

      /** A listener which gets informed about the agent's activities */
   private Listener listener_;

   /**
      The agent is constructed and handed a listener.

      @param li the listener 
   */
   public Agent(Listener li) {
      System.out.println("\nConstruction, setting the listener");
      listener_=li;
   }

   /**
      Starts the agent and makes it do its (modest) work
   */
   public void act() {
         // say initial hello
      System.out.println("About to call listener (starting).");
      listener_.receive("Listen, agent starts to work...");

      for (int a=1; a<=5; ++a) {
            // (pretend to) work for a second
         try { Thread.sleep(1000); }
         catch (InterruptedException x) {}
            // and talk about it
         System.out.println("About to call listener (done with #"+a+").");
         listener_.receive("Listen, agent done with action #"+a+".");
      }
         // say final hello
      System.out.println("About to call the listener (finished).");
      listener_.receive("Listen, agent finished work, bye-bye!");
   }
}

Listener

A listener to be installed in an agent to get informed about the agent's activities

public interface Listener {
   /**
      Receives a message

      @param message the message 
   */
   void receive(String message);
}

ListenerImpl

An implementation of the Listener interface, printing messages it receives to System.out

public class ListenerImpl implements Listener, java.io.Serializable {
   /**
      Receives a message and prints it to System.out

      @param message the message
   */
   public void receive(String message) {
      System.out.println(message);
   }
}
Last modified Dec 30th, 2005