Loading...
Searching...
No Matches
MqttSession.java
Go to the documentation of this file.
1package com.navigine.idl.java;
2
3import java.util.concurrent.atomic.AtomicBoolean;
4
17public abstract class MqttSession {
35 public abstract void connect(String server, int port, String username, String password);
36
52 public abstract void setSubTopic(String subTopic);
53
70 public abstract void addListener(MqttSessionListener listener);
71
86 public abstract void removeListener(MqttSessionListener listener);
87
101 public abstract void disconnect();
102
121 public abstract void publish(String topic, String message);
122
123 private static final class CppProxy extends MqttSession
124 {
125 private final long nativeRef;
126 private final AtomicBoolean destroyed = new AtomicBoolean(false);
127
128 private CppProxy(long nativeRef)
129 {
130 if (nativeRef == 0) throw new RuntimeException("nativeRef is zero");
131 this.nativeRef = nativeRef;
132 }
133
134 private native void nativeDestroy(long nativeRef);
135 public void _djinni_private_destroy()
136 {
137 boolean destroyed = this.destroyed.getAndSet(true);
138 if (!destroyed) nativeDestroy(this.nativeRef);
139 }
140 protected void finalize() throws java.lang.Throwable
141 {
142 _djinni_private_destroy();
143 super.finalize();
144 }
145
146 // MqttSession methods
147
148 @Override
149 public void connect(String server, int port, String username, String password)
150 {
151 assert !this.destroyed.get() : "trying to use a destroyed object";
152 native_connect(this.nativeRef, server, port, username, password);
153 }
154 private native void native_connect(long _nativeRef, String server, int port, String username, String password);
155
156 @Override
157 public void setSubTopic(String subTopic)
158 {
159 assert !this.destroyed.get() : "trying to use a destroyed object";
160 native_setSubTopic(this.nativeRef, subTopic);
161 }
162 private native void native_setSubTopic(long _nativeRef, String subTopic);
163
164 @Override
165 public void addListener(MqttSessionListener listener)
166 {
167 assert !this.destroyed.get() : "trying to use a destroyed object";
168 native_addListener(this.nativeRef, listener);
169 }
170 private native void native_addListener(long _nativeRef, MqttSessionListener listener);
171
172 @Override
173 public void removeListener(MqttSessionListener listener)
174 {
175 assert !this.destroyed.get() : "trying to use a destroyed object";
176 native_removeListener(this.nativeRef, listener);
177 }
178 private native void native_removeListener(long _nativeRef, MqttSessionListener listener);
179
180 @Override
181 public void disconnect()
182 {
183 assert !this.destroyed.get() : "trying to use a destroyed object";
184 native_disconnect(this.nativeRef);
185 }
186 private native void native_disconnect(long _nativeRef);
187
188 @Override
189 public void publish(String topic, String message)
190 {
191 assert !this.destroyed.get() : "trying to use a destroyed object";
192 native_publish(this.nativeRef, topic, message);
193 }
194 private native void native_publish(long _nativeRef, String topic, String message);
195 }
196}