org.eclipse.paho.client.mqttv3.internal
Class ClientState

java.lang.Object
  extended by org.eclipse.paho.client.mqttv3.internal.ClientState

public class ClientState
extends Object

The core of the client, which holds the state information for pending and in-flight messages. Messages that have been accepted for delivery are moved between several objects while being delivered. 1) When the client is not running messages are stored in a persistent store that implements the MqttClientPersistent Interface. The default is MqttDefaultFilePersistencew which stores messages safely across failures and system restarts. If no persistence is specified there is a fall back to MemoryPersistence which will maintain the messages while the Mqtt client is instantiated. 2) When the client or specifically ClientState is instantiated the messages are read from the persistent store into: - outboundqos2 hashtable if a QoS 2 PUBLISH or PUBREL - outboundqos1 hashtable if a QoS 1 PUBLISH (see restoreState) 3) On Connect, copy messages from the outbound hashtables to the pendingMessages or pendingFlows vector in messageid order. - Initial message publish goes onto the pendingmessages buffer. - PUBREL goes onto the pendingflows buffer (see restoreInflightMessages) 4) Sender thread reads messages from the pendingflows and pendingmessages buffer one at a time. The message is removed from the pendingbuffer but remains on the outbound* hashtable. The hashtable is the place where the full set of outstanding messages are stored in memory. (Persistence is only used at start up) 5) Receiver thread - receives wire messages: - if QoS 1 then remove from persistence and outboundqos1 - if QoS 2 PUBREC send PUBREL. Updating the outboundqos2 entry with the PUBREL and update persistence. - if QoS 2 PUBCOMP remove from persistence and outboundqos2 Notes: because of the multithreaded nature of the client it is vital that any changes to this class take concurrency into account. For instance as soon as a flow / message is put on the wire it is possible for the receiving thread to receive the ack and to be processing the response before the sending side has finished processing. For instance a connect may be sent, the conack received before the connect notify send has been processed!


Constructor Summary
protected ClientState(MqttClientPersistence persistence, CommsTokenStore tokenStore, CommsCallback callback, ClientComms clientComms)
           
 
Method Summary
protected  boolean checkQuiesceLock()
           
protected  void clearState()
           
protected  void close()
          Tidy up - ensure that tokens are released as they are maintained over a disconnect / connect cycle.
 void connected()
          Called when the client has successfully connected to the broker
protected  void deliveryComplete(MqttPublish message)
           
 void disconnected(MqttException reason)
          Called when the client has been disconnected from the broker.
protected  MqttWireMessage get()
          This returns the next piece of work, ie message, for the CommsSender to send over the network.
 Properties getDebug()
           
protected  long getKeepAlive()
           
protected  void notifyComplete(MqttToken token)
          Called when waiters and callbacks have processed the message.
protected  void notifyQueueLock()
           
protected  void notifyReceivedAck(MqttAck ack)
          Called by the CommsReceiver when an ack has arrived.
protected  void notifyReceivedMsg(MqttWireMessage message)
          Called by the CommsReceiver when a message has been received.
protected  void notifyResult(MqttWireMessage ack, MqttToken token, MqttException ex)
           
protected  void notifySent(MqttWireMessage message)
          Called by the CommsSender when a message has been sent
 void quiesce(long timeout)
          Quiesce the client state, preventing any new messages getting sent, and preventing the callback on any newly received messages.
 Vector resolveOldTokens(MqttException reason)
          Called during shutdown to work out if there are any tokens still to be notified and waiters to be unblocked.
protected  void restoreState()
          Restores the state information from persistence.
 void send(MqttWireMessage message, MqttToken token)
          Submits a message for delivery.
protected  void setCleanSession(boolean cleanSession)
           
 void setKeepAliveInterval(long interval)
           
protected  void setKeepAliveSecs(long keepAliveSecs)
           
protected  void undo(MqttPublish message)
          This removes the MqttSend message from the outbound queue and persistence.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClientState

protected ClientState(MqttClientPersistence persistence,
                      CommsTokenStore tokenStore,
                      CommsCallback callback,
                      ClientComms clientComms)
               throws MqttException
Throws:
MqttException
Method Detail

setKeepAliveSecs

protected void setKeepAliveSecs(long keepAliveSecs)

getKeepAlive

protected long getKeepAlive()

setCleanSession

protected void setCleanSession(boolean cleanSession)

clearState

protected void clearState()
                   throws MqttException
Throws:
MqttException

restoreState

protected void restoreState()
                     throws MqttException
Restores the state information from persistence.

Throws:
MqttException

send

public void send(MqttWireMessage message,
                 MqttToken token)
          throws MqttException
Submits a message for delivery. This method will block until there is room in the inFlightWindow for the message. The message is put into persistence before returning.

Parameters:
message - the message to send
token - the token that can be used to track delivery of the message
Throws:
MqttException

undo

protected void undo(MqttPublish message)
             throws MqttPersistenceException
This removes the MqttSend message from the outbound queue and persistence.

Parameters:
message -
Throws:
MqttPersistenceException

get

protected MqttWireMessage get()
                       throws MqttException
This returns the next piece of work, ie message, for the CommsSender to send over the network. Calls to this method block until either: - there is a message to be sent - the keepAlive interval is exceeded, which triggers a ping message to be returned - #disconnected(MqttException, boolean) is called

Returns:
the next message to send, or null if the client is disconnected
Throws:
MqttException

setKeepAliveInterval

public void setKeepAliveInterval(long interval)

notifySent

protected void notifySent(MqttWireMessage message)
Called by the CommsSender when a message has been sent

Parameters:
message -

checkQuiesceLock

protected boolean checkQuiesceLock()

notifyReceivedAck

protected void notifyReceivedAck(MqttAck ack)
                          throws MqttException
Called by the CommsReceiver when an ack has arrived.

Parameters:
message -
Throws:
MqttException

notifyReceivedMsg

protected void notifyReceivedMsg(MqttWireMessage message)
                          throws MqttException
Called by the CommsReceiver when a message has been received. Handles inbound messages and other flows such as PUBREL.

Parameters:
message -
Throws:
MqttException

notifyComplete

protected void notifyComplete(MqttToken token)
                       throws MqttException
Called when waiters and callbacks have processed the message. For messages where delivery is complete the message can be removed from persistence and counters adjusted accordingly. Also tidy up by removing token from store...

Parameters:
message -
Throws:
MqttException

notifyResult

protected void notifyResult(MqttWireMessage ack,
                            MqttToken token,
                            MqttException ex)

connected

public void connected()
Called when the client has successfully connected to the broker


resolveOldTokens

public Vector resolveOldTokens(MqttException reason)
Called during shutdown to work out if there are any tokens still to be notified and waiters to be unblocked. Notifying and unblocking takes place after most shutdown processing has completed. The tokenstore is tidied up so it only contains outstanding delivery tokens which are valid after reconnect (if clean session is false)

Parameters:
reason - The root cause of the disconnection, or null if it is a clean disconnect

disconnected

public void disconnected(MqttException reason)
Called when the client has been disconnected from the broker.

Parameters:
reason - The root cause of the disconnection, or null if it is a clean disconnect

quiesce

public void quiesce(long timeout)
Quiesce the client state, preventing any new messages getting sent, and preventing the callback on any newly received messages. After the timeout expires, delete any pending messages except for outbound ACKs, and wait for those ACKs to complete.


notifyQueueLock

protected void notifyQueueLock()

deliveryComplete

protected void deliveryComplete(MqttPublish message)
                         throws MqttPersistenceException
Throws:
MqttPersistenceException

close

protected void close()
Tidy up - ensure that tokens are released as they are maintained over a disconnect / connect cycle.


getDebug

public Properties getDebug()


Copyright © 2013. All Rights Reserved.