org.graphstream.stream.netstream
Class NetStreamReceiver

java.lang.Object
  extended by java.lang.Thread
      extended by org.graphstream.stream.netstream.NetStreamReceiver
All Implemented Interfaces:
Runnable

public class NetStreamReceiver
extends Thread

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.

Since:
Aug 13, 2011
Author:
Yoann Pigné
See Also:
NetStreamConstants, Copyright (c) 2010 University of Luxembourg NetStreamReceiver.java

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
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.
 
Method Summary
 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
 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.
 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.
 
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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NetStreamReceiver

public NetStreamReceiver(String hostname,
                         int port)
                  throws IOException,
                         UnknownHostException
New NetStream Receiver, awaiting in its own thread at the given host name and port, for new graph events.

Parameters:
hostname - The host name to listen at messages.
port - The port to listen at messages.
Throws:
IOException
UnknownHostException

NetStreamReceiver

public NetStreamReceiver(int port)
                  throws IOException,
                         UnknownHostException
New NetStream Receiver, awaiting in its own thread at "localhost" on the given port, for new graph events.

Parameters:
port - The port to listen at messages.
Throws:
IOException
UnknownHostException

NetStreamReceiver

public NetStreamReceiver(String hostname,
                         int port,
                         boolean debug)
                  throws IOException,
                         UnknownHostException
New NetStream Receiver, awaiting in its own thread at the given host name and port, for new graph events.

Parameters:
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.
Throws:
IOException
UnknownHostException
Method Detail

isRunning

public boolean isRunning()
False as soon as the receiver terminates.


getStream

public ThreadProxyPipe getStream(String name)
Gives the stream (a ThreadProxyPipe) identified with this name. If no pipe exists under this name, a new one is created and returned

Parameters:
name - Identifier of the stream.
Returns:
the identified pipe

getDefaultStream

public ThreadProxyPipe getDefaultStream()
Gives the default stream (a ThreadProxyPipe) identified with the name "default". It is created if it does not exist.

Returns:
the default pipe

setDebugOn

public void setDebugOn(boolean on)
Enable or disable debugging.


register

public void register(String name,
                     ThreadProxyPipe stream)
              throws Exception
Register a stream. All events with the given stream name will be directed to it. The user has to ensure the ThreadProxyPipe can be safely written to by the Receiver's thread.

Parameters:
name - Filter only message with this name to the given message box.
stream - The ThreadProxyPipe to push the events to.
Throws:
Exception - If another Pipe is already registered at the given name.

quit

public void quit()
Stop the receiver.


hasActiveConnections

public boolean hasActiveConnections()
Ask the receiver about its active connections


setUnpacker

public void setUnpacker(NetStreamUnpacker unpaker)
Sets an optional NetStreamUnpaker whose "unpack" method will be called on each message. It allows to do extra decoding on the all byte array message. You can also decrypt things.

Parameters:
unpaker -

removeUnpacker

public void removeUnpacker()

run

public void run()
Wait for connections, accept them, demultiplexes them and dispatch messages to registered message boxes.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

poll

public void poll()
Wait until one or several chunks of message are acceptable. This method should be called in a loop. It can be used to block a program until some data is available.



Copyright © 2012. All Rights Reserved.