org.teleal.cling.transport
Class RouterImpl

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

public class RouterImpl
extends Object
implements Router

Default implementation of network message router.

Initializes and starts listenting 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.
 UpnpServiceConfiguration getConfiguration()
           
protected  Map<InetAddress,DatagramIO> getDatagramIOs()
           
 int getLocalStreamPort(InetAddress address)
           
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.
 
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

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()

getLocalStreamPort

public int getLocalStreamPort(InetAddress address)
Specified by:
getLocalStreamPort in interface Router
Parameters:
address - A bound address of a local network interface.
Returns:
The local port this router is listening on for TCP stream (HTTP) requests or 0 if the given address was not managed by this router.

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.

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 © 2010 Teleal GmbH, Switzerland. All Rights Reserved.