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

  1. // Definition of an entity for the multi-user client
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.util.*;
  8.  
  9. public class Entity {
  10.     int id;                     // entity id; also, SSRC for main RTP session
  11.     int state = OKAY;           // OKAY, STALE, DEAD, GONE
  12.     boolean needInfo = true;    // true if we need to update our registry info
  13.  
  14.     boolean hidden = false;     // user doesn't want to see this avatar
  15.     boolean muted = false;      // user doesn't want to hear this avatar (including text)
  16.  
  17.     // from update messages:
  18.     boolean changed = false;    // true if we've changed recently
  19.     long lastHeardFrom;         // local timestamp of last update from entity
  20.     Vec3 location;              // X, Y, Z
  21.     Rotation orientation;       // rotation axis and angle
  22.     long timestamp;             // time for which location and orientation
  23.     int region = 0;             // region that this entity is in
  24.     float size = 0;             // approximate size (for acuity filtering)
  25.     int seqnum = 0;             // sequence number of most recent update
  26.  
  27.     // from registry host:
  28.     boolean registry_changed;   // set if the registry data has changed
  29.     String cname;               // unique canonical name (as per RTP)
  30.     String url;                 // URL of file describing the entity (usually VRML)
  31.     boolean has_new_url;        // new URL has been set
  32.     String nickname;            // entity's nickname
  33.     long creationTime;          // world timestamp of entity's creation
  34.     boolean persistent = false; // true if entity persists after owner leaves
  35.     boolean avatar = false;     // true if the entity is run by a human
  36.     String owner;               // username of entity's owner
  37.     String[] info;              // additional (SDES-style) entity info
  38.  
  39.     int text_SSRC;              // SSRC of text data
  40.     // unused as yet:
  41.     int audio_SSRC;             // SSRC of audio data
  42.     int facial_expression_SSRC; // SSRC of MPEG-4 facial expression data
  43.     int body_posture_SSRC;      // SSRC of MPEG-4 body posture data
  44.  
  45.     Object representation;      // reference to VRML node of shared object (for example)
  46.  
  47.     // accessor methods:
  48.     public int getId() { return id; }
  49.     public synchronized int getState() { return state; }
  50.     public synchronized void setState(int state) { state = state; }
  51.     public final static int OKAY = 0, STALE = 1, DEAD = 2, GONE = 3;
  52.     public synchronized boolean needsInfo() { return needInfo; }
  53.     public synchronized void needsAllInfo() { needInfo = true; }
  54.     public synchronized void gotAllInfo() { needInfo = false; }
  55.     public synchronized boolean hasNewURL() { return has_new_url; }
  56.     public synchronized void markNewURL() { has_new_url = true; }
  57.     public synchronized void unmarkNewURL() { has_new_url = false; }
  58.  
  59.     public synchronized boolean isHidden() { return hidden; }
  60.     public synchronized void Hide(boolean flag) { hidden = flag; changed = true; }
  61.     public synchronized boolean isMuted() { return muted; }
  62.     public synchronized void Mute(boolean flag) { muted = flag; changed = true; }
  63.  
  64.     public synchronized int getTextSSRC() { return text_SSRC; }
  65.     public synchronized void setTextSSRC(int ssrc) { text_SSRC = ssrc; }
  66.  
  67.     public synchronized Vec3 getLocation() { return location; }
  68.     public synchronized Rotation getOrientation() { return orientation; }
  69.     public synchronized long getTimestamp() { return timestamp; }
  70.     public synchronized int getRegion() { return region; }
  71.     public synchronized float getSize() { return size; }
  72.     public synchronized long getLastUpdate() { return lastHeardFrom; }
  73.     public synchronized boolean hasChanged() { return changed; }
  74.     public synchronized void markUnchanged() { changed = false; }
  75.     public int getSequenceNumber() { return seqnum; }
  76.  
  77.     public synchronized String getName() { return cname; }
  78.     public synchronized void setName(String name) { cname = new String(name); }
  79.     public synchronized String getURL() { return url; }
  80.     public synchronized void setURL(String u) { url = new String(u); }
  81.     public synchronized String getNickName() { return nickname; }
  82.     public synchronized void setNickName(String name) { nickname = new String(name); }
  83.     public synchronized long getCreationTime() { return creationTime; }
  84.     public synchronized void setCreationTime(long time) { creationTime = time; }
  85.     public synchronized boolean isPersistent() { return persistent; }
  86.     public synchronized void setPersistent(boolean flag) { persistent = flag; }
  87.     public synchronized boolean isAvatar() { return avatar; }
  88.     public synchronized void setAvatar(boolean flag) { avatar = flag; }
  89.     public synchronized String getOwner() { return owner; }
  90.     public synchronized void setOwner(String own) { owner = new String(own); }
  91.     public synchronized String[] getInfo() { return info; }
  92.     public synchronized void setInfo(String[] inf) { info = inf; }
  93.     public synchronized boolean registryHasChanged() { return registry_changed; }
  94.     public synchronized void markRegistryChanged() { registry_changed = true; }
  95.     public synchronized void markRegistryUnchanged() { registry_changed = false; }
  96.  
  97.     public synchronized Object getRepresentation() { return representation; }
  98.     public synchronized void setRepresentation(Object obj) { representation = obj; }
  99.  
  100.     public synchronized void dump() {
  101.         System.out.println("Entity " + id + " (state = " + state + ")");
  102.         System.out.println("\tlocation = " + location + ", orientation = " + orientation);
  103.         System.out.println("\ttimestamp = " + timestamp);
  104.         System.out.println("\tregion = " + region + ", size = " + size);
  105.         System.out.println("\tchanged = " + changed + ", last heard from = " + lastHeardFrom);
  106.         if (text_SSRC != 0)
  107.             System.out.println("Text SSRC = " + text_SSRC);
  108.         if (needInfo == false) {
  109.             if (nickname != null) System.out.println("\tnickname = " + nickname);
  110.             System.out.println("\tname = " + cname + "; url = " + url);
  111.             if (owner != null) System.out.println("\towner = " + owner);
  112.             System.out.println("\tavatar = " + avatar + ", persistent = " + persistent);
  113.             System.out.println("\tcreation time = " + creationTime);
  114.             System.out.println("\trepresentation = " + representation);
  115.             if (info != null)
  116.                 for (int i = 0; i < info.length; ++i)
  117.                     if (info[i] != null)
  118.                         System.out.println("\t" + info[i]);
  119.         }
  120.     }
  121.  
  122.     // constructor -- not public!
  123.     Entity(int entid) {
  124.         id = entid;
  125.         lastHeardFrom = System.currentTimeMillis(); 
  126.         location = new Vec3(0, 0, 0);
  127.         orientation = new Rotation(0, 1, 0, 0);
  128.     }
  129.  
  130.     public synchronized void update(UpdateMessage update) {
  131.         if (update.getEntityId() != id)
  132.             // id mismatch; ignore it
  133.             return;
  134.         if (update.getSequenceNumber() < seqnum)
  135.             // packet is out-of-sequence; ignore it
  136.             return;
  137.         changed = true;
  138.         location = update.getLocation();
  139.         orientation = update.getOrientation();
  140.         timestamp = update.getTimestamp();
  141.         region = update.getRegion();
  142.         size = update.getSize();
  143.         if (update.isGone())
  144.             state = GONE;
  145.         else
  146.             state = OKAY;
  147.         lastHeardFrom = System.currentTimeMillis(); 
  148.         }
  149.  
  150.     public synchronized void age() {
  151.         if ((lastHeardFrom - System.currentTimeMillis()) > 10000)
  152.             state = STALE;  // 10 seconds since last update
  153.     }
  154.  
  155. }
  156.  
  157.