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

  1. // The Virtual Light Company 1996
  2. //
  3. // From Chapter 20: Late Night VRML 2.0 and java
  4. //
  5. // This is the Registry server starter class.
  6.  
  7. package registry;
  8.  
  9. import java.net.*;
  10. import java.io.*;
  11. import registry.*;
  12.  
  13. public class Registry extends Thread
  14. {
  15.     private boolean debug;
  16.  
  17.     private int    _port;
  18.     private boolean server_available = false;
  19.     private ServerSocket    listen_socket = null;
  20.     private RegistryManager manager;
  21.     private RegistryServer    server;
  22.  
  23.     private ThreadGroup     thread_group;
  24.  
  25.     public Registry(int port, ThreadGroup tg, RegistryServer rserver, boolean debug)
  26.     {
  27.         super(tg, "Registry thread");
  28.  
  29.         thread_group = tg;
  30.  
  31.         this.debug = debug;
  32.         _port = port;
  33.  
  34.         System.out.println("Starting Registry Server on TCP port " + port);
  35.  
  36.         try
  37.         {
  38.             listen_socket = new ServerSocket(port);
  39.         }
  40.         catch(IOException e)
  41.         {
  42.             System.err.println("Unable to open Registry socket " + e.getMessage());
  43.             return;
  44.         }
  45.  
  46.         manager = new RegistryManager();
  47.  
  48.         server = rserver;
  49.         server_available = true;
  50.     }
  51.  
  52.     public void run()
  53.     {
  54.         Socket new_connection;
  55.         RegistryMessage msg;
  56.  
  57.         // if the server failed to start then exit and do nothing.
  58.         if(!server_available)
  59.             return;
  60.  
  61.         while(true)
  62.         {
  63.             try
  64.             {
  65.                 new_connection = listen_socket.accept();
  66.                 Thread.yield();
  67.             }
  68.             catch(IOException e)
  69.             {
  70.                 System.err.println("Registry Socket accept error " + e.getMessage());
  71.                 new_connection = null;
  72.             }
  73.  
  74.             if(new_connection != null)
  75.             {
  76.                 msg = new RegistryMessage(new_connection, thread_group, server, debug);
  77.                 msg.start();
  78.                 manager.add(msg);
  79.             }
  80.         }
  81.     }
  82. }
  83.