org.lastbamboo.common.turn.server
Class TurnClientImpl

java.lang.Object
  extended by org.lastbamboo.common.turn.server.TurnClientImpl
All Implemented Interfaces:
TurnClient

public final class TurnClientImpl
extends Object
implements TurnClient

Default TURN client implementation. Starts the server on behalf of the TURN client and keeps track of remote hosts that have permission to send incoming data to this client.

Note that this class does not implement CloseListener because doing so would require us to keep track of a separate data structure of "canceled" connections. Instead, we simply allow the write attempt to throw an IOException, allowing the caller to respond with "Send Error Response" messages to the TURN client as appropriate.

Note that the lifetime of TURN allocations is handled externally according to how the connection has been idle.

We deviate from the TURN spec here because it just seems to make sense to only close TURN sessions when there's been no data, not after fixed periods regardless of whether there's data or not.


Constructor Summary
TurnClientImpl(InetAddress publicAddress, org.littleshoot.mina.common.IoSession ioSession)
          Creates a new TURN client abstraction for the specified TURN client address and port.
 
Method Summary
 void addConnection(org.littleshoot.mina.common.IoSession session)
          Adds the specified connection for this TURN client.
 void close()
          Closes the client and all associated connections.
 org.littleshoot.mina.common.IoSession getIoSession()
          Accessor for the handler for reading and writing data with this client.
 InetSocketAddress getMappedAddress()
          Accessor for the MAPPED ADDRESS, otherwise known as the server reflexive address.
 int getNumConnections()
          Returns the number of connections for this TURN client.
 InetSocketAddress getRelayAddress()
          Accessor for the IP and port this server has allocated on behalf of the TURN client.
 void handleConnect(InetSocketAddress remoteAddress)
          Tells the client to appropriately handle a connect request to the specified remote host.
 boolean hasActiveDestination()
          Returns whether or not the TURN client has set its "active destination" turning off TURN messaging on that connection.
 boolean hasIncomingPermission(org.littleshoot.mina.common.IoSession session)
          Returns whether or not the remote host who created the specified session has permission to open a connection to this TURN client.
 void removeConnection(org.littleshoot.mina.common.IoSession session)
          Removes the connection.
 void startServer()
          Starts the separate server running on the client's allocated address for accepting connections from remote hosts.
 boolean write(InetSocketAddress remoteAddress, org.littleshoot.mina.common.ByteBuffer data)
          Writes data from this TURN client to the specified remote address.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TurnClientImpl

public TurnClientImpl(InetAddress publicAddress,
                      org.littleshoot.mina.common.IoSession ioSession)
Creates a new TURN client abstraction for the specified TURN client address and port.

Parameters:
publicAddress - The public address of the server. The port will be determined later using an ephemeral port when we bind.
ioSession - The handler for writing data back to the TURN client.
Method Detail

startServer

public void startServer()
Description copied from interface: TurnClient
Starts the separate server running on the client's allocated address for accepting connections from remote hosts.

Specified by:
startServer in interface TurnClient

write

public boolean write(InetSocketAddress remoteAddress,
                     org.littleshoot.mina.common.ByteBuffer data)
Description copied from interface: TurnClient
Writes data from this TURN client to the specified remote address. If there's already a connection to the remote address, this uses that. Otherwise, it opens a connection and begins allowing incoming data from that connection.

Specified by:
write in interface TurnClient
Parameters:
remoteAddress - The IP address and port of the remote host to send data to.
data - The data to send the remote host.
Returns:
true if the data was send to an existing binding. Otherwise, this returns false to indicate that there was no existing matching 5-tuple for the remote host and that we're attempting to connect to that remote host.

handleConnect

public void handleConnect(InetSocketAddress remoteAddress)
Description copied from interface: TurnClient
Tells the client to appropriately handle a connect request to the specified remote host.

Specified by:
handleConnect in interface TurnClient
Parameters:
remoteAddress - The address to handle.

getRelayAddress

public InetSocketAddress getRelayAddress()
Description copied from interface: TurnClient
Accessor for the IP and port this server has allocated on behalf of the TURN client. This is the endpoint other hosts can contact this client on.

Specified by:
getRelayAddress in interface TurnClient
Returns:
The IP address and port this server has allocated for the client.

getMappedAddress

public InetSocketAddress getMappedAddress()
Description copied from interface: TurnClient
Accessor for the MAPPED ADDRESS, otherwise known as the server reflexive address.

Specified by:
getMappedAddress in interface TurnClient
Returns:
The MAPPED ADDRESS.

close

public void close()
Description copied from interface: TurnClient
Closes the client and all associated connections.

Specified by:
close in interface TurnClient

getIoSession

public org.littleshoot.mina.common.IoSession getIoSession()
Description copied from interface: TurnClient
Accessor for the handler for reading and writing data with this client.

Specified by:
getIoSession in interface TurnClient
Returns:
The handler for reading and writing data with this client.

hasActiveDestination

public boolean hasActiveDestination()
Description copied from interface: TurnClient
Returns whether or not the TURN client has set its "active destination" turning off TURN messaging on that connection.

Specified by:
hasActiveDestination in interface TurnClient
Returns:
true if the client has set the active destination, otherwise false.

removeConnection

public void removeConnection(org.littleshoot.mina.common.IoSession session)
Description copied from interface: TurnClient
Removes the connection.

Specified by:
removeConnection in interface TurnClient
Parameters:
session - The class for reading and writing data with the remote host.

addConnection

public void addConnection(org.littleshoot.mina.common.IoSession session)
Description copied from interface: TurnClient
Adds the specified connection for this TURN client. This is typically called when a remote host makes a connection to the TURN server.

Specified by:
addConnection in interface TurnClient
Parameters:
session - The class for reading and writing data with the remote host.

hasIncomingPermission

public boolean hasIncomingPermission(org.littleshoot.mina.common.IoSession session)
Description copied from interface: TurnClient
Returns whether or not the remote host who created the specified session has permission to open a connection to this TURN client. The remote host has permission if the TURN client has issued a connect request to that host that is still active.

Specified by:
hasIncomingPermission in interface TurnClient
Parameters:
session - The connection from the remote host.
Returns:
true if the remote host has permission to connect to the TURN client, otherwise false.

getNumConnections

public int getNumConnections()
Returns the number of connections for this TURN client.

Specified by:
getNumConnections in interface TurnClient
Returns:
The number of connections for this TURN client.


Copyright © 2013 LittleShoot. All Rights Reserved.