home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch19 / registry / RegistryManager.java < prev    next >
Encoding:
Java Source  |  1997-02-16  |  1.3 KB  |  60 lines

  1. // Simple Multiuser Server
  2. // (c) Copyright Justin Couch   justin@vlc.com.au
  3. // The Virtual Light Company 1996
  4. //
  5. // From Chapter 20: Late Night VRML 2.0 and java
  6. //
  7. // This is the Registry manager class. Maintains the list of active connections.
  8. // New connections are added with the add() method. Once every five seconds
  9. // it loops through the list of connections and looks for the inactive sessions
  10. // indicated by the isActive() method for each connection. Dead ones are
  11. // removed.
  12.  
  13. package registry;
  14.  
  15. import java.util.*;
  16. import registry.RegistryMessage;
  17.  
  18. class RegistryManager extends Thread
  19. {
  20.     Vector connection_list = null;
  21.  
  22.     public RegistryManager()
  23.     {
  24.         connection_list = new Vector(5, 2);
  25.     }
  26.  
  27.     public void run()
  28.     {
  29.         int    i;
  30.         RegistryMessage msg;
  31.  
  32.         while(true)
  33.         {
  34.             // lock the vector so that we don't get clashes when trying to
  35.             // add a new connection
  36.             synchronized(connection_list)
  37.             {
  38.                 for(i = 0; i < connection_list.size(); i++)
  39.                 {
  40.                     msg = (RegistryMessage)connection_list.elementAt(i);
  41.                     if(!msg.isAlive())
  42.                     {
  43.                         connection_list.removeElementAt(i);
  44.                         i--;  // adjust for the removed element
  45.                     }
  46.                 }
  47.             }
  48.             Thread.yield();
  49.         }
  50.     }
  51.  
  52.     public void add(RegistryMessage msg)
  53.     {
  54.         synchronized(connection_list)
  55.         {
  56.             connection_list.addElement(msg);
  57.         }
  58.     }
  59. }
  60.