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

  1. // A world, as it appears at the client API end
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.util.*;
  8. import java.net.*;
  9. import java.io.*;
  10.  
  11. public class World {
  12.     Socket registry_socket;            // tcp socket of registry
  13.     DataInputStream registry_input;    // input stream for that socket
  14.     PrintStream registry_output;       // output stream for that socket
  15.     String banner;                     // received from registry on WELCOME
  16.  
  17.     DatagramSocket incoming_socket;    // used for receiving updates
  18.     int packets_received = 0;          // total number of packets received
  19.  
  20.     long connected_time;               // date and time at which we connected
  21.  
  22.     boolean identified_to_registry = false;
  23.  
  24.     Vec3 location = new Vec3();        // location of our viewpoint
  25.     float acuity = 1.0f;               // size over distance
  26.     int horizon = 0;                   // maximum number of visible entities
  27.     int[] visRegions;                  // list of visible regions
  28.     boolean suspended = false;         // true to supress updates
  29.     boolean request_refresh = false;   // true when we want all new updates
  30.     boolean gone = false;              // set when we're leaving the world
  31.  
  32.     Vector entities = new Vector();    // vector of entities
  33.     Vector local_entities = new Vector();  // vector of locally-created entities
  34.  
  35.     int text_port = 0;                 // port on which client listens for text
  36.  
  37.     FilterSender filter_sender;        // filter sender thread
  38.     UpdateReceiver update_receiver;    // update receiver thread
  39.     Reaper reaper;                     // reaper thread
  40.     RegistryUpdater registry_updater;  // registry updater thread
  41.     ChangeMonitor change_monitor;      // change monitor thread
  42.  
  43.     public String getBanner() { return banner; }
  44.  
  45.     public long getConnectedTime() { return connected_time; }
  46.  
  47.     public synchronized int getPacketCount() { return packets_received; }
  48.  
  49.     public synchronized void setViewpoint(Vec3 loc) {
  50.         location = loc;
  51.         synchronized(filter_sender) { filter_sender.notify(); }
  52.     }
  53.  
  54.     public synchronized void setVisualAcuity(float acu) {
  55.         acuity = acu;
  56.         synchronized(filter_sender) { filter_sender.notify(); }
  57.     }
  58.  
  59.     public synchronized void setVisualHorizonCount(int n) {
  60.         horizon = n;
  61.         synchronized(filter_sender) { filter_sender.notify(); }
  62.     }
  63.  
  64.     public synchronized void setVisibleRegions(int[] regions) {
  65. //        visRegions = regions.clone();
  66.         synchronized(filter_sender) { filter_sender.notify(); }
  67.     }
  68.  
  69.     public synchronized void suspend(boolean flag) {
  70.         suspended = flag;
  71.         synchronized(filter_sender) { filter_sender.notify(); }
  72.     }
  73.  
  74.     public synchronized void refresh() {
  75.         request_refresh = true;
  76.         synchronized(filter_sender) { filter_sender.notify(); }
  77.     }
  78.  
  79.     public synchronized int getTextPort() { return text_port; }
  80.  
  81.     public synchronized void setTextPort(int port) {
  82.         text_port = port;
  83.         synchronized(filter_sender) { filter_sender.notify(); }
  84.     }
  85.  
  86.     // method to enumerate entities -- both global and local
  87.     public Enumeration getEntities() { return entities.elements(); }
  88.     public Enumeration getLocalEntities() { return local_entities.elements(); }
  89.  
  90.     public World(String hostname, int port)
  91.         throws IOException, ConnectionRefusedException
  92.         {
  93.         // connect up to registry host
  94.         InetAddress host = InetAddress.getByName(hostname);
  95.         registry_socket = new Socket(host, port, true);
  96.         registry_input = new DataInputStream(registry_socket.getInputStream());
  97.         registry_output = new PrintStream(registry_socket.getOutputStream(), true);
  98.         registry_output.println("HELLO");
  99.         String response = registry_input.readLine();
  100.         if (response.length() > 7)
  101.             banner = response.substring(8);
  102.         else
  103.             banner = "";
  104.         if (response.startsWith("REFUSED"))
  105.             throw new ConnectionRefusedException(banner);
  106.         connected_time = System.currentTimeMillis();
  107.         // create datagram socket and initialize LocalEntity outgoing info
  108.         incoming_socket = new DatagramSocket();
  109.         LocalEntity.setHost(host, port + 2);  // data port
  110.         // start threads
  111.         update_receiver = new UpdateReceiver(this);
  112.         filter_sender = new FilterSender(this, host, port);
  113.         reaper = new Reaper(this);
  114.         registry_updater = new RegistryUpdater(this);
  115.         change_monitor = new ChangeMonitor(this);
  116.     }
  117.  
  118.     public void finalize() {
  119.         // delete all the local entities
  120.         // ...
  121.         // tell the filter host to stop sending us updates
  122.         gone = true;
  123.         synchronized(filter_sender) { filter_sender.notify(); }
  124.         // shut down the various threads
  125.         change_monitor.stop();
  126.         registry_updater.stop();
  127.         reaper.stop();
  128.         // tell the registry host that we're out of here
  129.         synchronized(registry_socket) {
  130.             registry_output.println("GOODBYE");
  131.             try { registry_socket.close(); } catch(IOException e) { }
  132.         }
  133.         update_receiver.stop();
  134.         filter_sender.stop();
  135.     }
  136.  
  137.     public void identity(String username, String password)
  138.            throws IOException, BadIdentityException {
  139.         String response;
  140.         synchronized(registry_socket) {
  141.             registry_output.println("IDENT " + username + " " + password);
  142.             try { response = registry_input.readLine(); }
  143.             catch(IOException e) {
  144.                 System.out.println("error reading response to IDENTITY: " + e);
  145.                 throw new BadIdentityException("bad response to IDENTITY");
  146.             }
  147.         }
  148.         if (response.startsWith("OKAY"))
  149.             identified_to_registry = true;
  150.         else
  151.             throw new BadIdentityException("no such user or bad password");
  152.     }
  153.  
  154.     public Entity addEntity(int entid) {
  155.         Entity e = new Entity(entid);
  156.         int i;
  157.         for (i = 0; i < entities.size(); ++i)
  158.             if (entities.elementAt(i) == null) {
  159.                 entities.setElementAt(e, i);
  160.                 return e;
  161.             }
  162.         entities.addElement(e);
  163.         return e;
  164.     }
  165.  
  166.     public Entity getEntity(int entid) {
  167.         int i;
  168.         for (i = 0; i < entities.size(); ++i) {
  169.             Entity e = (Entity) entities.elementAt(i);
  170.             if (e.getId() == entid)
  171.                 return e;
  172.         }
  173.         return null;
  174.     }
  175.  
  176.     public Entity getEntityByTextId(int tssrc) {
  177.         int i;
  178.         for (i = 0; i < entities.size(); ++i) {
  179.             Entity e = (Entity) entities.elementAt(i);
  180.             if (e.getTextSSRC() == tssrc)
  181.                 return e;
  182.         }
  183.         return null;
  184.     }
  185.  
  186.  
  187. }
  188.           
  189.  
  190.