Chat
Class Sender

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--Chat.Sender
All Implemented Interfaces:
java.lang.Runnable

class Sender
extends java.lang.Thread

A sender thread. Reads commands from the console and processes them sychronously, creating connections to other machines and updating Console and HostList objects.

See Also:
Receiver, Console, HostList

Field Summary
private  Console console
          The link to common console.
private  HostList hostlist
          The link to common host list.
 
Fields inherited from class java.lang.Thread
contextClassLoader, daemon, eetop, group, inheritableThreadLocals, inheritedAccessControlContext, MAX_PRIORITY, MIN_PRIORITY, name, NORM_PRIORITY, priority, single_step, stillborn, stopThreadPermission, target, threadInitNumber, threadLocals, threadQ
 
Constructor Summary
(package private) Sender(Console console, HostList hostlist)
          Initializes the sender thread with the link to console and host list.
 
Method Summary
private  boolean connect(java.lang.String host, int port, boolean silent)
          Connects to host specified and fills the hostlist with the data returned.
private  void connect(java.lang.String script, java.lang.String room)
          Registers this host on the server, and tries to connect to one of the hosts on the list returned.
private  void disconnect()
          Disconnects from the network.
private  void file(java.lang.String name)
          Sends a file to all the users from the hostlist, but not myself.
private  void message(java.lang.String text)
          Sends a chat message to all the users, including myself.
private  boolean process(java.lang.String line)
          Process a command from the console.
 boolean processHandled(java.lang.String line)
          Process a command from the console and handle caught exceptions
 void run()
          Sender thread's body.
private  void send(HostList.HostItem item, java.lang.String message)
          Send a prepared message to one remote user.
private  void send2all(java.lang.String message)
          Send a prepared message to all users from the host list (excluding myself).
 
Methods inherited from class java.lang.Thread
, activeCount, countStackFrames, currentThread, destroy, dumpStack, enumerate, exit, getContextClassLoader, getName, getPriority, getThreadGroup, checkAccess, init, interrupt, interrupt0, interrupted, isAlive, isDaemon, isInterrupted, isInterrupted, join, join, join, nextThreadNum, registerNatives, resume, resume0, setContextClassLoader, setDaemon, setName, setPriority, setPriority0, sleep, sleep, start, stop, stop, stop0, suspend, suspend0, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

console

private final Console console
The link to common console.

hostlist

private final HostList hostlist
The link to common host list.
Constructor Detail

Sender

Sender(Console console,
       HostList hostlist)
Initializes the sender thread with the link to console and host list. Also starts the thread.
Parameters:
console - link to console.
hostlist - link to hostlist.
Method Detail

send

private void send(HostList.HostItem item,
                  java.lang.String message)
           throws java.io.IOException
Send a prepared message to one remote user.
Parameters:
item - remote host identification.
message - a string to be sent.

send2all

private void send2all(java.lang.String message)
               throws java.io.IOException
Send a prepared message to all users from the host list (excluding myself).
Parameters:
message - a string to be sent.

connect

private void connect(java.lang.String script,
                     java.lang.String room)
              throws java.io.IOException
Registers this host on the server, and tries to connect to one of the hosts on the list returned.
Parameters:
script - server URL.
room - a name of the room.

connect

private boolean connect(java.lang.String host,
                        int port,
                        boolean silent)
                 throws java.io.IOException
Connects to host specified and fills the hostlist with the data returned.
Parameters:
host - host IP or host name.
port - host port.
silent - don't print errors, just return the result.
Returns:
true when successful.

disconnect

private void disconnect()
                 throws java.io.IOException
Disconnects from the network. This is done by sending out a DISCONNECT message and clearing the hostlist.

message

private void message(java.lang.String text)
              throws java.io.IOException
Sends a chat message to all the users, including myself.
Parameters:
text - a message to be send.

file

private void file(java.lang.String name)
           throws java.io.IOException
Sends a file to all the users from the hostlist, but not myself.
Parameters:
name - a name of the file.

process

private boolean process(java.lang.String line)
                 throws java.io.IOException
Process a command from the console.
Parameters:
line - the command.
Returns:
true if the sender thread should be terminated.

processHandled

public boolean processHandled(java.lang.String line)
Process a command from the console and handle caught exceptions
Parameters:
line - the command.
Returns:
true if the sender thread should be terminated.

run

public void run()
Sender thread's body. Waits in the loop for commands from the console.
Overrides:
run in class java.lang.Thread