org.red5.server.net.remoting

## Class RemotingConnection

- java.lang.Object
- - org.red5.server.net.remoting.RemotingConnection

All Implemented Interfaces:
  IEventDispatcher, IEventHandler, IEventListener, IAttributeStore, ICastingAttributeStore, IConnection, ICoreObject, IRemotingConnection, AttributeStoreMXBean

    public class RemotingConnection
    extends Object
    implements IRemotingConnection

Connection class so the Red5 object works in methods invoked through remoting. Attributes are stored in the session of the implementing servlet container.

Author:
  The Red5 Project (red5@osflash.org), Joachim Bauch (jojo@struktur.de), Paul Gregoire (mondain@gmail.com)

### Nested Class Summary

### Nested classes/interfaces inherited from interface org.red5.server.api.IConnection

`IConnection.Encoding`

### Field Summary

Fields
`protected List<IRemotingHeader>` `headers` Headers to be returned to the client.
`protected RemotingPacket` `packet` Remoting packet that triggered the connection.
`protected javax.servlet.http.HttpServletRequest` `request` Servlet request
`protected IScope` `scope` Scope
`protected javax.servlet.http.HttpSession` `session` Session used to store properties.

### Fields inherited from interface org.red5.server.api.IConnection

`PERSISTENT, POLLING, TRANSIENT`

### Constructor Summary

Constructors
Constructor and Description
`RemotingConnection(javax.servlet.http.HttpServletRequest request, IScope scope, RemotingPacket packet)` Create servlet connection from request and scope.

### Method Summary

Methods
`void` `addHeader(String name, Object value)` Tell the client to add a header with all further requests.
`void` `addHeader(String name, Object value, boolean mustUnderstand)` Tell the client to add a header with all further requests.
`void` `cleanup()` Cleans up the remoting connection client from the HttpSession and client registry.
`void` `close()` Close this connection.
`boolean` `connect(IScope scope)` Try to connect to the scope.
`boolean` `connect(IScope scope, Object[] params)` Try to connect to the scope with a list of connection parameters.
`void` `dispatchEvent(IEvent event)` Dispatches event
`void` `dispatchEvent(Object event)`
`Object` `getAttribute(String name)` Return the value for a given attribute.
`Object` `getAttribute(String name, Object defaultValue)` Return the value for a given attribute and set it if it doesn't exist.
`Set<String>` `getAttributeNames()` Get the attribute names.
`Map<String,Object>` `getAttributes()` Get the attributes.
`Iterator<IBasicScope>` `getBasicScopes()` Get the basic scopes this connection has subscribed.
`Boolean` `getBoolAttribute(String name)` Get Boolean attribute by name
`Byte` `getByteAttribute(String name)` Get Byte attribute by name
`IClient` `getClient()` Get the client object associated with this connection.
`long` `getClientBytesRead()` Return number of written bytes the client reports to have received.
`Map<String,Object>` `getConnectParams()` Return the parameters that were given in the call to "connect".
`Double` `getDoubleAttribute(String name)` Get Double attribute by name
`long` `getDroppedMessages()` Total number of messages that have been dropped.
`IConnection.Encoding` `getEncoding()` Return encoding (AMF0 or AMF3).
`Collection<IRemotingHeader>` `getHeaders()` Return headers to send.
`String` `getHost()` Get the hostname that the client is connected to.
`Integer` `getIntAttribute(String name)` Get Integer attribute by name
`int` `getLastPingTime()` Return roundtrip time of last ping command.
`List<?>` `getListAttribute(String name)` Get List attribute by name
`Long` `getLongAttribute(String name)` Get boolean attribute by name
`Map<?,?>` `getMapAttribute(String name)` Get Long attribute by name
`String` `getPath()` Get the path for this connection.
`long` `getPendingMessages()` Total number of messages that are pending to be sent to the connection.
`long` `getPendingVideoMessages()` Return pending video messages number.
`long` `getReadBytes()` Total number of bytes read from the connection.
`long` `getReadMessages()` Total number of messages read from the connection.
`String` `getRemoteAddress()` Get the IP address the client is connected from.
`List<String>` `getRemoteAddresses()` Get the IP addresses the client is connected from.
`int` `getRemotePort()` Get the port the client is connected from.
`IScope` `getScope()` Get the scope this is connected to.
`String` `getSessionId()` Get the session id, this may be `null`.
`Set<?>` `getSetAttribute(String name)` Get Set attribute by name
`Short` `getShortAttribute(String name)` Get Short attribute by name
`String` `getStringAttribute(String name)` Get String attribute by name
`String` `getType()` Get the connection type.
`long` `getWrittenBytes()` Total number of bytes written to the connection.
`long` `getWrittenMessages()` Total number of messages written to the connection.
`boolean` `handleEvent(IEvent event)` Handle an event
`boolean` `hasAttribute(String name)` Check the object has an attribute.
`void` `initialize(IClient client)` Initialize the connection.
`boolean` `isConnected()` Is the client connected to the scope.
`void` `notifyEvent(IEvent event)` Notify of event.
`void` `ping()` Start measuring the roundtrip time for a packet on the connection.
`boolean` `removeAttribute(String name)` Remove an attribute.
`void` `removeAttributes()` Remove all attributes.
`void` `removeHeader(String name)` Tell the client to no longer send a header with all further requests.
`boolean` `setAttribute(String name, Object value)` Set an attribute on this object.
`void` `setAttributes(IAttributeStore values)` Set multiple attributes on this object.
`void` `setAttributes(Map<String,Object> values)` Set multiple attributes on this object.
`void` `setBandwidth(int mbits)` Sets the bandwidth using a mbit/s value.
`protected void` `setPacket(RemotingPacket packet)` Update the current packet.
`String` `toString()` Return string representation of the connection.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### scope

    protected IScope scope

Scope

#### request

    protected javax.servlet.http.HttpServletRequest request

Servlet request

#### packet

    protected RemotingPacket packet

Remoting packet that triggered the connection.

#### session

    protected javax.servlet.http.HttpSession session

Session used to store properties.

#### headers

    protected List<IRemotingHeader> headers

Headers to be returned to the client.

### Constructor Detail

#### RemotingConnection

    public RemotingConnection(javax.servlet.http.HttpServletRequest request,
                      IScope scope,
                      RemotingPacket packet)

Create servlet connection from request and scope.

Parameters:
  `request` - Servlet request
  `scope` - Scope
  `packet` - packet

### Method Detail

#### toString

    public String toString()

Return string representation of the connection.

Overrides:
  `toString` in class `Object`
Returns:
  string

#### setPacket

    protected void setPacket(RemotingPacket packet)

Update the current packet.

Parameters:
  `packet` -

#### getEncoding

    public IConnection.Encoding getEncoding()

Return encoding (AMF0 or AMF3).

Specified by:
  `getEncoding` in interface `IConnection`
Returns:
  Encoding, currently AMF0

#### getType

    public String getType()

Get the connection type.

Specified by:
  `getType` in interface `IConnection`
Returns:
  string containing one of connection types

#### initialize

    public void initialize(IClient client)

Initialize the connection.

Specified by:
  `initialize` in interface `IConnection`
Parameters:
  `client` - Client object associated with connection

#### connect

    public boolean connect(IScope scope)

Try to connect to the scope.

Specified by:
  `connect` in interface `IConnection`
Parameters:
  `scope` - Scope object
Returns:
  `true` on success, `false` otherwise

#### connect

    public boolean connect(IScope scope,
                  Object[] params)

Try to connect to the scope with a list of connection parameters.

Specified by:
  `connect` in interface `IConnection`
Parameters:
  `scope` - Scope object
  `params` - Connections parameters
Returns:
  `true` on success, `false` otherwise

#### isConnected

    public boolean isConnected()

Is the client connected to the scope. Result depends on connection type, `true` for persistent and polling connections, `false` for transient.

Specified by:
  `isConnected` in interface `IConnection`
Returns:
  `true` if the connection is persistent or polling, otherwise `false`

#### close

    public void close()

Close this connection. This will disconnect the client from the associated scope.

Specified by:
  `close` in interface `IConnection`

#### getConnectParams

    public Map<String,Object> getConnectParams()

Return the parameters that were given in the call to "connect".

Specified by:
  `getConnectParams` in interface `IConnection`
Returns:
  Connection parameters passed from client-side (Flex/Flash application)

#### getClient

    public IClient getClient()

Get the client object associated with this connection.

Specified by:
  `getClient` in interface `IConnection`
Returns:
  Client object

#### getHost

    public String getHost()

Get the hostname that the client is connected to. If they are connected to an IP, the IP address will be returned as a String.

Specified by:
  `getHost` in interface `IConnection`
Returns:
  String containing the hostname

#### getRemoteAddress

    public String getRemoteAddress()

Get the IP address the client is connected from.

Specified by:
  `getRemoteAddress` in interface `IConnection`
Returns:
  The IP address of the client

#### getRemoteAddresses

    public List<String> getRemoteAddresses()

Get the IP addresses the client is connected from. If a client is connected through RTMPT and uses a proxy to connect, this will contain all hosts the client used to connect to the server.

Specified by:
  `getRemoteAddresses` in interface `IConnection`
Returns:
  The IP addresses of the client

#### getRemotePort

    public int getRemotePort()

Get the port the client is connected from.

Specified by:
  `getRemotePort` in interface `IConnection`
Returns:
  The port of the client

#### getPath

    public String getPath()

Get the path for this connection. This is not updated if you switch scope.

Specified by:
  `getPath` in interface `IConnection`
Returns:
  path Connection path

#### getSessionId

    public String getSessionId()

Get the session id, this may be `null`.

Specified by:
  `getSessionId` in interface `IConnection`
Returns:
  Session id

#### getReadBytes

    public long getReadBytes()

Total number of bytes read from the connection.

Specified by:
  `getReadBytes` in interface `IConnection`
Returns:
  Number of read bytes

#### getWrittenBytes

    public long getWrittenBytes()

Total number of bytes written to the connection.

Specified by:
  `getWrittenBytes` in interface `IConnection`
Returns:
  Number of written bytes

#### getPendingMessages

    public long getPendingMessages()

Total number of messages that are pending to be sent to the connection.

Specified by:
  `getPendingMessages` in interface `IConnection`
Returns:
  Number of pending messages

#### getPendingVideoMessages

    public long getPendingVideoMessages()

Return pending video messages number.

Returns:
  Pending video messages number

#### getReadMessages

    public long getReadMessages()

Total number of messages read from the connection.

Specified by:
  `getReadMessages` in interface `IConnection`
Returns:
  Number of read messages

#### getWrittenMessages

    public long getWrittenMessages()

Total number of messages written to the connection.

Specified by:
  `getWrittenMessages` in interface `IConnection`
Returns:
  Number of written messages

#### getDroppedMessages

    public long getDroppedMessages()

Total number of messages that have been dropped.

Specified by:
  `getDroppedMessages` in interface `IConnection`
Returns:
  Number of dropped messages

#### ping

    public void ping()

Start measuring the roundtrip time for a packet on the connection.

Specified by:
  `ping` in interface `IConnection`

#### getLastPingTime

    public int getLastPingTime()

Return roundtrip time of last ping command.

Specified by:
  `getLastPingTime` in interface `IConnection`
Returns:
  roundtrip time in milliseconds

#### getScope

    public IScope getScope()

Get the scope this is connected to.

Specified by:
  `getScope` in interface `IConnection`
Returns:
  The connected scope

#### getBasicScopes

    public Iterator<IBasicScope> getBasicScopes()

Get the basic scopes this connection has subscribed. This list will contain the shared objects and broadcast streams the connection connected to.

Specified by:
  `getBasicScopes` in interface `IConnection`
Returns:
  List of basic scopes

#### dispatchEvent

    public void dispatchEvent(Object event)

#### dispatchEvent

    public void dispatchEvent(IEvent event)

Dispatches event

Specified by:
  `dispatchEvent` in interface `IEventDispatcher`
Parameters:
  `event` - Event object

#### handleEvent

    public boolean handleEvent(IEvent event)

Handle an event

Specified by:
  `handleEvent` in interface `IEventHandler`
Parameters:
  `event` - event to handle
Returns:
  true if event was handled, false if it should bubble

#### notifyEvent

    public void notifyEvent(IEvent event)

Notify of event.

Specified by:
  `notifyEvent` in interface `IEventListener`
Parameters:
  `event` - the event object

#### getBoolAttribute

    public Boolean getBoolAttribute(String name)

Get Boolean attribute by name

Specified by:
  `getBoolAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getByteAttribute

    public Byte getByteAttribute(String name)

Get Byte attribute by name

Specified by:
  `getByteAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getDoubleAttribute

    public Double getDoubleAttribute(String name)

Get Double attribute by name

Specified by:
  `getDoubleAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getIntAttribute

    public Integer getIntAttribute(String name)

Get Integer attribute by name

Specified by:
  `getIntAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getListAttribute

    public List<?> getListAttribute(String name)

Get List attribute by name

Specified by:
  `getListAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getLongAttribute

    public Long getLongAttribute(String name)

Get boolean attribute by name

Specified by:
  `getLongAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getMapAttribute

    public Map<?,?> getMapAttribute(String name)

Get Long attribute by name

Specified by:
  `getMapAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getSetAttribute

    public Set<?> getSetAttribute(String name)

Get Set attribute by name

Specified by:
  `getSetAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getShortAttribute

    public Short getShortAttribute(String name)

Get Short attribute by name

Specified by:
  `getShortAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getStringAttribute

    public String getStringAttribute(String name)

Get String attribute by name

Specified by:
  `getStringAttribute` in interface `ICastingAttributeStore`
Parameters:
  `name` - Attribute name
Returns:
  Attribute

#### getAttribute

    public Object getAttribute(String name)

Return the value for a given attribute.

Specified by:
  `getAttribute` in interface `IAttributeStore`
Parameters:
  `name` - the name of the attribute to get
Returns:
  the attribute value or null if the attribute doesn't exist

#### getAttribute

    public Object getAttribute(String name,
                      Object defaultValue)

Return the value for a given attribute and set it if it doesn't exist.

This is a utility function that internally performs the following code:

`if (!hasAttribute(name)) setAttribute(name, defaultValue);
return getAttribute(name);
`

Specified by:
  `getAttribute` in interface `IAttributeStore`
Parameters:
  `name` - the name of the attribute to get
  `defaultValue` - the value of the attribute to set if the attribute doesn't exist
Returns:
  the attribute value

#### getAttributeNames

    public Set<String> getAttributeNames()

Get the attribute names. The resulting set will be read-only.

Specified by:
  `getAttributeNames` in interface `IAttributeStore`
Specified by:
  `getAttributeNames` in interface `AttributeStoreMXBean`
Returns:
  set containing all attribute names

#### getAttributes

    public Map<String,Object> getAttributes()

Get the attributes. The resulting map will be read-only.

Specified by:
  `getAttributes` in interface `IAttributeStore`
Returns:
  map containing all attributes

#### hasAttribute

    public boolean hasAttribute(String name)

Check the object has an attribute.

Specified by:
  `hasAttribute` in interface `IAttributeStore`
Specified by:
  `hasAttribute` in interface `AttributeStoreMXBean`
Parameters:
  `name` - the name of the attribute to check
Returns:
  true if the attribute exists otherwise false

#### removeAttribute

    public boolean removeAttribute(String name)

Remove an attribute.

Specified by:
  `removeAttribute` in interface `IAttributeStore`
Specified by:
  `removeAttribute` in interface `AttributeStoreMXBean`
Parameters:
  `name` - the name of the attribute to remove
Returns:
  true if the attribute was found and removed otherwise false

#### removeAttributes

    public void removeAttributes()

Remove all attributes.

Specified by:
  `removeAttributes` in interface `IAttributeStore`
Specified by:
  `removeAttributes` in interface `AttributeStoreMXBean`

#### setAttribute

    public boolean setAttribute(String name,
                       Object value)

Set an attribute on this object.

Specified by:
  `setAttribute` in interface `IAttributeStore`
Parameters:
  `name` - the name of the attribute to change
  `value` - the new value of the attribute
Returns:
  true if the attribute value changed otherwise false

#### setAttributes

    public void setAttributes(Map<String,Object> values)

Set multiple attributes on this object.

Specified by:
  `setAttributes` in interface `IAttributeStore`
Parameters:
  `values` - the attributes to set

#### setAttributes

    public void setAttributes(IAttributeStore values)

Set multiple attributes on this object.

Specified by:
  `setAttributes` in interface `IAttributeStore`
Parameters:
  `values` - the attributes to set

#### setBandwidth

    public void setBandwidth(int mbits)

Sets the bandwidth using a mbit/s value.

Specified by:
  `setBandwidth` in interface `IConnection`

#### addHeader

    public void addHeader(String name,
                 Object value)

Tell the client to add a header with all further requests. This is returned to the client as response for the next request received.

Specified by:
  `addHeader` in interface `IRemotingConnection`
Parameters:
  `name` - name of the header to add
  `value` - value of the header to add

#### addHeader

    public void addHeader(String name,
                 Object value,
                 boolean mustUnderstand)

Tell the client to add a header with all further requests. This is returned to the client as response for the next request received.

Specified by:
  `addHeader` in interface `IRemotingConnection`
Parameters:
  `name` - name of the header to add
  `value` - value of the header to add
  `mustUnderstand` - a boolean flag specifying if the server must pocess this header before handling following headers or messages

#### removeHeader

    public void removeHeader(String name)

Tell the client to no longer send a header with all further requests. This is returned to the client as response for the next request received.

Specified by:
  `removeHeader` in interface `IRemotingConnection`
Parameters:
  `name` - name of the header to remove

#### getHeaders

    public Collection<IRemotingHeader> getHeaders()

Return headers to send.

Specified by:
  `getHeaders` in interface `IRemotingConnection`
Returns:
  headers to send

#### getClientBytesRead

    public long getClientBytesRead()

Return number of written bytes the client reports to have received. This is the last value of the BytesRead message received from a client.

Specified by:
  `getClientBytesRead` in interface `IConnection`
Returns:
  number of written bytes received by the client
See Also:
  `BytesRead`

#### cleanup

    public void cleanup()

Cleans up the remoting connection client from the HttpSession and client registry. This should also fix APPSERVER-328