|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Thread
org.miv.mbox.net.Receiver
public class Receiver
Receives messages and dispatches them to message boxes.
A message receiver listen at a given address and port. Several senders (Sender)
can connect to it, the receiver will demultiplex the data flow and dispatch
incoming messages to registered message boxes.
A receiver is created by giving it the host and port on which it must listen
at incoming messages (using a MBoxLocator,
although, the receiver is not a real message box, is represents a set of
them). Then one registers several message boxes inside the receiver. The
receiver runs in its own thread.
There exist two way to receive messages with the receiver. One is to register
a MBoxListener using the
register(String,MBoxListener) method. This will return a message box
implementation of type MBoxStandalone that can buffer
messages from the receiver thread an later dispatch them to the MBoxListener.
The other way is to directly implement the MBox
interface or reify the MBoxBase abstract class to
receive messages directly by registering using the
register(String,org.miv.mbox.MBox) method. This is more flexible,
but the user must ensure proper locking since the
MBox.post(String,Object...) method will be invoked
from the receiver thread (the MBoxBase class already does this job).
MBox,
MBoxBase,
MBoxListener,
Sender| Nested Class Summary | |
|---|---|
protected class |
Receiver.IncomingBuffer
The connection to a sender. |
| Nested classes/interfaces inherited from class java.lang.Thread |
|---|
Thread.State, Thread.UncaughtExceptionHandler |
| Field Summary | |
|---|---|
protected HashMap<String,MBox> |
boxes
Pairs (key,value) where the key is the listener ID and the value the MBox of the listener. |
protected boolean |
debug
Show debugging messages. |
protected HashMap<SelectionKey,Receiver.IncomingBuffer> |
incoming
Current active incoming connections. |
protected SelectionKey |
key
Key for the selector. |
protected String |
lastError
Last encountered error. |
protected static String |
LIGHT_YELLOW
|
protected MBoxLocator |
locator
Host name for this application. |
protected boolean |
loop
While true, the received is running. |
protected static String |
RESET
|
protected Selector |
selector
Multiplexor. |
protected ServerSocketChannel |
server
Receiver socket. |
| Fields inherited from class java.lang.Thread |
|---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
| Constructor Summary | |
|---|---|
Receiver(MBoxLocator locator)
New receiver, awaiting for messages in its own thread at the given locator. |
|
Receiver(MBoxLocator locator,
boolean debug)
New receiver, awaiting for messages in its own thread at the given locator. |
|
Receiver(String hostname,
int port)
Like Receiver(MBoxLocator) but specify the host name and port
explicitly. |
|
Receiver(String hostname,
int port,
boolean debug)
Like Receiver(MBoxLocator,boolean) but specify the host name and
port explicitly. |
|
| Method Summary | |
|---|---|
protected void |
debug(String message,
Object... data)
|
protected void |
error(String message,
Object... data)
|
protected void |
error(Throwable e,
String message,
Object... data)
|
MBoxLocator |
getLocator()
Locator of this receiver. |
MBox |
getMBox(String name)
Message box attached to the given message box name, or null if no MBox is registered at this name. |
protected void |
init()
Initialise the server socket. |
boolean |
isRunning()
False as soon as the receiver terminates. |
void |
poll()
Wait until one or several chunks of message are acceptable. |
void |
quit()
Stop the receiver. |
protected void |
readDataChunk(SelectionKey key)
When data is readable on a socket, send it to the appropriate buffer (creating it if needed). |
void |
register(String name,
MBox box)
Register a message box. |
MBoxStandalone |
register(String name,
MBoxListener listener)
Register a message box listener for incoming messages. |
void |
run()
Wait for connections, accept them, demultiplexes them and dispatch messages to registered message boxes. |
void |
setDebugOn(boolean on)
Enable or disable debugging. |
| Methods inherited from class java.lang.Thread |
|---|
activeCount, checkAccess, clone, 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 |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected MBoxLocator locator
protected ServerSocketChannel server
protected Selector selector
protected SelectionKey key
protected boolean loop
protected boolean debug
protected String lastError
protected HashMap<String,MBox> boxes
register(String,MBoxListener)protected HashMap<SelectionKey,Receiver.IncomingBuffer> incoming
protected static final String LIGHT_YELLOW
protected static final String RESET
| Constructor Detail |
|---|
public Receiver(MBoxLocator locator)
throws IOException,
UnknownHostException
locator - the host and port of this receiver to listen at messages.
IOException
UnknownHostException
public Receiver(MBoxLocator locator,
boolean debug)
throws IOException,
UnknownHostException
locator - the host and port of this receiver to listen at messages.debug - If true informations are output for each message received.
IOException
UnknownHostException
public Receiver(String hostname,
int port)
throws IOException,
UnknownHostException
Receiver(MBoxLocator) but specify the host name and port
explicitly.
hostname - The host name to listen at messages.port - The port to listen at messages.
IOException
UnknownHostException
public Receiver(String hostname,
int port,
boolean debug)
throws IOException,
UnknownHostException
Receiver(MBoxLocator,boolean) but specify the host name and
port explicitly.
hostname - The host name to listen at messages.port - The port to listen at messages.debug - If true informations are output for each message received.
IOException
UnknownHostException| Method Detail |
|---|
public boolean isRunning()
public MBoxLocator getLocator()
public MBox getMBox(String name)
name - Identifier of the MBox listener.
protected void init()
throws IOException,
UnknownHostException
IOException
UnknownHostExceptionpublic void setDebugOn(boolean on)
public MBoxStandalone register(String name,
MBoxListener listener)
throws IdAlreadyInUseException
MBoxBase.processMessages() to receive its pending
messages.
name - Filter only message with this name to the given listener.listener - The listener to register.
IdAlreadyInUseException - If another message box is already registered
at the given name.
public void register(String name,
MBox box)
throws IdAlreadyInUseException
MBoxListener, the messages are directly
posted to the given message box that must ensure proper locking (since
the receiver runs in a distinct thread) that is implemented by the user.
name - Filter only message with this name to the given message box.box - The message box implementation to post messages to.
IdAlreadyInUseException - If another message box is already registered
at the given name.public void quit()
public void run()
run in interface Runnablerun in class Threadpublic void poll()
protected void readDataChunk(SelectionKey key)
throws IOException
IOException
protected void error(String message,
Object... data)
protected void error(Throwable e,
String message,
Object... data)
protected void debug(String message,
Object... data)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||