org.jmythapi.protocol.impl
Class BackendConnection

java.lang.Object
  extended by org.jmythapi.protocol.impl.BackendConnection
All Implemented Interfaces:
Closeable, IVersionable, IBackendConnection

public class BackendConnection
extends Object
implements IVersionable, Closeable, IBackendConnection


Field Summary
 
Fields inherited from interface org.jmythapi.protocol.IBackendConnection
DEFAULT_COMMAND_PORT
 
Constructor Summary
BackendConnection(String hostname)
           
BackendConnection(String hostname, int port)
           
 
Method Summary
 void addEventListener(IMythEventPacketListener listener)
          Registers a new event listener.
 boolean canReadData()
          Checks if data is available to read without blocking.
 boolean canReadPacket()
          Checks if at least PacketUtils.SIZE_STRING_LENGTH bytes can be read from the underlying input stream without blocking.
 void close()
          Closes the connection to the MythTV-backend.
 void enableEventListening()
          Enables event listening.
 int getConnectTimeout()
           
 String getHostname()
          Gets the host name of the backend.
 int getPort()
          Gets the port of the backend.
 int getReadTimeout()
           
 ProtocolVersion getVersionNr()
          Gets the protocol-version of an object.
 boolean isAnnotated()
           
 boolean isClosed()
          Gets the curernt connection state.
 boolean isOpen()
           
protected  boolean negotiate()
           
 void open()
          Opens the connection.
 void open(int streamBufferSize, boolean tcpNoDelay)
          Opens the connection.
 int readData(byte[] b, int offset, int len)
          Reads bytes from the socket.
 IMythPacket readPacket()
          Receives the next packet from the backend.
protected  IMythPacket readPacketFromQueue()
           
protected  IMythPacket readPacketFromSocket()
           
 void removeEventListener(IMythEventPacketListener listener)
          Unregisters an event listener.
 void setConnectTimeout(int connectTimeout)
           
 void setInitialVersionNr(ProtocolVersion initialProtoVersion)
          Sets the initla protocol-version to use.
 void setMsgDebugOut(PrintStream out)
          Sets a print stream used to log packets.
 void setReadTimeout(int readTimeout)
           
 String toString()
           
 void writeMessage(IMythRequest msg)
          Sends a request-message to the backend.
 void writePacket(IMythPacket packet)
          Sends a packet to the backend.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BackendConnection

public BackendConnection(String hostname)

BackendConnection

public BackendConnection(String hostname,
                         int port)
Method Detail

setInitialVersionNr

public void setInitialVersionNr(ProtocolVersion initialProtoVersion)
                         throws IllegalStateException
Description copied from interface: IBackendConnection
Sets the initla protocol-version to use.

This function sets the initial protocol-version that will be used by the connection during version negotiation in the course of IBackendConnection.open().

This is mainly usefull to speedup connection establishing.

Specified by:
setInitialVersionNr in interface IBackendConnection
Parameters:
initialProtoVersion - the initial protocol version.
Throws:
IllegalStateException - if the connection is already opened.

getConnectTimeout

public int getConnectTimeout()
Specified by:
getConnectTimeout in interface IBackendConnection

setConnectTimeout

public void setConnectTimeout(int connectTimeout)
Specified by:
setConnectTimeout in interface IBackendConnection

getReadTimeout

public int getReadTimeout()
Specified by:
getReadTimeout in interface IBackendConnection

setReadTimeout

public void setReadTimeout(int readTimeout)
Specified by:
setReadTimeout in interface IBackendConnection

setMsgDebugOut

public void setMsgDebugOut(PrintStream out)
Description copied from interface: IBackendConnection
Sets a print stream used to log packets.

This method sets a print-stream that is used to log incoming and outgoing packets.
This is mainly used for debugging.

Usage example

Printing the packets to std-out.

    backendConnection.setMsgDebugOut(System.out);
  
Due to the above statement the following will be printed to std-out, e.g.
 

Specified by:
setMsgDebugOut in interface IBackendConnection
Parameters:
out - a print-stream to log messages.

getHostname

public String getHostname()
Description copied from interface: IBackendConnection
Gets the host name of the backend.

Specified by:
getHostname in interface IBackendConnection
Returns:
the host name of the backend.

getPort

public int getPort()
Description copied from interface: IBackendConnection
Gets the port of the backend.

Specified by:
getPort in interface IBackendConnection
Returns:
the port of the backend.

getVersionNr

public ProtocolVersion getVersionNr()
Description copied from interface: IVersionable
Gets the protocol-version of an object.

Specified by:
getVersionNr in interface IVersionable
Returns:
the version a given object was created for.

open

public void open()
          throws IOException
Description copied from interface: IBackendConnection
Opens the connection.

This function opens the connection to the MythTV-backend and negotiates the protocol version to use.

Specified by:
open in interface IBackendConnection
Throws:
IOException - on communication errors.

open

public void open(int streamBufferSize,
                 boolean tcpNoDelay)
          throws IOException
Description copied from interface: IBackendConnection
Opens the connection.

This function opens the connection to the MythTV-backend and negotiates the protocol version to use.

Specified by:
open in interface IBackendConnection
Parameters:
streamBufferSize - the stream buffer size to use
Throws:
IOException - on communication errors

close

public void close()
Description copied from interface: IBackendConnection
Closes the connection to the MythTV-backend.

Specified by:
close in interface Closeable
Specified by:
close in interface IBackendConnection

isClosed

public boolean isClosed()
Description copied from interface: IBackendConnection
Gets the curernt connection state.

Specified by:
isClosed in interface IBackendConnection
Returns:
true if the connection was not established so far or is already closed.

isOpen

public boolean isOpen()

isAnnotated

public boolean isAnnotated()

negotiate

protected boolean negotiate()
                     throws IOException
Throws:
IOException

readPacket

public IMythPacket readPacket()
                       throws IOException
Description copied from interface: IBackendConnection
Receives the next packet from the backend.

Specified by:
readPacket in interface IBackendConnection
Returns:
the next read packet.
Throws:
IOException - on communication errors.

readPacketFromQueue

protected IMythPacket readPacketFromQueue()
                                   throws IOException
Throws:
IOException

readPacketFromSocket

protected IMythPacket readPacketFromSocket()
                                    throws IOException
Throws:
IOException

canReadPacket

public boolean canReadPacket()
                      throws IOException
Description copied from interface: IBackendConnection
Checks if at least PacketUtils.SIZE_STRING_LENGTH bytes can be read from the underlying input stream without blocking.

This function can be used to check if we should start reading the next packet.

Specified by:
canReadPacket in interface IBackendConnection
Returns:
true if the next packet can be read
Throws:
IOException - on communication errors

writePacket

public void writePacket(IMythPacket packet)
                 throws IOException
Description copied from interface: IBackendConnection
Sends a packet to the backend.

Specified by:
writePacket in interface IBackendConnection
Parameters:
packet - to packet to transfer
Throws:
IOException - on communication errors.

writeMessage

public void writeMessage(IMythRequest msg)
                  throws IOException
Description copied from interface: IBackendConnection
Sends a request-message to the backend.

Specified by:
writeMessage in interface IBackendConnection
Parameters:
msg - the request message to be send
Throws:
IOException - on communication errors
UnknownCommandException - if the given command is unknown
UnsupportedCommandException - if the given command is not supported by the given protocol version

readData

public int readData(byte[] b,
                    int offset,
                    int len)
             throws IOException
Description copied from interface: IBackendConnection
Reads bytes from the socket.

Specified by:
readData in interface IBackendConnection
Parameters:
b - the byte array to fill with data
offset - to offset used to start writing into the array
len - the amount of bytes to read
Returns:
the amount of read bytes.
Throws:
IOException - on communication errors

canReadData

public boolean canReadData()
                    throws IOException
Description copied from interface: IBackendConnection
Checks if data is available to read without blocking.

Specified by:
canReadData in interface IBackendConnection
Returns:
true if at least one byte is available to read without blocking.
Throws:
IOException - on communication errors

enableEventListening

public void enableEventListening()
Description copied from interface: IBackendConnection
Enables event listening.

This function starts a new thread, which reads incoming IMythPacket and passes them to event listeners, if the received packets are event packets.

Specified by:
enableEventListening in interface IBackendConnection

addEventListener

public void addEventListener(IMythEventPacketListener listener)
Description copied from interface: IBackendConnection
Registers a new event listener.

Specified by:
addEventListener in interface IBackendConnection
Parameters:
listener - the listener to register

removeEventListener

public void removeEventListener(IMythEventPacketListener listener)
Description copied from interface: IBackendConnection
Unregisters an event listener.

Specified by:
removeEventListener in interface IBackendConnection
Parameters:
listener - the listener to unregister.

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008-2013. All Rights Reserved.