|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface IBackendConnection
This interface represents a connection to a MythTV-backend, -recorder or -encoder.
It is used by the IBackend
, IRecorder
or IRemoteEncoder
interfaces to send commands to a backend and
to receive the response messages. But it is also possible to create the protocol packets directly and to send them over
a backend connection to the MythTV backend.
See IMythPacket
for a detailed description how the content of a packet needs to be structured.
A request message provide a more structured to set request arguments.
A response message has additional advantages. All response messages are property-aware
.
This means that each type of response message has an enumeration property class, describing at which position in the response-arguments list,
the value for a specific property can be found. Additionally for each property a version-range
is specified,
describing in which protocol version a specific property is part of a response.
Read IMythRequest
and IMythResponse
for detailed informations about request- and response-messages, and MythProtoVersionAnnotation
for
more infos about protocol versions.
See here for an example how to read request- and response-packets.
See here for an example how to read request- and response-messages.
// establish a connection IBackendConnection connection = new BackendConnection("mythbox",6543); connection.open(); // annotate the client as monitoring client connection.writePacket(new MythPacket(connection.getVersionNr(),"ANN Monitor MythClient 0")); // check the response IMythPacket annResponse = connection.readPacket(); if(!annResponse.getPacketArg(0).equals("OK")) { System.err.println("ANN command failed"); } else { // get the next free recorder connection.writePacket(new MythPacket(connection.getVersionNr(),new String[]{ // the command (incl. space separated command arguments, if required) "GET_NEXT_FREE_RECORDER", // additional request arguments "-1" })); IMythPacket recorder = connection.readPacket(); if(recorder.getPacketArg(1).equals("nohost")) { System.out.println("No free recoder available"); } else { // do something with the recorder .... System.out.println("Next free recorder is: " + recorder.getPacketArg(0)); } } // closing the connection connection.close();
Establish a connection | |
21 | MYTH_PROTO_VERSION 65 |
13 | ACCEPT[]:[]65 |
Annotate the client as monitoring client | |
24 | ANN Monitor MythClient 0 |
2 | OK |
Get the next free recorder | |
29 | GET_NEXT_FREE_RECORDER[]:[]-1 |
26 | 1[]:[]192.168.0.2[]:[]6543 |
Close the connection | |
4 | DONE |
request messages
to the backend and to convert response packets
into more easily to read response messages
.
// get the next free recorder connection.writeMessage(new AMythRequest( // the command (and additional command arguments, if any) new AMythCommand( connection.getVersionNr(), "GET_NEXT_FREE_RECORDER" ), // additional request arguments "-1" )); // read the response packet IMythPacket resp = connection.readPacket(); // convert it into a proper response object RecorderInfo recorder = ResponseUtils.readFrom(RecorderInfo.class, resp); if(recorder == null) { System.out.println("No free recoder available"); } else { // do something with the recorder .... System.out.println("Next free recorder is: " + recorder.getRecorderID()); }
IMythPacket
Field Summary | |
---|---|
static int |
DEFAULT_COMMAND_PORT
The default MythTV-backend 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()
|
boolean |
isClosed()
Gets the curernt connection state. |
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. |
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)
|
void |
writeMessage(IMythRequest msg)
Sends a request-message to the backend. |
void |
writePacket(IMythPacket packet)
Sends a packet to the backend. |
Methods inherited from interface org.jmythapi.IVersionable |
---|
getVersionNr |
Field Detail |
---|
static final int DEFAULT_COMMAND_PORT
Method Detail |
---|
int getConnectTimeout()
void setConnectTimeout(int connectTimeout)
int getReadTimeout()
void setReadTimeout(int readTimeout)
void setMsgDebugOut(PrintStream out)
This method sets a print-stream that is used to log
incoming and outgoing packets.
This is mainly used for debugging.
backendConnection.setMsgDebugOut(System.out);
out
- a print-stream to log messages.void setInitialVersionNr(ProtocolVersion initialProtoVersion) throws IllegalStateException
This function sets the initial protocol-version that will be used by the connection
during version negotiation in the course of open()
.
This is mainly usefull to speedup connection establishing.
initialProtoVersion
- the initial protocol version.
IllegalStateException
- if the connection is already opened.String getHostname()
int getPort()
void open() throws IOException
This function opens the connection to the MythTV-backend and negotiates the protocol version to use.
IOException
- on communication errors.void open(int streamBufferSize, boolean tcpNoDelay) throws IOException
This function opens the connection to the MythTV-backend and negotiates the protocol version to use.
streamBufferSize
- the stream buffer size to usetcpNoDelay
-
IOException
- on communication errorsvoid close()
boolean isClosed()
true
if the connection was not established so far or is already closed.IMythPacket readPacket() throws IOException
IOException
- on communication errors.boolean canReadPacket() throws IOException
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.
true
if the next packet can be read
IOException
- on communication errorsvoid writePacket(IMythPacket packet) throws IOException
packet
- to packet to transfer
IOException
- on communication errors.void writeMessage(IMythRequest msg) throws IOException
msg
- the request message to be send
IOException
- on communication errors
UnknownCommandException
- if the given command is unknown
UnsupportedCommandException
- if the given command is not supported by the given protocol versionint readData(byte[] b, int offset, int len) throws IOException
b
- the byte array to fill with dataoffset
- to offset used to start writing into the arraylen
- the amount of bytes to read
IOException
- on communication errorsboolean canReadData() throws IOException
true
if at least one byte is available to read without blocking.
IOException
- on communication errorsvoid enableEventListening()
This function starts a new thread, which reads incoming IMythPacket
and passes them
to event listeners, if the received packets are event packets.
void addEventListener(IMythEventPacketListener listener)
listener
- the listener to registervoid removeEventListener(IMythEventPacketListener listener)
listener
- the listener to unregister.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |