home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-16 | 2.3 KB | 90 lines |
- // Simple Multiuser Server
- // (c) Copyright Justin Couch justin@vlc.com.au
- // The Virtual Light Company 1996
- //
- // From Chapter 20: Late Night VRML 2.0 and java
- //
- // This is the Chat message handler class. The real work of parsing an
- // individual message is done here and sending out messages to the other
- // users based on whatever filtering parameters are needed.
-
- package chat;
-
- import java.net.*;
- import java.io.*;
- import filter.UserServer;
-
- class ChatMessage extends Thread
- {
- private static int msg_number = 0;
-
- private int sequence_num;
- private int text_id;
- private int msg_type;
- private int msg_length;
- private String msg;
- private UserServer server;
- private DatagramPacket _packet;
-
- // all that gets done here is to copy over the packet into the internal
- // reference. We leave all the real work to the run method because that
- // allows java's thread handling system to share the workload much better.
- // If we didn't then the whole chat Server thread would block until we have
- // dealt with the response here.
- public ChatMessage(DatagramPacket packet, ThreadGroup tg, UserServer userver)
- {
- super(tg, "Chat message " + msg_number++);
-
- setPriority(5);
-
- System.out.println("Got new chat message");
-
- _packet = packet;
- server = userver;
- }
-
- // analyse the packet and send out the text to the other users.
- public void run()
- {
- int i;
- ByteArrayInputStream buffer =
- new ByteArrayInputStream(_packet.getData());
- DataInputStream data = new DataInputStream(buffer);
-
- try
- {
- // some quick checks on the first couple of bytes
- if(data.readUnsignedByte() != 0x80) // hex value
- System.out.println("Chat error: First byte not 0x80");
-
- if(data.readUnsignedByte() != 80) // decimal value
- System.out.println("Chat error: Second byte not 80");
-
- // sequence number
- sequence_num = data.readUnsignedShort();
-
- // timestamp is ignored
- data.readInt();
-
- // Synchronisation source - text_id
- text_id = data.readInt();
-
- // message type
- msg_type = data.readUnsignedShort();
-
- // message length
- msg_length = data.readUnsignedShort();
-
- // the message (at last!)
- msg = data.readLine();
-
- server.send_chat(sequence_num, msg_type, text_id, msg);
- }
- catch(IOException e)
- {
- System.err.println("Chat server error " + e.getMessage());
- }
- }
- }
-
-