home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch19 / muserver.java < prev   
Text File  |  1997-02-16  |  4KB  |  166 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 main file. In here the program starts. It creates the four
  8. // different servers and looks after any cleanup that needs to be done of
  9. // shutdown.
  10. //
  11. // This program sits in an endless loop every 30 seconds it wakes up and
  12. // tests the ports to see if they are alive. This will probably do
  13. // something more later on.
  14.  
  15. import registry.*;
  16. import filter.Filter;
  17. import filter.UserServer;
  18. import chat.Chat;
  19. import update.Update;
  20.  
  21. public class muserver extends Thread
  22. {
  23.     int        base_port;
  24.     RegistryServer    rserver;
  25.     Registry    registry;
  26.     UserServer    userver;
  27.     Filter        filter;
  28.     Chat        chat;
  29.     Update        update;
  30.  
  31.     ThreadGroup    serverThreadGroup = new ThreadGroup("Internal Server ThreadGroup");
  32.     ThreadGroup    filterThreadGroup = new ThreadGroup("Filter Server ThreadGroup");
  33.     ThreadGroup    updateThreadGroup = new ThreadGroup("Update Server ThreadGroup");
  34.     ThreadGroup    registryThreadGroup = new ThreadGroup("Registry Server ThreadGroup");
  35.     ThreadGroup    chatThreadGroup = new ThreadGroup("Chat Server ThreadGroup");
  36.  
  37.     // construct the basic system of the four servers
  38.     public muserver(int base_pt, boolean debug)
  39.     {
  40.         System.out.println("Starting Multi User Environment server");
  41.  
  42.         setPriority(2);
  43.         if(debug)
  44.             System.out.println("Debugging is ON");
  45.  
  46.         base_port = base_pt;
  47.  
  48.         rserver = new RegistryServer(serverThreadGroup);
  49.         userver = new UserServer(serverThreadGroup, debug);
  50.         filter = new Filter(base_port, filterThreadGroup, userver);
  51.         registry = new Registry(base_port, registryThreadGroup, rserver, debug);
  52.         update = new Update(base_port + 2, updateThreadGroup, userver);
  53.         chat = new Chat(base_port + 4, chatThreadGroup, userver);
  54.  
  55.         userver.start();
  56.         rserver.start();
  57.         filter.start();
  58.         registry.start();
  59.         update.start();
  60.         chat.start();
  61.  
  62.         this.start();
  63.     }
  64.  
  65.     public void run()
  66.     {
  67.         String input;
  68.  
  69.         while(true)
  70.         {
  71.             try
  72.             {
  73.                 sleep(30000);
  74.             }
  75.             catch(InterruptedException e)
  76.             {
  77.             }
  78.  
  79.             if(!chat.isAlive())
  80.             {
  81.                 System.err.println("Restarting chat server");
  82.                 chat = new Chat(base_port + 4, chatThreadGroup, userver);
  83.                 chat.start();
  84.             }
  85.  
  86.             if(!update.isAlive())
  87.             {
  88.                 System.err.println("Restarting update server");
  89.                 update = new Update(base_port + 2, updateThreadGroup, userver);
  90.                 update.start();
  91.             }
  92.  
  93.             if(!filter.isAlive())
  94.             {
  95.                 System.err.println("Restarting filter server");
  96.                 filter = new Filter(base_port, filterThreadGroup, userver);
  97.                 filter.start();
  98.             }
  99.  
  100.             if(!registry.isAlive())
  101.             {
  102.                 System.err.println("Restarting registry server");
  103.                 registry = new Registry(base_port, registryThreadGroup, rserver);
  104.                 registry.start();
  105.             }
  106.  
  107.             if(!rserver.isAlive())
  108.             {
  109.                 System.err.println("Registry server has died, exiting.");
  110.                 System.exit(2);
  111.             }
  112.  
  113.             if(!userver.isAlive())
  114.             {
  115.                 System.err.println("User server has dies, exiting");
  116.                 System.exit(3);
  117.             }
  118.  
  119.             Thread.yield();
  120.         }
  121.     }
  122.  
  123.     public static void main(String[] args)
  124.     {
  125.         int    i;
  126.         int    port = 3000;
  127.         boolean _debug = false;
  128.  
  129.         if(args.length > 0)
  130.         {
  131.             for(i = 0; i < args.length; i++)
  132.             {
  133.                 if(args[i].charAt(0) == '-')
  134.                 {
  135.                     if(args[i].equals("-d"))
  136.                         _debug = true;
  137.                     else
  138.                     {
  139.                         System.out.println("Usage: muserver [-d] [port]");
  140.                         System.exit(1);
  141.                     }
  142.                 }
  143.                 else
  144.                 {
  145.                     try
  146.                     {
  147.                         port = Integer.parseInt(args[i]);
  148.                     }
  149.                     catch (NumberFormatException e)
  150.                     {
  151.                         System.out.println("Invalid number for port");
  152.                         System.out.println("using default port 3000");
  153.  
  154.                         // paranoia setting
  155.                         port = 3000;
  156.                     }
  157.                     break;
  158.                 }
  159.             }
  160.         }
  161.  
  162.         new muserver(port, _debug);
  163.     }
  164. }
  165.  
  166.