-
public class ClientStateThe 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!
-
-
Field Summary
Fields Modifier and Type Field Description private MqttClientPersistencepersistenceprivate longfastReconnectCheckStartTimeprivate longlastOutboundActivityprivate longlastInboundActivity
-
Method Summary
Modifier and Type Method Description voidsetPersistence(MqttClientPersistence persistence)longgetFastReconnectCheckStartTime()longgetLastOutboundActivity()longgetLastInboundActivity()voidsend(MqttWireMessage message, MqttToken token)Submits a message for delivery. MqttTokencheckForActivity()Check and send a ping if needed and check for ping timeout. MqttTokensendForcePingRequest()Check and send a ping and check for ping timeout. voidcheckActivity()voidsetKeepAliveInterval(long interval)voidnotifySentCallback(MqttToken token)voidconnected()Called when the client has successfully connected to the broker VectorresolveOldTokens(MqttException reason)Called during shutdown to work out if there are any tokens still to be notified and waiters to be unblocked. voiddisconnected(MqttException reason)Called when the client has been disconnected from the broker. voidquiesce(long timeout)Quiesce the client state, preventing any new messages getting sent, and preventing the callback on any newly received messages. voidnotifyQueueLock()PropertiesgetDebug()intgetInflightMsgs()intgetMaxInflightMsgs()voidpersistBufferedMessage(MqttWireMessage message)Persists a buffered message to the persistence layer voidunPersistBufferedMessage(MqttWireMessage message)-
-
Method Detail
-
setPersistence
void setPersistence(MqttClientPersistence persistence)
-
getFastReconnectCheckStartTime
long getFastReconnectCheckStartTime()
-
getLastOutboundActivity
long getLastOutboundActivity()
-
getLastInboundActivity
long getLastInboundActivity()
-
send
void send(MqttWireMessage message, MqttToken token)
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 sendtoken- the token that can be used to track delivery of the message
-
checkForActivity
MqttToken checkForActivity()
Check and send a ping if needed and check for ping timeout. Need to send a ping if nothing has been sent or received in the last keepalive interval. It is important to check for both sent and received packets in order to catch the case where an app is solely sending QoS 0 messages or receiving QoS 0 messages. QoS 0 message are not good enough for checking a connection is alive as they are one way messages. If a ping has been sent but no data has been received in the last keepalive interval then the connection is deamed to be broken.
-
sendForcePingRequest
MqttToken sendForcePingRequest()
Check and send a ping and check for ping timeout.
-
checkActivity
void checkActivity()
-
setKeepAliveInterval
void setKeepAliveInterval(long interval)
-
notifySentCallback
void notifySentCallback(MqttToken token)
-
connected
void connected()
Called when the client has successfully connected to the broker
-
resolveOldTokens
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
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
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
void notifyQueueLock()
-
getDebug
Properties getDebug()
-
getInflightMsgs
int getInflightMsgs()
-
getMaxInflightMsgs
int getMaxInflightMsgs()
-
persistBufferedMessage
void persistBufferedMessage(MqttWireMessage message)
Persists a buffered message to the persistence layer
- Parameters:
message- The MqttWireMessage to persist
-
unPersistBufferedMessage
void unPersistBufferedMessage(MqttWireMessage message)
- Parameters:
message- The MqttWireMessage to un-persist
-
-
-
-