home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch19 / update / UpdateMessage.java < prev   
Encoding:
Java Source  |  1997-01-27  |  2.9 KB  |  117 lines

  1. // Simple Multiuser Server
  2. // (c) Copyright Justin Couch   justin@vlc.com.au
  3. // The Virtual Light Company 1996
  4. //
  5. // From Chapter 20: Late Night VRML 2.0 and java
  6. //
  7. // This is the Chat message handler class. The real work of parsing an
  8. // individual message is done here and sending out messages to the other
  9. // users based on whatever filtering parameters are needed.
  10.  
  11. package update;
  12.  
  13. import java.net.*;
  14. import java.io.*;
  15. import filter.UserServer;
  16.  
  17. class UpdateMessage extends Thread
  18. {
  19.     private static int    msg_number = 0;
  20.  
  21.     private int sequence_num;
  22.     private int timestamp;
  23.     private int entity_id;
  24.     private float[] position = new float[3];
  25.     private float[] rotation = new float[4];
  26.     private int region_id;
  27.     private float acuity;
  28.     private int flags;
  29.  
  30.     private UserServer    server;
  31.     private DatagramPacket _packet;
  32.  
  33.     // all that gets done here is to copy over the packet into the internal
  34.     // reference. We leave all the real work to the run method because that
  35.     // allows java's thread handling system to share the workload much better.
  36.     // If we didn't then the whole chat Server thread would block until we have
  37.     // dealt with the response here.
  38.     public UpdateMessage(DatagramPacket packet, ThreadGroup tg, UserServer userver)
  39.     {
  40.         super(tg, "Update Message " + msg_number++);
  41.  
  42.         setPriority(5);
  43.  
  44.         _packet = packet;
  45.         server = userver;
  46.     }
  47.  
  48.     // analyse the packet and send out the text to the other users.
  49.     public void run()
  50.     {
  51.         int    i;
  52.         ByteArrayInputStream buffer = 
  53.             new ByteArrayInputStream(_packet.getData());
  54.         DataInputStream data = new DataInputStream(buffer);
  55.  
  56.         try
  57.         {
  58.             // some quick checks on the first couple of bytes. If they are not
  59.             // correct then just dump the packet by exiting.
  60.             if(data.readUnsignedByte() != 0x80)  // hex value
  61.             {
  62.                 System.out.println("Update packet error: First byte not 0x80");
  63.                 return;
  64.             }
  65.  
  66.             if(data.readUnsignedByte() != 79)  // decimal value
  67.             {
  68.                 System.out.println("Update packet error: Second byte not 79");
  69.                 return;
  70.             }
  71.  
  72.             // sequence number
  73.             sequence_num = data.readUnsignedShort();
  74.  
  75.             // timestamp
  76.             timestamp = data.readInt();
  77.  
  78.             // Synchronisation source - Entity ID
  79.             entity_id = data.readInt();
  80.  
  81.             // position
  82.             position[0] = data.readFloat();
  83.             position[1] = data.readFloat();
  84.             position[2] = data.readFloat();
  85.  
  86.             // orientation
  87.             rotation[0] = data.readFloat();
  88.             rotation[1] = data.readFloat();
  89.             rotation[2] = data.readFloat();
  90.             rotation[3] = data.readFloat();
  91.  
  92.             // Region
  93.             region_id = data.readInt();
  94.  
  95.             // acuity
  96.             acuity = data.readFloat();
  97.  
  98.             // flags
  99.             flags = data.readInt();
  100.  
  101.             server.send_update(_packet.getAddress().getHostName(),
  102.                                timestamp,
  103.                                sequence_num,
  104.                                entity_id,
  105.                                position,
  106.                                rotation,
  107.                                region_id,
  108.                                acuity);
  109.         }
  110.         catch(IOException e)
  111.         {
  112.             System.err.println("Update server error" + e.getMessage());
  113.         }
  114.     }
  115. }
  116.  
  117.