/*
============================================================================
Licensed Materials - Property of IBM
5747-SM3
(C) Copyright IBM Corp. 1999, 2012 All Rights Reserved.
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with
IBM Corp.
============================================================================
*/
package com.ibm.msg.android.service;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
import android.app.Service;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import com.ibm.msg.android.service.MessageStore.StoredMessage;
/**
* <p>
* This performs MQTT operations for a specific client {host,port,clientId}
* </p>
* <p>
* Most of the major API here is intended to implement the most general forms of
* the methods in IMqttAsyncClient, with slight adjustments for the Android
* environment<br>
* These adjustments usually consist of adding two parameters to each method :-
* <ul>
* <li>invocationContext - a string passed from the application to identify the
* context of the operation (mainly included for support of the javascript API
* implementation)</li>
* <li>activityToken - a string passed from the Activity to relate back to a
* callback method or other context-specific data</li>
* </ul>
* </p>
* <p>
* Operations are very much asynchronous, so success and failure are notified by
* packing the relevant data into Intent objects which are broadcast back to the
* Activity via the MqttService.callbackToActivity() method.
* </p>
*/
class MqttServiceClient implements MqttCallback {
// Strings for Intents etc..
private static final String TAG = "MqttServiceClient";
// Error status messages
private static final String NOT_CONNECTED = "not connected";
// fields for the connection definition
private String serverURI;
private String clientId;
private MqttClientPersistence persistence = null;
private MqttConnectOptions connectOptions;
// Client handle, used for callbacks...
private String clientHandle;
// our client object - instantiated on connect
private MqttAsyncClient myClient = null;
// our (parent) service object
private MqttService service = null;
// Saved sent messages and their corresponding Topics, activityTokens and
// invocationContexts, so we can handle "deliveryComplete" callbacks
// from the mqttClient
private Map<IMqttDeliveryToken, String /* Topic */> savedTopics = new HashMap<IMqttDeliveryToken, String>();
private Map<IMqttDeliveryToken, MqttMessage> savedSentMessages = new HashMap<IMqttDeliveryToken, MqttMessage>();
private Map<IMqttDeliveryToken, String> savedActivityTokens = new HashMap<IMqttDeliveryToken, String>();
private Map<IMqttDeliveryToken, String> savedInvocationContexts = new HashMap<IMqttDeliveryToken, String>();
private WakeLock wakelock = null;
private String wakeLockTag = null;
/**
* Constructor
*
* @param service
* our "parent" service - we make callbacks to it
* @param serverURI
* the URI of the MQTT server to which we will connect
* @param clientId
* the name by which we will identify ourselves to the MQTT
* server
* @param persistence
* @param clientHandle
* the "handle" by which the activity will identify us
*/
MqttServiceClient(MqttService service, String serverURI, String clientId,
MqttClientPersistence persistence, String clientHandle) {
this.serverURI = serverURI.toString();
this.service = service;
this.clientId = clientId;
this.persistence = persistence;
this.clientHandle = clientHandle;
StringBuffer buff = new StringBuffer(this.getClass().getCanonicalName());
buff.append(" ");
buff.append(clientId);
buff.append(" ");
buff.append("on host ");
buff.append(serverURI);
wakeLockTag = buff.toString();
}
// The major API implementation follows :-
/**
* Connect to the server specified when we were instantiated
*
* @param options
* timeout, etc
* @param invocationContext
* arbitrary data to be passed back to the application
* @param activityToken
* arbitrary identifier to be passed back to the Activity
*/
public void connect(MqttConnectOptions options, String invocationContext,
String activityToken) {
connectOptions = options;
if (connectOptions.isCleanSession()) { // if it's a clean session,
// discard old data
service.messageStore.clearArrivedMessages(clientHandle);
}
service.traceDebug(TAG, "Connecting {" + serverURI + "} as {"
+ clientId + "}");
final Bundle resultBundle = new Bundle();
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
activityToken);
resultBundle.putString(
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
invocationContext);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
MqttServiceConstants.CONNECT_ACTION);
try {
if (persistence == null) {
// ask Android where we can put files
File myDir = service.getExternalFilesDir(TAG);
if (myDir == null) {
resultBundle.putString(
MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
"No external storage available");
resultBundle.putSerializable(
MqttServiceConstants.CALLBACK_EXCEPTION, new MqttPersistenceException());
service.callbackToActivity(clientHandle, Status.ERROR,
resultBundle);
return;
}
// use that to setup MQTT client persistence storage
persistence = new MqttDefaultFilePersistence(
myDir.getAbsolutePath());
}
myClient = new MqttAsyncClient(serverURI, clientId, persistence);
myClient.setCallback(this);
IMqttActionListener listener = new MqttServiceClientListener(
resultBundle) {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
//since the device's cpu can go to sleep
//we may miss pings, so we get a wake lock
acquireWakeLock();
service.callbackToActivity(clientHandle, Status.OK,
resultBundle);
deliverBacklog();
}
};
myClient.connect(connectOptions, invocationContext, listener);
}
catch (Exception e) {
handleException(resultBundle, e);
}
}
private void handleException(final Bundle resultBundle, Exception e) {
resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
e.getLocalizedMessage());
resultBundle.putSerializable(MqttServiceConstants.CALLBACK_EXCEPTION, e);
service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
}
/**
* Attempt to deliver any outstanding messages we've received but which the
* application hasn't acknowledged. If "cleanSession" was specified, we'll
* have already
没有合适的资源?快使用搜索试试~ 我知道了~
MQTT——移动消息传递和 M2M 客户机开发API包
共392个文件
html:204个
java:33个
png:33个
5星 · 超过95%的资源 需积分: 9 24 下载量 143 浏览量
2014-04-19
16:16:34
上传
评论 4
收藏 7.08MB ZIP 举报
温馨提示
IBM官方提供的MQTT 移动消息传递和 M2M 客户机包,包括C JAVA android 开发API。 此API可以有效的帮助大家快速的开发MQTT client 程序。 我是从IBM官网下载的,下载很不容易,主要是网速过慢。 希望此包可以帮助到有需要的人。 MQTT IBM c java android API
资源推荐
资源详情
资源评论
收起资源包目录
MQTT——移动消息传递和 M2M 客户机开发API包 (392个子文件)
MqttExerciser.apk 317KB
MQTTV3SSample.c 11KB
MQTTV3ASSample.c 11KB
MQTTV3ASample.c 10KB
MQTTV3Sample.c 9KB
.classpath 364B
doxygen.css 16KB
doxygen.css 16KB
stylesheet.css 7KB
style.css 4KB
tabs.css 1KB
tabs.css 1KB
mqttv3as.dll 964KB
mqttv3cs.dll 952KB
mqttv3a.dll 88KB
mqttv3c.dll 80KB
MQTTVersion.exe 61KB
IBMMessagingBanner.gif 113KB
PlayButton.gif 10KB
EnterperiseMessagingButton.gif 8KB
CommunityButton.gif 8KB
websphere-brandmark.gif 2KB
inherit.gif 57B
MQTTAsync.h 54KB
MQTTClient.h 52KB
MQTTClientPersistence.h 11KB
mqttws31.js.html 465KB
index-all.html 233KB
MqttAsyncClient.html 126KB
IMqttAsyncClient.html 94KB
MqttClient.html 84KB
JSR47Logger.html 77KB
_m_q_t_t_async_8h.html 70KB
Logger.html 68KB
MqttException.html 67KB
_m_q_t_t_client_8h.html 66KB
MqttClient.html 55KB
IMqttClient.html 54KB
_m_q_t_t_async_8h_source.html 48KB
MqttException.html 45KB
MqttConnectOptions.html 42KB
MqttException.html 41KB
_m_q_t_t_client_8h_source.html 35KB
MqttConnectOptions.html 34KB
constant-values.html 34KB
IMqttToken.html 33KB
MqttPersistenceException.html 33KB
MqttToken.html 31KB
Messaging.Client.html 29KB
MqttDefaultFilePersistence.html 29KB
MqttDefaultFilePersistence.html 28KB
SampleAsyncCallBack.html 28KB
IMqttActionListener.html 27KB
MemoryPersistence.html 27KB
_m_q_t_t_client_persistence_8h.html 27KB
_m_q_t_t_client_persistence_8h.html 27KB
WebMessagingUtility.html 25KB
SampleAsyncWait.html 25KB
Sample.html 24KB
package-summary.html 23KB
MqttMessage.html 23KB
package-summary.html 23KB
IMqttToken.html 22KB
MqttClientPersistence.html 22KB
MqttClientPersistence.html 21KB
MqttMessage.html 21KB
IMqttDeliveryToken.html 21KB
MqttPersistenceException.html 21KB
MqttPersistenceException.html 20KB
MqttPersistenceException.html 20KB
package-use.html 19KB
MqttSecurityException.html 18KB
MqttMessage.html 18KB
MqttPersistable.html 18KB
MqttPersistable.html 18KB
MqttDeliveryToken.html 18KB
MqttSecurityException.html 18KB
MqttException.html 18KB
struct_m_q_t_t_async__connect_options.html 18KB
overview-tree.html 18KB
MqttTopic.html 17KB
Debug.html 16KB
LoggerFactory.html 16KB
struct_m_q_t_t_client__connect_options.html 16KB
MqttSecurityException.html 16KB
MqttDeliveryToken.html 15KB
MqttCallback.html 14KB
IMqttDeliveryToken.html 14KB
functions.html 14KB
functions_vars.html 13KB
subscribe.html 13KB
_m_q_t_t_client_persistence_8h_source.html 13KB
_m_q_t_t_client_persistence_8h_source.html 13KB
MqttCallback.html 13KB
MqttTopic.html 13KB
MqttPersistable.html 13KB
SimpleLogFormatter.html 12KB
Logger.html 12KB
MqttCallback.html 12KB
struct_m_q_t_t_client__persistence.html 12KB
共 392 条
- 1
- 2
- 3
- 4
资源评论
- ken8310012014-09-09可以,不是最新版,但能用
- 詹森波恩2014-11-05最全的资料了,多谢分享
- fmmdptt1632014-05-07挺好的,研究了下c端的,不过现在官网貌似更新新版的了
水桶前辈
- 粉丝: 3205
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- NC65 UAP65 流程开发 – 业务流 – 推单 详细笔记
- 两种计算CDF累加分布函数的Matlab程序,可以选择使用 .rar
- Windows操作系统介绍.zip
- 汉森的广义t分布的matlab程序(金融)cdf.zip
- Three.js介绍.zip0002
- 人工智能BBSO算法,MATLAB实现,很基本的人工智能算法,里面有很多源程序
- 高端大气科技互联网项目融资商业计划书产品公司品牌介绍PPT模板.pptx
- Sora AI文字生成视频实操教程、由给定的图片生成视频的教程说明书,祝你一臂之力 轻松上手Sora
- 网络安全说明:网络安全方面的资料,+适合网络安全的初学者
- 【移动应用开发】HarmonyOS应用服务开发,移动购物应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功