home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-16 | 10.3 KB | 496 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 Registry 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 registry;
-
- import java.net.*;
- import java.io.*;
- import java.util.*;
- import registry.RegistryServer;
-
- class RegistryMessage extends Thread
- {
- private static int msg_number = 0;
- private boolean debug = false;
-
- private int sequence_num;
- private int text_id;
- private int msg_type;
- private int msg_length;
- private String msg;
- private RegistryServer _server;
- private Socket _socket;
- private String nickname;
- private String identity;
-
- private DataInputStream in;
- private PrintStream out;
-
- private Vector entity_list;
-
- // 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 RegistryMessage(Socket s, ThreadGroup tg, RegistryServer server, boolean debug)
- {
- super(tg, "Registry Connection " + msg_number++);
-
- setPriority(5);
-
- this.debug = debug;
- _socket = s;
- _server = server;
-
- System.out.print("New connection from ");
- System.out.println(_socket.getInetAddress().getHostName());
-
- entity_list = new Vector(10, 5);
-
- nickname = "Guest";
- identity = "Guest";
- }
-
- // analyse the packet and send out the text to the other users.
- public void run()
- {
- String input = null;
-
- try
- {
- in = new DataInputStream(_socket.getInputStream());
- out = new PrintStream(_socket.getOutputStream());
- }
- catch(IOException e)
- {
- System.err.println("Registry Server: error getting I/O streams " + e.getMessage());
- }
-
- while(true)
- {
- try
- {
- input = in.readLine();
- if((input != null) && (!processCommand(input)))
- break;
-
- Thread.yield();
- }
- catch(IOException e)
- {
- System.err.println("Registry Message error " + e.getMessage());
- }
-
- }
-
- try
- {
- in.close();
- out.close();
- _socket.close();
- }
- catch(IOException e)
- {
- System.err.println("Registry Message error " + e.getMessage());
- }
-
- System.out.print("closing registry connection from ");
- System.out.println(_socket.getInetAddress().getHostName());
- }
-
- private boolean processCommand(String command)
- throws IOException
- {
- StringTokenizer strtok;
- String token;
- int entid;
- int i;
- boolean set_ok;
-
- if(debug)
- System.out.println("command recieved: " + command);
-
- strtok = new StringTokenizer(command);
-
- try
- {
- token = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED invalid character in command");
- return true;
- }
-
- if(token.equals("HELLO"))
- out.println("WELCOME");
- else if(token.equals("LIST"))
- {
- _server.printList(out);
- out.println(".");
- }
- else if(token.equals("GETINFO"))
- {
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- _server.printInfo(entid, out);
- out.println(".");
- }
- else if(token.equals("GETENTITY"))
- {
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED no entity ID given");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- _server.printEntityInfo(entid, out, nickname);
- out.println(".");
- }
- else if(token.equals("INFO"))
- {
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED no entity ID given");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- // check the ownership of the object
- if(!_server.isOwner(entid, identity))
- {
- out.println("REFUSED not owner");
- return true;
- }
-
- // ok, so we own it, lets continue.
- out.println("OKAY");
-
- Vector info_list = new Vector(5,2);
- String tmp;
-
- // first read in all the data to a buffer.
- while(true)
- {
- tmp = in.readLine();
- if(tmp.equals("."))
- break;
- info_list.addElement(tmp);
- }
-
- // now copy it over to a string array.
- String[] str_list = new String[info_list.size()];
-
- for(i = 0; i < info_list.size(); i++)
- str_list[i] = (String)info_list.elementAt(i);
-
- _server.set_info(entid, identity, str_list);
- }
- else if(token.equals("GOODBYE"))
- {
- // first, clean up all the entities that belong to this
- // client. Just loop and instruct the server to delete
- // every one of them.
- for(i = 0; i < entity_list.size(); i++)
- _server.delete(
- (int)((Integer)entity_list.elementAt(i)).intValue(),
- identity);
- entity_list = null;
-
- return false;
- }
- else if(token.equals("ALLOC"))
- {
- String name;
- try
- {
- name = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED entity cname not given");
- return true;
- }
-
- if(identity.equals("Guest"))
- {
- out.println("REFUSED No ident set");
- return true;
- }
-
- // now that we have passed muster we can allocate something
- entid = _server.allocate(name, identity);
-
- switch(entid)
- {
- case -1:
- out.println("REFUSED Entity cname in use");
- break;
- case -2:
- out.println("REFUSED User not identified");
- break;
- default:
- out.println(entid);
- entity_list.addElement(new Integer(entid));
- }
- }
- else if(token.equals("URL"))
- {
- String url;
-
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- url = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- if(_server.set_URL(entid, identity, url))
- out.println("OKAY");
- else
- out.println("REFUSED no reason available");
- }
- else if(token.equals("NICKNAME"))
- {
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- nickname = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED number format incorrect");
- return true;
- }
-
- if(_server.set_nickname(entid, identity, nickname))
- out.println("OKAY");
- else
- out.println("REFUSED no reason available");
- }
- else if(token.equals("RELEASE"))
- {
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED no entity ID given");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- if(_server.delete(entid, identity))
- out.println("OKAY");
- else
- out.println("REFUSED no reason available");
- }
- else if(token.equals("PERSISTENT"))
- {
- String persist;
-
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- persist = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- if(persist.equals("true"))
- set_ok = _server.set_persistent(entid, identity, true);
- else if(persist.equals("false"))
- set_ok = _server.set_persistent(entid, identity, false);
- else // barf
- {
- out.println("REFUSED Incorrect format");
- return true;
- }
-
- if(set_ok)
- out.println("OKAY");
- else
- out.println("REFUSED no reason available");
-
- }
- else if(token.equals("AVATAR"))
- {
- String av;
-
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- av = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- if(av.equals("true"))
- set_ok = _server.set_avatar(entid, identity, true);
- else if(av.equals("false"))
- set_ok = _server.set_avatar(entid, identity, false);
- else // barf
- {
- out.println("REFUSED Incorrect format");
- return true;
- }
-
- if(set_ok)
- out.println("OKAY");
- else
- out.println("REFUSED no reason available");
-
- }
- else if(token.equals("IDENT"))
- {
- String pass;
- String tmp_id = identity;
-
- try
- {
- tmp_id = strtok.nextToken();
- pass = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- // just in case something screwed up we don't
- // want to loose their current IDENT
- return true;
- }
-
- // now verify with the server
- if(_server.verifyIdent(tmp_id, pass))
- {
- identity = tmp_id;
- out.println("OKAY");
- }
- else
- out.println("REFUSED incorrect username or password");
- }
- else if(token.equals("CHOWN"))
- {
- String new_name;
-
- try
- {
- entid = Integer.parseInt(strtok.nextToken());
- new_name = strtok.nextToken();
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED incomplete command");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect entity ID format");
- return true;
- }
-
- if(_server.chown(entid, identity, new_name))
- out.println("OKAY");
- else
- out.println("REFUSED not owner");
- }
- else if(token.equals("LISTNEW"))
- {
- int time;
- try
- {
- time = Integer.parseInt(strtok.nextToken());
- }
- catch(NoSuchElementException e1)
- {
- out.println("REFUSED command requires a time to be specified");
- return true;
- }
- catch(NumberFormatException e2)
- {
- out.println("REFUSED incorrect time format");
- return true;
- }
-
- _server.listNew(time, out);
- out.println(".");
- }
- else
- out.println("OKAY");
-
- return true;
- }
- }
-