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

  1. // Thread that gets updates on entities as needed
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. class RegistryUpdater extends Thread {
  11.  
  12.     World world;
  13.  
  14.     public RegistryUpdater(World wrld) {
  15.         world = wrld;
  16.         setPriority(NORM_PRIORITY-1);
  17.         start();
  18.     }
  19.  
  20.     public void run() {
  21.         while (true) {
  22.             try { sleep(1); }
  23.             catch (InterruptedException ex) { }
  24.             for (int i = 0; i < world.entities.size(); ++i) {
  25.                 Entity e = (Entity) world.entities.elementAt(i);
  26.                 if (e != null) {
  27.                     if (e.needsInfo()) {
  28.                         synchronized(world.registry_socket) {
  29.                             String response;
  30.                             StringTokenizer tokens;
  31.                             world.registry_output.println("GETENTITY " + e.getId());
  32.                             try { response = world.registry_input.readLine(); }
  33.                             catch (IOException ex) {
  34.                                 System.out.println("could not read response to GETENTITY: " + ex);
  35.                                 return;
  36.                             }
  37.                             e.markRegistryChanged();
  38.                             tokens = new StringTokenizer(response);
  39.                             e.setName(tokens.nextToken());
  40.                             String newURL = tokens.nextToken();
  41.                             if (e.getURL() == null 
  42.                                  || !newURL.equals(e.getURL())) {
  43.                                 e.setURL(newURL);
  44.                                 e.markNewURL();
  45.                             }
  46.                             e.setOwner(tokens.nextToken());
  47.                             e.setAvatar(tokens.nextToken().equals("true"));
  48.                             e.setPersistent(tokens.nextToken().equals("true"));
  49.                             e.setCreationTime(Long.parseLong(tokens.nextToken()));
  50.                             try { response = world.registry_input.readLine(); }
  51.                             catch (IOException ex) {
  52.                                 System.out.println("could not read response to GETENTITY: " + ex);
  53.                                 return;
  54.                             }
  55.                             e.setNickName(response);
  56.                             try { response = world.registry_input.readLine(); }
  57.                             catch (IOException ex) {
  58.                                 System.out.println("could not read response to GETENTITY: " + ex);
  59.                                 return;
  60.                             }
  61.                             tokens = new StringTokenizer(response);
  62.                             e.setTextSSRC(Integer.parseInt(tokens.nextToken()));
  63.                             do {
  64.                                 try { response = world.registry_input.readLine(); }
  65.                                 catch (IOException ex) {
  66.                                     System.out.println("could not read response to GETENTITY: " + ex);
  67.                                     return;
  68.                                 }
  69.                             } while (!response.equals("."));
  70.                             world.registry_output.println("GETINFO " + e.getId());
  71.                             String[] info = new String[20];
  72.                             int ind = 0;
  73.                             while (true) {
  74.                                 try { response = world.registry_input.readLine(); }
  75.                                 catch (IOException ex) {
  76.                                     System.out.println("could not read response to GETINFO: " + e);
  77.                                     break;
  78.                                 }
  79.                                 if (response.startsWith("."))
  80.                                     break;
  81.                                 if (ind < info.length)
  82.                                     info[ind++] = new String(response);
  83.                             } 
  84.                             e.setInfo(info);
  85.                             e.gotAllInfo();
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94.