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

  1. // The Virtual Light Company 1996
  2. //
  3. // From Chapter 20: Late Night VRML 2.0 and java
  4. //
  5. // This is the update data server starter class.
  6.  
  7. package update;
  8.  
  9. import java.net.*;
  10. import java.io.*;
  11. import filter.UserServer;
  12.  
  13. public class Update extends Thread
  14. {
  15.     private static final int BUFFER_LENGTH = 1024;
  16.     private int    _port;
  17.     private boolean        server_available = false;
  18.     private DatagramSocket    listen_socket = null;
  19.     private UpdateManager    manager = null;
  20.     private UserServer    server;
  21.  
  22.     private ThreadGroup    thread_group;
  23.  
  24.     public Update(int port, ThreadGroup tg, UserServer userver)
  25.     {
  26.         super(tg, "Update Server Thread");
  27.  
  28.         thread_group = tg;
  29.  
  30.         _port = port;
  31.  
  32.         System.out.println("Starting Filter Server on UDP port " + port);
  33.         try
  34.         {
  35.             listen_socket = new DatagramSocket(port);
  36.         }
  37.         catch(IOException e)
  38.         {
  39.             System.err.println("Unable to open Update socket " + e.getMessage());
  40.             return;
  41.         }
  42.  
  43.         manager = new UpdateManager();
  44.  
  45.         server = userver;
  46.         server_available = true;
  47.     }
  48.  
  49.     public void run()
  50.     {
  51.         byte[]    buffer;
  52.         DatagramPacket packet;
  53.         UpdateMessage a_update;
  54.  
  55.         // if the server failed to start then exit and do nothing.
  56.         if(!server_available)
  57.             return;
  58.  
  59.         // tell the chat manager to start running
  60.         manager.start();
  61.  
  62.         // now process the input.
  63.         while(true)
  64.         {
  65.             buffer = new byte[BUFFER_LENGTH];
  66.             packet = new DatagramPacket(buffer, BUFFER_LENGTH);
  67.  
  68.             try
  69.             {
  70.                 listen_socket.receive(packet);
  71.                 Thread.yield();
  72.             }
  73.             catch(IOException e)
  74.             {
  75.                 System.err.println("packet error");
  76.             }
  77.  
  78.             a_update = new UpdateMessage(packet, thread_group, server);
  79.             a_update.run();
  80.             manager.add(a_update);
  81.         }
  82.     }
  83. }
  84.  
  85.