forked from antlinker/libmqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmqtt_jni.cpp
More file actions
148 lines (118 loc) · 3.71 KB
/
Copy pathlibmqtt_jni.cpp
File metadata and controls
148 lines (118 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright Go-IIoT (https://github.com/goiiot)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "org_goiiot_libmqtt_LibMQTT.h"
#include "libmqtt.h"
#include "handlers_jni.h"
using namespace std;
static jclass mTopicCallbackClass;
static jmethodID on_topic_msg_mid;
// TODO: use concurrent hash map to keep thread safe and multiple handlers
static jobject mTopicCallbackObj;
/*
* Method: _handle
* Signature: (ILjava/lang/String;Lorg/goiiot/libmqtt/LibMQTT/TopicMessageCallback;)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1handle
(JNIEnv *env, jclass clazz, jint id, jstring topic, jobject callback) {
if (callback == NULL) {
return;
}
mTopicCallbackClass = clazz;
mTopicCallbackObj = callback;
on_topic_msg_mid = env->GetMethodID(
clazz, "onMessage", "(Ljava/lang/String;I[B)V");
auto c_topic = env->GetStringUTFChars(topic, 0);
auto handler = [](int client, char *topic, int qos, char *payload, int size) {
JNIEnv *g_env;
jvm->AttachCurrentThread((void **)&g_env, NULL);
if (g_env == NULL) {
return;
} else {
auto result = g_env->NewByteArray(size);
g_env->SetByteArrayRegion(result, 0, size, (jbyte *) payload);
g_env->CallVoidMethod(mTopicCallbackObj, on_topic_msg_mid,
g_env->NewStringUTF(topic), qos, result);
}
};
Libmqtt_handle(id, (char *) c_topic, (libmqtt_topic_handler) handler);
env->ReleaseStringUTFChars(topic, c_topic);
}
/*
* Method: _conn
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1connect
(JNIEnv *env, jclass c, jint id) {
Libmqtt_connect(id, &conn_handler);
}
/*
* Method: _wait
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1wait
(JNIEnv *env, jclass c, jint id) {
env->MonitorEnter(c);
Libmqtt_wait(id);
env->MonitorExit(c);
}
/*
* Method: _pub
* Signature: (ILjava/lang/String;I[B)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1pub
(JNIEnv *env, jclass c, jint id, jstring topic, jint qos, jbyteArray payload) {
auto c_topic = env->GetStringUTFChars(topic, 0);
auto len = env->GetArrayLength(payload);
auto body = env->GetByteArrayElements(payload, 0);
Libmqtt_publish(id, (char *)c_topic, qos, (char *)body, len);
env->ReleaseStringUTFChars(topic, c_topic);
env->ReleaseByteArrayElements(payload, body, 0);
}
/*
* Method: _sub
* Signature: (ILjava/lang/String;I)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1sub
(JNIEnv *env, jclass c, jint id, jstring topic, jint qos) {
auto c_topic = env->GetStringUTFChars(topic, 0);
Libmqtt_subscribe(id, (char *)c_topic, qos);
env->ReleaseStringUTFChars(topic, c_topic);
}
/*
* Method: _unsub
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1unsub
(JNIEnv *env, jclass c, jint id, jstring topic) {
auto c_topic = env->GetStringUTFChars(topic, 0);
Libmqtt_unsubscribe(id, (char *)c_topic);
env->ReleaseStringUTFChars(topic, c_topic);
}
/*
* Method: _destroy
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_org_goiiot_libmqtt_LibMQTT__1destroy
(JNIEnv *env, jclass c, jint id, jboolean force) {
Libmqtt_destroy(id, force);
}