org.fourthline.cling.transport
Class RouterImpl

java.lang.Object
  extended by org.fourthline.cling.transport.RouterImpl
All Implemented Interfaces:
Router

public class RouterImpl
extends Object
implements Router

Default implementation of network message router.

Initializes and starts listening for data on the network immediately on construction.

Author:
Christian Bauer

Field Summary
protected  UpnpServiceConfiguration configuration
           
protected  Map<InetAddress,DatagramIO> datagramIOs
           
protected  Map<NetworkInterface,MulticastReceiver> multicastReceivers
           
protected  NetworkAddressFactory networkAddressFactory
           
protected  ProtocolFactory protocolFactory
           
protected  StreamClient streamClient
           
protected  Map<InetAddress,StreamServer> streamServers
           
 
Constructor Summary
RouterImpl(UpnpServiceConfiguration configuration, ProtocolFactory protocolFactory)
          Creates a NetworkAddressFactory from the given UpnpServiceConfiguration and initializes listening services.
 
Method Summary
 void broadcast(byte[] bytes)
          Sends the given bytes as a broadcast on all bound DatagramIOs, using source port 9.
 List<NetworkAddress> getActiveStreamServers(InetAddress preferredAddress)
           
 UpnpServiceConfiguration getConfiguration()
           
protected  Map<InetAddress,DatagramIO> getDatagramIOs()
           
protected  Map<NetworkInterface,MulticastReceiver> getMulticastReceivers()
           
 NetworkAddressFactory getNetworkAddressFactory()
           
 ProtocolFactory getProtocolFactory()
           
protected  StreamClient getStreamClient()
           
protected  Map<InetAddress,StreamServer> getStreamServers()
           
 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 DatagramIOs.
 StreamResponseMessage send(StreamRequestMessage msg)
          Sends the TCP stream request with the StreamClient.
 void shutdown()
          Unbinds all sockets and stops all listening threads for datagrams and streams.
protected  void startAddressBasedTransports(Iterator<InetAddress> addresses)
           
protected  void startInterfaceBasedTransports(Iterator<NetworkInterface> interfaces)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

configuration

protected final UpnpServiceConfiguration configuration

protocolFactory

protected final ProtocolFactory protocolFactory

streamClient

protected final StreamClient streamClient

networkAddressFactory

protected final NetworkAddressFactory networkAddressFactory

multicastReceivers

protected final Map<NetworkInterface,MulticastReceiver> multicastReceivers

datagramIOs

protected final Map<InetAddress,DatagramIO> datagramIOs

streamServers

protected final Map<InetAddress,StreamServer> streamServers
Constructor Detail

RouterImpl

public RouterImpl(UpnpServiceConfiguration configuration,
                  ProtocolFactory protocolFactory)
           throws InitializationException
Creates a NetworkAddressFactory from the given UpnpServiceConfiguration and initializes listening services. First an instance of MulticastReceiver is bound to eatch 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.

Parameters:
configuration - The configuration used by this router.
protocolFactory - The protocol factory used by this router.
Throws:
InitializationException - When initialization of any listening network service fails.
Method Detail

startInterfaceBasedTransports

protected void startInterfaceBasedTransports(Iterator<NetworkInterface> interfaces)
                                      throws InitializationException
Throws:
InitializationException

startAddressBasedTransports

protected void startAddressBasedTransports(Iterator<InetAddress> addresses)
                                    throws InitializationException
Throws:
InitializationException

getConfiguration

public UpnpServiceConfiguration getConfiguration()
Specified by:
getConfiguration in interface Router
Returns:
The configuration used by this router.

getProtocolFactory

public ProtocolFactory getProtocolFactory()
Specified by:
getProtocolFactory in interface Router
Returns:
The protocol factory used by this router.

getNetworkAddressFactory

public NetworkAddressFactory getNetworkAddressFactory()
Specified by:
getNetworkAddressFactory in interface Router
Returns:
The network interface and address binding configuration of this router.

getMulticastReceivers

protected Map<NetworkInterface,MulticastReceiver> getMulticastReceivers()

getDatagramIOs

protected Map<InetAddress,DatagramIO> getDatagramIOs()

getStreamClient

protected StreamClient getStreamClient()

getStreamServers

protected Map<InetAddress,StreamServer> getStreamServers()

getActiveStreamServers

public List<NetworkAddress> getActiveStreamServers(InetAddress preferredAddress)
Specified by:
getActiveStreamServers in interface Router
Parameters:
preferredAddress - A preferred stream server bound address or null.
Returns:
An empty list if no stream server is currently active, otherwise a single network address if the preferred address is active, or a list of all active bound stream servers.

shutdown

public void shutdown()
Description copied from interface: Router
Unbinds all sockets and stops all listening threads for datagrams and streams.

Specified by:
shutdown in interface Router

received

public void received(IncomingDatagramMessage msg)
Obtains the asynchronous protocol 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 resoures. 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.

Specified by:
received in interface Router
Parameters:
msg - The received datagram message.

received

public void received(UpnpStream stream)
Obtains the synchronous protocol Executor and runs the UpnpStream directly.

Specified by:
received in interface Router
Parameters:
stream - The received UpnpStream.

send

public void send(OutgoingDatagramMessage msg)
Sends the UDP datagram on all bound DatagramIOs.

Specified by:
send in interface Router
Parameters:
msg - The UDP datagram message to send.

send

public StreamResponseMessage send(StreamRequestMessage msg)
Sends the TCP stream request with the StreamClient.

Specified by:
send in interface Router
Parameters:
msg - The TCP (HTTP) stream message to send.
Returns:
The return value of the StreamClient.sendRequest(StreamRequestMessage) method or null if no StreamClient is available.

broadcast

public void broadcast(byte[] bytes)
Sends the given bytes as a broadcast on all bound DatagramIOs, using source port 9.

TODO: Support source port parameter

Specified by:
broadcast in interface Router
Parameters:
bytes - The byte payload of the UDP datagram.


Copyright © 2013 4th Line GmbH, Switzerland. All Rights Reserved.