Created the facilities for transmitting distributed objects and derived

classes over the wire (by inspecting their fields and sending those
directly).


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@8 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-05-30 00:16:00 +00:00
parent a523e71302
commit 9cd146a43c
14 changed files with 456 additions and 23 deletions
@@ -1,12 +1,15 @@
//
// $Id: DObjectFactory.java,v 1.1 2001/05/29 03:27:59 mdb Exp $
// $Id: DObjectFactory.java,v 1.2 2001/05/30 00:16:00 mdb Exp $
package com.samskivert.cocktail.cher.dobj;
package com.samskivert.cocktail.cher.dobj.net;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.HashMap;
import com.samskivert.cocktail.cher.Log;
import com.samskivert.cocktail.cher.dobj.DObject;
import com.samskivert.cocktail.cher.io.ObjectStreamException;
/**
@@ -16,22 +19,40 @@ import com.samskivert.cocktail.cher.io.ObjectStreamException;
*/
public class DObjectFactory
{
/**
* Writes the supplied distributed object out to the specified data
* output stream.
*/
public static void writeTo (DataOutputStream out, DObject dobj)
throws IOException
{
// first we write the class of the object to the stream
out.writeUTF(dobj.getClass().getName());
// then we write the object itself
dobj.writeTo(out);
Log.info("Marshalling object: " + dobj);
// look up the marshaller for this object
Class clazz = dobj.getClass();
Marshaller marsh = getMarshaller(clazz);
// then write the class of the object to the stream
out.writeUTF(clazz.getName());
// then use the marshaller to write the object itself
marsh.writeTo(out, dobj);
}
/**
* Reads a distributed object from the specified input stream.
*/
public static DObject readFrom (DataInputStream in)
throws IOException
{
try {
// read in the class name and create an instance of that class
Class clazz = Class.forName(in.readUTF());
DObject dobj = (DObject)clazz.newInstance();
dobj.readFrom(in);
Log.info("Unmarshalling object: " + dobj);
// look up the marshaller for that class
Marshaller marsh = getMarshaller(clazz);
// use it to reconstitute the object from the stream
marsh.readFrom(in, dobj);
return dobj;
} catch (Exception e) {
@@ -39,4 +60,17 @@ public class DObjectFactory
throw new ObjectStreamException(errmsg);
}
}
protected static Marshaller getMarshaller (Class clazz)
throws IOException
{
Marshaller marsh = (Marshaller)_marshallers.get(clazz);
// create a new marshaller if we don't already have one
if (marsh == null) {
_marshallers.put(clazz, marsh = new Marshaller(clazz));
}
return marsh;
}
protected static HashMap _marshallers = new HashMap();
}