HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Developing Collaborative Games Using Java Applets"
Vol. 2, Issue 12, p. 8

	

Listing 1a: Active object. 

import java.util.*; 

public abstract class ActiveObject extends Thread 
{ 
/* for efficiency, use a linked-list implementation */ 
   private Vector eventQueue = new Vector(); 
   private BitSet callbackSet = new BitSet(); 
   private int[] expectedEvts; 

/* current active session */ 
ActiveSession session; 

   public synchronized void pushEvent(Object evt) 
   { 
//check if intended for callback 
      if (callbackSet.get(evt.hashCode())) {handleEvent(evt); return;} 
//check beginning of queue 
      eventQueue.addElement(evt); 
//notify waiting object 
      if (checkQueue(expectedEvts) != null) notify(); 
   } 

   private Object checkQueue(int[] evts) 
   { 
if (evts == null) return null; 
      Object firstEvent = eventQueue.elementAt(0); 
      for (int i=0; i<evts.length; i++) 
         if (evts[i] == firstEvent.hashCode()) 
            return firstEvent; 
      return null; 
   } 

   protected void registerCallback(int[] evts) 
   { 
for (int i=0; i<evts.length; i++) 
         callbackSet.set(evts[i]); 
   } 

protected void send(Object evt, int destid) 
{ 
session.send(evt,destid); 
} 

protected void mcast(Object evt, int omitid) 
{ 
session.mcast(evt,omitid); 
} 

   protected Object waitFor(int evt) 
   { 
      int[] evts = new int[1]; evts[0]=evt; 
      return waitFor(evts); 
   } 

   public synchronized Object waitFor(int[] evts) 
   { 
//save expected events 
      expectedEvts = evts; 

//if queue is empty, wait 
      if (eventQueue.size()==0) 
         try {wait();} catch (Exception e) {} 

//check front of queue and return if valid 
      Object arrivedEvent = checkQueue(evts); 
      if (arrivedEvent != null) 
{ 
         eventQueue.removeElementAt(0); 
         return arrivedEvent; 
} 

//throw ProtocolException here! 
   } 

/* implement active control flow here in subclass */ 
   public abstract void run(); 

/* implement passive callback event handling here in subclass */ 
   protected abstract void handleEvent(Object evt); 
} 

Listing 1b: Active session. 

import java.util.*; 

public class ActiveSession extends Object 
{ 
   static Hashtable sessions = new Hashtable(); 

   private Vector members = new Vector(); //Vector of ActiveObjects 

   public ActiveSession(ActiveObject server, 
String sessionName, int sessionID) 
   { 
      ActiveSession.sessions.put(sessionName+sessionID,this); 
      members.addElement(server); 
   } 

   public static ActiveSession join(ActiveObject obj, 
String sessionName, int sessionID) 
   { 
ActiveSession s = (ActiveSession) 
   (ActiveSession.sessions.get(sessionName+sessionID)); 
      s.members.addElement(obj); 
      return s; 
   } 

   public void send(Object evt, int clientid) 
   { 
      ((ActiveObject) members.elementAt(clientid)).pushEvent(evt); 
   } 

   public void mcast(Object evt, int omitid) 
   { 
      for (int i=0; i<members.size(); i++) 
         if (i!=omitid) 
((ActiveObject) members.elementAt(i)).pushEvent(evt); 
   } 
} 




 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.