Purple Code

com.purpletech.message
Class MessageClient

java.lang.Object
  |
  +--com.purpletech.message.MessageClient
All Implemented Interfaces:
MessagePeer, java.lang.Runnable
Direct Known Subclasses:
QuoteBot, StallMessageClient

public class MessageClient
extends java.lang.Object
implements MessagePeer, java.lang.Runnable

A blocking message client.

connect() starts a thread that listens for incoming messages

Version:
$Id: MessageClient.java,v 1.3 2002/11/24 05:32:55 alex Exp $
Author:
Alex Chaffee

Field Summary
protected  boolean doSendSystemMessages
           
protected  java.lang.String host
           
protected  java.util.Vector listeners
           
protected  java.io.PrintStream outPrint
           
protected  int port
           
protected  MessageClientPrinter printer
           
 
Constructor Summary
MessageClient(java.lang.String host, int port, MessageClientPrinter printer)
           
MessageClient(java.lang.String host, int port, java.io.PrintStream outPrint)
           
MessageClient(java.lang.String host, MessageClientPrinter printer)
           
MessageClient(java.lang.String host, java.io.PrintStream outPrint)
           
 
Method Summary
 void addMessageListener(MessageListener x)
          adds an event listener that receives all incoming messages received by this peer
 void bind(java.lang.String name)
           
 void connect()
          Try to connect to the network
 void disconnect()
          Try to disconnect from the server
 java.lang.String getHost()
           
 int getID()
          get the ID of this peer
protected  java.io.InputStream getInputStream()
          subclasses can override this to provide filters on the input stream
protected  java.io.OutputStream getOutputStream()
          subclasses can override this to provide filters on the output stream
 int getPort()
           
 boolean isConnected()
          are we connected or not?
 int lookup(java.lang.String name)
          Look up a peer id that's been bound to a name on this server
protected  Message processSystemMessage(Message m)
          process a SystemMessage
 Message receiveMessage()
          Actively receive a message.
 void removeMessageListener(MessageListener x)
           
 void run()
          continually look for messages and dispatch them while connected
 void sendMessage(Message msg)
          sends a message out across the pipe
 void setSendSystemMessages(boolean doIt)
          If doSendSystemMessages is TRUE, then all system messages get dispatched to all listeners.
 void start()
          Spawn a thread to start receiving messages.
 void stop()
          Stop receiving messages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

host

protected java.lang.String host

port

protected int port

printer

protected MessageClientPrinter printer

outPrint

protected java.io.PrintStream outPrint

listeners

protected java.util.Vector listeners

doSendSystemMessages

protected boolean doSendSystemMessages
Constructor Detail

MessageClient

public MessageClient(java.lang.String host,
                     int port,
                     MessageClientPrinter printer)

MessageClient

public MessageClient(java.lang.String host,
                     int port,
                     java.io.PrintStream outPrint)

MessageClient

public MessageClient(java.lang.String host,
                     MessageClientPrinter printer)

MessageClient

public MessageClient(java.lang.String host,
                     java.io.PrintStream outPrint)
Method Detail

getHost

public java.lang.String getHost()

getPort

public int getPort()

getID

public int getID()
Description copied from interface: MessagePeer
get the ID of this peer

Specified by:
getID in interface MessagePeer

setSendSystemMessages

public void setSendSystemMessages(boolean doIt)
If doSendSystemMessages is TRUE, then all system messages get dispatched to all listeners. (Default = FALSE)

Specified by:
setSendSystemMessages in interface MessagePeer

getOutputStream

protected java.io.OutputStream getOutputStream()
                                        throws java.io.IOException
subclasses can override this to provide filters on the output stream

java.io.IOException

getInputStream

protected java.io.InputStream getInputStream()
                                      throws java.io.IOException
subclasses can override this to provide filters on the input stream

java.io.IOException

connect

public void connect()
             throws java.io.IOException
Try to connect to the network

Specified by:
connect in interface MessagePeer
java.io.IOException

start

public void start()
Spawn a thread to start receiving messages. Note that client code can just spawn their own thread and pass the thread this object (it's a Runnable).

Specified by:
start in interface MessagePeer

stop

public void stop()
Description copied from interface: MessagePeer
Stop receiving messages. Stops the thread (maybe after a little while).

Specified by:
stop in interface MessagePeer

disconnect

public void disconnect()
Description copied from interface: MessagePeer
Try to disconnect from the server

Specified by:
disconnect in interface MessagePeer

sendMessage

public void sendMessage(Message msg)
sends a message out across the pipe

Specified by:
sendMessage in interface MessagePeer

receiveMessage

public Message receiveMessage()
Actively receive a message. Blocks until a message arrives.

Usually don't call this method; instead, call addMessageListener and wait for a notification.

Returns:
null if there is no message waiting

isConnected

public boolean isConnected()
Description copied from interface: MessagePeer
are we connected or not?

Specified by:
isConnected in interface MessagePeer

addMessageListener

public void addMessageListener(MessageListener x)
Description copied from interface: MessagePeer
adds an event listener that receives all incoming messages received by this peer

Specified by:
addMessageListener in interface MessagePeer

removeMessageListener

public void removeMessageListener(MessageListener x)
Specified by:
removeMessageListener in interface MessagePeer

run

public void run()
continually look for messages and dispatch them while connected

Specified by:
run in interface java.lang.Runnable

processSystemMessage

protected Message processSystemMessage(Message m)
process a SystemMessage

Returns:
null if the message gets eaten, or the old message (or possibly a new one)

bind

public void bind(java.lang.String name)
Specified by:
bind in interface MessagePeer
Parameters:
name - name to bind this client to

lookup

public int lookup(java.lang.String name)
Look up a peer id that's been bound to a name on this server

Specified by:
lookup in interface MessagePeer

Documentation generated March 5 2003