home *** CD-ROM | disk | FTP | other *** search
Java Source | 2017-09-21 | 5.1 KB | 204 lines |
- /**************************************************************/
- /* StarClient.java - Eric Metois 03/24/96 */
- /**************************************************************/
-
- import java.util.StringTokenizer;
- import java.net.*;
- import java.io.*;
- import java.applet.*;
- import java.awt.*;
-
- /*******************************************************************/
- /*** INTERFACE (to be implemented by the owner of a 'StarClient' ***/
- /*******************************************************************/
- interface WebStarHandler
- {
- public void WS_up(String msg, Object who);
- public void WS_down(String msg, Object who);
- public void WS_debugLog(String msg, Object who);
- public void WS_newMsg(String msg, Object who);
- }
-
- /************************/
- /*** StarClient class ***/
- /************************/
-
- public class StarClient extends Object implements Runnable
- {
- private WebStarHandler owner;
-
- private Socket s = null;
- private String host;
- private int port;
-
- private Thread life = null;
- private int starID = -1;
- private String starIDstr = "";
- private boolean runState = false;
- private boolean connectState = false;
-
- private DataInputStream input = null;
- private DataOutputStream output = null;
-
- /*******************************/
- /*** CONSTRUCTOR and CONTROL ***/
- /*******************************/
-
- StarClient(WebStarHandler dad) /* constructor needs an owner */
- {
- owner = dad;
- }
-
- public void start(String theHost, int thePort)
- {
- host = theHost;
- port = thePort;
- runState = true;
- owner.WS_debugLog("Starting the StarClient", this);
- if(life == null)
- {
- life = new Thread(this);
- life.start();
- }
- }
-
- public void stop()
- {
- if(life != null)
- {
- life.stop();
- life = null;
- }
- disconnect();
- }
-
- public boolean isConnected() { return connectState; }
-
- /************************/
- /*** THE SOCKET STUFF ***/
- /************************/
-
- public void connect() /* connects and sets up communication properties */
- {
- connectState = true;
- owner.WS_debugLog("Connecting the StarClient", this);
- try
- {
- /* connect */
- s = new Socket (host, port);
- /* input and output streams */
- input = new DataInputStream(s.getInputStream());
- output = new DataOutputStream(s.getOutputStream());
- } catch (IOException e) {
- owner.WS_debugLog("Failed to connect to " + host, this);
- connectState = false;
- }
- }
-
- public void disconnect()
- {
- owner.WS_debugLog("Disconnecting the StarClient", this);
- life = null;
- if(connectState)
- {
- try {
- input.close(); output.close(); } catch (IOException e) { }
- connectState = false;
- if (s != null)
- {
- try { s.close() ; } catch (IOException e) { }
- }
- }
- input = null; output = null; s = null; starID = -1;
- owner.WS_down("StarClient disconnected.", this);
- }
-
- public synchronized void send(String outMsg) /* Sends out a message */
- {
- if(starID < 0) return; /* No anonymous message allowed -> needs a valid starID */
- try {
- output.writeBytes(starIDstr);
- output.writeBytes(outMsg);
- output.writeByte('\n');
- } catch (IOException e) {
- owner.WS_debugLog("Error while sending: " + outMsg, this);
- }
- }
-
- public int getStarID() /* returns the current starID if valid... fetches it otherwise */
- {
- String line = "";
- int FromID = -1;
- String tok;
-
- if(starID < 0) /* Needs to be fetched (the first thing that is sent by he server) */
- {
- try {
- line = input.readLine();
- } catch (IOException e) {
- owner.WS_debugLog("Error while reading data", this);
- disconnect();
- return starID;
- }
- StringTokenizer st = new StringTokenizer(line);
- if(st.hasMoreTokens())
- FromID = (Integer.valueOf(st.nextToken())).intValue();
- if(FromID == 0) /* i.e. from the server */
- {
- if(st.hasMoreTokens())
- {
- tok = st.nextToken();
- if( (tok.equals("WELCOME"))&&(st.hasMoreTokens()) )
- {
- starID = (Integer.valueOf(st.nextToken())).intValue();
- starIDstr = starID + " ";
- }
- }
- }
- }
- return starID;
- }
-
- /***************************/
- /*** THE READING THREAD ***/
- /***************************/
-
- public void run()
- {
- String inMsg = "";
-
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- /*** attempt connection ***/
- connect();
- if(!connectState) return;
- /*** fetches the starID ***/
- if(getStarID() < 0)
- {
- owner.WS_debugLog("Failed at reading the assigned starID", this);
- disconnect();
- return;
- }
-
- owner.WS_up("StarClient connected.", this);
-
- /*** reading thread ***/
- while(life != null)
- {
- inMsg = "";
- try {
- inMsg = input.readLine();
- } catch (IOException e) {
- owner.WS_debugLog("Error while reading data", this);
- disconnect();
- return;
- }
- if(inMsg.length() > 2) owner.WS_newMsg(inMsg, this);
- try {Thread.sleep(10);} catch (InterruptedException e){ }
- }
- }
- }
-
-
-
-
-