jBittorrentAPI
Class MessageReceiver

java.lang.Object
  extended by java.lang.Thread
      extended by jBittorrentAPI.MessageReceiver
All Implemented Interfaces:
java.lang.Runnable

public class MessageReceiver
extends java.lang.Thread

Thread created to listen for incoming message from remote peers. When data is read, message type is determined and a new Message object (either Message_HS or Message_PP) is created and passed to the corresponding receiver


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  java.io.DataInputStream dis
           
private  boolean hsOK
           
private  java.io.InputStream is
           
private  javax.swing.event.EventListenerList listeners
           
private  boolean run
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
MessageReceiver(java.lang.String id, java.io.InputStream is)
          Create a new Message receiver for a given peer
 
Method Summary
 void addIncomingListener(IncomingListener listener)
           
protected  void fireMessageReceived(Message m)
           
 IncomingListener[] getIncomingListeners()
           
private  int read(byte[] data)
          Reads bytes from the DataInputStream
private  int read1(byte[] data)
          Reads bytes from the input stream.
 void removeIncomingListener(IncomingListener listener)
           
 void run()
          Reads data from the inputstream, creates new messages according to the received data and fires MessageReceived method of the listeners with the new message in parameter.
 void stopThread()
          Stops the current thread by completing the run() method
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

dis

private java.io.DataInputStream dis

hsOK

private boolean hsOK

is

private java.io.InputStream is

listeners

private final javax.swing.event.EventListenerList listeners

run

private boolean run
Constructor Detail

MessageReceiver

public MessageReceiver(java.lang.String id,
                       java.io.InputStream is)
                throws java.io.IOException
Create a new Message receiver for a given peer

Parameters:
id - The id of the peer that has been assigned this receiver
is - InputStream
Throws:
java.io.IOException
Method Detail

addIncomingListener

public void addIncomingListener(IncomingListener listener)

fireMessageReceived

protected void fireMessageReceived(Message m)

getIncomingListeners

public IncomingListener[] getIncomingListeners()

read

private int read(byte[] data)
Reads bytes from the DataInputStream

Parameters:
data - byte[]
Returns:
int

read1

private int read1(byte[] data)
           throws java.io.IOException,
                  java.lang.InterruptedException,
                  java.lang.Exception
Reads bytes from the input stream. This read method read exactly the number of bytes corresponding to the length of the byte array given in parameter

Parameters:
data - byte[]
Returns:
int
Throws:
java.io.IOException
java.lang.InterruptedException
java.lang.Exception

removeIncomingListener

public void removeIncomingListener(IncomingListener listener)

run

public void run()
Reads data from the inputstream, creates new messages according to the received data and fires MessageReceived method of the listeners with the new message in parameter. Loops as long as the 'run' variable is true

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

stopThread

public void stopThread()
Stops the current thread by completing the run() method