home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / multi / ChangeMonitor.java < prev    next >
Encoding:
Java Source  |  1997-02-21  |  1.4 KB  |  49 lines

  1. // Thread that looks for newly updated entities
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. class ChangeMonitor extends Thread {
  11.  
  12.     World world;
  13.  
  14.     static final int PERIOD = 10000;   // every 10 seconds
  15.  
  16.     public ChangeMonitor(World wrld) {
  17.         world = wrld;
  18.         setPriority(NORM_PRIORITY-1);
  19.         start();
  20.     }
  21.  
  22.     public void run() {
  23.         while (true) {
  24.             try { sleep(PERIOD); }
  25.             catch (InterruptedException e2) { }
  26.             synchronized(world.registry_socket) {
  27.                 world.registry_output.println("LISTNEW " + PERIOD);
  28.                 while (true) {
  29.                     String response;
  30.                     try { response = world.registry_input.readLine(); }
  31.                     catch (IOException ex) {
  32.                         System.out.println("could not read response to LIST NEW: " + ex);
  33.                         break;
  34.                     }
  35.                     if (response.startsWith("."))
  36.                         break;
  37.                     int entid = Integer.parseInt(response);
  38.                     Entity e = world.getEntity(entid);
  39.                     if (e == null)
  40.                         e = world.addEntity(entid);
  41.                     else
  42.                         e.needsAllInfo();
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49.