home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch19 / registry / RegistryServer.java < prev   
Encoding:
Java Source  |  1997-01-27  |  6.3 KB  |  332 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 Registry  class. Details about each avatar are kept here.
  8. // Avatar entity IDs start from 1
  9.  
  10. package registry;
  11.  
  12. import java.io.*;
  13. import java.util.*;
  14.  
  15. public class RegistryServer extends Thread
  16. {
  17.     private static int next_entity_id = 0;
  18.  
  19.     private Vector entity_list;
  20.     private Hashtable    name_list;
  21.     private Hashtable    user_identities;
  22.  
  23.     public RegistryServer(ThreadGroup tg)
  24.     {
  25.         super(tg, "Registry Server Thread");
  26.  
  27.         setPriority(7);
  28.  
  29.         System.out.println("Starting Registry Server");
  30.  
  31.         entity_list = new Vector(10,2);
  32.         name_list = new Hashtable(100, 0.8f);
  33.         user_identities = new Hashtable(20, 0.8f);
  34.     }
  35.  
  36.     // allocate a new entity.
  37.     // return values:
  38.     //    -1 if cname already in use
  39.     //    -2 if user not identified
  40.     //     > 0 sucessful, the new id number
  41.     // The caller must make sure that the ident is legally valid to add
  42.     // a new entity.
  43.     public synchronized int allocate(String cname, String ident)
  44.     {
  45.         Entity entity;
  46.  
  47.         // first check the name list for the given entity;
  48.         if(name_list.get(cname) != null)
  49.             return(-1);
  50.  
  51.         next_entity_id++;
  52.  
  53.         entity = new Entity(cname, next_entity_id, ident);
  54.  
  55.         // need to add an entity class to the Vector
  56.         entity_list.addElement(entity);
  57.         name_list.put(cname, entity);
  58.  
  59.         return(next_entity_id);
  60.     }
  61.  
  62.     public synchronized boolean set_URL(int entid, String ident,  String url)
  63.     {
  64.         int    i;
  65.         Entity    e;
  66.  
  67.         for(i = 0; i < entity_list.size(); i++)
  68.         {
  69.             e = (Entity)entity_list.elementAt(i);
  70.  
  71.             // currently no checking on the ownership of the entity.
  72.             if(e.ID == entid)
  73.             {
  74.                 if(!e.get_owner().equals(ident))
  75.                     return false;
  76.  
  77.                 e.set_URL(url);
  78.                 break;
  79.             }
  80.         }
  81.  
  82.         return true;
  83.     }
  84.  
  85.     public synchronized boolean set_nickname(int entid, String ident, String new_name)
  86.     {
  87.         int    i;
  88.         Entity    e;
  89.  
  90.         for(i = 0; i < entity_list.size(); i++)
  91.         {
  92.             e = (Entity)entity_list.elementAt(i);
  93.  
  94.             // currently no checking on the ownership of the entity.
  95.             if(e.ID == entid)
  96.             {
  97.                 if(!e.get_owner().equals(ident))
  98.                     return false;
  99.  
  100.                 e.set_nickname(new_name);
  101.                 break;
  102.             }
  103.         }
  104.  
  105.         return true;
  106.     }
  107.  
  108.     public synchronized boolean chown(int entid, String old_owner, String new_owner)
  109.     {
  110.         int    i;
  111.         Entity    e;
  112.  
  113.         for(i = 0; i < entity_list.size(); i++)
  114.         {
  115.             e = (Entity)entity_list.elementAt(i);
  116.  
  117.             // currently no checking on the ownership of the entity.
  118.             if(e.ID == entid)
  119.             {
  120.                 if(!e.get_owner().equals(old_owner))
  121.                     return false;
  122.  
  123.                 e.set_owner(new_owner);
  124.                 break;
  125.             }
  126.         }
  127.  
  128.         return true;
  129.     }
  130.  
  131.     public synchronized boolean set_persistent(int entid, String ident, boolean persist)
  132.     {
  133.         int    i;
  134.         Entity    e;
  135.  
  136.         for(i = 0; i < entity_list.size(); i++)
  137.         {
  138.             e = (Entity)entity_list.elementAt(i);
  139.  
  140.             // currently no checking on the ownership of the entity.
  141.             if(e.ID == entid)
  142.             {
  143.                 if(!e.get_owner().equals(ident))
  144.                     return false;
  145.  
  146.                 e.set_persistent(persist);
  147.                 break;
  148.             }
  149.         }
  150.  
  151.         return true;
  152.     }
  153.  
  154.     public synchronized boolean set_avatar(int entid, String ident, boolean avatar)
  155.     {
  156.         int    i;
  157.         Entity    e;
  158.  
  159.         for(i = 0; i < entity_list.size(); i++)
  160.         {
  161.             e = (Entity)entity_list.elementAt(i);
  162.  
  163.             // currently no checking on the ownership of the entity.
  164.             if(e.ID == entid)
  165.             {
  166.                 if(!e.get_owner().equals(ident))
  167.                     return false;
  168.  
  169.                 e.set_avatar(avatar);
  170.                 break;
  171.             }
  172.         }
  173.  
  174.         return true;
  175.     }
  176.  
  177.     public synchronized boolean set_info(int entid, String ident, String[] info)
  178.     {
  179.         int    i;
  180.         Entity    e;
  181.  
  182.         for(i = 0; i < entity_list.size(); i++)
  183.         {
  184.             e = (Entity)entity_list.elementAt(i);
  185.  
  186.             // currently no checking on the ownership of the entity.
  187.             if(e.ID == entid)
  188.             {
  189.                 if(!e.get_owner().equals(ident))
  190.                     return false;
  191.  
  192.                 e.set_publicInfo(info);
  193.                 break;
  194.             }
  195.         }
  196.  
  197.         return true;
  198.     }
  199.  
  200.     public synchronized void printInfo(int entid, PrintStream out)
  201.     {
  202.         int    i, j;
  203.         Entity    e;
  204.         String[] info;
  205.  
  206.         for(i = 0; i < entity_list.size(); i++)
  207.         {
  208.             e = (Entity)entity_list.elementAt(i);
  209.  
  210.             // currently no checking on the ownership of the entity.
  211.             if(e.ID == entid)
  212.             {
  213.                 info = e.get_publicInfo();
  214.  
  215.                 if(info == null)
  216.                     break;
  217.  
  218.                 for(j = 0; j < info.length; j++)
  219.                     out.println(info[j]);
  220.  
  221.                 break;
  222.             }
  223.         }
  224.     }
  225.  
  226.     public synchronized void printEntityInfo(int entid, PrintStream out, String nick)
  227.     {
  228.         int    i;
  229.         Entity    e;
  230.  
  231.         for(i = 0; i < entity_list.size(); i++)
  232.         {
  233.             e = (Entity)entity_list.elementAt(i);
  234.  
  235.             // currently no checking on the ownership of the entity.
  236.             if(e.ID == entid)
  237.             {
  238.                 out.print(e.get_cname() + " " + e.get_URL() + " ");
  239.                 out.print(e.get_owner() + " " + e.isAvatar() + " ");
  240.                 out.print(e.isPersistent() + " ");
  241.                 out.println(e.get_registrationTime());
  242.                 out.println(e.get_nickname());
  243.                 // this is incorrect because it should be a list
  244.                 // of the SSRC values. not just a single flag.
  245.                 out.println(e.get_flags());
  246.                 break;
  247.             }
  248.         }
  249.         
  250.     }
  251.  
  252.     public synchronized void printList(PrintStream out)
  253.     {
  254.         int    i;
  255.         for(i = 0; i < entity_list.size(); i++)
  256.             out.println(((Entity)entity_list.elementAt(i)).ID);
  257.     }
  258.  
  259.     public synchronized boolean delete(int entid, String ident)
  260.     {
  261.         int    i;
  262.         Entity    e;
  263.  
  264.         for(i = 0; i < entity_list.size(); i++)
  265.         {
  266.             e = (Entity)entity_list.elementAt(i);
  267.  
  268.             // currently no checking on the ownership of the entity.
  269.             if(e.ID == entid)
  270.             {
  271.                 if(!e.get_owner().equals(ident))
  272.                     return false;
  273.  
  274.                 entity_list.removeElementAt(i);
  275.                 name_list.remove(e.get_cname());
  276.                 break;
  277.             }
  278.         }
  279.  
  280.         return true;
  281.     }
  282.  
  283.     public boolean verifyIdent(String uname, String passwd)
  284.     {
  285.         String curr_passwd = (String)user_identities.get(uname);
  286.  
  287.         if(curr_passwd == null)
  288.         {
  289.             user_identities.put(uname, passwd);
  290.             return true;
  291.         }
  292.  
  293.         return(curr_passwd.equals(passwd));
  294.     }
  295.  
  296.     public synchronized boolean isOwner(int entid, String ident)
  297.     {
  298.         int    i;
  299.         Entity    e;
  300.  
  301.         for(i = 0; i < entity_list.size(); i++)
  302.         {
  303.             e = (Entity)entity_list.elementAt(i);
  304.  
  305.             if(e.ID == entid)
  306.             {
  307.                 if(e.get_owner().equals(ident))
  308.                     return true;
  309.                 break;
  310.             }
  311.         }
  312.  
  313.         return false;
  314.     }
  315.  
  316.     public synchronized void listNew(int time, PrintStream out)
  317.     {
  318.         int    i;
  319.         Entity    e;
  320.         long    earliest_time = System.currentTimeMillis() - time;
  321.  
  322.         for(i = 0; i < entity_list.size(); i++)
  323.         {
  324.             e = (Entity)entity_list.elementAt(i);
  325.  
  326.             if(e.get_lastUpdateTime() > earliest_time)
  327.                 out.println(e.ID);
  328.         }
  329.     }
  330. }
  331.  
  332.