net.sf.xenqtt.message
Class MqttClientChannel

java.lang.Object
  extended by net.sf.xenqtt.message.MqttClientChannel
All Implemented Interfaces:
MqttChannel, MqttChannelRef

public final class MqttClientChannel
extends Object

An MqttChannel to use for the client side of the connection.


Constructor Summary
MqttClientChannel(String host, int port, MessageHandler handler, Selector selector, long messageResendIntervalMillis, BlockingCommand<?> connectionCompleteCommand, net.sf.xenqtt.message.MutableMessageStats stats)
          Starts an asynchronous connection to the specified host and port.
 
Method Summary
 void cancelBlockingCommands()
          Cancels all blocking commands.
 void close()
          Closes the underlying channels, sockets, etc
 void close(Throwable cause)
          Closes the underlying channels, sockets, etc and sends cause to the MessageHandler.channelClosed(MqttChannel, Throwable) callback.
 void deregister()
          Deregisters this channel.
 boolean finishConnect()
          Finishes a connection.
 String getLocalAddress()
           
 String getRemoteAddress()
           
 List<MqttMessage> getUnsentMessages()
           
 long houseKeeping(long now)
          Performs housekeeping: message resends, ping requests, etc
 int inFlightMessageCount()
           
 boolean isConnected()
          Tells whether or not this channel is connected.
 boolean isConnectionPending()
          Tells whether or not a connection operation is in progress on this channel.
 boolean isOpen()
          Tells whether or not this channel is open.
 boolean read(long now)
          Reads data.
 boolean register(Selector selector, MessageHandler handler)
          Registers this channel with the specified selector.
 boolean send(MqttMessage message)
          Sends the specified message asynchronously.
 boolean send(MqttMessage message, BlockingCommand<MqttMessage> blockingCommand)
          Sends the specified message asynchronously.
 int sendQueueDepth()
           
 String toString()
           
 boolean write(long now)
          Writes as much data as possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MqttClientChannel

public MqttClientChannel(String host,
                         int port,
                         MessageHandler handler,
                         Selector selector,
                         long messageResendIntervalMillis,
                         BlockingCommand<?> connectionCompleteCommand,
                         net.sf.xenqtt.message.MutableMessageStats stats)
                  throws IOException
Starts an asynchronous connection to the specified host and port. When a SelectionKey for the specified selector has SelectionKey.OP_CONNECT as a ready op then finishConnect() should be called.

Parameters:
messageResendIntervalMillis - Millis between attempts to resend a message that MqttMessage.isAckable(). 0 to disable message resends
connectionCompleteCommand - If not null then this latch is complete when the ConnAckMessage is received.
Throws:
IOException
Method Detail

deregister

public final void deregister()
Description copied from interface: MqttChannel
Deregisters this channel. Cancels the underlying SelectionKey.

Specified by:
deregister in interface MqttChannel
See Also:
MqttChannel.deregister()

register

public final boolean register(Selector selector,
                              MessageHandler handler)
Description copied from interface: MqttChannel
Registers this channel with the specified selector. This channel must not already be registered with another selector. The current MessageHandler is replaced with the specified one.

Specified by:
register in interface MqttChannel
Returns:
A return value of true does NOT necessarily mean this channel is open but false does mean it is closed (or the connect hasn't finished yet).
See Also:
MqttChannel.register(java.nio.channels.Selector, net.sf.xenqtt.message.MessageHandler)

finishConnect

public final boolean finishConnect()
Description copied from interface: MqttChannel
Finishes a connection. This should be called when a SelectionKeys SelectionKey.OP_CONNECT op is ready.

Specified by:
finishConnect in interface MqttChannel
Returns:
True if and only if this channel is now connected.
See Also:
MqttChannel.finishConnect()

read

public final boolean read(long now)
Description copied from interface: MqttChannel
Reads data. This will read as many messages as it can and pass them to a MessageHandler.This should be called when a SelectionKeys SelectionKey.OP_READ op is ready.

Specified by:
read in interface MqttChannel
Parameters:
now - The timestamp to use as the "current" time
Returns:
True if the channel is left open. False if it is closed by this method or already closed when this method is called or the connect hasn't finished yet.
See Also:
MqttChannel.read(long)

send

public final boolean send(MqttMessage message)
Description copied from interface: MqttChannel
Sends the specified message asynchronously. When a DisconnectMessage or a ConnAckMessage where ConnAckMessage.getReturnCode() is not ConnectReturnCode.ACCEPTED is sent the channel is closed automatically. This is the same as calling #send(MqttMessage, BlockingCommand, long) with a null BlockingCommand and 0 timestamp.

Specified by:
send in interface MqttChannel
Parameters:
message - The message to send
Returns:
A return value of true does NOT necessarily mean this channel is open but false does mean it is closed (or the connect hasn't finished yet).
See Also:
MqttChannel.send(net.sf.xenqtt.message.MqttMessage)

send

public final boolean send(MqttMessage message,
                          BlockingCommand<MqttMessage> blockingCommand)
Description copied from interface: MqttChannel
Sends the specified message asynchronously. When a DisconnectMessage or a ConnAckMessage where ConnAckMessage.getReturnCode() is not ConnectReturnCode.ACCEPTED is sent the channel is closed automatically.

Specified by:
send in interface MqttChannel
Parameters:
message - The message to send
blockingCommand - If not null then this latch is complete when processing the message is complete. The definition of complete is:
  • If the message is a ConnectMessage processing is complete when the ConnAckMessage is received.
  • else if the message is ackable processing is complete when the ack is received.
  • else processing is complete when the message is written to the socket.
  • If any exception occurs or the channel is closed all in flight messages are complete
Returns:
A return value of true does NOT necessarily mean this channel is open but false does mean it is closed (or the connect hasn't finished yet).
See Also:
MqttChannel.send(net.sf.xenqtt.message.MqttMessage, net.sf.xenqtt.message.BlockingCommand)

write

public final boolean write(long now)
Description copied from interface: MqttChannel
Writes as much data as possible. This should be called when a SelectionKeys SelectionKey.OP_WRITE op is ready.

Specified by:
write in interface MqttChannel
Parameters:
now - The timestamp to use as the "current" time
Returns:
True if the channel is left open. False if it is closed by this method or already closed when this method is called or the connect hasn't finished yet.
See Also:
MqttChannel.write(long)

close

public final void close()
Description copied from interface: MqttChannel
Closes the underlying channels, sockets, etc

Specified by:
close in interface MqttChannel
See Also:
MqttChannel.close()

close

public final void close(Throwable cause)
Description copied from interface: MqttChannel
Closes the underlying channels, sockets, etc and sends cause to the MessageHandler.channelClosed(MqttChannel, Throwable) callback.

Specified by:
close in interface MqttChannel
See Also:
MqttChannel.close(java.lang.Throwable)

isOpen

public final boolean isOpen()
Description copied from interface: MqttChannel
Tells whether or not this channel is open. This channel is open if the underlying channels, sockets, etc are open

Specified by:
isOpen in interface MqttChannel
Returns:
true if, and only if, this channel is open
See Also:
MqttChannel.isOpen()

isConnected

public final boolean isConnected()
Description copied from interface: MqttChannel
Tells whether or not this channel is connected. This channel is connected if MqttChannel.isOpen() is true, Connect/ConnectAck has finished, and no disconnect has been received/sent.

Specified by:
isConnected in interface MqttChannel
Returns:
True if and only if this channel is connected.
See Also:
MqttChannel.isConnected()

isConnectionPending

public final boolean isConnectionPending()
Description copied from interface: MqttChannel
Tells whether or not a connection operation is in progress on this channel.

Specified by:
isConnectionPending in interface MqttChannel
Returns:
true if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the MqttChannel.finishConnect() method
See Also:
MqttChannel.isConnectionPending()

houseKeeping

public final long houseKeeping(long now)
Description copied from interface: MqttChannel
Performs housekeeping: message resends, ping requests, etc

Specified by:
houseKeeping in interface MqttChannel
Parameters:
now - The timestamp to use as the "current" time
Returns:
Maximum millis until this method should be called again. This result is only valid when this method is called. Future calls to MqttChannel.read(long) or MqttChannel.write(long) may change this value. Returns < 0 if this method closes the channel.
See Also:
MqttChannel.houseKeeping(long)

sendQueueDepth

public final int sendQueueDepth()
Specified by:
sendQueueDepth in interface MqttChannel
Returns:
The number of messages in the send queue. This includes any message currently in the process of being sent
See Also:
MqttChannel.sendQueueDepth()

inFlightMessageCount

public final int inFlightMessageCount()
Specified by:
inFlightMessageCount in interface MqttChannel
Returns:
The number of messages currently in flight (QoS level > 0)
See Also:
MqttChannel.inFlightMessageCount()

cancelBlockingCommands

public final void cancelBlockingCommands()
Description copied from interface: MqttChannel
Cancels all blocking commands. This is not done when the channel is closed because we may want to reconnect instead of releasing the commands.

Specified by:
cancelBlockingCommands in interface MqttChannel
See Also:
MqttChannel.cancelBlockingCommands()

getUnsentMessages

public final List<MqttMessage> getUnsentMessages()
Specified by:
getUnsentMessages in interface MqttChannel
Returns:
All messages that have not been sent. This includes messages queued to be sent, any partially sent message, and all in flight messages.
See Also:
MqttChannel.getUnsentMessages()

getRemoteAddress

public final String getRemoteAddress()
Specified by:
getRemoteAddress in interface MqttChannel
Returns:
The channel's remote address
See Also:
MqttChannel.getRemoteAddress()

getLocalAddress

public String getLocalAddress()
Specified by:
getLocalAddress in interface MqttChannel
Returns:
The channel's local address
See Also:
MqttChannel.getLocalAddress()

toString

public final String toString()
Overrides:
toString in class Object
See Also:
Object.toString()


Copyright © 2013. All Rights Reserved.