*/
public interface ConnectionCallback {
void onConnected();
+
void onDisconnected();
+
void onConnectionFailed();
}
/**
* Aitt constructor to create AITT object
+ *
* @param appContext context of the application invoking the constructor
- * @param id Unique identifier for the Aitt instance
+ * @param id Unique identifier for the Aitt instance
*/
public Aitt(Context appContext, String id) throws InstantiationException {
this(appContext, id, Definitions.AITT_LOCALHOST, false);
/**
* Aitt constructor to create AITT object
- * @param appContext context of the application invoking the constructor
- * @param id Unique identifier for the Aitt instance
- * @param ip IP address of the device, on which application is running
+ *
+ * @param appContext context of the application invoking the constructor
+ * @param id Unique identifier for the Aitt instance
+ * @param ip IP address of the device, on which application is running
* @param clearSession "clear" the current session when the client disconnects
*/
public Aitt(Context appContext, String id, String ip, boolean clearSession) throws InstantiationException {
}
this.ip = ip;
this.appContext = appContext;
- mJniInterface.registerJniCallback(new JniInterface.JniCallback(){
+ mJniInterface.registerJniCallback(new JniInterface.JniCallback() {
@Override
public void jniDataPush(String _topic, byte[] payload) {
messageCallback(_topic, payload);
/**
* Method to set connection status callback
+ *
* @param callback ConnectionCallback to which status should be updated
*/
public void setConnectionCallback(ConnectionCallback callback) {
throw new IllegalArgumentException("Invalid callback");
}
connectionCallback = callback;
- mJniInterface.setConnectionCallback(new JniInterface.JniConnectionCallback(){
+ mJniInterface.setConnectionCallback(new JniInterface.JniConnectionCallback() {
@Override
public void jniConnectionCB(int status) {
connectionStatusCallback(status);
/**
* Method to connect to MQTT broker
+ *
* @param brokerIp Broker IP address to which, device has to connect
*/
public void connect(@Nullable String brokerIp) {
/**
* Method to connect to MQTT broker
+ *
* @param brokerIp Broker IP address to which, device has to connect
- * @param port Broker port number to which, device has to connect
+ * @param port Broker port number to which, device has to connect
*/
public void connect(@Nullable String brokerIp, int port) {
if (brokerIp == null || brokerIp.isEmpty()) {
/**
* Method to publish message to a specific topic
- * @param topic String to which message needs to be published
+ *
+ * @param topic String to which message needs to be published
* @param message Byte message that needs to be published
*/
public void publish(String topic, byte[] message) {
/**
* Method to publish message to a specific topic
- * @param topic String to which message needs to be published
- * @param message Byte message that needs to be published
+ *
+ * @param topic String to which message needs to be published
+ * @param message Byte message that needs to be published
* @param protocol Protocol to be used to publish message
- * @param qos QoS at which the message should be delivered
- * @param retain Boolean to decide whether or not the message should be retained by the broker
+ * @param qos QoS at which the message should be delivered
+ * @param retain Boolean to decide whether or not the message should be retained by the broker
*/
public void publish(String topic, byte[] message, Protocol protocol, QoS qos, boolean retain) {
EnumSet<Protocol> protocolSet = EnumSet.of(protocol);
/**
* Method to publish message to a specific topic
- * @param topic String to which message needs to be published
- * @param message Byte message that needs to be published
+ *
+ * @param topic String to which message needs to be published
+ * @param message Byte message that needs to be published
* @param protocols Protocol to be used to publish message
- * @param qos QoS at which the message should be delivered
- * @param retain Boolean to decide whether or not the message should be retained by the broker
+ * @param qos QoS at which the message should be delivered
+ * @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) {
}
}
- if(jniProtocols > 0) {
+ if (jniProtocols > 0) {
mJniInterface.publish(topic, message, message.length, jniProtocols, qos.ordinal(), retain);
}
- for(Protocol pro : protocols) {
+ for (Protocol pro : protocols) {
try {
synchronized (this) {
if (!publishTable.containsKey(topic)) {
PortTable portTable = hostTable.hostMap.get(hostIp);
for (Integer port : portTable.portMap.keySet()) {
Object transportHandler = portTable.portMap.get(port).second;
- if(portTable.portMap.get(port).first == pro)
+ if (portTable.portMap.get(port).first == pro)
publishHandler(pro, portTable, topic, transportHandler, hostIp, port, message);
}
}
/**
* Method to create transportHandler and publish message based on protocol
- * @param protocol protocol using which data needs to be published
- * @param portTable portTable has information about port and associated protocol with transport Handler object
- * @param topic The topic to which data is published
+ *
+ * @param protocol protocol using which data needs to be published
+ * @param portTable portTable has information about port and associated protocol with transport Handler object
+ * @param topic The topic to which data is published
* @param transportHandlerObject transportHandler object used to publish message
- * @param ip IP address of the destination
- * @param port Port number of the destination
- * @param message Data to be tranferred over WebRTC
+ * @param ip IP address of the destination
+ * @param port Port number of the destination
+ * @param message Data to be tranferred over WebRTC
*/
- private void publishHandler(Protocol protocol, PortTable portTable, String topic, Object transportHandlerObject, String ip, int port, byte[] message){
+ private void publishHandler(Protocol protocol, PortTable portTable, String topic, Object transportHandlerObject, String ip, int port, byte[] message) {
TransportHandler transportHandler;
- if(transportHandlerObject == null){
+ if (transportHandlerObject == null) {
transportHandler = TransportFactory.createTransport(protocol);
- if(transportHandler!=null)
+ if (transportHandler != null)
transportHandler.setAppContext(appContext);
portTable.portMap.replace(port, new Pair<>(protocol, transportHandler));
- }else{
+ } else {
transportHandler = (TransportHandler) transportHandlerObject;
}
- if(transportHandler!=null){
- transportHandler.publish(topic,ip, port, message);
+ if (transportHandler != null) {
+ transportHandler.publish(topic, ip, port, message);
}
}
/**
* Method to differentiate android specific protocol
+ *
* @param protocols Protocol to be classified
* @return true if the protocol is specific to android implementation
*/
- private boolean classifyProtocol(Protocol protocols){
+ private boolean classifyProtocol(Protocol protocols) {
return protocols.equals(Protocol.WEBRTC) || protocols.equals(Protocol.IPC);
}
/**
* Method to subscribe to a specific topic
- * @param topic String to which applications can subscribe, to receive data
+ *
+ * @param topic String to which applications can subscribe, to receive data
* @param callback Callback object specific to a subscribe call
*/
public void subscribe(String topic, SubscribeCallback callback) {
/**
* Method to subscribe to a specific topic
- * @param topic String to which applications can subscribe, to receive data
+ *
+ * @param topic String to which applications can subscribe, to receive data
* @param callback Callback object specific to a subscribe call
* @param protocol Protocol supported by application, invoking subscribe
- * @param qos QoS at which the message should be delivered
+ * @param qos QoS at which the message should be delivered
*/
public void subscribe(String topic, SubscribeCallback callback, Protocol protocol, QoS qos) {
EnumSet<Protocol> protocolSet = EnumSet.of(protocol);
/**
* Method to subscribe to a specific topic
- * @param topic String to which applications can subscribe, to receive data
- * @param callback Callback object specific to a subscribe call
+ *
+ * @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 qos QoS at which the message should be delivered
+ * @param qos QoS at which the message should be delivered
*/
public void subscribe(String topic, SubscribeCallback callback, EnumSet<Protocol> protocols, QoS qos) {
}
}
- if(jniProtocols > 0) {
+ if (jniProtocols > 0) {
Long pObject = mJniInterface.subscribe(topic, jniProtocols, qos.ordinal());
synchronized (this) {
aittSubId.put(topic, pObject);
}
}
- for(Protocol pro : protocols) {
+ for (Protocol pro : protocols) {
try {
TransportHandler transportHandler = TransportFactory.createTransport(pro);
/**
* Method to verify Aitt pub & sub parameters
- * @param topic String to which applications can subscribe, to receive data
+ *
+ * @param topic String to which applications can subscribe, to receive data
* @param protocols Protocol supported by application, invoking subscribe
*/
- private void checkParams(String topic, EnumSet<Protocol> protocols){
+ private void checkParams(String topic, EnumSet<Protocol> protocols) {
if (topic == null || topic.isEmpty()) {
throw new IllegalArgumentException(INVALID_TOPIC);
}
/**
* Method to map subscribe callback instance to subscribing topic
- * @param topic String to which application can subscribe
+ *
+ * @param topic String to which application can subscribe
* @param callback Subscribe callback instance created during subscribe call
*/
private void addCallBackToSubscribeMap(String topic, SubscribeCallback callback) {
/**
* Method to unsubscribe to a topic, subscribed by application
+ *
* @param topic String topic to which application had subscribed
*/
public void unsubscribe(String topic) {
/**
* Method invoked from JNI layer to Java layer for MQTT connection status update
+ *
* @param status Status of the MQTT connection
- * 0: MQTT Connection disconnected
- * 1: MQTT connection success
- * 2: MQTT connection failed
+ * 0: MQTT Connection disconnected
+ * 1: MQTT connection success
+ * 2: MQTT connection failed
*/
private void connectionStatusCallback(int status) {
switch (status) {
/**
* Method invoked from JNI layer to Java layer for message exchange
- * @param topic Topic to which message callback is called
+ *
+ * @param topic Topic to which message callback is called
* @param payload Byte data shared from JNI layer to Java layer
*/
private void messageCallback(String topic, byte[] payload) {
* "port": 20123,
* },
* }
- */
-
+ */
+
/**
* Method to receive discovery message with device, protocol and other details and update publish table
+ *
* @param payload Byte data having discovery related message
*/
private void discoveryMessageCallback(byte[] payload) {
/**
* Method used to update Publish table of the application
- * @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 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
*/
private void updatePublishTable(String topic, String host, int port, Protocol protocol) {
- synchronized(this) {
+ synchronized (this) {
if (!publishTable.containsKey(topic)) {
PortTable portTable = new PortTable();
portTable.portMap.put(port, new Pair(protocol, null));
/**
* Method that receives message from JNI layer for topics other than discovery topics
+ *
* @param message The data received from JNI layer to be sent to application layer
*/
private void messageReceived(AittMessage message) {
*/
public void close() {
synchronized (this) {
- if(subscribeCallbacks!=null) {
+ if (subscribeCallbacks != null) {
subscribeCallbacks.clear();
subscribeCallbacks = null;
}
- if(aittSubId!=null) {
+ if (aittSubId != null) {
aittSubId.clear();
aittSubId = null;
}
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowJniInterface.class)
public class AittUnitTest {
- @Mock
- private final Context appContext = mock(Context.class);
-
- private static final int DISCOVERY_MESSAGES_COUNT = 6;
- private final String brokerIp = "192.168.0.1";
- private final int port = 1803;
- private final String topic = "aitt/test";
- private final String message = "test message";
- private final String aittId = "aitt";
-
- ShadowJniInterface shadowJniInterface = new ShadowJniInterface();
-
- private Method messageCallbackMethod;
-
- @Before
- public void initialize() {
- try {
- messageCallbackMethod = Aitt.class.getDeclaredMethod("messageCallback", String.class, byte[].class);
- messageCallbackMethod.setAccessible(true);
- } catch(Exception e) {
- fail("Failed to Initialize " + e);
- }
- }
-
- private byte[] createDiscoveryMessage(int count) {
- int start;
- FlexBuffersBuilder builder = new FlexBuffersBuilder(ByteBuffer.allocate(512));
- start = builder.startMap();
- switch (count) {
- case 1:
- /*
- * {
- * "status": "connected",
- * "host": "127.0.0.1",
- * "aitt/topic1": {
- * "protocol": TCP,
- * "port": 1000,
- * }
- * }
- */
- builder.putString("status", Definitions.JOIN_NETWORK);
- builder.putString("host", Definitions.AITT_LOCALHOST);
- int secondStart = builder.startMap();
- builder.putInt("port", 1000);
- builder.putInt("protocol", Aitt.Protocol.TCP.getValue());
- builder.endMap("aitt/topic1", secondStart);
- break;
- case 2:
- /*
- * {
- * "status": "connected",
- * "host": "127.0.0.2",
- * "aitt/topic1": {
- * "protocol": MQTT,
- * "port": 2000,
- * }
- * }
- */
- builder.putString("status", Definitions.JOIN_NETWORK);
- builder.putString("host", "127.0.0.2");
- secondStart = builder.startMap();
- builder.putInt("port", 2000);
- builder.putInt("protocol", Aitt.Protocol.MQTT.getValue());
- builder.endMap("aitt/topic1", secondStart);
- break;
- case 3:
- /*
- * {
- * "status": "connected",
- * "host": "127.0.0.1",
- * "aitt/topic2": {
- * "protocol": MQTT,
- * "port": 2000,
- * }
- * }
- */
- builder.putString("status", Definitions.JOIN_NETWORK);
- builder.putString("host", Definitions.AITT_LOCALHOST);
- secondStart = builder.startMap();
- builder.putInt("port", 2000);
- builder.putInt("protocol", Aitt.Protocol.MQTT.getValue());
- builder.endMap("aitt/topic2", secondStart);
- break;
- case 4:
- /*
- * {
- * "status": "connected",
- * "host": "127.0.0.1",
- * "aitt/topic2": {
- * "protocol": TCP,
- * "port": 4000,
- * }
- * }
- */
- builder.putString("status", Definitions.JOIN_NETWORK);
- builder.putString("host", Definitions.AITT_LOCALHOST);
- secondStart = builder.startMap();
- builder.putInt("port", 4000);
- builder.putInt("protocol", Aitt.Protocol.TCP.getValue());
- builder.endMap("aitt/topic2", secondStart);
- break;
- case 5:
- /*
- * {
- * "status": "connected",
- * "host": "127.0.0.1",
- * "aitt/topic2": {
- * "protocol": WEBRTC,
- * "port": 2000,
- * }
- * }
- */
- builder.putString("status", Definitions.JOIN_NETWORK);
- builder.putString("host", Definitions.AITT_LOCALHOST);
- secondStart = builder.startMap();
- builder.putInt("port", 2000);
- builder.putInt("protocol", Aitt.Protocol.WEBRTC.getValue());
- builder.endMap("aitt/topic2", secondStart);
- break;
- case 6:
- /*
- * {
- * "status": "disconnected",
- * "host": "127.0.0.1",
- * "aitt/topic1": {
- * "protocol": TCP,
- * "port": 1000,
- * }
- * }
- */
- builder.putString("status", Definitions.WILL_LEAVE_NETWORK);
- builder.putString("host", Definitions.AITT_LOCALHOST);
- secondStart = builder.startMap();
- builder.putInt("port", 1000);
- builder.putInt("protocol", Aitt.Protocol.TCP.getValue());
- builder.endMap("aitt/topic1", secondStart);
- break;
- default:
- return null;
- }
- builder.endMap(null, start);
- ByteBuffer bb = builder.finish();
- byte[] array = new byte[bb.remaining()];
- bb.get(array,0,array.length);
- return array;
- }
-
- @Test
- public void testAittConstructor_P(){
- String id = "aitt";
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, id);
- assertNotNull("Aitt Instance not null", aitt);
- } catch(Exception e) {
- fail("Failed testInitialize " + e);
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testInitializeInvalidId_N() {
- String _id = "";
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, _id);
- aitt.close();
- } catch(InstantiationException e) {
- fail("Error during testInitializeInvalidId" + e);
- }
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testInitializeInvalidContext_N() {
- String _id = "";
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(null, _id);
- aitt.close();
- } catch(InstantiationException e) {
- fail("Error during testInitializeInvalidContext" + e);
- }
- }
-
- @Test(expected = InstantiationException.class)
- public void testConstructorFail_N() throws InstantiationException {
- shadowJniInterface.setInitReturn(false);
- String id = "aitt";
- Aitt aitt = new Aitt(appContext,id);
- aitt.close();
- }
-
- @Test
- public void testConnect_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- aitt.close();
- } catch(Exception e) {
- fail("Failed testConnect " + e);
- }
- }
-
- @Test
- public void testConnectWithoutIP_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(null);
-
- aitt.close();
- } catch(Exception e) {
- fail("Failed testConnectWithoutIP " + e);
- }
- }
-
- @Test
- public void testDisconnect_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testDisconnect " + e);
- }
- }
-
- @Test
- public void testPublishMqtt_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- byte[] payload = message.getBytes();
- aitt.publish(topic, payload);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testPublishMqtt " + e);
- }
- }
-
- @Test
- public void testPublishWebRTC_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- byte[] payload = message.getBytes();
- aitt.publish(topic, payload, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE, false);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testPublishWebRTC " + e);
- }
- }
-
- @Test
- public void testPublishInvalidTopic_N(){
- try {
- 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();
- } catch(Exception e){
- fail("Failed testPublishInvalidTopic" + e);
- }
- }
-
- @Test
- public void testPublishAnyProtocol_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- byte[] payload = message.getBytes();
- aitt.publish(topic, payload, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE, false);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testPublishAnyProtocol " + e);
- }
- }
-
- @Test
- public void testPublishProtocolSet_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- byte[] payload = message.getBytes();
- EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
- aitt.publish(topic, payload, protocols, Aitt.QoS.AT_MOST_ONCE, false);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testPublishProtocolSet " + e);
- }
- }
-
- @Test
- public void testPublishInvalidProtocol_N(){
- try{
- 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();
- } catch(Exception e){
- fail("Failed testPublishInvalidProtocol" + e);
- }
- }
-
- @Test
- public void testSubscribeMqtt_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage message) {
- String _topic = message.getTopic();
- byte[] payload = message.getPayload();
- }
- });
+ @Mock
+ private final Context appContext = mock(Context.class);
+
+ private static final int DISCOVERY_MESSAGES_COUNT = 6;
+ private final String brokerIp = "192.168.0.1";
+ private final int port = 1803;
+ private final String topic = "aitt/test";
+ private final String message = "test message";
+ private final String aittId = "aitt";
+
+ ShadowJniInterface shadowJniInterface = new ShadowJniInterface();
+
+ private Method messageCallbackMethod;
+
+ @Before
+ public void initialize() {
+ try {
+ messageCallbackMethod = Aitt.class.getDeclaredMethod("messageCallback", String.class, byte[].class);
+ messageCallbackMethod.setAccessible(true);
+ } catch (Exception e) {
+ fail("Failed to Initialize " + e);
+ }
+ }
+
+ private byte[] createDiscoveryMessage(int count) {
+ int start;
+ FlexBuffersBuilder builder = new FlexBuffersBuilder(ByteBuffer.allocate(512));
+ start = builder.startMap();
+ switch (count) {
+ case 1:
+ /*
+ * {
+ * "status": "connected",
+ * "host": "127.0.0.1",
+ * "aitt/topic1": {
+ * "protocol": TCP,
+ * "port": 1000,
+ * }
+ * }
+ */
+ builder.putString("status", Definitions.JOIN_NETWORK);
+ builder.putString("host", Definitions.AITT_LOCALHOST);
+ int secondStart = builder.startMap();
+ builder.putInt("port", 1000);
+ builder.putInt("protocol", Aitt.Protocol.TCP.getValue());
+ builder.endMap("aitt/topic1", secondStart);
+ break;
+ case 2:
+ /*
+ * {
+ * "status": "connected",
+ * "host": "127.0.0.2",
+ * "aitt/topic1": {
+ * "protocol": MQTT,
+ * "port": 2000,
+ * }
+ * }
+ */
+ builder.putString("status", Definitions.JOIN_NETWORK);
+ builder.putString("host", "127.0.0.2");
+ secondStart = builder.startMap();
+ builder.putInt("port", 2000);
+ builder.putInt("protocol", Aitt.Protocol.MQTT.getValue());
+ builder.endMap("aitt/topic1", secondStart);
+ break;
+ case 3:
+ /*
+ * {
+ * "status": "connected",
+ * "host": "127.0.0.1",
+ * "aitt/topic2": {
+ * "protocol": MQTT,
+ * "port": 2000,
+ * }
+ * }
+ */
+ builder.putString("status", Definitions.JOIN_NETWORK);
+ builder.putString("host", Definitions.AITT_LOCALHOST);
+ secondStart = builder.startMap();
+ builder.putInt("port", 2000);
+ builder.putInt("protocol", Aitt.Protocol.MQTT.getValue());
+ builder.endMap("aitt/topic2", secondStart);
+ break;
+ case 4:
+ /*
+ * {
+ * "status": "connected",
+ * "host": "127.0.0.1",
+ * "aitt/topic2": {
+ * "protocol": TCP,
+ * "port": 4000,
+ * }
+ * }
+ */
+ builder.putString("status", Definitions.JOIN_NETWORK);
+ builder.putString("host", Definitions.AITT_LOCALHOST);
+ secondStart = builder.startMap();
+ builder.putInt("port", 4000);
+ builder.putInt("protocol", Aitt.Protocol.TCP.getValue());
+ builder.endMap("aitt/topic2", secondStart);
+ break;
+ case 5:
+ /*
+ * {
+ * "status": "connected",
+ * "host": "127.0.0.1",
+ * "aitt/topic2": {
+ * "protocol": WEBRTC,
+ * "port": 2000,
+ * }
+ * }
+ */
+ builder.putString("status", Definitions.JOIN_NETWORK);
+ builder.putString("host", Definitions.AITT_LOCALHOST);
+ secondStart = builder.startMap();
+ builder.putInt("port", 2000);
+ builder.putInt("protocol", Aitt.Protocol.WEBRTC.getValue());
+ builder.endMap("aitt/topic2", secondStart);
+ break;
+ case 6:
+ /*
+ * {
+ * "status": "disconnected",
+ * "host": "127.0.0.1",
+ * "aitt/topic1": {
+ * "protocol": TCP,
+ * "port": 1000,
+ * }
+ * }
+ */
+ builder.putString("status", Definitions.WILL_LEAVE_NETWORK);
+ builder.putString("host", Definitions.AITT_LOCALHOST);
+ secondStart = builder.startMap();
+ builder.putInt("port", 1000);
+ builder.putInt("protocol", Aitt.Protocol.TCP.getValue());
+ builder.endMap("aitt/topic1", secondStart);
+ break;
+ default:
+ return null;
+ }
+ builder.endMap(null, start);
+ ByteBuffer bb = builder.finish();
+ byte[] array = new byte[bb.remaining()];
+ bb.get(array, 0, array.length);
+ return array;
+ }
+
+ @Test
+ public void testAittConstructor_P() {
+ String id = "aitt";
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, id);
+ assertNotNull("Aitt Instance not null", aitt);
+ } catch (Exception e) {
+ fail("Failed testInitialize " + e);
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testInitializeInvalidId_N() {
+ String _id = "";
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, _id);
+ aitt.close();
+ } catch (InstantiationException e) {
+ fail("Error during testInitializeInvalidId" + e);
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testInitializeInvalidContext_N() {
+ String _id = "";
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(null, _id);
+ aitt.close();
+ } catch (InstantiationException e) {
+ fail("Error during testInitializeInvalidContext" + e);
+ }
+ }
+
+ @Test(expected = InstantiationException.class)
+ public void testConstructorFail_N() throws InstantiationException {
+ shadowJniInterface.setInitReturn(false);
+ String id = "aitt";
+ Aitt aitt = new Aitt(appContext, id);
+ aitt.close();
+ }
+
+ @Test
+ public void testConnect_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ aitt.close();
+ } catch (Exception e) {
+ fail("Failed testConnect " + e);
+ }
+ }
+
+ @Test
+ public void testConnectWithoutIP_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(null);
+
+ aitt.close();
+ } catch (Exception e) {
+ fail("Failed testConnectWithoutIP " + e);
+ }
+ }
+
+ @Test
+ public void testDisconnect_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testDisconnect " + e);
+ }
+ }
+
+ @Test
+ public void testPublishMqtt_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ byte[] payload = message.getBytes();
+ aitt.publish(topic, payload);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testPublishMqtt " + e);
+ }
+ }
+
+ @Test
+ public void testPublishWebRTC_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ byte[] payload = message.getBytes();
+ aitt.publish(topic, payload, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE, false);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testPublishWebRTC " + e);
+ }
+ }
+
+ @Test
+ public void testPublishInvalidTopic_N() {
+ try {
+ 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();
- } catch(Exception e) {
- fail("Failed testSubscribeMqtt " + e);
- }
- }
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testPublishInvalidTopic" + e);
+ }
+ }
+
+ @Test
+ public void testPublishAnyProtocol_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ byte[] payload = message.getBytes();
+ aitt.publish(topic, payload, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE, false);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testPublishAnyProtocol " + e);
+ }
+ }
+
+ @Test
+ public void testPublishProtocolSet_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ byte[] payload = message.getBytes();
+ EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
+ aitt.publish(topic, payload, protocols, Aitt.QoS.AT_MOST_ONCE, false);
- @Test
- public void testSubscribeWebRTC_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testPublishProtocolSet " + e);
+ }
+ }
+
+ @Test
+ public void testPublishInvalidProtocol_N() {
+ try {
+ 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);
+ });
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testPublishInvalidProtocol" + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeMqtt_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage message) {
+ String _topic = message.getTopic();
+ byte[] payload = message.getPayload();
+ }
+ });
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeMqtt " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeWebRTC_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage message) {
+ String _topic = message.getTopic();
+ byte[] payload = message.getPayload();
+ }
+ },
+ Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeWebRTC " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeInvalidTopic_N() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+ aitt.connect(brokerIp, port);
+
+ String _topic = "";
+
+ assertThrows(IllegalArgumentException.class, () -> {
+ aitt.subscribe(_topic, new Aitt.SubscribeCallback() {
@Override
public void onMessageReceived(AittMessage message) {
- String _topic = message.getTopic();
- byte[] payload = message.getPayload();
}
- },
- Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeWebRTC " + e);
- }
- }
-
- @Test
- public void testSubscribeInvalidTopic_N() {
- try{
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
- aitt.connect(brokerIp, port);
-
- String _topic = "";
-
- assertThrows(IllegalArgumentException.class, () -> {
- aitt.subscribe(_topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage message) {
- }
+ });
});
- });
- aitt.disconnect();
- } catch(Exception e){
- fail("Failed testSubscribeInvalidTopic " + e);
- }
- }
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeInvalidTopic " + e);
+ }
+ }
- @Test
- public void testSubscribeInvalidCallback_N() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
+ @Test
+ public void testSubscribeInvalidCallback_N() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
- aitt.connect(brokerIp, port);
+ aitt.connect(brokerIp, port);
- String _topic = "topic";
+ String _topic = "topic";
- assertThrows(IllegalArgumentException.class, () -> {
- aitt.subscribe(_topic, null);
- });
+ assertThrows(IllegalArgumentException.class, () -> {
+ aitt.subscribe(_topic, null);
+ });
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeInvalidCallback " + e);
- }
- }
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeInvalidCallback " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeAnyProtocol_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage message) {
+ String _topic = message.getTopic();
+ byte[] payload = message.getPayload();
+ }
+ },
+ Aitt.Protocol.UDP, Aitt.QoS.AT_MOST_ONCE);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeAnyProtocol " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeInvalidProtocol_N() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ aitt.connect(brokerIp, port);
+ EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
+
+ assertThrows(IllegalArgumentException.class, () -> {
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage message) {
+ String _topic = message.getTopic();
+ byte[] payload = message.getPayload();
+ }
+ },
+ protocols, Aitt.QoS.AT_MOST_ONCE);
+ });
- @Test
- public void testSubscribeAnyProtocol_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeAnyProtocol " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeProtocolSet_P() {
+ 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);
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage message) {
+ String _topic = message.getTopic();
+ byte[] payload = message.getPayload();
+ }
+ },
+ protocols, Aitt.QoS.EXACTLY_ONCE);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeProtocolSet " + e);
+ }
+ }
+
+ @Test
+ public void testUnsubscribe_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage message) {
+ }
+ });
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
+ aitt.unsubscribe(topic);
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testUnsubscribe " + e);
+ }
+ }
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage message) {
- String _topic = message.getTopic();
- byte[] payload = message.getPayload();
- }
- },
- Aitt.Protocol.UDP, Aitt.QoS.AT_MOST_ONCE);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeAnyProtocol " + e);
- }
- }
-
- @Test
- public void testSubscribeInvalidProtocol_N() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- aitt.connect(brokerIp, port);
- EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
-
- assertThrows(IllegalArgumentException.class, () -> {
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage message) {
- String _topic = message.getTopic();
- byte[] payload = message.getPayload();
- }
- },
- protocols, Aitt.QoS.AT_MOST_ONCE);
- });
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeAnyProtocol " + e);
- }
- }
-
- @Test
- public void testSubscribeProtocolSet_P() {
- 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);
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage message) {
- String _topic = message.getTopic();
- byte[] payload = message.getPayload();
- }
- },
- protocols, Aitt.QoS.EXACTLY_ONCE);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeProtocolSet " + e);
- }
- }
-
- @Test
- public void testUnsubscribe_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage message) {
- }
- });
-
- aitt.unsubscribe(topic);
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testUnsubscribe " + e);
- }
- }
-
- @Test
- public void testUnsubscribeInvalidTopic_N() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- aitt.connect(brokerIp, port);
- String _topic = "";
-
- assertThrows(IllegalArgumentException.class, () -> {
- aitt.unsubscribe(_topic);
- });
-
- aitt.disconnect();
- } catch(Exception e){
- fail("Failed testUnsubscribeInvalidTopic " + e);
- }
- }
-
- @Test
- public void testSetConnectionCallback_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.setConnectionCallback(new Aitt.ConnectionCallback() {
- @Override
- public void onConnected() {
- }
+ @Test
+ public void testUnsubscribeInvalidTopic_N() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
- @Override
- public void onDisconnected() {
- }
+ aitt.connect(brokerIp, port);
+ String _topic = "";
- @Override
- public void onConnectionFailed() {
+ assertThrows(IllegalArgumentException.class, () -> {
+ aitt.unsubscribe(_topic);
+ });
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testUnsubscribeInvalidTopic " + e);
+ }
+ }
+
+ @Test
+ public void testSetConnectionCallback_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.setConnectionCallback(new Aitt.ConnectionCallback() {
+ @Override
+ public void onConnected() {
+ }
+
+ @Override
+ public void onDisconnected() {
+ }
+
+ @Override
+ public void onConnectionFailed() {
+ }
+ });
+ aitt.connect(brokerIp, port);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSetConnectionCallback " + e);
+ }
+ }
+
+ @Test
+ public void testSetConnectionCallbackInvalidCallback_N() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertThrows(IllegalArgumentException.class, () -> {
+ aitt.setConnectionCallback(null);
+ });
+
+ aitt.connect(brokerIp, port);
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSetConnectionCallbackInvalidCallback " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeMultipleCallbacks_P() {
+ 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.SubscribeCallback callback2 = message -> {
+ };
+
+ aitt.subscribe(topic, callback1);
+ aitt.subscribe(topic, callback2);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeMultipleCallbacks " + e);
+ }
+ }
+
+ // The test covers different cases of updating the publish table
+ @Test
+ public void testDiscoveryMessageCallbackConnected_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ int counter = 1;
+ while (counter < DISCOVERY_MESSAGES_COUNT) {
+ byte[] discoveryMessage = createDiscoveryMessage(counter);
+ messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) discoveryMessage);
+ counter++;
}
- });
- aitt.connect(brokerIp, port);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSetConnectionCallback " + e);
- }
- }
-
- @Test
- public void testSetConnectionCallbackInvalidCallback_N() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertThrows(IllegalArgumentException.class, () -> {
- aitt.setConnectionCallback(null);
- });
-
- aitt.connect(brokerIp, port);
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSetConnectionCallbackInvalidCallback " + e);
- }
- }
-
- @Test
- public void testSubscribeMultipleCallbacks_P() {
- 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.SubscribeCallback callback2 = message -> {};
-
- aitt.subscribe(topic, callback1);
- aitt.subscribe(topic, callback2);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeMultipleCallbacks " + e);
- }
- }
-
- // The test covers different cases of updating the publish table
- @Test
- public void testDiscoveryMessageCallbackConnected_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- int counter = 1;
- while (counter < DISCOVERY_MESSAGES_COUNT) {
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testDiscoveryMessageCallback " + e);
+ }
+ }
+
+ @Test
+ public void testDiscoveryMessageCallbackDisconnected_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+
+ assertNotNull("Aitt Instance not null", aitt);
+ aitt.connect(brokerIp, port);
+
+ int counter = 1;
byte[] discoveryMessage = createDiscoveryMessage(counter);
messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) discoveryMessage);
- counter++;
- }
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testDiscoveryMessageCallback " + e);
- }
- }
-
- @Test
- public void testDiscoveryMessageCallbackDisconnected_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
-
- assertNotNull("Aitt Instance not null", aitt);
- aitt.connect(brokerIp, port);
-
- int counter = 1;
- byte[] discoveryMessage = createDiscoveryMessage(counter);
- messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) discoveryMessage);
-
- counter = 6;
- byte[] disconnectMessage = createDiscoveryMessage(counter);
- messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) disconnectMessage);
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testDiscoveryMessageCallback " + e);
- }
- }
-
- @Test
- public void testDiscoveryMessageCallbackEmptyPayload_P() {
- 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];
- messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) discoveryMessage);
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testDiscoveryMessageCallbackEmptyPayload " + e);
- }
- }
-
- @Test
- public void testSubscribeCallbackVerifyTopic_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
- aitt.connect(brokerIp, port);
-
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage aittMessage) {
- String recvTopic = aittMessage.getTopic();
- assertEquals("Received topic and subscribed topic are equal", recvTopic, topic);
- }
- });
-
- messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
-
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeCallback " + e);
- }
- }
-
- @Test
- public void testSubscribeCallbackVerifyPayload_P() {
- try {
- shadowJniInterface.setInitReturn(true);
- Aitt aitt = new Aitt(appContext, aittId);
- aitt.connect(brokerIp, port);
-
- aitt.subscribe(topic, new Aitt.SubscribeCallback() {
- @Override
- public void onMessageReceived(AittMessage aittMessage) {
- String recvMessage = new String(aittMessage.getPayload(), StandardCharsets.UTF_8);
- assertEquals("Received message and sent message are equal", message, recvMessage);
- }
- });
- messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
+ counter = 6;
+ byte[] disconnectMessage = createDiscoveryMessage(counter);
+ messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) disconnectMessage);
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testDiscoveryMessageCallback " + e);
+ }
+ }
+
+ @Test
+ public void testDiscoveryMessageCallbackEmptyPayload_P() {
+ 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];
+ messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, (Object) discoveryMessage);
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testDiscoveryMessageCallbackEmptyPayload " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeCallbackVerifyTopic_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+ aitt.connect(brokerIp, port);
+
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage aittMessage) {
+ String recvTopic = aittMessage.getTopic();
+ assertEquals("Received topic and subscribed topic are equal", recvTopic, topic);
+ }
+ });
+
+ messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
+
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeCallback " + e);
+ }
+ }
+
+ @Test
+ public void testSubscribeCallbackVerifyPayload_P() {
+ try {
+ shadowJniInterface.setInitReturn(true);
+ Aitt aitt = new Aitt(appContext, aittId);
+ aitt.connect(brokerIp, port);
+
+ aitt.subscribe(topic, new Aitt.SubscribeCallback() {
+ @Override
+ public void onMessageReceived(AittMessage aittMessage) {
+ String recvMessage = new String(aittMessage.getPayload(), StandardCharsets.UTF_8);
+ assertEquals("Received message and sent message are equal", message, recvMessage);
+ }
+ });
+
+ messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
- aitt.disconnect();
- } catch(Exception e) {
- fail("Failed testSubscribeCallback " + e);
- }
- }
+ aitt.disconnect();
+ } catch (Exception e) {
+ fail("Failed testSubscribeCallback " + e);
+ }
+ }
}
import org.webrtc.VideoSink;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;
+
import static org.webrtc.SessionDescription.Type.ANSWER;
import static org.webrtc.SessionDescription.Type.OFFER;
private boolean isInitiator;
private boolean isChannelReady;
private boolean isStarted;
- private boolean isReciever;
+ private boolean isReceiver;
private PeerConnection peerConnection;
private PeerConnectionFactory factory;
private VideoTrack videoTrackFromSource;
private DataChannel localDataChannel;
private FrameVideoCapturer videoCapturer;
private ReceiveDataCallback dataCallback;
- private String recieverIP;
- private Integer recieverPort;
+ private String receiverIP;
+ private Integer receiverPort;
/**
* WebRTC constructor to create webRTC instance
+ *
* @param appContext Application context creating webRTC instance
*/
public WebRTC(Context appContext) {
this.appContext = appContext;
- this.isReciever = false;
+ this.isReceiver = false;
}
/**
* WebRTC constructor to create webRTC instance
+ *
* @param appContext Application context creating webRTC instance
- * @param socket Java server socket for webrtc signalling
+ * @param socket Java server socket for webrtc signalling
*/
- WebRTC(Context appContext , Socket socket) {
- Log.d(TAG , "InWebRTC Constructor");
+ WebRTC(Context appContext, Socket socket) {
+ Log.d(TAG, "InWebRTC Constructor");
this.appContext = appContext;
this.socket = socket;
- this.isReciever = true;
+ this.isReceiver = true;
}
/**
* To create data call-back mechanism
+ *
* @param cb aitt callback registered to receive a webrtc data
*/
- public void registerDataCallback(ReceiveDataCallback cb){
+ public void registerDataCallback(ReceiveDataCallback cb) {
this.dataCallback = cb;
}
/**
* Method to establish communication with peer node
- * @param recieverIP IP Address of the destination(peer) node
- * @param recieverPort Port number of the destination(peer) node
+ *
+ * @param receiverIP IP Address of the destination(peer) node
+ * @param receiverPort Port number of the destination(peer) node
*/
- public void connect(String recieverIP , Integer recieverPort){
- this.recieverIP = recieverIP;
- this.recieverPort = recieverPort;
+ public void connect(String receiverIP, Integer receiverPort) {
+ this.receiverIP = receiverIP;
+ this.receiverPort = receiverPort;
initialize();
}
/**
* Method to initialize webRTC APIs while establishing connection
*/
- private void initialize(){
+ private void initialize() {
initializePeerConnectionFactory();
initializePeerConnections();
- if(!isReciever){
+ if (!isReceiver) {
createVideoTrack();
addVideoTrack();
}
- isInitiator = isReciever;
+ isInitiator = isReceiver;
sdpThread = new SDPThread();
new Thread(sdpThread).start();
try {
message.put("type", "offer");
message.put("sdp", sessionDescription.description);
- sendMessage(true , message);
+ sendMessage(true, message);
} catch (JSONException | IOException e) {
Log.e(TAG, "Error during create offer", e);
}
/**
* Method to send signalling messages over socket connection
- * @param isJSON Boolean to check if message is JSON
+ *
+ * @param isJSON Boolean to check if message is JSON
* @param message Data to be sent over webRTC connection
* @throws IOException Throws IOException if writing to outStream fails
*/
/**
* ProxyVideoSink constructor to create its instance
+ *
* @param dataCb DataCall back to be set to self-object
*/
- ProxyVideoSink(ReceiveDataCallback dataCb){
+ ProxyVideoSink(ReceiveDataCallback dataCb) {
this.dataCallback = dataCb;
}
/**
* Method to send data through data call back
+ *
* @param frame VideoFrame to be transferred using media channel
*/
@Override
/**
* Method used to convert VideoFrame to NV21 data format
+ *
* @param i420Buffer VideoFrame in I420 buffer format
* @return the video frame in NV21 data format
*/
/**
* Method to create video track
*/
- private void createVideoTrack(){
+ private void createVideoTrack() {
videoCapturer = new FrameVideoCapturer();
VideoSource videoSource = factory.createVideoSource(false);
- videoCapturer.initialize(null , null ,videoSource.getCapturerObserver());
+ videoCapturer.initialize(null, null, videoSource.getCapturerObserver());
videoTrackFromSource = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
videoTrackFromSource.setEnabled(true);
}
private void addVideoTrack() {
MediaStream mediaStream = factory.createLocalMediaStream("ARDAMS");
mediaStream.addTrack(videoTrackFromSource);
- if(peerConnection!=null){
+ if (peerConnection != null) {
peerConnection.addStream(mediaStream);
}
}
/**
* Method to create peer connection
+ *
* @param factory Peer connection factory object
* @return return factory object
*/
message.put("id", iceCandidate.sdpMid);
message.put(CANDIDATE, iceCandidate.sdp);
Log.d(TAG, "onIceCandidate: sending candidate " + message);
- sendMessage(true , message);
+ sendMessage(true, message);
} catch (JSONException | IOException e) {
Log.e(TAG, "Error during onIceCandidate", e);
}
@Override
public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {
MediaStreamTrack track = rtpReceiver.track();
- if (track instanceof VideoTrack && isReciever) {
+ if (track instanceof VideoTrack && isReceiver) {
Log.i(TAG, "onAddVideoTrack");
VideoTrack remoteVideoTrack = (VideoTrack) track;
remoteVideoTrack.setEnabled(true);
- ProxyVideoSink videoSink = new ProxyVideoSink(dataCallback);
+ ProxyVideoSink videoSink = new ProxyVideoSink(dataCallback);
remoteVideoTrack.addSink(videoSink);
}
}
/**
* Method used to send video data
- * @param frame Video frame in byte format
- * @param width width of the video frame
+ *
+ * @param frame Video frame in byte format
+ * @param width width of the video frame
* @param height height of the video frame
*/
- public void sendVideoData(byte[] frame , int width , int height){
- videoCapturer.send(frame , width , height);
+ public void sendVideoData(byte[] frame, int width, int height) {
+ videoCapturer.send(frame, width, height);
}
/**
* Method to send message data
+ *
* @param message message to be sent in byte format
*/
public void sendMessageData(byte[] message) {
/**
* Interface to create data call back mechanism
*/
- public interface ReceiveDataCallback{
+ public interface ReceiveDataCallback {
void pushData(byte[] frame);
}
private static class Packet implements Serializable {
boolean isString;
String obj;
- Packet(String s){
+
+ Packet(String s) {
isString = true;
obj = s;
}
- Packet(JSONObject json){
+ Packet(JSONObject json) {
isString = false;
obj = json.toString();
}
/**
* Method to read incoming message and convert it to byte format
+ *
* @param buffer Message incoming in Byte buffer format
* @return returns byteBuffer message in byte format
*/
/**
* Method to decode message
+ *
* @param message Message received in JSON object format
*/
private void decodeMessage(JSONObject message) {
/**
* Method used to create a socket for SDP negotiation
*/
- private void createSocket(){
+ private void createSocket() {
try {
- if(!isReciever){
- socket = new Socket(recieverIP, recieverPort);
+ if (!isReceiver) {
+ socket = new Socket(receiverIP, receiverPort);
}
outStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
/**
* Method to invoke Signalling handshake message
*/
- private void invokeSendMessage(){
+ private void invokeSendMessage() {
try {
- sendMessage(false , "got user media");
+ sendMessage(false, "got user media");
} catch (Exception e) {
Log.e(TAG, "Error during invoke send message", e);
}
/**
* Method to check if the message in received packet is "got user media"
*/
- private void checkPacketMessage(String message){
+ private void checkPacketMessage(String message) {
if (message.equals("got user media")) {
maybeStart();
}
/**
* Method to invoke MaybeStart()
*/
- private void invokeMaybeStart(){
+ private void invokeMaybeStart() {
if (!isInitiator && !isStarted) {
maybeStart();
}