home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Cdrom / Pavilions / BrainOpera / online / net-music / rhythm-tree / starclient.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  5.1 KB  |  204 lines

  1. /**************************************************************/
  2. /*  StarClient.java - Eric Metois 03/24/96                    */
  3. /**************************************************************/
  4.  
  5. import java.util.StringTokenizer;
  6. import java.net.*;
  7. import java.io.*;
  8. import java.applet.*;
  9. import java.awt.*;
  10.  
  11. /*******************************************************************/
  12. /*** INTERFACE (to be implemented by the owner of a 'StarClient' ***/
  13. /*******************************************************************/
  14. interface WebStarHandler
  15. {
  16.   public void WS_up(String msg, Object who);
  17.   public void WS_down(String msg, Object who);
  18.   public void WS_debugLog(String msg, Object who);
  19.   public void WS_newMsg(String msg, Object who);
  20. }
  21.  
  22. /************************/
  23. /*** StarClient class ***/
  24. /************************/
  25.  
  26. public class StarClient extends Object implements Runnable
  27. {
  28.   private WebStarHandler owner;
  29.  
  30.   private Socket s = null;
  31.   private String host;
  32.   private int port;
  33.  
  34.   private Thread life = null;
  35.   private int starID = -1;
  36.   private String starIDstr = "";
  37.   private boolean runState = false;
  38.   private boolean connectState = false;
  39.  
  40.   private DataInputStream input = null;
  41.   private DataOutputStream output = null;
  42.  
  43.   /*******************************/
  44.   /*** CONSTRUCTOR and CONTROL ***/
  45.   /*******************************/
  46.  
  47.   StarClient(WebStarHandler dad) /* constructor needs an owner */
  48.     {
  49.       owner = dad;
  50.     }
  51.  
  52.   public void start(String theHost, int thePort)
  53.     {
  54.       host = theHost;
  55.       port = thePort;
  56.       runState = true;
  57.       owner.WS_debugLog("Starting the StarClient", this);
  58.       if(life == null)
  59.     {
  60.       life = new Thread(this);
  61.       life.start();
  62.     }
  63.     }
  64.  
  65.   public void stop()
  66.     {
  67.       if(life != null) 
  68.     {
  69.       life.stop();
  70.       life = null;
  71.     }
  72.       disconnect();
  73.     }
  74.  
  75.   public boolean isConnected() { return connectState; }
  76.  
  77.   /************************/
  78.   /*** THE SOCKET STUFF ***/
  79.   /************************/
  80.  
  81.   public void connect() /* connects and sets up communication properties */
  82.     {
  83.       connectState = true;
  84.       owner.WS_debugLog("Connecting the StarClient", this);
  85.       try 
  86.     {
  87.       /* connect */
  88.       s = new Socket (host, port);
  89.       /* input and output streams */
  90.       input = new DataInputStream(s.getInputStream());
  91.       output = new DataOutputStream(s.getOutputStream());
  92.     } catch (IOException e) {
  93.       owner.WS_debugLog("Failed to connect to " + host, this);
  94.       connectState = false;
  95.     }
  96.     }
  97.   
  98.   public void disconnect()
  99.     {
  100.       owner.WS_debugLog("Disconnecting the StarClient", this);
  101.       life = null;
  102.       if(connectState)
  103.     {
  104.       try {
  105.         input.close(); output.close(); } catch (IOException e) { }
  106.       connectState = false;
  107.       if (s != null) 
  108.       {
  109.         try { s.close() ; } catch (IOException e) { }
  110.       }
  111.     }
  112.       input = null; output = null; s = null; starID = -1;
  113.       owner.WS_down("StarClient disconnected.", this);
  114.     }
  115.  
  116.   public synchronized void send(String outMsg) /* Sends out a message */
  117.     {
  118.       if(starID < 0) return; /* No anonymous message allowed -> needs a valid starID */
  119.       try {
  120.     output.writeBytes(starIDstr);
  121.     output.writeBytes(outMsg);
  122.     output.writeByte('\n'); 
  123.       } catch (IOException e) {
  124.     owner.WS_debugLog("Error while sending: " + outMsg, this);
  125.       }
  126.     }
  127.  
  128.   public int getStarID()  /* returns the current starID if valid... fetches it otherwise */
  129.     {
  130.       String line = "";
  131.       int FromID = -1;
  132.       String tok;
  133.       
  134.       if(starID < 0) /* Needs to be fetched (the first thing that is sent by he server) */
  135.     {
  136.       try {
  137.         line = input.readLine();
  138.       } catch (IOException e) {
  139.         owner.WS_debugLog("Error while reading data", this);
  140.         disconnect();
  141.         return starID;
  142.       }
  143.       StringTokenizer st = new StringTokenizer(line);
  144.       if(st.hasMoreTokens()) 
  145.         FromID = (Integer.valueOf(st.nextToken())).intValue();
  146.       if(FromID == 0) /* i.e. from the server */
  147.         {
  148.           if(st.hasMoreTokens()) 
  149.         {
  150.           tok = st.nextToken();
  151.           if( (tok.equals("WELCOME"))&&(st.hasMoreTokens()) )
  152.             {
  153.               starID = (Integer.valueOf(st.nextToken())).intValue();
  154.               starIDstr = starID + " ";
  155.             }
  156.         }
  157.         }
  158.     }
  159.       return starID;
  160.     }
  161.  
  162.   /***************************/
  163.   /*** THE READING THREAD  ***/
  164.   /***************************/
  165.  
  166.   public void run()
  167.     {
  168.       String inMsg = "";
  169.  
  170.       Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  171.       /*** attempt connection ***/
  172.       connect();
  173.       if(!connectState) return;
  174.       /*** fetches the starID ***/
  175.       if(getStarID() < 0)
  176.     {
  177.       owner.WS_debugLog("Failed at reading the assigned starID", this);
  178.       disconnect();
  179.       return;
  180.     }
  181.  
  182.       owner.WS_up("StarClient connected.", this);
  183.  
  184.       /*** reading thread ***/
  185.       while(life != null)
  186.     {
  187.       inMsg = "";
  188.       try {
  189.         inMsg = input.readLine();
  190.       } catch (IOException e) {
  191.         owner.WS_debugLog("Error while reading data", this);
  192.         disconnect();
  193.         return;
  194.       }
  195.       if(inMsg.length() > 2) owner.WS_newMsg(inMsg, this);
  196.       try {Thread.sleep(10);} catch (InterruptedException e){ } 
  197.     }
  198.     }
  199. }
  200.  
  201.       
  202.  
  203.       
  204.