@ApplicationScoped public class RouterImpl extends Object implements Router
Initializes and starts listening for data on the network when enabled.
Modifier and Type | Field and Description |
---|---|
protected UpnpServiceConfiguration |
configuration |
protected Map<InetAddress,DatagramIO> |
datagramIOs |
protected boolean |
enabled |
protected Map<NetworkInterface,MulticastReceiver> |
multicastReceivers |
protected NetworkAddressFactory |
networkAddressFactory |
protected ProtocolFactory |
protocolFactory |
protected Lock |
readLock |
protected ReentrantReadWriteLock |
routerLock |
protected StreamClient |
streamClient |
protected Map<InetAddress,StreamServer> |
streamServers |
protected Lock |
writeLock |
Modifier | Constructor and Description |
---|---|
protected |
RouterImpl() |
|
RouterImpl(UpnpServiceConfiguration configuration,
ProtocolFactory protocolFactory) |
Modifier and Type | Method and Description |
---|---|
void |
broadcast(byte[] bytes)
Sends the given bytes as a broadcast on all bound
DatagramIO s,
using source port 9. |
boolean |
disable()
Unbinds all sockets and stops all listening threads for datagrams and streams.
|
boolean |
disable(DisableRouter event) |
boolean |
enable()
Initializes listening services: First an instance of
MulticastReceiver
is bound to each network interface. |
boolean |
enable(EnableRouter event) |
List<NetworkAddress> |
getActiveStreamServers(InetAddress preferredAddress) |
UpnpServiceConfiguration |
getConfiguration() |
protected int |
getLockTimeoutMillis() |
ProtocolFactory |
getProtocolFactory() |
void |
handleStartFailure(InitializationException ex)
Called by the
Router.enable() method before it returns. |
boolean |
isEnabled() |
protected void |
lock(Lock lock) |
protected void |
lock(Lock lock,
int timeoutMilliseconds) |
void |
received(IncomingDatagramMessage msg)
Obtains the asynchronous protocol
Executor and runs the protocol created
by the ProtocolFactory for the given message. |
void |
received(UpnpStream stream)
Obtains the synchronous protocol
Executor and runs the
UpnpStream directly. |
void |
send(OutgoingDatagramMessage msg)
Sends the UDP datagram on all bound
DatagramIO s. |
StreamResponseMessage |
send(StreamRequestMessage msg)
Sends the TCP stream request with the
StreamClient . |
void |
shutdown()
Disables the router and releases all other resources.
|
protected void |
startAddressBasedTransports(Iterator<InetAddress> addresses) |
protected void |
startInterfaceBasedTransports(Iterator<NetworkInterface> interfaces) |
protected void |
unlock(Lock lock) |
protected UpnpServiceConfiguration configuration
protected ProtocolFactory protocolFactory
protected volatile boolean enabled
protected ReentrantReadWriteLock routerLock
protected Lock readLock
protected Lock writeLock
protected NetworkAddressFactory networkAddressFactory
protected StreamClient streamClient
protected final Map<NetworkInterface,MulticastReceiver> multicastReceivers
protected final Map<InetAddress,DatagramIO> datagramIOs
protected final Map<InetAddress,StreamServer> streamServers
protected RouterImpl()
@Inject public RouterImpl(UpnpServiceConfiguration configuration, ProtocolFactory protocolFactory)
configuration
- The configuration used by this router.protocolFactory
- The protocol factory used by this router.public boolean enable(@Observes @Default EnableRouter event) throws RouterException
RouterException
public boolean disable(@Observes @Default DisableRouter event) throws RouterException
RouterException
public UpnpServiceConfiguration getConfiguration()
getConfiguration
in interface Router
public ProtocolFactory getProtocolFactory()
getProtocolFactory
in interface Router
public boolean enable() throws RouterException
MulticastReceiver
is bound to each network interface. Then an instance of DatagramIO
and
StreamServer
is bound to each bind address returned by the network
address factory, respectively. There is only one instance of
StreamClient
created and managed by this router.enable
in interface Router
true
if the router was enabled. false
if it's already running.RouterException
public boolean disable() throws RouterException
Router
disable
in interface Router
true
if the router was disabled. false
if it wasn't running.RouterException
public void shutdown() throws RouterException
Router
shutdown
in interface Router
RouterException
public boolean isEnabled()
public void handleStartFailure(InitializationException ex) throws InitializationException
Router
Router.enable()
method before it returns.handleStartFailure
in interface Router
ex
- The cause of the failure.InitializationException
- if the exception was not recoverable.public List<NetworkAddress> getActiveStreamServers(InetAddress preferredAddress) throws RouterException
getActiveStreamServers
in interface Router
preferredAddress
- A preferred stream server bound address or null
.RouterException
public void received(IncomingDatagramMessage msg)
Executor
and runs the protocol created
by the ProtocolFactory
for the given message.
If the factory doesn't create a protocol, the message is dropped immediately without creating another thread or consuming further resources. This means we can filter the datagrams in the protocol factory and e.g. completely disable discovery or only allow notification message from some known services we'd like to work with.
public void received(UpnpStream stream)
Executor
and runs the
UpnpStream
directly.received
in interface Router
stream
- The received UpnpStream
.public void send(OutgoingDatagramMessage msg) throws RouterException
DatagramIO
s.send
in interface Router
msg
- The UDP datagram message to send.RouterException
- if a recoverable error, such as thread interruption, occurs.public StreamResponseMessage send(StreamRequestMessage msg) throws RouterException
StreamClient
.send
in interface Router
msg
- The TCP (HTTP) stream message to send.StreamClient.sendRequest(StreamRequestMessage)
method or null
if no StreamClient
is available.RouterException
- if a recoverable error, such as thread interruption, occurs.public void broadcast(byte[] bytes) throws RouterException
DatagramIO
s,
using source port 9.
TODO: Support source port parameter
broadcast
in interface Router
bytes
- The byte payload of the UDP datagram.RouterException
- if a recoverable error, such as thread interruption, occurs.protected void startInterfaceBasedTransports(Iterator<NetworkInterface> interfaces) throws InitializationException
InitializationException
protected void startAddressBasedTransports(Iterator<InetAddress> addresses) throws InitializationException
InitializationException
protected void lock(Lock lock, int timeoutMilliseconds) throws RouterException
RouterException
protected void lock(Lock lock) throws RouterException
RouterException
protected void unlock(Lock lock)
protected int getLockTimeoutMillis()
Copyright © 2016 4th Line GmbH, Switzerland. All rights reserved.