home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch19 / filter / FilterMessage.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.1 KB  |  132 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 filter;
  12.  
  13. import java.net.*;
  14. import java.io.*;
  15.  
  16. class FilterMessage extends Thread
  17. {
  18.     // these define the flags in bytes 2 & 3
  19.     private static final int SUSPEND = 0x0001;
  20.     private static final int REQUEST_REFRESH = 0x0002;
  21.     private static final int GONE = 0x0004;
  22.  
  23.     private static int     msg_number = 0;
  24.  
  25.     private UserServer    server;
  26.     private DatagramPacket _packet;
  27.         private int     sequence_num;
  28.         private int     text_id;
  29.     private int     flags = 0;
  30.     private int     port = 0;
  31.     private float[] view_pos = new float[3];
  32.     private float    acuity = 0;
  33.     private int    horizon = 0;
  34.     private int    region_count = 0;
  35.     private int     port_count = 0;
  36.     private int[]    region_list;
  37.     private int[]    port_list;
  38.         
  39.  
  40.     // all that gets done here is to copy over the packet into the internal
  41.     // reference. We leave all the real work to the run method because that
  42.     // allows java's thread handling system to share the workload much better.
  43.     // If we didn't then the whole chat Server thread would block until we have
  44.     // dealt with the response here.
  45.     public FilterMessage(DatagramPacket packet, ThreadGroup tg, UserServer userver)
  46.     {
  47.         super(tg, "Filter Message " + msg_number++);
  48.  
  49.         setPriority(5);
  50.  
  51.         _packet = packet;
  52.         server = userver;
  53.     }
  54.  
  55.     // analyse the packet and send out the text to the other users.
  56.     public void run()
  57.     {
  58.         int    i;
  59.         ByteArrayInputStream buffer = 
  60.             new ByteArrayInputStream(_packet.getData());
  61.         DataInputStream data = new DataInputStream(buffer);
  62.         String hostname = _packet.getAddress().getHostName();
  63.     
  64.         try
  65.         {
  66.             // get the response port number
  67.             port = data.readUnsignedShort();
  68.         
  69.             // get flags
  70.             flags = data.readUnsignedShort();
  71.  
  72.             // get view point position
  73.             view_pos[0] = data.readFloat();
  74.             view_pos[1] = data.readFloat();
  75.             view_pos[2] = data.readFloat();
  76.  
  77.             // acuity
  78.             acuity = data.readFloat();
  79.  
  80.             // horizon count
  81.             horizon = data.readInt();
  82.  
  83.             // list of regions
  84.             region_count = data.readUnsignedShort();
  85.  
  86.             region_list = new int[region_count];
  87.  
  88.             for(i = 0; i < region_count; i++)
  89.             {
  90.                 region_list[i] = data.readUnsignedShort();
  91.             }
  92.  
  93.             // deal with the ports
  94.             port_count = data.readUnsignedShort();
  95.  
  96.             port_list = new int[port_count];
  97.  
  98.             for(i = 0; i < port_count; i++)
  99.             {
  100.                 port_list[i] =  data.readUnsignedShort();
  101.             }
  102.  
  103.             switch(flags)
  104.             {
  105.                 case SUSPEND:
  106.                     server.suspend(hostname);
  107.                     break;
  108.                 case REQUEST_REFRESH:
  109.                     server.refresh(hostname);
  110.                     break;
  111.                 case GONE:
  112.                     server.remove(hostname);
  113.                     break;
  114.                 default:
  115.                     // now tell the users server to add the filter message
  116.                     server.set_filter(hostname,
  117.                               port,
  118.                               acuity,
  119.                               horizon,
  120.                               region_list,
  121.                               port_list);
  122.             }
  123.         }
  124.         catch(IOException e)
  125.         {
  126.             System.err.println("Filter server error " + e.getMessage());
  127.         }    
  128.  
  129.         // all done, go and process it now.
  130.     }
  131. }
  132.