Using Network Objects

Example 1: A ping server

Client side:

    String urladdr= "http://myhost:2001/pingServer";
    PingServer server= (PingServer)Manager.lookup(url);
    String text= server.ping();
This code imports a ping server owned by a process running in the machine myhost which listens to requests on port 2001.


Importing a network object

NetInterface netObj= (NetInterface)Manager.lookup(url);

where NetInterface is a network interface supported by the network object and url is a url address taking the form:

http://host:port/object-name


Writing the network interface for the ping server

Server side:

The interface must extends the interface NetObj:

    public interface PingServer extends NetObj {
      public String ping() throws NetException;
    }
All the methods must throw NetException.


Writing the concrete ping server

The concrete class must extends ConcreteNetObj and implements the network interfaces that will be supported:

    public class ConcretePingServer
           extends ConcreteNetObj
           implements PingServer {
      public String ping() throws NetException {
        return "Hello World!";
      }
    }


Specifying the socket port for listening to requests


Creating and exporting the ping server

    ConcretePingServer server= new ConcretePingServer();
    Manager.bind("pingServer", server);
The general form for exporting a network object is:

Manager.bind(object-name, concrete-object);


The class Manager

import netobj.*;

class Manager {
    public static void bind(String objname, NetObj netObj)
        throws NetException;
    public static NetObj lookup(String urladdr)
        throws MalformedURLException, NetException,
               UnknownHostException;
    public static void setSocketPort

(int port); }


Example 2: A ping server factory

The client part:

    String url= "http://myhost:2001/pingServerFactory";
    factory= (Factory)Manager.lookup(url);
    PingServer server1= factory.makePingServer();
    PingServer server2= factory.makePingServer();
The factory runs on myhost and builds ping servers running also on myhost.

A remote invocation can return a network reference.


Writing the network interface for the factory

public interface Factory extends NetObj {
  public PingServer makePingServer() throws NetException;
}
makePingServer cannot return a ConcretePingServer.


Writing the concrete factory

    public class ConcreteFactory extends ConcreteNetObj
                                 implements Factory {
      public PingServer makePingServer() throws NetException {
        return new ConcretePingServer();
      }         
    }


Creating and exporting the concrete factory

    ConcreteFactory factory= new ConcreteFactory();
    Manager.bind("pingServerFactory", factory);