home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch19 / chat / ChatMessage.java < prev   
Encoding:
Java Source  |  1997-02-16  |  2.3 KB  |  90 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 chat;
  12.  
  13. import java.net.*;
  14. import java.io.*;
  15. import filter.UserServer;
  16.  
  17. class ChatMessage extends Thread
  18. {
  19.     private static int    msg_number = 0;
  20.  
  21.     private int sequence_num;
  22.     private int text_id;
  23.     private int msg_type;
  24.     private int msg_length;
  25.     private String msg;
  26.     private UserServer    server;
  27.     private DatagramPacket _packet;
  28.  
  29.     // all that gets done here is to copy over the packet into the internal
  30.     // reference. We leave all the real work to the run method because that
  31.     // allows java's thread handling system to share the workload much better.
  32.     // If we didn't then the whole chat Server thread would block until we have
  33.     // dealt with the response here.
  34.     public ChatMessage(DatagramPacket packet, ThreadGroup tg, UserServer userver)
  35.     {
  36.         super(tg, "Chat message " + msg_number++);
  37.  
  38.         setPriority(5);
  39.  
  40.         System.out.println("Got new chat message");
  41.  
  42.         _packet = packet;
  43.         server = userver;
  44.     }
  45.  
  46.     // analyse the packet and send out the text to the other users.
  47.     public void run()
  48.     {
  49.         int    i;
  50.         ByteArrayInputStream buffer = 
  51.             new ByteArrayInputStream(_packet.getData());
  52.         DataInputStream data = new DataInputStream(buffer);
  53.  
  54.         try
  55.         {
  56.             // some quick checks on the first couple of bytes
  57.             if(data.readUnsignedByte() != 0x80)  // hex value
  58.                 System.out.println("Chat error: First byte not 0x80");
  59.  
  60.             if(data.readUnsignedByte() != 80)  // decimal value
  61.                 System.out.println("Chat error: Second byte not 80");
  62.  
  63.             // sequence number
  64.             sequence_num = data.readUnsignedShort();
  65.  
  66.             // timestamp is ignored
  67.             data.readInt();
  68.  
  69.             // Synchronisation source - text_id
  70.             text_id = data.readInt();
  71.  
  72.             // message type
  73.             msg_type = data.readUnsignedShort();
  74.  
  75.             // message length
  76.             msg_length = data.readUnsignedShort();
  77.  
  78.             // the message (at last!)
  79.             msg = data.readLine();
  80.  
  81.             server.send_chat(sequence_num, msg_type, text_id, msg);
  82.         }
  83.         catch(IOException e)
  84.         {
  85.             System.err.println("Chat server error " + e.getMessage());
  86.         }
  87.     }
  88. }
  89.  
  90.