The Implementation of Network Objects

Requirements:

Process identification

Choice: third alternative.

class Space implements Serializable {
  InetAddress iaddr; // IP number of the processor
  int port;          // socket port for opening connections

  ... methods hashCode and equals for using ...
  ... spaces as keys in hash tables ...

  ... toString for printing spaces ...
}
A space identifies a Java process allocating or referencing network objects.

Representation of references

Three cases:

Wire Representation

A wire representation is a `telephone number' which can be sent through a stream:

class WireRep implements Serializable {
  Space sp; // The process owner of the represented network object
  long id;  // The object identification within the process
  String stubClassName; // For convenience

  ... hashCode, equals and toString ...
  ... readResolve ...
}
The wire representation is assigned the first time a network object is sent to any other process.

Object serialization

Java implements object serialization by using reflection.

Writing process:

    OutputStream out= ... ; // any output stream
    ObjectOutputStream objOut= new ObjectOutputStream(out);
    objOut.writeInt(i);        // throws IOException
    objOut.writeObject(space); // throws IOException
    ...
Reading process:

    InputStream in= ... ; // any output stream
    ObjectInputStream objIn= new ObjectInputStream(in);
    int j= objIn.readInt();
    Object space= objIn.readObject();
    ...
Inconvenience: must catch exceptions IOException, OptionalDataException and ClassNotFoundException.

Implementation of writeObject

Class hierarchy for network objects

Any reference of a network object (local or remote) points to an object being a subclass of NetObjBase.

Serializing a network object

In Java, every class C defining the method writeReplace marks the class C being special. When an object o of class C is found, instead of writing the object o, the serialization writes o.writeReplace().

abstract class NetObjBase implements NetObj {
  WireRep wrep; // network identifier of the object

  protected Object writeReplace() { // a hook
    sendNetObj(this); // the object manager
    return wrep;
  }
}
Network object are always passed by reference: when a network object is written to an object stream, the object is replaced by its wire representation.

Deserializing a network object

In Java, every class C defining the method readResolve marks the class C being special. When an object o of class C is found on an input stream, instead of returning the object o, the deserialization gives o.readResolve().

    class WireRep implements Serializable {
      ...
      Object readResolve() {
          return receiveNetObj(this); // the object manager
      }
    }
When a wire representation if read from an object stream, it is replaced by: (i) its concrete representation if it is found locally, or (ii) a unique stub if it is remote reference.