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

  1. // A local entity
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. public class LocalEntity {
  11.  
  12.     static InetAddress address;
  13.     static int port;
  14.  
  15.     static ByteArrayOutputStream bout = new ByteArrayOutputStream();
  16.     static DataOutputStream dout = new DataOutputStream(bout);
  17.     static DatagramSocket socket;
  18.  
  19.     static void setHost(InetAddress addr, int prt)
  20.         throws SocketException {
  21.         address = addr;
  22.         port = prt;
  23.         socket = new DatagramSocket();
  24.     }
  25.  
  26.     World world;
  27.     int id;
  28.     String cname;
  29.     String url;
  30.     String[] info;
  31.     boolean is_avatar = true;
  32.     boolean is_persistent = false;
  33.     int text_id;
  34.     String nickname;
  35.  
  36.     int seqnum = 0;
  37.     Vec3 location = new Vec3();
  38.     Rotation orientation = new Rotation();
  39.     long timestamp = System.currentTimeMillis();
  40.     int region = 0;
  41.     float size = 2f;
  42.     boolean gone = false;
  43.  
  44.     public int getId() { return id; }
  45.     public String getName() { return cname; }
  46.     public synchronized String getURL() { return url; }
  47.     public synchronized void setURL(String s) { url = s; }
  48.     public synchronized String getNickName() { return nickname; }
  49.     public synchronized void setNickName(String name) { nickname = new String(name); }
  50.     public synchronized String[] getInfo() { return info; }
  51.     public synchronized void setInfo(String[] inf) { info = inf; }
  52.     public synchronized boolean isAvatar() { return is_avatar; }
  53.     public synchronized void setAvatar(boolean flag) { is_avatar = flag; }
  54.     public synchronized boolean isPersistent() { return is_persistent; }
  55.     public synchronized void setPersistent(boolean per) { is_persistent = per; }
  56.     public synchronized int getTextId() { return text_id; }
  57.     public synchronized void setTextId(int id) { text_id = id; }
  58.  
  59.     public synchronized Vec3 getLocation() { return location; }
  60.     public synchronized void setLocation(Vec3 loc) { location = loc; }
  61.     public synchronized Rotation getOrientation() { return orientation; }
  62.     public synchronized void setOrientation(Rotation ori) { orientation = ori; }
  63.     public synchronized long getTimestamp() { return timestamp; }
  64.     public synchronized void setTimestamp(long time) { timestamp = time; }    
  65.     public synchronized int getRegion() { return region; }
  66.     public synchronized void setRegion(int r) { region = r; }
  67.     public synchronized float getSize() { return size; }
  68.     public synchronized void setSize(float siz) { size = siz; }
  69.  
  70.     public LocalEntity(World wrld, String name)
  71.         throws PermissionDeniedException {
  72.         world = wrld;
  73.         cname = new String(name);
  74.         synchronized(world.registry_socket) {
  75.             String response;
  76.             world.registry_output.println("ALLOC " + cname);
  77.             try { response = world.registry_input.readLine(); }
  78.             catch (IOException ex) {
  79.                 throw new PermissionDeniedException("bad response");
  80.             }
  81.             if (response.startsWith("REFUSED"))
  82.                 throw new PermissionDeniedException(response.substring(8));
  83.             id = Integer.parseInt(response);
  84.         }
  85.         world.local_entities.addElement(this);
  86.     }
  87.  
  88.     public void finalize() {
  89.         gone = true;
  90.         try { sendUpdate(); } catch(IOException ex) { }
  91.         if (is_persistent == false) {
  92.             synchronized(world.registry_socket) {
  93.                 world.registry_output.println("RELEASE " + id);
  94.                 try { world.registry_input.readLine(); }
  95.                 catch (IOException ex) { }
  96.             }
  97.         }
  98.     }
  99.  
  100.     public synchronized void sendUpdate() throws IOException {
  101.         synchronized (bout) {
  102.             bout.reset();
  103.             dout.writeByte(0x80);  // version = 2, padding = 0, extension = 0, CSRC count = 0
  104.             dout.writeByte(79);    // mark = 0, payload type = 79
  105.             dout.writeShort(seqnum);
  106.             dout.writeInt((int) (timestamp >> 16));
  107.             dout.writeInt(id);
  108.             dout.writeFloat(location.getX());
  109.             dout.writeFloat(location.getY());
  110.             dout.writeFloat(location.getZ());
  111.             dout.writeFloat(orientation.getX());
  112.             dout.writeFloat(orientation.getY());
  113.             dout.writeFloat(orientation.getZ());
  114.             dout.writeFloat(orientation.getAngle());
  115.             dout.writeInt(region);
  116.             dout.writeFloat(size);
  117.             dout.writeInt(gone ? 0x0001 : 0x0000);  // flags
  118.             DatagramPacket packet = new DatagramPacket(bout.toByteArray(),
  119.                                   bout.size(), address, port);
  120.             ++seqnum;  // move up
  121.             synchronized(socket) {
  122.                 socket.send(packet);
  123.             }
  124.         }
  125.     }
  126.  
  127.     private void validate_response()
  128.         throws IOException, PermissionDeniedException {
  129.         String response = world.registry_input.readLine();
  130.         String msg = "";
  131.         if (response.length() > 7)
  132.             msg = response.substring(8);
  133.         if (response.startsWith("REFUSED"))
  134.             throw new PermissionDeniedException(msg);
  135.     }
  136.  
  137.     public synchronized void updateRegistry()
  138.         throws IOException, PermissionDeniedException {
  139.         synchronized(world.registry_socket) {
  140.             world.registry_output.println("URL " + id + " " + ((url == null) ? "no_url" : url));
  141.             validate_response();
  142.             world.registry_output.println("PERSISTENT " + id + " " + is_persistent);
  143.             validate_response(); 
  144.             world.registry_output.println("AVATAR " + id + " " + is_avatar);
  145.             validate_response();
  146.             world.registry_output.println("TEXTID " + id + " " + text_id);
  147.             validate_response();
  148.             world.registry_output.println("NICKNAME " + id + " " + ((nickname == null) ? "guest" : nickname));
  149.             validate_response();
  150.             if (info != null) {
  151.                 world.registry_output.println("INFO " + id);
  152.                 validate_response(); 
  153.                 for (int i = 0; i < info.length; ++i)
  154.                     if (info[i] != null)
  155.                         world.registry_output.println(info[i]);
  156.                 world.registry_output.println(".");
  157.             }
  158.         }
  159.     }
  160.  
  161. }
  162.  
  163.