public class NetStreamReceiver extends Thread implements NetStreamDecoder
This class implements a receiver according to specifications the NetStream protocol.
See NetStreamConstants
for a full description of the protocol, the
sender and the receiver.
NetStreamConstants
,
Copyright (c) 2010 University of Luxembourg
NetStreamReceiver.java
Modifier and Type | Class and Description |
---|---|
(package private) class |
NetStreamReceiver.DefaultUnpacker |
protected class |
NetStreamReceiver.IncomingBuffer
The connection to a sender.
|
Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
protected ThreadProxyPipe |
currentStream
The current pipe commands are being written to.
|
protected boolean |
debug
Show debugging messages.
|
protected NetStreamDecoder |
decoder
Utility class that decodes messages according to the NetStream Protocol
|
private String |
hostname
the hostname this receiver is listening at.
|
protected HashMap<SelectionKey,NetStreamReceiver.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 boolean |
loop
While true, the received is running.
|
private int |
port
the port listened to.
|
protected static String |
RESET |
protected Selector |
selector
Multiplexor.
|
protected ServerSocketChannel |
server
Receiver socket.
|
private NetStreamUnpacker |
unpacker |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Constructor and Description |
---|
NetStreamReceiver(int port)
New NetStream Receiver, awaiting in its own thread at "localhost" on the
given port, for new graph events.
|
NetStreamReceiver(String hostname,
int port)
New NetStream Receiver, awaiting in its own thread at the given host name
and port, for new graph events.
|
NetStreamReceiver(String hostname,
int port,
boolean debug)
New NetStream Receiver, awaiting in its own thread at the given host name
and port, for new graph events.
|
Modifier and Type | Method and Description |
---|---|
protected void |
debug(String message,
Object... data) |
void |
decodeMessage(InputStream in)
Decode one message.
|
protected void |
error(String message,
Object... data) |
protected void |
error(Throwable e,
String message,
Object... data) |
ThreadProxyPipe |
getDefaultStream()
Gives the default stream (a ThreadProxyPipe) identified with the name
"default".
|
ThreadProxyPipe |
getStream(String name)
Gives the stream (a ThreadProxyPipe) identified with this name.
|
boolean |
hasActiveConnections()
Ask the receiver about its active connections
|
protected void |
init()
Initialize 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,
ThreadProxyPipe stream)
Register a stream.
|
void |
removeUnpacker() |
void |
run()
Wait for connections, accept them, demultiplexes them and dispatch
messages to registered message boxes.
|
void |
setDebugOn(boolean on)
Enable or disable debugging.
|
void |
setUnpacker(NetStreamUnpacker unpaker)
Sets an optional NetStreamUnpaker whose "unpack" method will be called on
each message.
|
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
private int port
protected ServerSocketChannel server
protected SelectionKey key
protected boolean loop
protected boolean debug
protected ThreadProxyPipe currentStream
protected NetStreamDecoder decoder
protected HashMap<SelectionKey,NetStreamReceiver.IncomingBuffer> incoming
private NetStreamUnpacker unpacker
protected static final String LIGHT_YELLOW
protected static final String RESET
public NetStreamReceiver(String hostname, int port) throws IOException, UnknownHostException
hostname
- The host name to listen at messages.port
- The port to listen at messages.IOException
UnknownHostException
public NetStreamReceiver(int port) throws IOException, UnknownHostException
port
- The port to listen at messages.IOException
UnknownHostException
public NetStreamReceiver(String hostname, int port, boolean debug) throws IOException, UnknownHostException
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
public boolean isRunning()
protected void init() throws IOException, UnknownHostException
IOException
UnknownHostException
public void setDebugOn(boolean on)
setDebugOn
in interface NetStreamDecoder
public void quit()
public boolean hasActiveConnections()
public void setUnpacker(NetStreamUnpacker unpaker)
unpaker
- public void removeUnpacker()
public void run()
public void poll()
protected void readDataChunk(SelectionKey key) throws IOException
IOException
public ThreadProxyPipe getStream(String name)
NetStreamDecoder
getStream
in interface NetStreamDecoder
name
- Identifier of the stream.public ThreadProxyPipe getDefaultStream()
NetStreamDecoder
getDefaultStream
in interface NetStreamDecoder
public void register(String name, ThreadProxyPipe stream) throws Exception
NetStreamDecoder
register
in interface NetStreamDecoder
name
- Filter only message with this name to the given message box.stream
- The ThreadProxyPipe to push the events to.Exception
- If another Pipe is already registered at the given name.public void decodeMessage(InputStream in) throws IOException
NetStreamDecoder
decodeMessage
in interface NetStreamDecoder
IOException
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses