├── .gitignore
├── .project
├── README.md
├── build.xml
├── libs
├── guava-17.0.jar
└── jettison-1.1.jar
├── src
├── YunBaDemo.java
├── io
│ └── yunba
│ │ └── java
│ │ ├── core
│ │ ├── Constants.java
│ │ ├── EventBusMessageDelivery.java
│ │ ├── MQTTMessage.java
│ │ ├── MQTTStack.java
│ │ ├── MessageDelivery.java
│ │ └── event
│ │ │ ├── IEvent.java
│ │ │ └── MessageArrivedEvent.java
│ │ ├── manager
│ │ └── YunBaManager.java
│ │ └── util
│ │ ├── CommonUtil.java
│ │ └── MqttUtil.java
└── org
│ └── eclipse
│ └── paho
│ └── client
│ └── mqttv3
│ ├── IMqttActionListener.java
│ ├── IMqttAsyncClient.java
│ ├── IMqttClient.java
│ ├── IMqttDeliveryToken.java
│ ├── IMqttToken.java
│ ├── MqttAsyncClient.java
│ ├── MqttCallback.java
│ ├── MqttClient.java
│ ├── MqttClientPersistence.java
│ ├── MqttConnectOptions.java
│ ├── MqttDeliveryToken.java
│ ├── MqttException.java
│ ├── MqttMessage.java
│ ├── MqttPersistable.java
│ ├── MqttPersistenceException.java
│ ├── MqttSecurityException.java
│ ├── MqttToken.java
│ ├── MqttTopic.java
│ ├── internal
│ ├── ClientComms.java
│ ├── ClientDefaults.java
│ ├── ClientState.java
│ ├── CommsCallback.java
│ ├── CommsReceiver.java
│ ├── CommsSender.java
│ ├── CommsTokenStore.java
│ ├── DestinationProvider.java
│ ├── ExceptionHelper.java
│ ├── FileLock.java
│ ├── LocalNetworkModule.java
│ ├── MessageCatalog.java
│ ├── MqttPersistentData.java
│ ├── NetworkModule.java
│ ├── ResourceBundleCatalog.java
│ ├── SSLNetworkModule.java
│ ├── TCPNetworkModule.java
│ ├── Token.java
│ ├── nls
│ │ ├── logcat.properties
│ │ ├── logcat_cs.properties
│ │ ├── logcat_de.properties
│ │ ├── logcat_es.properties
│ │ ├── logcat_fr.properties
│ │ ├── logcat_hu.properties
│ │ ├── logcat_it.properties
│ │ ├── logcat_ja.properties
│ │ ├── logcat_ko.properties
│ │ ├── logcat_pl.properties
│ │ ├── logcat_pt_BR.properties
│ │ ├── logcat_ru.properties
│ │ ├── logcat_zh_CN.properties
│ │ ├── logcat_zh_TW.properties
│ │ ├── messages.properties
│ │ ├── messages_cs.properties
│ │ ├── messages_de.properties
│ │ ├── messages_es.properties
│ │ ├── messages_fr.properties
│ │ ├── messages_hu.properties
│ │ ├── messages_it.properties
│ │ ├── messages_ja.properties
│ │ ├── messages_ko.properties
│ │ ├── messages_pl.properties
│ │ ├── messages_pt_BR.properties
│ │ ├── messages_ru.properties
│ │ ├── messages_zh_CN.properties
│ │ └── messages_zh_TW.properties
│ ├── security
│ │ ├── SSLSocketFactoryFactory.java
│ │ └── SimpleBase64Encoder.java
│ └── wire
│ │ ├── CountingInputStream.java
│ │ ├── MqttAck.java
│ │ ├── MqttConnack.java
│ │ ├── MqttConnect.java
│ │ ├── MqttDisconnect.java
│ │ ├── MqttExpand.java
│ │ ├── MqttExpandAck.java
│ │ ├── MqttExpandPublish.java
│ │ ├── MqttInputStream.java
│ │ ├── MqttOutputStream.java
│ │ ├── MqttPersistableWireMessage.java
│ │ ├── MqttPingReq.java
│ │ ├── MqttPingResp.java
│ │ ├── MqttPubAck.java
│ │ ├── MqttPubComp.java
│ │ ├── MqttPubRec.java
│ │ ├── MqttPubRel.java
│ │ ├── MqttPublish.java
│ │ ├── MqttReceivedMessage.java
│ │ ├── MqttSuback.java
│ │ ├── MqttSubscribe.java
│ │ ├── MqttUnsubAck.java
│ │ ├── MqttUnsubscribe.java
│ │ ├── MqttWireMessage.java
│ │ ├── MultiByteArrayInputStream.java
│ │ └── MultiByteInteger.java
│ ├── logging
│ ├── JSR47Logger.java
│ ├── Logger.java
│ ├── LoggerFactory.java
│ ├── MLogger.java
│ ├── SimpleLogFormatter.java
│ ├── jsr47min.properties
│ └── package.html
│ ├── package.html
│ ├── persist
│ ├── MemoryPersistence.java
│ ├── MqttDefaultFilePersistence.java
│ └── package.html
│ └── util
│ ├── Debug.java
│ └── package.html
└── yunba-java-sdk.jar
/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | /dist
3 | /bin
4 | /bin1
5 | /.settings
6 | .classpath
7 | .DS_Store
8 | .opts
9 | src/org/eclipse/paho/util/YunBaDemo.javA
10 |
11 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 | A listener is registered on an MqttToken and a token is associated
7 | * with an action like connect or publish. When used with tokens on the MqttAsyncClient
8 | * the listener will be called back on the MQTT clients thread. The listener will be informed
9 | * if the action succeeds or fails. It is important that the listener returns control quickly
10 | * otherwise the operation of the MQTT client will be stalled.
11 | * A subclass of IMqttToken that allows the delivery of a message to be tracked.
6 | * Unlike instances of IMqttToken delivery tokens can be used across connection
7 | * and client restarts. This enables the delivery of a messages to be tracked
8 | * after failures. There are two approaches
9 | *
18 | * An action is in progress until either:
19 | *
10 | *
17 | *
20 | *
27 | *
Until the message has been delivered, the message being delivered will
35 | * be returned. Once the message has been delivered null
will be
36 | * returned.
37 | * @return the message associated with this token or null if already delivered.
38 | * @throws MqttException if there was a problem completing retrieving the message
39 | */
40 | public MqttMessage getMessage() throws MqttException;
41 | }
42 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/IMqttToken.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3;
13 |
14 | import org.codehaus.jettison.json.JSONObject;
15 |
16 |
17 |
18 |
19 | /**
20 | * Provides a mechanism for tracking the completion of an asynchronous task.
21 | *
22 | *
When using the asynchronous/non-blocking MQTT programming interface all 23 | * methods /operations that take any time and in particular those that involve 24 | * any network operation return control to the caller immediately. The operation 25 | * then proceeds to run in the background so as not to block the invoking thread. 26 | * An IMqttToken is used to track the state of the operation. An application can use the 27 | * token to wait for an operation to complete. A token is passed to callbacks 28 | * once the operation completes and provides context linking it to the original 29 | * request. A token is associated with a single operation.
30 | *
31 | * An action is in progress until either: 32 | *
The timeout specifies the maximum time it will block for. If the action 54 | * completes before the timeout then control returns immediately, if not 55 | * it will block until the timeout expires.
56 | *If the action being tracked fails or the timeout expires an exception will 57 | * be thrown. In the event of a timeout the action may complete after timeout. 58 | *
59 | * 60 | * @param timeout the maximum amount of time to wait for, in milliseconds. 61 | * @throws MqttException if there was a problem with the action associated with the token. 62 | */ 63 | public void waitForCompletion(long timeout) throws MqttException; 64 | 65 | /** 66 | * Returns whether or not the action has finished. 67 | *True will be returned both in the case where the action finished successfully 68 | * and in the case where it failed. If the action failed {@link #getException()} will 69 | * be non null. 70 | *
71 | */ 72 | public boolean isComplete(); 73 | 74 | /** 75 | * Returns an exception providing more detail if an operation failed 76 | *While an action in in progress and when an action completes successfully 77 | * null will be returned. Certain errors like timeout or shutting down will not 78 | * set the exception as the action has not failed or completed at that time 79 | *
80 | * @return exception may return an exception if the operation failed. Null will be 81 | * returned while action is in progress and if action completes successfully. 82 | */ 83 | public MqttException getException(); 84 | 85 | /** 86 | * Register a listener to be notified when an action completes. 87 | *Once a listener is registered it will be invoked when the action the token 88 | * is associated with either succeeds or fails. 89 | *
90 | * @param listener to be invoked once the action completes 91 | */ 92 | public void setActionCallback(IMqttActionListener listener); 93 | 94 | /** 95 | * Return the async listener for this token. 96 | * @return listener that is set on the token or null if a listener is not registered. 97 | */ 98 | public IMqttActionListener getActionCallback(); 99 | 100 | /** 101 | * Returns the MQTT client that is responsible for processing the asynchronous 102 | * action 103 | */ 104 | public IMqttAsyncClient getClient(); 105 | 106 | /** 107 | * Returns the topic string(s) for the action being tracked by this 108 | * token. If the action has not been initiated or the action has not 109 | * topic associated with it such as connect then null will be returned. 110 | * 111 | * @return the topic string(s) for the subscribe being tracked by this token or null 112 | */ 113 | public String[] getTopics(); 114 | 115 | /** 116 | * Store some context associated with an action. 117 | *Allows the caller of an action to store some context that can be 118 | * accessed from within the ActionListener associated with the action. This 119 | * can be useful when the same ActionListener is associated with multiple 120 | * actions
121 | * @param userContext to associate with an action 122 | */ 123 | public void setUserContext(Object userContext); 124 | 125 | /** 126 | * Retrieve the context associated with an action. 127 | *Allows the ActionListener associated with an action to retrieve any context 128 | * that was associated with the action when the action was invoked. If not 129 | * context was provided null is returned.
130 | 131 | * @return Object context associated with an action or null if there is none. 132 | */ 133 | public Object getUserContext(); 134 | 135 | /** 136 | * Returns the message ID of the message that is associated with the token. 137 | * A message id of zero will be returned for tokens associated with 138 | * connect, disconnect and ping operations as there can only ever 139 | * be one of these outstanding at a time. For other operations 140 | * the MQTT message id flowed over the network. 141 | */ 142 | public long getMessageId(); 143 | 144 | public String getAlias(); 145 | 146 | public void setAlias(String alias); 147 | 148 | public JSONObject getResult(); 149 | 150 | public void setResult(JSONObject result); 151 | 152 | } 153 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/MqttCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3; 13 | 14 | 15 | /** 16 | * Enables an application to be notified when asynchronous 17 | * events related to the client occur. 18 | * Classes implementing this interface 19 | * can be registered on both types of client: {@link IMqttClient#setCallback(MqttCallback)} 20 | * and {@link IMqttAsyncClient#setCallback(MqttCallback)} 21 | */ 22 | public interface MqttCallback { 23 | /** 24 | * This method is called when the connection to the server is lost. 25 | * 26 | * @param cause the reason behind the loss of connection. 27 | */ 28 | public void connectionLost(Throwable cause); 29 | 30 | /** 31 | * This method is called when a message arrives from the server. 32 | * 33 | *34 | * This method is invoked synchronously by the MQTT client. An 35 | * acknowledgement is not sent back to the server until this 36 | * method returns cleanly.
37 | *
38 | * If an implementation of this method throws an Exception
, then the
39 | * client will be shut down. When the client is next re-connected, any QoS
40 | * 1 or 2 messages will be redelivered by the server.
42 | * Any additional messages which arrive while an 43 | * implementation of this method is running, will build up in memory, and 44 | * will then back up on the network.
45 | *46 | * If an application needs to persist data, then it 47 | * should ensure the data is persisted prior to returning from this method, as 48 | * after returning from this method, the message is considered to have been 49 | * delivered, and will not be reproducable.
50 | *51 | * It is possible to send a new message within an implementation of this callback 52 | * (for example, a response to this message), but the implementation must not 53 | * disconnect the client, as it will be impossible to send an acknowledgement for 54 | * the message being processed, and a deadlock will occur.
55 | * 56 | * @param topic name of the topic on the message was published to 57 | * @param message the actual message. 58 | * @throws Exception if a terminal error has occurred, and the client should be 59 | * shut down. 60 | */ 61 | public void messageArrived(String topic, MqttMessage message) throws Exception; 62 | 63 | 64 | 65 | public void presenceMessageArrived(String topic, MqttMessage message) throws Exception; 66 | 67 | /** 68 | * Called when delivery for a message has been completed, and all 69 | * acknowledgements have been received. For QOS 0 messages it is 70 | * called once the message has been handed to the network for 71 | * delivery. For QOS 1 it is called when PUBACK is received and 72 | * for QOS 2 when PUBCOMP is received. The token will be the same 73 | * token as that returned when the message was published. 74 | * 75 | * @param token the delivery token associated with the message. 76 | */ 77 | public void deliveryComplete(IMqttDeliveryToken token); 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/MqttClientPersistence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3; 13 | 14 | import java.util.Enumeration; 15 | 16 | /** 17 | * Represents a persistent data store, used to store outbound and inbound messages while they 18 | * are in flight, enabling delivery to the QOS specified. You can specify an implementation 19 | * of this interface using {@link MqttClient#MqttClient(String, String, MqttClientPersistence)}, 20 | * which the {@link MqttClient} will use to persist QoS 1 and 2 messages. 21 | *22 | * If the methods defined throw the MqttPersistenceException then the state of the data persisted 23 | * should remain as prior to the method being called. For example, if {@link #put(String, MqttPersistable)} 24 | * throws an exception at any point then the data will be assumed to not be in the persistent store. 25 | * Similarly if {@link #remove(String)} throws an exception then the data will be 26 | * assumed to still be held in the persistent store.
27 | *28 | * It is up to the persistence interface to log any exceptions or error information 29 | * which may be required when diagnosing a persistence failure.
30 | */ 31 | public interface MqttClientPersistence { 32 | /** 33 | * Initialise the persistent store. 34 | * If a persistent store exists for this client ID then open it, otherwise 35 | * create a new one. If the persistent store is already open then just return. 36 | * An application may use the same client ID to connect to many different 37 | * servers, so the client ID in conjunction with the 38 | * connection will uniquely identify the persistence store required. 39 | * 40 | * @param clientId The client for which the persistent store should be opened. 41 | * @param serverURI The connection string as specified when the MQTT client instance was created. 42 | * @throws MqttPersistenceException if there was a problem opening the persistent store. 43 | */ 44 | public void open(String clientId, String serverURI) throws MqttPersistenceException; 45 | 46 | /** 47 | * Close the persistent store that was previously opened. 48 | * This will be called when a client application disconnects from the broker. 49 | * @throws MqttPersistenceException 50 | */ 51 | public void close() throws MqttPersistenceException; 52 | 53 | /** 54 | * Puts the specified data into the persistent store. 55 | * @param key the key for the data, which will be used later to retrieve it. 56 | * @param persistable the data to persist 57 | * @throws MqttPersistenceException if there was a problem putting the data 58 | * into the persistent store. 59 | */ 60 | public void put(String key, MqttPersistable persistable) throws MqttPersistenceException; 61 | 62 | /** 63 | * Gets the specified data out of the persistent store. 64 | * @param key the key for the data, which was used when originally saving it. 65 | * @return the un-persisted data 66 | * @throws MqttPersistenceException if there was a problem getting the data 67 | * from the persistent store. 68 | */ 69 | public MqttPersistable get(String key) throws MqttPersistenceException; 70 | 71 | /** 72 | * Remove the data for the specified key. 73 | */ 74 | public void remove(String key) throws MqttPersistenceException; 75 | 76 | /** 77 | * Returns an Enumeration over the keys in this persistent data store. 78 | * @return an enumeration of {@link String} objects. 79 | */ 80 | public Enumeration keys() throws MqttPersistenceException; 81 | 82 | /** 83 | * Clears persistence, so that it no longer contains any persisted data. 84 | */ 85 | public void clear() throws MqttPersistenceException; 86 | 87 | /** 88 | * Returns whether or not data is persisted using the specified key. 89 | * @param key the key for data, which was used when originally saving it. 90 | */ 91 | public boolean containsKey(String key) throws MqttPersistenceException; 92 | } 93 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/MqttDeliveryToken.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3; 13 | 14 | /** 15 | * Provides a mechanism to track the delivery progress of a message. 16 | * 17 | *18 | * Used to track the the delivery progress of a message when a publish is 19 | * executed in a non-blocking manner (run in the background)
20 | * 21 | * @see MqttToken 22 | */ 23 | public class MqttDeliveryToken extends MqttToken implements IMqttDeliveryToken { 24 | 25 | 26 | public MqttDeliveryToken() { 27 | super(); 28 | } 29 | 30 | public MqttDeliveryToken(String logContext) { 31 | super(logContext); 32 | } 33 | 34 | /** 35 | * Returns the message associated with this token. 36 | *Until the message has been delivered, the message being delivered will
37 | * be returned. Once the message has been delivered null
will be
38 | * returned.
39 | * @return the message associated with this token or null if already delivered.
40 | * @throws MqttException if there was a problem completing retrieving the message
41 | */
42 | public MqttMessage getMessage() throws MqttException {
43 | return internalTok.getMessage();
44 | }
45 |
46 | protected void setMessage(MqttMessage msg) {
47 | internalTok.setMessage(msg);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/MqttPersistable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3;
13 |
14 | /**
15 | * Represents an object used to pass data to be persisted across the
16 | * {@link org.eclipse.paho.client.mqttv3.MqttClientPersistence MqttClientPersistence}
17 | * interface.
18 | *
19 | * When data is passed across the interface the header and payload are 20 | * separated, so that unnecessary message copies may be avoided. 21 | * For example, if a 10 MB payload was published it would be inefficient 22 | * to create a byte array a few bytes larger than 10 MB and copy the 23 | * MQTT message header and payload into a contiguous byte array.
24 | *25 | * When the request to persist data is made a separate byte array and offset 26 | * is passed for the header and payload. Only the data between 27 | * offset and length need be persisted. 28 | * So for example, a message to be persisted consists of a header byte 29 | * array starting at offset 1 and length 4, plus a payload byte array 30 | * starting at offset 30 and length 40000. There are three ways in which 31 | * the persistence implementation may return data to the client on 32 | * recovery: 33 | *
MqttPersistenceException
26 | */
27 | public MqttPersistenceException() {
28 | super(REASON_CODE_CLIENT_EXCEPTION);
29 | }
30 |
31 | /**
32 | * Constructs a new MqttPersistenceException
with the specified code
33 | * as the underlying reason.
34 | * @param reasonCode the reason code for the exception.
35 | */
36 | public MqttPersistenceException(int reasonCode) {
37 | super(reasonCode);
38 | }
39 | /**
40 | * Constructs a new MqttPersistenceException
with the specified
41 | * Throwable
as the underlying reason.
42 | * @param cause the underlying cause of the exception.
43 | */
44 | public MqttPersistenceException(Throwable cause) {
45 | super(cause);
46 | }
47 | /**
48 | * Constructs a new MqttPersistenceException
with the specified
49 | * Throwable
as the underlying reason.
50 | * @param reason the reason code for the exception.
51 | * @param cause the underlying cause of the exception.
52 | */
53 | public MqttPersistenceException(int reason, Throwable cause) {
54 | super(reason, cause);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/MqttSecurityException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3;
13 |
14 | /**
15 | * Thrown when a client is not authorized to perform an operation, or
16 | * if there is a problem with the security configuration.
17 | */
18 | public class MqttSecurityException extends MqttException {
19 | private static final long serialVersionUID = 300L;
20 |
21 | /**
22 | * Constructs a new MqttSecurityException
with the specified code
23 | * as the underlying reason.
24 | * @param reasonCode the reason code for the exception.
25 | */
26 | public MqttSecurityException(int reasonCode) {
27 | super(reasonCode);
28 | }
29 |
30 | /**
31 | * Constructs a new MqttSecurityException
with the specified
32 | * Throwable
as the underlying reason.
33 | * @param cause the underlying cause of the exception.
34 | */
35 | public MqttSecurityException(Throwable cause) {
36 | super(cause);
37 | }
38 | /**
39 | * Constructs a new MqttSecurityException
with the specified
40 | * code and Throwable
as the underlying reason.
41 | * @param reasonCode the reason code for the exception.
42 | * @param cause the underlying cause of the exception.
43 | */
44 | public MqttSecurityException(int reasonCode, Throwable cause) {
45 | super(reasonCode, cause);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/MqttToken.java:
--------------------------------------------------------------------------------
1 | package org.eclipse.paho.client.mqttv3;
2 |
3 | import org.codehaus.jettison.json.JSONObject;
4 | import org.eclipse.paho.client.mqttv3.internal.Token;
5 |
6 | /**
7 | * Provides a mechanism for tracking the completion of an asynchronous action.
8 | * 9 | * A token that implements the ImqttToken interface is returned from all non-blocking 10 | * method with the exception of publish. 11 | *
12 | * 13 | * @see IMqttToken 14 | */ 15 | 16 | public class MqttToken implements IMqttToken { 17 | /** 18 | * A reference to the the class that provides most of the implementation of the 19 | * MqttToken. MQTT application programs must not use the internal class. 20 | */ 21 | public Token internalTok = null; 22 | 23 | public MqttToken() { 24 | } 25 | 26 | public MqttToken(String logContext) { 27 | internalTok = new Token(logContext); 28 | } 29 | 30 | public MqttException getException() { 31 | return internalTok.getException(); 32 | } 33 | 34 | public boolean isComplete() { 35 | return internalTok.isComplete(); 36 | } 37 | 38 | public void setActionCallback(IMqttActionListener listener) { 39 | internalTok.setActionCallback(listener); 40 | 41 | } 42 | public IMqttActionListener getActionCallback() { 43 | return internalTok.getActionCallback(); 44 | } 45 | 46 | public void waitForCompletion() throws MqttException { 47 | internalTok.waitForCompletion(-1); 48 | } 49 | 50 | public void waitForCompletion(long timeout) throws MqttException { 51 | internalTok.waitForCompletion(timeout); 52 | } 53 | 54 | public IMqttAsyncClient getClient() { 55 | return internalTok.getClient(); 56 | } 57 | 58 | public String[] getTopics() { 59 | return internalTok.getTopics(); 60 | } 61 | 62 | public Object getUserContext() { 63 | return internalTok.getUserContext(); 64 | } 65 | 66 | public void setUserContext(Object userContext) { 67 | internalTok.setUserContext(userContext); } 68 | 69 | public long getMessageId() { 70 | return internalTok.getMessageID(); 71 | } 72 | 73 | private String alias; 74 | 75 | @Override 76 | public String getAlias() { 77 | return alias; 78 | } 79 | 80 | @Override 81 | public void setAlias(String alias) { 82 | this.alias = alias; 83 | } 84 | 85 | private JSONObject result; 86 | 87 | @Override 88 | public JSONObject getResult() { 89 | return result; 90 | } 91 | 92 | @Override 93 | public void setResult(JSONObject result) { 94 | this.result = result; 95 | } 96 | 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/MqttTopic.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3; 13 | 14 | import org.eclipse.paho.client.mqttv3.internal.ClientComms; 15 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttPublish; 16 | 17 | /** 18 | * Represents a topic destination, used for publish/subscribe messaging. 19 | */ 20 | public class MqttTopic { 21 | 22 | private ClientComms comms; 23 | private String name; 24 | 25 | public MqttTopic(String name, ClientComms comms) { 26 | this.comms = comms; 27 | this.name = name; 28 | } 29 | 30 | /** 31 | * Publishes a message on the topic. This is a convenience method, which will 32 | * create a new {@link MqttMessage} object with a byte array payload and the 33 | * specified QoS, and then publish it. All other values in the 34 | * message will be set to the defaults. 35 | 36 | * @param payload the byte array to use as the payload 37 | * @param qos the Quality of Service. Valid values are 0, 1 or 2. 38 | * @param retained whether or not this message should be retained by the server. 39 | * @throws IllegalArgumentException if value of QoS is not 0, 1 or 2. 40 | * @see #publish(MqttMessage) 41 | * @see MqttMessage#setQos(int) 42 | * @see MqttMessage#setRetained(boolean) 43 | */ 44 | public MqttDeliveryToken publish(byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException { 45 | MqttMessage message = new MqttMessage(payload); 46 | message.setQos(qos); 47 | message.setRetained(retained); 48 | return this.publish(message); 49 | } 50 | 51 | /** 52 | * Publishes the specified message to this topic, but does not wait for delivery 53 | * of the message to complete. The returned {@link MqttDeliveryToken token} can be used 54 | * to track the delivery status of the message. Once this method has 55 | * returned cleanly, the message has been accepted for publication by the 56 | * client. Message delivery will be completed in the background when a connection 57 | * is available. 58 | * 59 | * @param message the message to publish 60 | * @return an MqttDeliveryToken for tracking the delivery of the message 61 | */ 62 | public MqttDeliveryToken publish(MqttMessage message) throws MqttException, MqttPersistenceException { 63 | MqttDeliveryToken token = new MqttDeliveryToken(comms.getClient().getClientId()); 64 | token.setMessage(message); 65 | comms.sendNoWait(createPublish(message), token); 66 | token.internalTok.waitUntilSent(); 67 | return token; 68 | } 69 | 70 | /** 71 | * Returns the name of the queue or topic. 72 | * 73 | * @return the name of this destination. 74 | */ 75 | public String getName() { 76 | return name; 77 | } 78 | 79 | /** 80 | * Create a PUBLISH packet from the specified message. 81 | */ 82 | private MqttPublish createPublish(MqttMessage message) { 83 | return new MqttPublish(this.getName(), message); 84 | } 85 | 86 | /** 87 | * Returns a string representation of this topic. 88 | * @return a string representation of this topic. 89 | */ 90 | public String toString() { 91 | return getName(); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/ClientDefaults.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | public class ClientDefaults { 15 | public static final int MAX_MSG_SIZE = 1024 * 1024 * 256; // 256 MB 16 | } 17 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/CommsSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | 15 | import java.io.OutputStream; 16 | import java.text.SimpleDateFormat; 17 | 18 | import org.eclipse.paho.client.mqttv3.MqttException; 19 | import org.eclipse.paho.client.mqttv3.MqttToken; 20 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttAck; 21 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttOutputStream; 22 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage; 23 | import org.eclipse.paho.client.mqttv3.logging.Logger; 24 | import org.eclipse.paho.client.mqttv3.logging.LoggerFactory; 25 | 26 | 27 | public class CommsSender implements Runnable { 28 | /** 29 | * Sends MQTT packets to the server on its own thread 30 | */ 31 | private boolean running = false; 32 | private Object lifecycle = new Object(); 33 | private ClientState clientState = null; 34 | private MqttOutputStream out; 35 | private ClientComms clientComms = null; 36 | private CommsTokenStore tokenStore = null; 37 | private Thread sendThread = null; 38 | 39 | private final static String className = "CommsSender"; 40 | private Logger log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT, className); 41 | 42 | public CommsSender(ClientComms clientComms, ClientState clientState, CommsTokenStore tokenStore, OutputStream out) { 43 | this.out = new MqttOutputStream(out); 44 | this.clientComms = clientComms; 45 | this.clientState = clientState; 46 | this.tokenStore = tokenStore; 47 | log.setResourceName(clientComms.getClient().getClientId()); 48 | } 49 | 50 | /** 51 | * Starts up the Sender thread. 52 | */ 53 | public void start(String threadName) { 54 | synchronized (lifecycle) { 55 | if (running == false) { 56 | running = true; 57 | sendThread = new Thread(this, threadName); 58 | sendThread.start(); 59 | } 60 | } 61 | } 62 | 63 | /** 64 | * Stops the Sender's thread. This call will block. 65 | */ 66 | public void stop() { 67 | final String methodName = "stop"; 68 | 69 | synchronized (lifecycle) { 70 | //@TRACE 800=stopping sender 71 | log.fine(className,methodName,"800"); 72 | if (running) { 73 | running = false; 74 | if (!Thread.currentThread().equals(sendThread)) { 75 | try { 76 | // first notify get routine to finish 77 | clientState.notifyQueueLock(); 78 | // Wait for the thread to finish. 79 | sendThread.join(); 80 | } 81 | catch (InterruptedException ex) { 82 | } 83 | } 84 | } 85 | sendThread=null; 86 | //@TRACE 801=stopped 87 | log.fine(className,methodName,"801"); 88 | } 89 | } 90 | 91 | 92 | //force destory the thread 93 | public void destory() { 94 | running = false; 95 | try { 96 | sendThread.interrupt(); 97 | } catch (Exception e) { 98 | //YLogger.e("CommsSender", "destory", e); 99 | } 100 | sendThread = null; 101 | } 102 | 103 | public void run() { 104 | final String methodName = "run"; 105 | MqttWireMessage message = null; 106 | while (running && (out != null)) { 107 | try { 108 | message = clientState.get(); 109 | if (message != null) { 110 | SimpleDateFormat time_formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS"); 111 | String current_time_str = time_formatter.format(System.currentTimeMillis()); 112 | System.out.println(current_time_str + " : Send msg to server: msgId = " + message.getKey() + " key = " + message.getTypeStr()); 113 | //@TRACE 802=network send key={0} msg={1} 114 | log.fine(className,methodName,"802", new Object[] {message.getKey(),message}); 115 | 116 | if (message instanceof MqttAck) { 117 | out.write(message); 118 | out.flush(); 119 | } else { 120 | MqttToken token = tokenStore.getToken(message); 121 | // While quiescing the tokenstore can be cleared so need 122 | // to check for null for the case where clear occurs 123 | // while trying to send a message. 124 | if (token != null) { 125 | synchronized (token) { 126 | out.write(message); 127 | out.flush(); 128 | clientState.notifySent(message); 129 | } 130 | } else { 131 | //YLogger.e(className, "null token :" + "msgId = " + message.getKey() + " key = " + message.getType()); 132 | } 133 | } 134 | } else { // null message 135 | //@TRACE 803=get message returned null, stopping} 136 | log.fine(className,methodName,"803"); 137 | 138 | running = false; 139 | clientComms.shutdownConnection(null, new MqttException(MqttException.REASON_CODE_CLIENT_TIMEOUT)); 140 | } 141 | } catch (MqttException me) { 142 | // YLogger.e(className, methodName, me); 143 | handleRunException(message, me); 144 | } catch (Throwable ex) { 145 | // YLogger.e(className, methodName, ex); 146 | handleRunException(message, ex); 147 | } 148 | } // end while 149 | 150 | //@TRACE 805=< 151 | log.fine(className, methodName,"805"); 152 | 153 | } 154 | 155 | private void handleRunException(MqttWireMessage message, Throwable ex) { 156 | final String methodName = "handleRunException"; 157 | //@TRACE 804=exception 158 | //log.fine(className,methodName,"804",null, ex); 159 | //YLogger.d(className, methodName); 160 | MqttException mex; 161 | if ( !(ex instanceof MqttException)) { 162 | mex = new MqttException(MqttException.REASON_CODE_CONNECTION_LOST, ex); 163 | } else { 164 | mex = (MqttException)ex; 165 | } 166 | 167 | running = false; 168 | clientComms.shutdownConnection(null, mex); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/DestinationProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | import org.eclipse.paho.client.mqttv3.MqttTopic; 15 | 16 | /** 17 | * This interface exists to act as a common type for 18 | * MqttClient and MqttMIDPClient so they can be passed to 19 | * ClientComms without either client class need to know 20 | * about the other. 21 | * Specifically, this allows the MIDP client to work 22 | * without the non-MIDP MqttClient/MqttConnectOptions 23 | * classes being present. 24 | */ 25 | public interface DestinationProvider { 26 | public MqttTopic getTopic(String topic); 27 | } 28 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/ExceptionHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | import org.eclipse.paho.client.mqttv3.MqttException; 15 | import org.eclipse.paho.client.mqttv3.MqttSecurityException; 16 | 17 | /** 18 | * Utility class to help create exceptions of the correct type. 19 | */ 20 | public class ExceptionHelper { 21 | public static MqttException createMqttException(int reasonCode) { 22 | if ((reasonCode == MqttException.REASON_CODE_FAILED_AUTHENTICATION) || 23 | (reasonCode == MqttException.REASON_CODE_NOT_AUTHORIZED)) { 24 | return new MqttSecurityException(reasonCode); 25 | } 26 | 27 | return new MqttException(reasonCode); 28 | } 29 | 30 | public static MqttException createMqttException(Throwable cause) { 31 | if (cause.getClass().getName().equals("java.security.GeneralSecurityException")) { 32 | return new MqttSecurityException(cause); 33 | } 34 | return new MqttException(cause); 35 | } 36 | 37 | /** 38 | * Returns whether or not the specified class is available to the current 39 | * class loader. This is used to protect the code against using Java SE 40 | * APIs on Java ME. 41 | */ 42 | public static boolean isClassAvailable(String className) { 43 | boolean result = false; 44 | try { 45 | Class.forName(className); 46 | result = true; 47 | } 48 | catch (ClassNotFoundException ex) { 49 | } 50 | return result; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/FileLock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | /** 14 | * FileLock - used to obtain a lock that can be used to prevent other MQTT clients 15 | * using the same persistent store. If the lock is already held then an exception 16 | * is thrown. 17 | * 18 | * Some Java runtimes such as JME MIDP do not support file locking or even 19 | * the Java classes that support locking. The class is coded to both compile 20 | * and work on all Java runtimes. In Java runtimes that do not support 21 | * locking it will look as though a lock has been obtained but in reality 22 | * no lock has been obtained. 23 | */ 24 | import java.io.File; 25 | import java.io.IOException; 26 | import java.io.RandomAccessFile; 27 | import java.lang.reflect.Method; 28 | 29 | public class FileLock { 30 | private File lockFile; 31 | private RandomAccessFile file; 32 | private Object fileLock; 33 | 34 | /** 35 | * Creates an NIO FileLock on the specified file if on a suitable Java runtime. 36 | * @param clientDir the a File of the directory to contain the lock file. 37 | * @param lockFilename name of the the file to lock 38 | * @throws Exception if the lock could not be obtained for any reason 39 | */ 40 | public FileLock(File clientDir, String lockFilename) throws Exception { 41 | // Create a file to obtain a lock on. 42 | lockFile = new File(clientDir,lockFilename); 43 | if (ExceptionHelper.isClassAvailable("java.nio.channels.FileLock")) { 44 | try { 45 | this.file = new RandomAccessFile(lockFile,"rw"); 46 | Method m = file.getClass().getMethod("getChannel",new Class[]{}); 47 | Object channel = m.invoke(file,new Object[]{}); 48 | m = channel.getClass().getMethod("tryLock",new Class[]{}); 49 | this.fileLock = m.invoke(channel, new Object[]{}); 50 | } catch(NoSuchMethodException nsme) { 51 | this.fileLock = null; 52 | } catch(IllegalArgumentException iae) { 53 | this.fileLock = null; 54 | } catch(IllegalAccessException iae) { 55 | this.fileLock = null; 56 | } 57 | if (fileLock == null) { 58 | // Lock not obtained 59 | release(); 60 | throw new Exception("Problem obtaining file lock"); 61 | } 62 | } 63 | } 64 | 65 | /** 66 | * Releases the lock. 67 | */ 68 | public void release() { 69 | try { 70 | if (fileLock != null) { 71 | Method m = fileLock.getClass().getMethod("release",new Class[]{}); 72 | m.invoke(fileLock, new Object[]{}); 73 | fileLock = null; 74 | } 75 | } catch (Exception e) { 76 | // Ignore exceptions 77 | } 78 | if (file != null) { 79 | try { 80 | file.close(); 81 | } catch (IOException e) { 82 | } 83 | file = null; 84 | } 85 | 86 | if (lockFile != null && lockFile.exists()) { 87 | lockFile.delete(); 88 | } 89 | lockFile = null; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/LocalNetworkModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | import java.io.IOException; 15 | import java.io.InputStream; 16 | import java.io.OutputStream; 17 | import java.lang.reflect.Method; 18 | 19 | import org.eclipse.paho.client.mqttv3.MqttException; 20 | 21 | 22 | /** 23 | * Special comms class that allows an MQTT client to use a non TCP / optimised 24 | * mechanism to talk to an MQTT server when running in the same JRE instance as the 25 | * MQTT server. 26 | * 27 | * This class checks for the existence of the optimised comms adatper class i.e. the one 28 | * that provides the optimised communication mechanism. If not available the request 29 | * to connect using the optimised mechanism is rejected. 30 | * 31 | * The only known server that implements this is the microbroker:- an MQTT server that 32 | * ships with a number of IBM products. 33 | */ 34 | public class LocalNetworkModule implements NetworkModule { 35 | private Class LocalListener; 36 | private String brokerName; 37 | private Object localAdapter; 38 | 39 | public LocalNetworkModule(String brokerName) { 40 | this.brokerName = brokerName; 41 | } 42 | 43 | public void start() throws IOException, MqttException{ 44 | if (!ExceptionHelper.isClassAvailable("com.ibm.mqttdirect.modules.local.bindings.LocalListener")) { 45 | throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SERVER_CONNECT_ERROR); 46 | } 47 | try { 48 | LocalListener = Class.forName("com.ibm.mqttdirect.modules.local.bindings.LocalListener"); 49 | Method connect_m = LocalListener.getMethod("connect", new Class[]{ java.lang.String.class }); 50 | localAdapter = connect_m.invoke(null,new Object[]{ brokerName }); 51 | } catch(Exception e) { 52 | } 53 | if(localAdapter == null) { 54 | throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SERVER_CONNECT_ERROR); 55 | } 56 | } 57 | 58 | public InputStream getInputStream() throws IOException { 59 | InputStream stream = null; 60 | try { 61 | Method m = LocalListener.getMethod("getClientInputStream",new Class[]{}); 62 | stream = (InputStream)m.invoke(this.localAdapter,new Object[]{}); 63 | } catch(Exception e) { 64 | } 65 | return stream; 66 | } 67 | 68 | public OutputStream getOutputStream() throws IOException { 69 | OutputStream stream = null; 70 | try { 71 | Method m = LocalListener.getMethod("getClientOutputStream",new Class[]{}); 72 | stream = (OutputStream)m.invoke(this.localAdapter,new Object[]{}); 73 | } catch(Exception e) { 74 | } 75 | return stream; 76 | } 77 | 78 | public void stop() throws IOException { 79 | if (localAdapter != null) { 80 | try { 81 | Method m = LocalListener.getMethod("close",new Class[]{}); 82 | m.invoke(this.localAdapter,new Object[]{}); 83 | } catch(Exception e) { 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/MessageCatalog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | /** 15 | * Catalog of human readable error messages. 16 | */ 17 | public abstract class MessageCatalog { 18 | private static MessageCatalog INSTANCE = null; 19 | 20 | public static final String getMessage(int id) { 21 | try { 22 | if (INSTANCE == null) { 23 | if (ExceptionHelper.isClassAvailable("java.util.ResourceBundle")) { 24 | try { 25 | // Hide this class reference behind reflection so that the class does not need to 26 | // be present when compiled on midp 27 | INSTANCE = (MessageCatalog)Class.forName("org.eclipse.paho.client.mqttv3.internal.ResourceBundleCatalog").newInstance(); 28 | } catch (Exception e) { 29 | return ""; 30 | } 31 | } else if (ExceptionHelper.isClassAvailable("org.eclipse.paho.client.mqttv3.internal.MIDPCatalog")){ 32 | try { 33 | INSTANCE = (MessageCatalog)Class.forName("org.eclipse.paho.client.mqttv3.internal.MIDPCatalog").newInstance(); 34 | } catch (Exception e) { 35 | return ""; 36 | } 37 | } 38 | } 39 | return INSTANCE.getLocalizedMessage(id); 40 | } catch(Exception e) { 41 | return ""; 42 | } 43 | } 44 | 45 | protected abstract String getLocalizedMessage(int id); 46 | } 47 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/internal/MqttPersistentData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.internal; 13 | 14 | import org.eclipse.paho.client.mqttv3.MqttPersistable; 15 | 16 | public class MqttPersistentData implements MqttPersistable { 17 | // Message key 18 | private String key = null; 19 | 20 | // Message header 21 | private byte[] header = null; 22 | private int hOffset = 0; 23 | private int hLength = 0; 24 | 25 | // Message payload 26 | private byte[] payload = null; 27 | private int pOffset = 0; 28 | private int pLength = 0; 29 | 30 | /** 31 | * Construct a data object to pass across the MQTT client persistence 32 | * interface.CountingInputStream
wrapping the supplied
26 | * input stream.
27 | */
28 | public CountingInputStream(InputStream in) {
29 | this.in = in;
30 | this.counter = 0;
31 | }
32 |
33 | public int read() throws IOException {
34 | try {
35 | int i = in.read();
36 | if (i != -1) {
37 | counter++;
38 | }
39 | return i;
40 | } catch (Exception e) {
41 | throw new IOException();
42 | }
43 |
44 | }
45 |
46 | /**
47 | * Returns the number of bytes read since the last reset.
48 | */
49 | public int getCounter() {
50 | return counter;
51 | }
52 |
53 | /**
54 | * Resets the counter to zero.
55 | */
56 | public void resetCounter() {
57 | counter = 0;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttAck.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 |
15 | /**
16 | * Abstract super-class of all acknowledgement messages.
17 | */
18 | public abstract class MqttAck extends MqttWireMessage {
19 | public MqttAck(byte type) {
20 | super(type);
21 | }
22 |
23 | protected byte getMessageInfo() {
24 | return 0;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttConnack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.DataInputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 |
21 | /**
22 | * An on-the-wire representation of an MQTT CONNACK.
23 | */
24 | public class MqttConnack extends MqttAck {
25 | private int returnCode;
26 |
27 | public MqttConnack(byte info, byte[] variableHeader) throws Exception {
28 | super(MqttWireMessage.MESSAGE_TYPE_CONNACK);
29 | ByteArrayInputStream bais = new ByteArrayInputStream(variableHeader);
30 | DataInputStream dis = new DataInputStream(bais);
31 | dis.readByte();
32 | returnCode = dis.readUnsignedByte();
33 | dis.close();
34 | }
35 |
36 | public int getReturnCode() {
37 | return returnCode;
38 | }
39 |
40 | protected byte[] getVariableHeader() throws MqttException {
41 | // Not needed, as the client never encodes a CONNACK
42 | return new byte[0];
43 | }
44 |
45 | /**
46 | * Returns whether or not this message needs to include a message ID.
47 | */
48 | public boolean isMessageIdRequired() {
49 | return false;
50 | }
51 |
52 | public String getKey() {
53 | return new String("Con");
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttConnect.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayOutputStream;
15 | import java.io.DataOutputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 | import org.eclipse.paho.client.mqttv3.MqttMessage;
20 |
21 | /**
22 | * An on-the-wire representation of an MQTT CONNECT message.
23 | */
24 | public class MqttConnect extends MqttWireMessage {
25 | private String clientId;
26 | private boolean cleanSession;
27 | private MqttMessage willMessage;
28 | private String userName;
29 | private char[] password;
30 | private int keepAliveInterval;
31 | private String willDestination;
32 | public static String KEY="Con";
33 |
34 |
35 | public MqttConnect(String clientId,
36 | boolean cleanSession,
37 | int keepAliveInterval,
38 | String userName,
39 | char[] password,
40 | MqttMessage willMessage,
41 | String willDestination) {
42 | super(MqttWireMessage.MESSAGE_TYPE_CONNECT);
43 | this.clientId = clientId;
44 | this.cleanSession = cleanSession;
45 | this.keepAliveInterval = keepAliveInterval;
46 | this.userName = userName;
47 | this.password = password;
48 | this.willMessage = willMessage;
49 | this.willDestination = willDestination;
50 | }
51 |
52 | protected byte getMessageInfo() {
53 | return (byte) 0;
54 | }
55 |
56 | public boolean isCleanSession() {
57 | return cleanSession;
58 | }
59 |
60 | protected byte[] getVariableHeader() throws MqttException {
61 | try {
62 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
63 | DataOutputStream dos = new DataOutputStream(baos);
64 | this.encodeUTF8(dos,"MQIsdp");
65 |
66 | //****change the version code . write by Quentin***//
67 | //********************for massage id refactor******//
68 | //dos.write(3);
69 | dos.write(19);
70 | //*************************************************//
71 | byte connectFlags = 0;
72 |
73 | if (cleanSession) {
74 | connectFlags |= 0x02;
75 | }
76 |
77 | if (willMessage != null ) {
78 | connectFlags |= 0x04;
79 | connectFlags |= (willMessage.getQos()<<3);
80 | if (willMessage.isRetained()) {
81 | connectFlags |= 0x20;
82 | }
83 | }
84 |
85 | if (userName != null) {
86 | connectFlags |= 0x80;
87 | if (password != null) {
88 | connectFlags |= 0x40;
89 | }
90 | }
91 | dos.write(connectFlags);
92 | dos.writeShort(keepAliveInterval);
93 | dos.flush();
94 | return baos.toByteArray();
95 | } catch(IOException ioe) {
96 | throw new MqttException(ioe);
97 | }
98 | }
99 |
100 | public byte[] getPayload() throws MqttException {
101 | try {
102 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
103 | DataOutputStream dos = new DataOutputStream(baos);
104 | this.encodeUTF8(dos,clientId);
105 |
106 | if (willMessage != null) {
107 | this.encodeUTF8(dos,willDestination);
108 | dos.writeShort(willMessage.getPayload().length);
109 | dos.write(willMessage.getPayload());
110 | }
111 |
112 | if (userName != null) {
113 | this.encodeUTF8(dos,userName);
114 | if (password != null) {
115 | this.encodeUTF8(dos,new String(password));
116 | }
117 | }
118 | dos.flush();
119 | return baos.toByteArray();
120 | }
121 | catch (IOException ex) {
122 | throw new MqttException(ex);
123 | }
124 | }
125 |
126 | /**
127 | * Returns whether or not this message needs to include a message ID.
128 | */
129 | public boolean isMessageIdRequired() {
130 | return false;
131 | }
132 |
133 | public String getKey() {
134 | return new String(KEY);
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttDisconnect.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import org.eclipse.paho.client.mqttv3.MqttException;
15 |
16 | /**
17 | * An on-the-wire representation of an MQTT DISCONNECT message.
18 | */
19 | public class MqttDisconnect extends MqttWireMessage {
20 | public static String KEY="Disc";
21 |
22 | public MqttDisconnect() {
23 | super(MqttWireMessage.MESSAGE_TYPE_DISCONNECT);
24 | }
25 |
26 | protected byte getMessageInfo() {
27 | return (byte) 0;
28 | }
29 |
30 | protected byte[] getVariableHeader() throws MqttException {
31 | return new byte[0];
32 | }
33 |
34 | /**
35 | * Returns whether or not this message needs to include a message ID.
36 | */
37 | public boolean isMessageIdRequired() {
38 | return false;
39 | }
40 |
41 | public String getKey() {
42 | try {
43 | return new String(KEY);
44 | } catch (Exception e) {
45 | return "";
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttExpand.java:
--------------------------------------------------------------------------------
1 | package org.eclipse.paho.client.mqttv3.internal.wire;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.DataOutputStream;
5 | import java.io.IOException;
6 |
7 | import org.eclipse.paho.client.mqttv3.MqttException;
8 |
9 | public class MqttExpand extends MqttWireMessage{
10 | public static final byte CMD_GET_ALIAS = 1;
11 | public static final byte CMD_GET_ALIAS_ACK = 2;
12 | public static final byte CMD_GET_TOPIC = 3;
13 | public static final byte CMD_GET_TOPIC_ACK = 4;
14 | public static final byte CMD_GET_ALIASLIST = 5;
15 | public static final byte CMD_GET_ALIASLIST_ACK = 6;
16 | public static final byte CMD_PUBLISH = 7;
17 | public static final byte CMD_PUBLISH_ACK = 8;
18 | public static final byte CMD_GET_STATUS = 9;
19 | public static final byte CMD_GET_STATUS_ACK = 10;
20 | public static final byte CMD_GET_STATUS_V2 = 19;
21 | public static final byte CMD_GET_STATUS_ACK_V2 = 20;
22 | public static final byte CMD_GET_TOPIC_V2 = 13;
23 | public static final byte CMD_GET_TOPIC_ACK_V2 = 14;
24 | public static final byte CMD_GET_ALIASLIST_V2 = 15;
25 | public static final byte CMD_GET_ALIASLIST_ACK_V2 = 16;
26 |
27 | public MqttExpand () {
28 | super(MqttWireMessage.MESSAGE_TYPE_EXPAND);
29 | }
30 |
31 | public MqttExpand (String topics, byte command) throws MqttException {
32 | super(MqttWireMessage.MESSAGE_TYPE_EXPAND);
33 | this.command = command;
34 | topic = topics;
35 | setPayload();
36 | }
37 | protected byte[] encodedPayload = null;
38 | protected String topic = null;
39 | protected byte command ;
40 |
41 | @Override
42 | protected byte[] getVariableHeader() throws MqttException {
43 | try {
44 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
45 | DataOutputStream dos = new DataOutputStream(baos);
46 |
47 | //****change the version code . write by Quentin***//
48 | //********************for massage id refactor******//
49 | //dos.writeShort(msgId);
50 | dos.writeLong(msgId);
51 | //*************************************************//
52 |
53 | dos.flush();
54 | return baos.toByteArray();
55 | }
56 | catch (IOException ex) {
57 | throw new MqttException(ex);
58 | }
59 | }
60 |
61 | public boolean isMessageIdRequired() {
62 | // all publishes require a message ID as it's used as the key to the token store
63 | return true;
64 | }
65 |
66 |
67 |
68 | @Override
69 | public byte[] getPayload() throws MqttException {
70 | return encodedPayload;
71 | }
72 |
73 |
74 |
75 | public void setPayload() throws MqttException {
76 | try {
77 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
78 | DataOutputStream dos = new DataOutputStream(baos);
79 | dos.writeByte(command);
80 | if(null != topic) {
81 | byte[] encodedString = topic.getBytes("UTF-8");
82 | dos.writeChar(topic.length());
83 | dos.write(encodedString);
84 | } else {
85 | dos.writeChar(0);
86 | }
87 |
88 | //*************************************************//
89 |
90 | dos.flush();
91 | encodedPayload = baos.toByteArray();
92 | }
93 | catch (IOException ex) {
94 | throw new MqttException(ex);
95 | }
96 | }
97 |
98 | @Override
99 | protected byte getMessageInfo() {
100 | return (byte) 0;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttExpandAck.java:
--------------------------------------------------------------------------------
1 | package org.eclipse.paho.client.mqttv3.internal.wire;
2 |
3 | import java.io.ByteArrayInputStream;
4 | import java.io.DataInputStream;
5 | import java.io.IOException;
6 |
7 | import org.codehaus.jettison.json.JSONObject;
8 | import org.eclipse.paho.client.mqttv3.MqttException;
9 |
10 | public class MqttExpandAck extends MqttAck {
11 | public byte command;
12 | public byte status;
13 | public String result;
14 |
15 | public MqttExpandAck(byte info, byte[] data) {
16 | super(MESSAGE_TYPE_EXPAND);
17 | DataInputStream dis = null;
18 | try {
19 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
20 | CountingInputStream counter = new CountingInputStream(bais);
21 | dis = new DataInputStream(counter);
22 | msgId = dis.readLong();
23 | byte[] payload = new byte[data.length - counter.getCounter()];
24 | dis.readFully(payload);
25 | command = payload[0];
26 | status = payload[1];
27 | if (payload.length > 2) {
28 | int lenth = getLenth(payload[2], payload[3]);
29 | byte[] msg = new byte[lenth];
30 |
31 | System.arraycopy(payload, 4, msg, 0, msg.length);
32 | result = new String(msg, "UTF-8");
33 | if (command == MqttExpand.CMD_GET_STATUS_ACK) {
34 | try {
35 | JSONObject js = new JSONObject();
36 | js.put("status", result);
37 | result = js.toString();
38 | } catch (Exception e) {
39 | // YLogger.e("JSONObject", "", e);
40 | }
41 | } else if(command == MqttExpand.CMD_GET_ALIAS_ACK) {
42 | // System.err.println("MqttExpand.CMD_GET_ALIAS_ACK = " + result);
43 | JSONObject js = new JSONObject();
44 | js.put("alias", result);
45 | result = js.toString();
46 |
47 | }
48 | // YLogger.d("MqttExpandAck", "result = " + result);
49 | } else {
50 | // YLogger.d("MqttExpandAck", "no data in payload");
51 | }
52 |
53 | } catch (Exception e) {
54 | // YLogger.e("MqttExpandAck", "", e);
55 | } finally {
56 | if (dis != null) {
57 | try {
58 | dis.close();
59 | } catch (Exception e) {
60 |
61 | }
62 | }
63 | }
64 |
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return "command " + command + " status " + status + " msg = " + result;
70 | }
71 |
72 | @Override
73 | protected byte[] getVariableHeader() throws MqttException {
74 | // TODO Auto-generated method stub
75 | return null;
76 | }
77 |
78 | public static int getLenth(byte b1, byte b2) throws IOException {
79 |
80 | return ((b1 << 8) & 0xFF00) | b2 & 0xFF;
81 |
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttExpandPublish.java:
--------------------------------------------------------------------------------
1 | package org.eclipse.paho.client.mqttv3.internal.wire;
2 |
3 |
4 |
5 | import io.yunba.java.util.MqttUtil;
6 |
7 | import java.io.ByteArrayOutputStream;
8 | import java.io.DataOutputStream;
9 | import java.io.IOException;
10 | import java.util.ArrayList;
11 | import java.util.HashMap;
12 | import java.util.Iterator;
13 | import java.util.List;
14 | import java.util.Map;
15 |
16 | import org.codehaus.jettison.json.JSONObject;
17 | import org.eclipse.paho.client.mqttv3.MqttException;
18 | ;
19 |
20 | public class MqttExpandPublish extends MqttExpand {
21 | public MqttExpandPublish () {
22 | super();
23 | }
24 |
25 | public String msg ;
26 | public JSONObject opts;
27 | public MqttExpandPublish (String topics, String msg, byte command, JSONObject opts) throws MqttException {
28 | super();
29 | this.command = command;
30 | this.opts = opts;
31 | this.msg = msg;
32 | topic = topics;
33 | setPayload();
34 | }
35 |
36 |
37 | public void setPayload() throws MqttException {
38 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
39 | DataOutputStream dos = new DataOutputStream(baos);
40 | try {
41 | dos.writeByte(command);
42 | byte[] tlv = getTLVBytes(opts, topic, msg);
43 | if (null != tlv) {
44 | dos.writeChar(tlv.length);
45 | dos.write(tlv);
46 | } else {
47 | dos.writeChar(0);
48 | }
49 |
50 | // *************************************************//
51 |
52 | dos.flush();
53 | encodedPayload = baos.toByteArray();
54 | } catch (Exception ex) {
55 | throw new MqttException(-105,ex);
56 | } finally {
57 | try {
58 | dos.close();
59 | baos.close();
60 | } catch (Exception e) {
61 |
62 | }
63 | }
64 | }
65 |
66 |
67 | public byte[] getTLVBytes(JSONObject opts, String topic, String msg) {
68 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
69 | DataOutputStream dos = new DataOutputStream(baos);
70 | try {
71 | if (!MqttUtil.isEmpty(topic)) {
72 | dos.writeByte(0);
73 | dos.writeChar(topic.getBytes().length);
74 | dos.write(topic.getBytes("UTF-8"));
75 | }
76 |
77 | if (!MqttUtil.isEmpty(msg)) {
78 | dos.writeByte(1);
79 | dos.writeChar(msg.getBytes().length);
80 | dos.write(msg.getBytes("UTF-8"));
81 | }
82 | if (null != opts && opts.length() > 0) {
83 | Iterator> keys = opts.keys();
84 |
85 | while (keys.hasNext()) {
86 | String key = (String) keys.next();
87 | String value = opts.getString(key);
88 | int keyToInt = getKey(key);
89 | if (keyToInt != -1) {
90 | dos.writeByte(keyToInt);
91 | dos.writeChar(value.getBytes().length);
92 | dos.write(value.getBytes("UTF-8"));
93 | }
94 | }
95 | }
96 | dos.flush();
97 |
98 | return baos.toByteArray();
99 | } catch (Exception ex) {
100 | //YLogger.e("MqttExpandPublish", "", ex);
101 | System.err.println(ex.getMessage());
102 | return null;
103 | } finally {
104 | try {
105 | dos.close();
106 | baos.close();
107 | } catch (Exception e) {
108 |
109 | }
110 | }
111 | }
112 |
113 |
114 | // 0: "topics",
115 | // 1: "payload",
116 | // 2: "platform",
117 | // 3: "time_to_live",
118 | // 4: "time_delay",
119 | // 5: "location",
120 | // 6: "qos",
121 | // 7: "apn_json",
122 | private static MapMqttInputStream
lets applications read instances of
25 | * MqttWireMessage
.
26 | */
27 | public class MqttInputStream extends InputStream {
28 | private DataInputStream in;
29 |
30 | public MqttInputStream(InputStream in) {
31 | this.in = new DataInputStream(in);
32 | }
33 |
34 | public int read() throws IOException {
35 | return in.read();
36 | }
37 |
38 | public int available() throws IOException {
39 | return in.available();
40 | }
41 |
42 | public void close() throws IOException {
43 | in.close();
44 | }
45 |
46 | /**
47 | * Reads an MqttWireMessage
from the stream.
48 | */
49 | public MqttWireMessage readMqttWireMessage() throws Exception, MqttException {
50 | ByteArrayOutputStream bais = new ByteArrayOutputStream();
51 | byte first = in.readByte();
52 | byte type = (byte) ((first >>> 4) & 0x0F);
53 | if ((type < MqttWireMessage.MESSAGE_TYPE_CONNECT) ||
54 | (type > MqttWireMessage.MESSAGE_TYPE_EXPAND)) {
55 | // Invalid MQTT message type...
56 | throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_INVALID_MESSAGE);
57 | }
58 | long remLen = MqttWireMessage.readMBI(in).getValue();
59 | bais.write(first);
60 | // bit silly, we decode it then encode it
61 | bais.write(MqttWireMessage.encodeMBI(remLen));
62 | byte[] packet = new byte[(int)(bais.size()+remLen)];
63 | in.readFully(packet,bais.size(),packet.length - bais.size());
64 | byte[] header = bais.toByteArray();
65 | System.arraycopy(header,0,packet,0, header.length);
66 | MqttWireMessage message = MqttWireMessage.createWireMessage(packet);
67 | return message;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.BufferedOutputStream;
15 | import java.io.IOException;
16 | import java.io.OutputStream;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 |
21 | /**
22 | * An MqttOutputStream
lets applications write instances of
23 | * MqttWireMessage
.
24 | */
25 | public class MqttOutputStream extends OutputStream {
26 | private BufferedOutputStream out;
27 |
28 | public MqttOutputStream(OutputStream out) {
29 | this.out = new BufferedOutputStream(out);
30 | }
31 |
32 | public void close() throws IOException {
33 | out.close();
34 | }
35 |
36 | public void flush() throws IOException {
37 | out.flush();
38 | }
39 |
40 | public void write(byte[] b) throws IOException {
41 | out.write(b);
42 | }
43 |
44 | public void write(byte[] b, int off, int len) throws IOException {
45 | out.write(b, off, len);
46 | }
47 |
48 | public void write(int b) throws IOException {
49 | out.write(b);
50 | }
51 |
52 | /**
53 | * Writes an MqttWireMessage
to the stream.
54 | */
55 | public void write(MqttWireMessage message) throws IOException, MqttException {
56 | byte[] bytes = message.getHeader();
57 | byte[] pl = message.getPayload();
58 | // out.write(message.getHeader());
59 | // out.write(message.getPayload());
60 | out.write(bytes,0,bytes.length);
61 | out.write(pl,0,pl.length);
62 | }
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPersistableWireMessage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import org.eclipse.paho.client.mqttv3.MqttException;
15 | import org.eclipse.paho.client.mqttv3.MqttPersistable;
16 | import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
17 |
18 | public abstract class MqttPersistableWireMessage extends MqttWireMessage
19 | implements MqttPersistable {
20 |
21 | public MqttPersistableWireMessage(byte type) {
22 | super(type);
23 | }
24 |
25 | public byte[] getHeaderBytes() throws MqttPersistenceException {
26 | try {
27 | return getHeader();
28 | }
29 | catch (MqttException ex) {
30 | throw new MqttPersistenceException(ex.getCause());
31 | }
32 | }
33 |
34 | public int getHeaderLength() throws MqttPersistenceException {
35 | return getHeaderBytes().length;
36 | }
37 |
38 | public int getHeaderOffset() throws MqttPersistenceException{
39 | return 0;
40 | }
41 |
42 | // public String getKey() throws MqttPersistenceException {
43 | // return new Integer(getMessageId()).toString();
44 | // }
45 |
46 | public byte[] getPayloadBytes() throws MqttPersistenceException {
47 | try {
48 | return getPayload();
49 | }
50 | catch (MqttException ex) {
51 | throw new MqttPersistenceException(ex.getCause());
52 | }
53 | }
54 |
55 | public int getPayloadLength() throws MqttPersistenceException {
56 | return 0;
57 | }
58 |
59 | public int getPayloadOffset() throws MqttPersistenceException {
60 | return 0;
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPingReq.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import org.eclipse.paho.client.mqttv3.MqttException;
15 |
16 | /**
17 | * An on-the-wire representation of an MQTT PINGREQ message.
18 | */
19 | public class MqttPingReq extends MqttWireMessage {
20 | public MqttPingReq() {
21 | super(MqttWireMessage.MESSAGE_TYPE_PINGREQ);
22 | }
23 |
24 | /**
25 | * Returns false
as message IDs are not required for MQTT
26 | * PINGREQ messages.
27 | */
28 | public boolean isMessageIdRequired() {
29 | return false;
30 | }
31 |
32 | protected byte[] getVariableHeader() throws MqttException {
33 | return new byte[0];
34 | }
35 |
36 | protected byte getMessageInfo() {
37 | return 0;
38 | }
39 |
40 | public String getKey() {
41 | return new String("Ping");
42 | }
43 | }
44 |
45 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPingResp.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import org.eclipse.paho.client.mqttv3.MqttException;
15 |
16 |
17 | /**
18 | * An on-the-wire representation of an MQTT PINGRESP.
19 | */
20 | public class MqttPingResp extends MqttAck {
21 | public MqttPingResp(byte info, byte[] variableHeader) {
22 | super(MqttWireMessage.MESSAGE_TYPE_PINGRESP);
23 | }
24 |
25 | protected byte[] getVariableHeader() throws MqttException {
26 | // Not needed, as the client never encodes a PINGRESP
27 | return new byte[0];
28 | }
29 |
30 | /**
31 | * Returns whether or not this message needs to include a message ID.
32 | */
33 | public boolean isMessageIdRequired() {
34 | return false;
35 | }
36 |
37 | public String getKey() {
38 | return new String("Ping");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPubAck.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.DataInputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 |
21 |
22 | /**
23 | * An on-the-wire representation of an MQTT PUBACK message.
24 | */
25 | public class MqttPubAck extends MqttAck {
26 | public MqttPubAck(byte info, byte[] data) throws IOException {
27 | super(MqttWireMessage.MESSAGE_TYPE_PUBACK);
28 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
29 | DataInputStream dis = new DataInputStream(bais);
30 | msgId = dis.readLong();
31 | dis.close();
32 | }
33 |
34 | public MqttPubAck(MqttPublish publish) {
35 | super(MqttWireMessage.MESSAGE_TYPE_PUBACK);
36 | msgId = publish.getMessageId();
37 | }
38 |
39 | protected byte[] getVariableHeader() throws MqttException {
40 | return encodeMessageId();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPubComp.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.DataInputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 |
21 |
22 | /**
23 | * An on-the-wire representation of an MQTT PUBCOMP message.
24 | */
25 | public class MqttPubComp extends MqttAck {
26 | public MqttPubComp(byte info, byte[] data) throws IOException {
27 | super(MqttWireMessage.MESSAGE_TYPE_PUBCOMP);
28 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
29 | DataInputStream dis = new DataInputStream(bais);
30 | msgId = dis.readLong();
31 | dis.close();
32 | }
33 |
34 | public MqttPubComp(MqttPublish publish) {
35 | super(MqttWireMessage.MESSAGE_TYPE_PUBCOMP);
36 | this.msgId = publish.getMessageId();
37 | }
38 |
39 | public MqttPubComp(long msgId) {
40 | super(MqttWireMessage.MESSAGE_TYPE_PUBCOMP);
41 | this.msgId = msgId;
42 | }
43 |
44 | protected byte[] getVariableHeader() throws MqttException {
45 | return encodeMessageId();
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPubRec.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.DataInputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 |
21 |
22 | /**
23 | * An on-the-wire representation of an MQTT PUBREC message.
24 | */
25 | public class MqttPubRec extends MqttAck {
26 | public MqttPubRec(byte info, byte[] data) throws IOException {
27 | super(MqttWireMessage.MESSAGE_TYPE_PUBREC);
28 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
29 | DataInputStream dis = new DataInputStream(bais);
30 | msgId = dis.readLong();
31 | dis.close();
32 | }
33 |
34 | public MqttPubRec(MqttPublish publish) {
35 | super(MqttWireMessage.MESSAGE_TYPE_PUBREC);
36 | msgId = publish.getMessageId();
37 | }
38 |
39 | protected byte[] getVariableHeader() throws MqttException {
40 | return encodeMessageId();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPubRel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.DataInputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 | /**
21 | * An on-the-wire representation of an MQTT PUBREL message.
22 | */
23 | public class MqttPubRel extends MqttPersistableWireMessage {
24 |
25 | /**
26 | * Createa a pubrel message based on a pubrec
27 | * @param pubRec
28 | */
29 | public MqttPubRel(MqttPubRec pubRec) {
30 | super(MqttWireMessage.MESSAGE_TYPE_PUBREL);
31 | this.setMessageId(pubRec.getMessageId());
32 | }
33 |
34 | /**
35 | * Creates a pubrel based on a pubrel set of bytes read fro the network
36 | * @param info
37 | * @param data
38 | * @throws IOException
39 | */
40 | public MqttPubRel(byte info, byte[] data) throws IOException {
41 | super(MqttWireMessage.MESSAGE_TYPE_PUBREL);
42 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
43 | DataInputStream dis = new DataInputStream(bais);
44 | msgId = dis.readLong();
45 | dis.close();
46 | }
47 |
48 | protected byte[] getVariableHeader() throws MqttException {
49 | return encodeMessageId();
50 | }
51 |
52 | protected byte getMessageInfo() {
53 | return (byte)( 2 | (this.duplicate?8:0));
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttPublish.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.ByteArrayOutputStream;
16 | import java.io.DataInputStream;
17 | import java.io.DataOutputStream;
18 | import java.io.IOException;
19 |
20 | import org.eclipse.paho.client.mqttv3.MqttException;
21 | import org.eclipse.paho.client.mqttv3.MqttMessage;
22 |
23 |
24 | /**
25 | * An on-the-wire representation of an MQTT SEND message.
26 | */
27 | public class MqttPublish extends MqttPersistableWireMessage {
28 |
29 | private MqttMessage message;
30 | private String topicName;
31 |
32 | private byte[] encodedPayload = null;
33 |
34 | public MqttPublish(String name, MqttMessage message) {
35 | super(MqttWireMessage.MESSAGE_TYPE_PUBLISH);
36 | this.topicName = name;
37 | this.message = message;
38 | }
39 |
40 | /**
41 | * Constructs a new MqttPublish object.
42 | * @param info the message info byte
43 | * @param data the variable header and payload bytes
44 | */
45 | public MqttPublish(byte info, byte[] data) throws MqttException, IOException {
46 | super(MqttWireMessage.MESSAGE_TYPE_PUBLISH);
47 | this.message = new MqttReceivedMessage();
48 | message.setQos((info >> 1) & 0x03);
49 | if ((info & 0x01) == 0x01) {
50 | message.setRetained(true);
51 | }
52 | if ((info & 0x08) == 0x08) {
53 | ((MqttReceivedMessage) message).setDuplicate(true);
54 | }
55 |
56 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
57 | CountingInputStream counter = new CountingInputStream(bais);
58 | DataInputStream dis = new DataInputStream(counter);
59 | topicName = this.decodeUTF8(dis);
60 | if (message.getQos() > 0) {
61 | msgId = dis.readLong();
62 | }
63 | byte[] payload = new byte[data.length-counter.getCounter()];
64 | dis.readFully(payload);
65 | dis.close();
66 | message.setPayload(payload);
67 | }
68 |
69 | protected byte getMessageInfo() {
70 | byte info = (byte) (message.getQos() << 1);
71 | if (message.isRetained()) {
72 | info |= 0x01;
73 | }
74 | if (message.isDuplicate() || duplicate ) {
75 | info |= 0x08;
76 | }
77 |
78 | return info;
79 | }
80 |
81 | public String getTopicName() {
82 | return topicName;
83 | }
84 |
85 | public MqttMessage getMessage() {
86 | return message;
87 | }
88 |
89 | protected static byte[] encodePayload(MqttMessage message) {
90 | return message.getPayload();
91 | }
92 |
93 | public byte[] getPayload() throws MqttException {
94 | if (encodedPayload == null) {
95 | encodedPayload = encodePayload(message);
96 | }
97 | return encodedPayload;
98 | }
99 |
100 | public int getPayloadLength() {
101 | int length = 0;
102 | try {
103 | length = getPayload().length;
104 | } catch(MqttException me) {
105 | }
106 | return length;
107 | }
108 |
109 | //****************set msgId to long, by Quentin**//
110 | public void setMessageId(long msgId) {
111 | super.setMessageId(msgId);
112 | if (message instanceof MqttReceivedMessage) {
113 | ((MqttReceivedMessage)message).setMessageId(msgId);
114 | }
115 | }
116 |
117 | protected byte[] getVariableHeader() throws MqttException {
118 | try {
119 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
120 | DataOutputStream dos = new DataOutputStream(baos);
121 | this.encodeUTF8(dos, topicName);
122 | if (message.getQos() > 0) {
123 |
124 |
125 | //****change the version code . write by Quentin***//
126 | //********************for massage id refactor******//
127 | //dos.writeShort(msgId);
128 | dos.writeLong(msgId);
129 | //*************************************************//
130 | }
131 | dos.flush();
132 | return baos.toByteArray();
133 | }
134 | catch (IOException ex) {
135 | throw new MqttException(ex);
136 | }
137 | }
138 |
139 | public boolean isMessageIdRequired() {
140 | // all publishes require a message ID as it's used as the key to the token store
141 | return true;
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttReceivedMessage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import org.eclipse.paho.client.mqttv3.MqttMessage;
15 |
16 | public class MqttReceivedMessage extends MqttMessage {
17 |
18 | //****************set msgId to long, by Quentin**//
19 | private long messageId;
20 | //****************set msgId to long, by Quentin**//
21 | public void setMessageId(long msgId) {
22 | this.messageId = msgId;
23 | }
24 |
25 | //****************set msgId to long, by Quentin**//
26 | public long getMessageId() {
27 | return messageId;
28 | }
29 |
30 | // This method exists here to get around the protected visibility of the
31 | // super class method.
32 | public void setDuplicate(boolean value) {
33 | super.setDuplicate(value);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttSuback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.DataInputStream;
16 | import java.io.IOException;
17 |
18 | import org.eclipse.paho.client.mqttv3.MqttException;
19 |
20 |
21 | /**
22 | * An on-the-wire representation of an MQTT SUBACK.
23 | */
24 | public class MqttSuback extends MqttAck {
25 | private int[] grantedQos; // Not currently made available to anyone.
26 |
27 | public MqttSuback(byte info, byte[] data) throws Exception {
28 | super(MqttWireMessage.MESSAGE_TYPE_SUBACK);
29 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
30 | DataInputStream dis = new DataInputStream(bais);
31 | msgId = dis.readLong();
32 | int index = 0;
33 | grantedQos = new int[data.length-2];
34 | int qos = dis.read();
35 | while (qos != -1 && index < grantedQos.length) {
36 | grantedQos[index] = qos;
37 | index++;
38 | qos = dis.read();
39 | }
40 | dis.close();
41 | }
42 |
43 | protected byte[] getVariableHeader() throws MqttException {
44 | // Not needed, as the client never encodes a SUBACK
45 | return new byte[0];
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/org/eclipse/paho/client/mqttv3/internal/wire/MqttSubscribe.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 2012 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * which accompanies this distribution, and is available at
7 | * http://www.eclipse.org/legal/epl-v10.html
8 | *
9 | * Contributors:
10 | * Dave Locke - initial API and implementation and/or initial documentation
11 | */
12 | package org.eclipse.paho.client.mqttv3.internal.wire;
13 |
14 |
15 | import java.io.ByteArrayOutputStream;
16 | import java.io.DataOutputStream;
17 | import java.io.IOException;
18 |
19 | import org.eclipse.paho.client.mqttv3.MqttException;
20 | import org.eclipse.paho.client.mqttv3.MqttMessage;
21 |
22 |
23 | /**
24 | * An on-the-wire representation of an MQTT SUBSCRIBE message.
25 | */
26 | public class MqttSubscribe extends MqttWireMessage {
27 | private String[] names;
28 | private int[] qos;
29 |
30 | /**
31 | * Constructor for on an on hte wire MQTT subscribe message
32 | * @param names - one or more topics to subscribe to
33 | * @param qos - the max QOS that each each topic will be subscribed at
34 | */
35 | public MqttSubscribe(String[] names, int[] qos) {
36 | super(MqttWireMessage.MESSAGE_TYPE_SUBSCRIBE);
37 | this.names = names;
38 | this.qos = qos;
39 |
40 | if (names.length != qos.length) {
41 | throw new IllegalArgumentException();
42 | }
43 |
44 | for (int i=0;iThe default log and trace facility uses Java's build in log facility:- 5 | java.util.logging. For systems where this is not available or where 6 | an alternative logging framework is required the logging facility can be 7 | replaced using {@link org.eclipse.paho.client.mqttv3.logging.LoggerFactory#setLogger(String)} 8 | which takes an implementation of the {@link org.eclipse.paho.client.mqttv3.logging.Logger} 9 | interface. 10 | 11 |
A sample java.util.logging properties file - jsr47min.properties is provided that demonstrates 12 | how to run with a memory based trace facility that runs with minimal performance 13 | overhead. The memory buffer can be dumped when a log/trace record is written matching 14 | the MemoryHandlers trigger level or when the push method is invoked on the MemoryHandler. 15 | {@link org.eclipse.paho.client.mqttv3.util.Debug Debug} provides method to make it easy 16 | to dump the memory buffer as well as other useful debug info. 17 | 18 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/persist/MemoryPersistence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.persist; 13 | 14 | import java.util.Enumeration; 15 | import java.util.Hashtable; 16 | 17 | import org.eclipse.paho.client.mqttv3.MqttClientPersistence; 18 | import org.eclipse.paho.client.mqttv3.MqttPersistable; 19 | import org.eclipse.paho.client.mqttv3.MqttPersistenceException; 20 | 21 | /** 22 | * Persistence that uses memory 23 | * 24 | * In cases where reliability is not required across client or device 25 | * restarts memory this memory peristence can be used. In cases where 26 | * reliability is required like when clean session is set to false 27 | * then a non-volatile form of persistence should be used. 28 | * 29 | */ 30 | public class MemoryPersistence implements MqttClientPersistence { 31 | 32 | private Hashtable data; 33 | 34 | /* (non-Javadoc) 35 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#close() 36 | */ 37 | public void close() throws MqttPersistenceException { 38 | data.clear(); 39 | } 40 | 41 | /* (non-Javadoc) 42 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#keys() 43 | */ 44 | public Enumeration keys() throws MqttPersistenceException { 45 | return data.keys(); 46 | } 47 | 48 | /* (non-Javadoc) 49 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#get(java.lang.String) 50 | */ 51 | public MqttPersistable get(String key) throws MqttPersistenceException { 52 | return (MqttPersistable)data.get(key); 53 | } 54 | 55 | /* (non-Javadoc) 56 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#open(java.lang.String, java.lang.String) 57 | */ 58 | public void open(String clientId, String serverURI) throws MqttPersistenceException { 59 | this.data = new Hashtable(); 60 | } 61 | 62 | /* (non-Javadoc) 63 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#put(java.lang.String, org.eclipse.paho.client.mqttv3.MqttPersistable) 64 | */ 65 | public void put(String key, MqttPersistable persistable) throws MqttPersistenceException { 66 | data.put(key, persistable); 67 | } 68 | 69 | /* (non-Javadoc) 70 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#remove(java.lang.String) 71 | */ 72 | public void remove(String key) throws MqttPersistenceException { 73 | data.remove(key); 74 | } 75 | 76 | /* (non-Javadoc) 77 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#clear() 78 | */ 79 | public void clear() throws MqttPersistenceException { 80 | data.clear(); 81 | } 82 | 83 | /* (non-Javadoc) 84 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#containsKey(java.lang.String) 85 | */ 86 | public boolean containsKey(String key) throws MqttPersistenceException { 87 | return data.containsKey(key); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/persist/package.html: -------------------------------------------------------------------------------- 1 |
2 | Contains implementations of the MqttClientPersistence interface. 3 | 4 |5 | An MQTT client needs a persistence mechanism to store messages while they 6 | are in the process of being delivered. This package contains several 7 | implementations of the interface. If a persistence class is not 8 | specified on the constructor to an MQTT client, 9 | {@link org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence MqttDefaultFilePersistence} 10 | is used by default. 11 | 12 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/util/Debug.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2012 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * which accompanies this distribution, and is available at 7 | * http://www.eclipse.org/legal/epl-v10.html 8 | * 9 | * Contributors: 10 | * Dave Locke - initial API and implementation and/or initial documentation 11 | */ 12 | package org.eclipse.paho.client.mqttv3.util; 13 | 14 | import java.util.Enumeration; 15 | import java.util.Properties; 16 | 17 | import org.eclipse.paho.client.mqttv3.internal.ClientComms; 18 | import org.eclipse.paho.client.mqttv3.logging.Logger; 19 | import org.eclipse.paho.client.mqttv3.logging.LoggerFactory; 20 | 21 | /** 22 | * Utility to help debug problems with the Paho MQTT client 23 | * Once initialised a call to dumpClientDebug will force any memory trace 24 | * together with pertinent client and system state to the main log facility. 25 | * 26 | * No client wide lock is taken when the dump is progress. This means the 27 | * set of client state may not be consistent as the client can still be 28 | * processing work while the dump is in progress. 29 | */ 30 | public class Debug { 31 | 32 | final static String className = ClientComms.class.getName(); 33 | Logger log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT,className); 34 | 35 | static String separator = "=============="; 36 | static String lineSep = System.getProperty("line.separator","\n"); 37 | 38 | String clientID; 39 | ClientComms comms; 40 | 41 | /** 42 | * Set the debug facility up for a specific client 43 | * @param clientID the ID of the client being debugged 44 | * @param comms the ClientComms object of the client being debugged 45 | */ 46 | public Debug(String clientID, ClientComms comms) { 47 | this.clientID = clientID; 48 | this.comms = comms; 49 | log.setResourceName(clientID); 50 | } 51 | 52 | /** 53 | * Dump maximum debug info. 54 | * This includes state specific to a client as well 55 | * as debug that is JVM wide like trace and system properties. 56 | * All state is written as debug log entries. 57 | */ 58 | public void dumpClientDebug() { 59 | dumpClientComms(); 60 | dumpConOptions(); 61 | dumpClientState(); 62 | dumpBaseDebug(); 63 | } 64 | 65 | /** 66 | * Dump of JVM wide debug info. 67 | * This includes trace and system properties. 68 | * Includes trace and system properties 69 | */ 70 | public void dumpBaseDebug() { 71 | dumpVersion(); 72 | dumpSystemProperties(); 73 | dumpMemoryTrace(); 74 | } 75 | 76 | /** 77 | * If memory trace is being used a request is made to push it 78 | * to the target handler. 79 | */ 80 | protected void dumpMemoryTrace() { 81 | log.dumpTrace(); 82 | } 83 | 84 | /** 85 | * Dump information that show the version of the MQTT client being used. 86 | */ 87 | protected void dumpVersion() { 88 | StringBuffer vInfo = new StringBuffer(); 89 | vInfo.append(lineSep+separator+" Version Info "+ separator+lineSep); 90 | vInfo.append(left("Version",20,' ') + ": "+ ClientComms.VERSION + lineSep); 91 | vInfo.append(left("Build Level",20,' ') + ": "+ ClientComms.BUILD_LEVEL + lineSep); 92 | vInfo.append(separator+separator+separator+lineSep); 93 | log.fine(className,"dumpVersion", vInfo.toString()); 94 | } 95 | 96 | /** 97 | * Dump the current set of system.properties to a log record 98 | */ 99 | public void dumpSystemProperties() { 100 | 101 | Properties sysProps = System.getProperties(); 102 | log.fine(className,"dumpSystemProperties", dumpProperties(sysProps, "SystemProperties").toString()); 103 | } 104 | 105 | /** 106 | * Dump interesting variables from ClientState 107 | */ 108 | public void dumpClientState() { 109 | Properties props = null; 110 | if (comms != null && comms.getClientState() != null ) { 111 | props = comms.getClientState().getDebug(); 112 | log.fine(className,"dumpClientState", dumpProperties(props, clientID + " : ClientState").toString()); 113 | } 114 | } 115 | 116 | /** 117 | * Dump interesting variables from ClientComms 118 | */ 119 | public void dumpClientComms() { 120 | Properties props = null; 121 | if (comms != null) { 122 | props = comms.getDebug(); 123 | log.fine(className,"dumpClientComms", dumpProperties(props, clientID + " : ClientComms").toString()); 124 | } 125 | } 126 | 127 | /** 128 | * Dump Connection options 129 | */ 130 | public void dumpConOptions() { 131 | Properties props = null; 132 | if (comms != null) { 133 | props = comms.getConOptions().getDebug(); 134 | log.fine(className,"dumpConOptions", dumpProperties(props, clientID + " : Connect Options").toString()); 135 | } 136 | } 137 | 138 | 139 | /** 140 | * Return a set of properties as a formatted string 141 | */ 142 | public static String dumpProperties(Properties props, String name) { 143 | 144 | StringBuffer propStr = new StringBuffer(); 145 | Enumeration propsE = props.propertyNames(); 146 | propStr.append(lineSep+separator+" "+name+" "+ separator+lineSep); 147 | while (propsE.hasMoreElements()) { 148 | String key = (String)propsE.nextElement(); 149 | propStr.append(left(key,28,' ') + ": "+ props.get(key)+lineSep); 150 | } 151 | propStr.append(separator+separator+separator+lineSep); 152 | 153 | return propStr.toString(); 154 | } 155 | 156 | /** 157 | * Left justify a string. 158 | * 159 | * @param s the string to justify 160 | * @param width the field width to justify within 161 | * @param fillChar the character to fill with 162 | * 163 | * @return the justified string. 164 | */ 165 | public static String left(String s, int width, char fillChar) { 166 | if (s.length() >= width) { 167 | return s; 168 | } 169 | StringBuffer sb = new StringBuffer(width); 170 | sb.append(s); 171 | for (int i = width - s.length(); --i >= 0;) { 172 | sb.append(fillChar); 173 | } 174 | return sb.toString(); 175 | } 176 | 177 | } 178 | -------------------------------------------------------------------------------- /src/org/eclipse/paho/client/mqttv3/util/package.html: -------------------------------------------------------------------------------- 1 |
2 | Provides helpers and utilities. 3 | 4 | 5 | -------------------------------------------------------------------------------- /yunba-java-sdk.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yunba/yunba-java-sdk/333ae6d7694fb70baa21c8abe29a88dc47342b5e/yunba-java-sdk.jar --------------------------------------------------------------------------------