[Problem] There are some code/comments which need to be simplified.
[Solution] Do refactoring by drawing common code as functions,
correcting comments and so on.
private static final String TAG = "AITT_ANDROID";
private static final String INVALID_TOPIC = "Invalid topic";
+ private static final String HOST_STRING = "host";
private final Map<String, HostTable> publishTable = new HashMap<>();
private final Map<String, Pair<Protocol, Object>> subscribeMap = new HashMap<>();
}
public static Protocol fromInt(long value) {
- for (Protocol type : values()) {
- if (type.getValue() == value) {
+ for (Protocol type : values())
+ if (type.getValue() == value)
return type;
- }
- }
+
return null;
}
}
* @param clearSession "clear" the current session when the client disconnects
*/
public Aitt(Context appContext, String id, String ip, boolean clearSession) throws InstantiationException {
- if (appContext == null) {
+ if (appContext == null)
throw new IllegalArgumentException("Invalid appContext");
- }
- if (id == null || id.isEmpty()) {
+ if (id == null || id.isEmpty())
throw new IllegalArgumentException("Invalid id");
- }
- if (ip == null || ip.isEmpty()) {
+ if (ip == null || ip.isEmpty())
throw new IllegalArgumentException("Invalid ip");
- }
+
mJniInterface = new JniInterface();
instance = mJniInterface.init(id, ip, clearSession);
- if (instance == 0L) {
+ if (instance == 0L)
throw new InstantiationException("Failed to instantiate native instance");
- }
+
this.ip = ip;
this.appContext = appContext;
}
* @param callback ConnectionCallback to which status should be updated
*/
public void setConnectionCallback(ConnectionCallback callback) {
- if (callback == null) {
+ if (callback == null)
throw new IllegalArgumentException("Invalid callback");
- }
+
connectionCallback = callback;
mJniInterface.setConnectionCallback(this::connectionStatusCallback);
}
* @param port Broker port number to which, device has to connect
*/
public void connect(@Nullable String brokerIp, int port) {
- if (brokerIp == null || brokerIp.isEmpty()) {
+ if (brokerIp == null || brokerIp.isEmpty())
brokerIp = Definitions.AITT_LOCALHOST;
- }
+
mJniInterface.connect(brokerIp, port);
//Subscribe to java discovery topic
mJniInterface.subscribe(Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, this::messageCallback, Protocol.MQTT.getValue(), QoS.EXACTLY_ONCE.ordinal());
}
/**
- * Method to publish message to a specific topic
+ * Method to publish message to a specific topic with a given protocol
*
* @param topic String to which message needs to be published
* @param message Byte message that needs to be published
}
/**
- * Method to publish message to a specific topic
+ * Method to publish message to a specific topic with a given protocol and qos
*
* @param topic String to which message needs to be published
* @param message Byte message that needs to be published
}
/**
- * Method to publish message to a specific topic
+ * Method to publish message to a specific topic with a given protocol, qos, and retain
*
* @param topic String to which message needs to be published
* @param message Byte message that needs to be published
}
/**
- * Method to publish message to a specific topic
+ * Method to publish message to a specific topic with protocols, qos, and retain
*
* @param topic String to which message needs to be published
* @param message Byte message that needs to be published
* @param retain Boolean to decide whether or not the message should be retained by the broker
*/
public void publish(String topic, byte[] message, EnumSet<Protocol> protocols, QoS qos, boolean retain) {
-
checkParams(topic, protocols);
- int jniProtocols = 0;
+ int jniProtocols = 0;
for (Protocol p : protocols) {
- if (!classifyProtocol(p)) {
+ if (isUsingNativePubSub(p)) {
jniProtocols += p.getValue();
protocols.remove(p);
}
}
- if (jniProtocols > 0) {
+ if (jniProtocols > 0)
mJniInterface.publish(topic, message, message.length, jniProtocols, qos.ordinal(), retain);
- }
+ publishTransportProtocols(topic, message, protocols);
+ }
+
+ /**
+ * Method to publish message to a specific topic with transport protocols
+ *
+ * @param topic String to which message needs to be published
+ * @param message Byte message that needs to be published
+ * @param protocols Protocols to be used to publish message
+ */
+ private void publishTransportProtocols(String topic, byte[] message, EnumSet<Protocol> protocols) {
for (Protocol protocol : protocols) {
try {
synchronized (this) {
- if (!publishTable.containsKey(topic)) {
- Log.e(TAG, "Invalid publish request over unsubscribed topic");
- return;
- }
- HostTable hostTable = publishTable.get(topic);
- if (hostTable == null) {
- Log.d(TAG, "Host table for topic [" + topic + "] is null.");
- continue;
- }
+ HostTable hostTable = getHostTable(topic);
for (String hostIp : hostTable.hostMap.keySet()) {
PortTable portTable = hostTable.hostMap.get(hostIp);
if (portTable == null) {
}
}
} catch (Exception e) {
- Log.e(TAG, "Error during publish", e);
+ Log.e(TAG, "Error during publishing transport protocols", e);
}
}
}
+ /**
+ * Method to get a host table related to a specific topic
+ *
+ * @param topic String to which message needs to be published
+ */
+ private HostTable getHostTable(String topic) {
+ if (!publishTable.containsKey(topic))
+ throw new IllegalArgumentException("Invalid publish request over an unsubscribed topic");
+
+ HostTable hostTable = publishTable.get(topic);
+ if (hostTable == null)
+ throw new IllegalArgumentException("Host table for topic [" + topic + "] is null.");
+
+ return hostTable;
+ }
+
// TODO: Update publish with proper stream interface.
public boolean publish(AittStream stream, String topic, byte[] message, Protocol protocol) {
if (stream == null) {
try {
synchronized (this) {
- if (!publishTable.containsKey(topic)) {
- Log.e(TAG, "Invalid publish request over unsubscribed topic");
- return false;
- }
- HostTable hostTable = publishTable.get(topic);
- if (hostTable == null) {
- Log.d(TAG, "Host table for topic [" + topic + "] is null.");
- return false;
- }
+ HostTable hostTable = getHostTable(topic);
for (String hostIp : hostTable.hostMap.keySet()) {
PortTable portTable = hostTable.hostMap.get(hostIp);
if (portTable == null) {
* Method to differentiate android specific protocol
*
* @param protocols Protocol to be classified
- * @return true if the protocol is specific to android implementation
+ * @return true if the protocol is using native publish/subscribe
*/
- private boolean classifyProtocol(Protocol protocols) {
- return protocols.equals(Protocol.WEBRTC) || protocols.equals(Protocol.IPC) || protocols.equals(Protocol.RTSP);
+ private boolean isUsingNativePubSub(Protocol protocols) {
+ return protocols.equals(Protocol.MQTT) || protocols.equals(Protocol.TCP) || protocols.equals(Protocol.TCP_SECURE);
}
/**
*
* @param topic String to which applications can subscribe, to receive data
* @param callback Callback object specific to a subscribe call
- * @param protocols Protocol supported by application, invoking subscribe
+ * @param protocols Protocols supported by application, invoking subscribe
* @param qos QoS at which the message should be delivered
*/
public void subscribe(String topic, SubscribeCallback callback, EnumSet<Protocol> protocols, QoS qos) {
-
checkParams(topic, protocols);
- if (callback == null) {
+ if (callback == null)
throw new IllegalArgumentException("Invalid callback");
- }
int jniProtocols = 0;
for (Protocol p : protocols) {
- if (!classifyProtocol(p)) {
+ if (isUsingNativePubSub(p)) {
jniProtocols += p.getValue();
protocols.remove(p);
}
/**
* Method to verify Aitt pub & sub parameters
*
- * @param topic String to which applications can subscribe, to receive data
- * @param protocols Protocol supported by application, invoking subscribe
+ * @param topic String to which applications can publish or subscribe data
+ * @param protocols Protocols which indicate the way to publish or subscribe data
*/
private void checkParams(String topic, EnumSet<Protocol> protocols) {
- if (topic == null || topic.isEmpty()) {
+ if (topic == null || topic.isEmpty())
throw new IllegalArgumentException(INVALID_TOPIC);
- }
- if (protocols.isEmpty()) {
+ if (protocols.isEmpty())
throw new IllegalArgumentException("Invalid protocols");
- }
}
/**
try {
ArrayList<SubscribeCallback> cbList = subscribeCallbacks.get(topic);
- if (cbList != null) {
- // check whether the list already contains same callback
- if (!cbList.contains(callback)) {
- cbList.add(callback);
- }
+ if (cbList != null && !cbList.contains(callback)) {
+ cbList.add(callback);
} else {
cbList = new ArrayList<>();
cbList.add(callback);
* @param topic String topic to which application had subscribed
*/
public void unsubscribe(String topic) {
- if (topic == null || topic.isEmpty()) {
+ if (topic == null || topic.isEmpty())
throw new IllegalArgumentException(INVALID_TOPIC);
- }
boolean isRemoved = false;
try {
}
if (!isRemoved) {
- Long paittSubId = null;
+ Long pAittSubId = null;
synchronized (this) {
- if (aittSubId.containsKey(topic)) {
- paittSubId = aittSubId.get(topic);
- }
- }
- if (paittSubId != null) {
- mJniInterface.unsubscribe(topic, paittSubId);
+ if (aittSubId.containsKey(topic))
+ pAittSubId = aittSubId.get(topic);
}
+ if (pAittSubId != null)
+ mJniInterface.unsubscribe(topic, pAittSubId);
}
synchronized (this) {
try {
ByteBuffer buffer = ByteBuffer.wrap(payload);
FlexBuffers.Map map = FlexBuffers.getRoot(buffer).asMap();
- String host = map.get("host").asString();
+ String host = map.get(HOST_STRING).asString();
String status = map.get(Definitions.STATUS).asString();
if (status != null && status.compareTo(Definitions.WILL_LEAVE_NETWORK) == 0) {
synchronized (this) {
for (Map.Entry<String, HostTable> entry : publishTable.entrySet()) {
HostTable hostTable = entry.getValue();
- if (hostTable != null) {
+ if (hostTable != null)
hostTable.hostMap.remove(host);
- }
}
}
return;
FlexBuffers.KeyVector topics = map.keys();
for (int i = 0; i < topics.size(); i++) {
String _topic = topics.get(i).toString();
- if (_topic.compareTo("host") == 0 || _topic.compareTo(Definitions.STATUS) == 0) {
+ if (HOST_STRING.compareTo(_topic) == 0 || Definitions.STATUS.compareTo(_topic) == 0)
continue;
- }
FlexBuffers.Map _map = map.get(_topic).asMap();
int port = _map.get("port").asInt();
* @param topic The topic to which, other parties have subscribed to
* @param host String which specifies a particular host
* @param port Port of the party which subscribed to given topic
- * @param protocol protocol supported by the party which subscribed to given topic
+ * @param protocol Protocol supported by the party which subscribed to given topic
*/
private void updatePublishTable(String topic, String host, int port, Protocol protocol) {
synchronized (this) {
}
// TODO: Handle multiple ports with the same topic.
- if (portTable.portMap.isEmpty() == false) {
+ if (!portTable.portMap.isEmpty()) {
StringBuilder portLists = new StringBuilder();
for (int p : portTable.portMap.keySet()) {
portLists.append(p);
String topic = message.getTopic();
synchronized (this) {
ArrayList<SubscribeCallback> cbList = subscribeCallbacks.get(topic);
-
- if (cbList != null) {
- for (int i = 0; i < cbList.size(); i++) {
- cbList.get(i).onMessageReceived(message);
- }
+ if (cbList == null) {
+ Log.d(TAG, "Subscribe callback list is null.");
+ return;
}
+
+ cbList.forEach(subscribeCallback -> subscribeCallback.onMessageReceived(message));
}
} catch (Exception e) {
Log.e(TAG, "Error during messageReceived", e);
}
}
+ /**
+ * Method that receives message from JNI layer for topics other than discovery topics
+ *
+ * @param protocol The data received from JNI layer to be sent to application layer
+ * @param topic The data received from JNI layer to be sent to application layer
+ * @param streamRole The data received from JNI layer to be sent to application layer
+ */
public AittStream createStream(Protocol protocol, String topic, AittStream.StreamRole streamRole) {
ModuleHandler moduleHandler = createModuleHandler(protocol);
- if (moduleHandler != null && protocol == Protocol.WEBRTC) {
- WebRTCStream webRTCStream = (WebRTCStream) ((WebRTCHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
- if (webRTCStream != null && streamRole == AittStream.StreamRole.SUBSCRIBER) {
- webRTCStream.setSelfIP(ip);
- webRTCStream.setJNIInterface(mJniInterface);
- }
-
- return webRTCStream;
+ if (moduleHandler == null) {
+ Log.e(TAG, "Fail to create a module handler.");
+ return null;
}
- if (moduleHandler != null && protocol == Protocol.RTSP) {
- RTSPStream rtspStream = (RTSPStream) ((RTSPHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
- if (rtspStream != null) {
- rtspStream.setJNIInterface(mJniInterface);
- }
- return rtspStream;
+ switch (protocol) {
+ case WEBRTC:
+ WebRTCStream webRTCStream = (WebRTCStream) ((WebRTCHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
+ if (webRTCStream != null && streamRole == AittStream.StreamRole.SUBSCRIBER) {
+ webRTCStream.setSelfIP(ip);
+ webRTCStream.setJNIInterface(mJniInterface);
+ }
+ return webRTCStream;
+ case RTSP:
+ RTSPStream rtspStream = (RTSPStream) ((RTSPHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
+ if (rtspStream != null)
+ rtspStream.setJNIInterface(mJniInterface);
+ return rtspStream;
+ default:
+ Log.d(TAG, "Not supported yet.");
+ break;
}
return null;
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.close();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(null);
aitt.close();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
byte[] payload = message.getBytes();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
aitt.connect(brokerIp, port);
+
String _topic = "";
byte[] payload = message.getBytes();
-
assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload));
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
byte[] payload = message.getBytes();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
aitt.connect(brokerIp, port);
+
String _topic = "";
byte[] payload = message.getBytes();
-
assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE, false));
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
byte[] payload = message.getBytes();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
aitt.connect(brokerIp, port);
+
String _topic = "";
byte[] payload = message.getBytes();
-
assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, Aitt.Protocol.IPC, Aitt.QoS.AT_MOST_ONCE, false));
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
byte[] payload = message.getBytes();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
aitt.connect(brokerIp, port);
+
String _topic = "";
byte[] payload = message.getBytes();
-
assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE, false));
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
byte[] payload = message.getBytes();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
+
String _topic = "";
byte[] payload = message.getBytes();
EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
-
assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, protocols, Aitt.QoS.AT_MOST_ONCE, false));
aitt.disconnect();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
aitt.connect(brokerIp, port);
+
byte[] payload = message.getBytes();
EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
-
assertThrows(IllegalArgumentException.class, () -> aitt.publish(topic, payload, protocols, Aitt.QoS.AT_MOST_ONCE, false));
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.subscribe(topic, message -> {
aitt.connect(brokerIp, port);
String _topic = "";
-
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
}));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.subscribe(topic, message -> {
aitt.connect(brokerIp, port);
String _topic = "";
-
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
}, Aitt.Protocol.TCP));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, Aitt.Protocol.TCP));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.subscribe(topic, message -> {
aitt.connect(brokerIp, port);
String _topic = "";
-
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
}, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.subscribe(topic, message -> {
aitt.connect(brokerIp, port);
String _topic = "";
-
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
}, Aitt.Protocol.IPC, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
aitt.subscribe(topic, message -> {
aitt.connect(brokerIp, port);
String _topic = "";
-
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
}, Aitt.Protocol.TCP, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, Aitt.Protocol.TCP, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
- EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
+ EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, message -> {
}, protocols, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
+
EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, protocols, Aitt.QoS.AT_MOST_ONCE));
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
-
aitt.subscribe(topic, message -> {
});
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
aitt.connect(brokerIp, port);
- String _topic = "";
+ String _topic = "";
assertThrows(IllegalArgumentException.class, () -> aitt.unsubscribe(_topic));
aitt.disconnect();
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
- assertNotNull("Aitt Instance not null", aitt);
aitt.setConnectionCallback(new Aitt.ConnectionCallback() {
@Override
public void onConnected() {
public void onConnectionFailed() {
}
});
- aitt.connect(brokerIp, port);
+ aitt.connect(brokerIp, port);
aitt.disconnect();
} catch (Exception e) {
fail("Failed testSetConnectionCallback " + e);
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
Aitt.SubscribeCallback callback1 = message -> {
};
+ aitt.subscribe(topic, callback1);
Aitt.SubscribeCallback callback2 = message -> {
};
-
- aitt.subscribe(topic, callback1);
aitt.subscribe(topic, callback2);
aitt.disconnect();
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
int counter = 1;
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testDiscoveryMessageCallback " + e);
+ fail("Failed testDiscoveryMessageCallbackConnected " + e);
}
}
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
int counter = 1;
counter = 6;
byte[] disconnectMessage = createDiscoveryMessage(counter);
messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, disconnectMessage);
+
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testDiscoveryMessageCallback " + e);
+ fail("Failed testDiscoveryMessageCallbackDisconnected " + e);
}
}
try {
shadowJniInterface.setInitReturn(true);
Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
aitt.connect(brokerIp, port);
byte[] discoveryMessage = new byte[0];
String receivedTopic = aittMessage.getTopic();
assertEquals("Received topic and subscribed topic are equal", receivedTopic, topic);
});
-
messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testSubscribeCallback " + e);
+ fail("Failed testSubscribeCallbackVerifyTopic " + e);
}
}
String receivedMessage = new String(aittMessage.getPayload(), StandardCharsets.UTF_8);
assertEquals("Received message and sent message are equal", message, receivedMessage);
});
-
messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testSubscribeCallback " + e);
+ fail("Failed testSubscribeCallbackVerifyPayload " + e);
}
}
private static final String TAG = "AITT-ANDROID";
private static final String AITT_ID = "AITT_ANDROID";
- private static final String BROKER_IP = "192.168.1.59"; // TODO: Replace with 'localhost' value.
private static final int PORT = 1883;
private static final String TEST_TOPIC = "android/test/tcp";
private static final String TEST_MESSAGE = "This is a test message for TCP protocol.";
private static final String ERROR_MESSAGE_AITT_NULL = "An AITT instance is null.";
+ private static String brokerIp;
+
@BeforeClass
public static void initialize() {
appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+ // IMPORTANT NOTE: Should give test arguments as follows.
+ // if using Android studio: Run -> Edit Configurations -> Find 'Instrumentation arguments'
+ // -> press '...' button -> add the name as "brokerIp" and the value
+ // (Broker WiFi IP) of broker argument
+ // if using gradlew commands: Add "-e brokerIp [Broker WiFi IP]"
+ brokerIp = InstrumentationRegistry.getArguments().getString("brokerIp");
}
@Test
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
byte[] payload = TEST_MESSAGE.getBytes();
aitt.publish(TEST_TOPIC, payload, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE, false);
} catch (Exception e) {
- fail("Failed to execute testPublishWithTCP_P, (" + e + ")");
+ fail("Failed to execute testPublishWithTCP, (" + e + ")");
}
}
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
String _topic = "";
byte[] payload = TEST_MESSAGE.getBytes();
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testPublishWithTCPInvalidTopic_N, (" + e + ")");
+ fail("Failed testPublishWithTCPInvalidTopic, (" + e + ")");
}
}
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
aitt.subscribe(TEST_TOPIC, message -> Log.i(TAG, "A subscription callback is called."), Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE);
aitt.disconnect();
} catch (Exception e) {
- fail("Failed to execute testSubscribeWithTCP_P, (" + e + ")");
+ fail("Failed to execute testSubscribeWithTCP, (" + e + ")");
}
}
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
Aitt.SubscribeCallback callback1 = message -> {
};
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testSubscribeWithTCPMultipleCallbacks_P, (" + e + ")");
+ fail("Failed testSubscribeWithTCPMultipleCallbacks, (" + e + ")");
}
}
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
String _topic = "";
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testSubscribeWithTCPInvalidTopic_N, (" + e + ")");
+ fail("Failed testSubscribeWithTCPInvalidTopic, (" + e + ")");
}
}
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
String _topic = "topic";
assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, null, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE));
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testSubscribeWithTCPInvalidCallback_N, (" + e + ")");
+ fail("Failed testSubscribeWithTCPInvalidCallback, (" + e + ")");
}
}
try {
Aitt aitt = new Aitt(appContext, AITT_ID);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
aitt.subscribe(TEST_TOPIC, message -> {
}, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE);
aitt.disconnect();
} catch (Exception e) {
- fail("Failed testUnsubscribe_P, (" + e + ")");
+ fail("Failed testUnsubscribe, (" + e + ")");
}
}
String wifiIp = wifiIpAddress();
Aitt aitt = new Aitt(appContext, AITT_ID, wifiIp, false);
assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
- aitt.connect(BROKER_IP, PORT);
+ aitt.connect(brokerIp, PORT);
AtomicBoolean message_received = new AtomicBoolean(false);
byte[] payload = TEST_MESSAGE.getBytes();
Assert.assertTrue(message_received.get());
} catch (Exception e) {
- fail("Failed to execute testPublishSubscribeWithTCP_P, (" + e + ")");
+ fail("Failed to execute testPublishSubscribeWithTCP, (" + e + ")");
}
}