NCSAHabanero

[ Previous ] [ Index ] [ Next ]

-------------------------------------------------------------------
How to enable a hablet to join a session
-------------------------------------------------------------------

Transfer State to Other Instances

Background Information

When a hablet joins a session, it needs to get a copy of the internal state of the existing hablets. The process of encapsulating the internal state and sending it to another  client that joins the session is called serializing.

In this pattern, we are dealing only with sending the current state to a new client which is starting up. We deal with sending events to existing clients in the Event Handling pattern.

If you would like a more detailed description of how the communication between hablets works, you can read about Habanero's event model.

How does this work?

If you extend Hablet, you will need two methods, one to package up the state of the original hablet and send it and another to receive that package and unpack it. These two methods are:
 protected void writeHablet(MarshallOutputStream out) throws IOException
and
 protected void readHablet(MarshallInputStream in) throws IOException
Within writeHablet(), you need to write out each variable that changes. You can read about how to decide which variables to send.

Then within readHablet(), you need to read in in the same order the variables you sent.

Here is an example of a generic way of doing writeHablet() and readHablet() if you need to save the state of a generic object:
Example from HabTextDemo2.java.
  protected void writeHablet(MarshallOutputStream out) throws IOException 
  { out.writeObject(outArea); 
  } 

  protected void readHablet(MarshallInputStream in) throws IOException 
  { try 
    { outArea=(TextArea)in.readObject(); } 
    catch (ClassNotFoundException e) 
    { Habanero.println("Object read was not a TextArea."); } 
  }

writeObject() and readObject use Java's standard serialization to marshall the data in the object you give it.

In our Running Example, we picked a more effecient method than the generic method above. Since we know that the data in outArea is textual, we simply wrote out the string:
Running Example from the example of habanerizing TextDemo.java.
Add these methods to the class HabTextDemo

  protected void writeHablet(MarshallOutputStream out) throws IOException 
  { out.writeUTF(outArea.getText()); 
  } 
  
  protected void readHablet(MarshallInputStream in) throws IOException 
  { outArea.setText(in.readUTF()); 
  }

 
MarshallOutputStream extends java.io.ObjectOutputStream, and MarshallInputStream extends java.io.ObjectInputStream, so writeHablet() and readHablet() can use any of the methods of java.io.ObjectOutputStream [Outside link] and methods of java.io.ObjectInputStream [Outside link] to send data to the other hablets. The example above uses writeUTF() and readUTF() since the data item is a string. (UTF is a string format.) If the data item were an integer, it would use writeInt() and readInt, for example.
 
NOTE 

A Hablet is Externalizable as opposed to Serializable. If you are habanerizing a multiple-class applet, the other classes should implement Serializable, then all their data will be automatically transmitted, unless you make the variable transient
See the Event Model, part A for a diagram of how readHablet() and writeHablet() work.

What next?



Habanero® is a registered trademark owned by The Board of Trustees of the University of Illinois. Copyright 1996-1998. All rights reserved.   Java(TM) is a proprietary trademark owned by Sun Microsystems, Inc. NCSA
The National Center for Supercomputing Applications

University of Illinois at Urbana-Champaign

Contacts page