public class StaticIMCConnection
extends java.lang.Object
// Listen for IMC messages on port 6070 and all messages sent will go to 127.0.0.1:6002
StaticIMCConnection conn = new StaticIMCConnection("127.0.0.1", 6002, 6070);
// activate listening for messages
conn.setPolling(true);
// wait up to 1 second to receive a message of type "EstimatedState"
IMCMessage m = conn.recv("EstimatedState", 1000);
if (m != null)
m.dump(System.out); // Print out the message if it was received
// Print all incoming Temperature messages to screen
while(true) {
System.out.println(conn.poll("Temperature"));
}
Constructor and Description |
---|
StaticIMCConnection(java.lang.String remoteHost,
int remotePort,
int localPort)
Create a connection to a remote IMC host
|
Modifier and Type | Method and Description |
---|---|
static void |
main(java.lang.String[] args) |
IMCMessage |
poll(java.lang.String abbrev)
Retrieve the last received message of a given type.
|
<T extends IMCMessage> |
recv(java.lang.Class<T> clazz,
long timeoutMillis)
Receive a message of given class.
|
IMCMessage |
recv(java.lang.String abbrev,
long timeoutMillis)
Synchronously wait for a message of a specific type
|
boolean |
send(IMCMessage m)
Send an IMC message to the remote host
|
void |
setPolling(boolean polling)
Activates/deactivates polling mechanism for listening for incoming
messages.
|
ImcSystemState |
state()
Retrieve the state of this connection
|
void |
stop()
Stop this connection and stops listening for messages.
|
public StaticIMCConnection(java.lang.String remoteHost, int remotePort, int localPort)
remoteHost
- The IP address of the remote host. Examples:
"127.0.0.1"
, "192.168.0.109"
.remotePort
- The remote port to connect to.localPort
- The local port to bind to and listen for incoming messagespublic boolean send(IMCMessage m)
m
- The message to be senttrue
if no error was found while sending. Since we
are using UDP no guarantee is given in terms of successful
message delivery, all we know is that the message was sent out.public void stop()
public void setPolling(boolean polling)
poll(String)
polling
- Whether to use polling of messages.public IMCMessage recv(java.lang.String abbrev, long timeoutMillis)
abbrev
- The message type you want to receive. Examples:
"Heartbeat"
, EstimatedState
, etc.timeoutMillis
- Maximum amount of time to wait for a message to be received in
milliseconds after which null
will be returned
([] in Matlab).null
if no new message of
that type was received in timeoutMillis
millisecondspublic IMCMessage poll(java.lang.String abbrev)
abbrev
- The type of message to be retrieved. Examples:
"Heartbeat"
, EstimatedState
, etc.null
if
no message of that type was yet received in this connection.setPolling(boolean)
public <T extends IMCMessage> T recv(java.lang.Class<T> clazz, long timeoutMillis)
recv(String, long)
but the message gets cast to selected class.clazz
- The class of the message to be received.timeoutMillis
- Amount of time to wait for a new message, in milliseconds.null
if no new message
of that type was received in timeoutMillis
millisecondsrecv(String, long)
public ImcSystemState state()
ImcSystemState
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception