home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-27 | 6.3 KB | 332 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 class. Details about each avatar are kept here.
- // Avatar entity IDs start from 1
-
- package registry;
-
- import java.io.*;
- import java.util.*;
-
- public class RegistryServer extends Thread
- {
- private static int next_entity_id = 0;
-
- private Vector entity_list;
- private Hashtable name_list;
- private Hashtable user_identities;
-
- public RegistryServer(ThreadGroup tg)
- {
- super(tg, "Registry Server Thread");
-
- setPriority(7);
-
- System.out.println("Starting Registry Server");
-
- entity_list = new Vector(10,2);
- name_list = new Hashtable(100, 0.8f);
- user_identities = new Hashtable(20, 0.8f);
- }
-
- // allocate a new entity.
- // return values:
- // -1 if cname already in use
- // -2 if user not identified
- // > 0 sucessful, the new id number
- // The caller must make sure that the ident is legally valid to add
- // a new entity.
- public synchronized int allocate(String cname, String ident)
- {
- Entity entity;
-
- // first check the name list for the given entity;
- if(name_list.get(cname) != null)
- return(-1);
-
- next_entity_id++;
-
- entity = new Entity(cname, next_entity_id, ident);
-
- // need to add an entity class to the Vector
- entity_list.addElement(entity);
- name_list.put(cname, entity);
-
- return(next_entity_id);
- }
-
- public synchronized boolean set_URL(int entid, String ident, String url)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(ident))
- return false;
-
- e.set_URL(url);
- break;
- }
- }
-
- return true;
- }
-
- public synchronized boolean set_nickname(int entid, String ident, String new_name)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(ident))
- return false;
-
- e.set_nickname(new_name);
- break;
- }
- }
-
- return true;
- }
-
- public synchronized boolean chown(int entid, String old_owner, String new_owner)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(old_owner))
- return false;
-
- e.set_owner(new_owner);
- break;
- }
- }
-
- return true;
- }
-
- public synchronized boolean set_persistent(int entid, String ident, boolean persist)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(ident))
- return false;
-
- e.set_persistent(persist);
- break;
- }
- }
-
- return true;
- }
-
- public synchronized boolean set_avatar(int entid, String ident, boolean avatar)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(ident))
- return false;
-
- e.set_avatar(avatar);
- break;
- }
- }
-
- return true;
- }
-
- public synchronized boolean set_info(int entid, String ident, String[] info)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(ident))
- return false;
-
- e.set_publicInfo(info);
- break;
- }
- }
-
- return true;
- }
-
- public synchronized void printInfo(int entid, PrintStream out)
- {
- int i, j;
- Entity e;
- String[] info;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- info = e.get_publicInfo();
-
- if(info == null)
- break;
-
- for(j = 0; j < info.length; j++)
- out.println(info[j]);
-
- break;
- }
- }
- }
-
- public synchronized void printEntityInfo(int entid, PrintStream out, String nick)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- out.print(e.get_cname() + " " + e.get_URL() + " ");
- out.print(e.get_owner() + " " + e.isAvatar() + " ");
- out.print(e.isPersistent() + " ");
- out.println(e.get_registrationTime());
- out.println(e.get_nickname());
- // this is incorrect because it should be a list
- // of the SSRC values. not just a single flag.
- out.println(e.get_flags());
- break;
- }
- }
-
- }
-
- public synchronized void printList(PrintStream out)
- {
- int i;
- for(i = 0; i < entity_list.size(); i++)
- out.println(((Entity)entity_list.elementAt(i)).ID);
- }
-
- public synchronized boolean delete(int entid, String ident)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- // currently no checking on the ownership of the entity.
- if(e.ID == entid)
- {
- if(!e.get_owner().equals(ident))
- return false;
-
- entity_list.removeElementAt(i);
- name_list.remove(e.get_cname());
- break;
- }
- }
-
- return true;
- }
-
- public boolean verifyIdent(String uname, String passwd)
- {
- String curr_passwd = (String)user_identities.get(uname);
-
- if(curr_passwd == null)
- {
- user_identities.put(uname, passwd);
- return true;
- }
-
- return(curr_passwd.equals(passwd));
- }
-
- public synchronized boolean isOwner(int entid, String ident)
- {
- int i;
- Entity e;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- if(e.ID == entid)
- {
- if(e.get_owner().equals(ident))
- return true;
- break;
- }
- }
-
- return false;
- }
-
- public synchronized void listNew(int time, PrintStream out)
- {
- int i;
- Entity e;
- long earliest_time = System.currentTimeMillis() - time;
-
- for(i = 0; i < entity_list.size(); i++)
- {
- e = (Entity)entity_list.elementAt(i);
-
- if(e.get_lastUpdateTime() > earliest_time)
- out.println(e.ID);
- }
- }
- }
-
-