Implement reconnection and ping-pong between cloud services. Make 07/19907/5
authorOndrej Tomcik <ondrej.tomcik@kistler.com>
Mon, 15 May 2017 09:10:34 +0000 (11:10 +0200)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Wed, 17 May 2017 08:42:23 +0000 (08:42 +0000)
heartbeat configurable. Fix tests.

Change-Id: I2483f6548f906fd1cf780d63e1ce82fb2b66ca86
Signed-off-by: Ondrej Tomcik <ondrej.tomcik@kistler.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/19907
Reviewed-by: jung seungho <shonest.jung@samsung.com>
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
53 files changed:
cloud/account/Dockerfile [new file with mode: 0644]
cloud/account/src/main/java/org/iotivity/cloud/accountserver/AccountServer.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/AccountDBManager.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/MongoDB.java
cloud/docker-compose.yml [new file with mode: 0644]
cloud/interface/Dockerfile [new file with mode: 0644]
cloud/interface/pom.xml
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/RouteResource.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Acl.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroup.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInvite.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Certificate.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Crl.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/mq/MessageQueue.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/DeviceServerSystemTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/RouteResourceTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CertificateTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CrlTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/mq/MessageQueueTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java
cloud/messagequeue/Dockerfile [new file with mode: 0644]
cloud/messagequeue/src/main/java/org/iotivity/cloud/mqserver/Constants.java
cloud/messagequeue/src/main/java/org/iotivity/cloud/mqserver/MessageQueueServer.java
cloud/resourcedirectory/Dockerfile [new file with mode: 0644]
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/Constants.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/ResourceDirectoryServer.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/DBManager.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/MongoDB.java
cloud/resourcedirectory/src/test/java/org/iotivity/cloud/testrdserver/RDServerTestUtils.java
cloud/stack/src/main/java/org/iotivity/cloud/base/OICConstants.java
cloud/stack/src/main/java/org/iotivity/cloud/base/connector/CoapConnector.java
cloud/stack/src/main/java/org/iotivity/cloud/base/connector/ConnectorPool.java
cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/PingMessage.java [new file with mode: 0644]
cloud/stack/src/main/java/org/iotivity/cloud/base/resource/CloudPingResource.java [new file with mode: 0644]
cloud/stack/src/main/java/org/iotivity/cloud/util/Log.java

diff --git a/cloud/account/Dockerfile b/cloud/account/Dockerfile
new file mode 100644 (file)
index 0000000..270160e
--- /dev/null
@@ -0,0 +1,14 @@
+FROM openjdk:8-jre
+
+ADD ./target/CloudAccount-0.0.1-SNAPSHOT.jar iotivity/AccountServer.jar
+ADD ./target/lib/* iotivity/lib/
+ADD ./properties/* iotivity/properties/
+
+WORKDIR iotivity/
+
+ENV TLS_MODE=0
+ENV MONGODB_ADDRESS=mongodb
+ENV MONGODB_PORT=27017
+
+EXPOSE 5685
+ENTRYPOINT ["java", "-jar", "AccountServer.jar"]
index a5d3f1c..220e058 100644 (file)
@@ -24,6 +24,7 @@ package org.iotivity.cloud.accountserver;
 import java.net.InetSocketAddress;
 import java.util.Scanner;
 
+import org.iotivity.cloud.accountserver.db.AccountDBManager;
 import org.iotivity.cloud.accountserver.resources.account.AccountResource;
 import org.iotivity.cloud.accountserver.resources.account.session.SessionResource;
 import org.iotivity.cloud.accountserver.resources.account.tokenrefresh.TokenRefreshResource;
@@ -34,6 +35,7 @@ import org.iotivity.cloud.accountserver.resources.acl.verify.AclVerifyResource;
 import org.iotivity.cloud.accountserver.resources.credprov.cert.CertificateResource;
 import org.iotivity.cloud.accountserver.resources.credprov.crl.CrlResource;
 import org.iotivity.cloud.base.ServerSystem;
+import org.iotivity.cloud.base.resource.CloudPingResource;
 import org.iotivity.cloud.base.server.CoapServer;
 import org.iotivity.cloud.util.Log;
 
@@ -44,49 +46,43 @@ import org.iotivity.cloud.util.Log;
  */
 public class AccountServer {
 
+    private static int     coapServerPort;
+    private static boolean tlsMode;
+    private static String  databaseHost;
+    private static String  webLogHost;
+
     public static void main(String[] args) throws Exception {
         System.out.println("-----Account SERVER-----");
-
         Log.Init();
 
-        if (!(args.length == 2 || args.length == 4)) {
-            Log.e("coap server <Port> and TLS mode required\n"
-                    + "and WebSocketLog-Server <Address> <Port> (optional)\n"
-                    + "ex) 5685 0 127.0.0.1 8080\n");
+        if (!parseConfiguration(args)) {
+            Log.e("\nCoAP-server <Port> Database <Address> <Port> TLS-mode <0|1> are required. WebSocketLog-Server <Addres> <Port> is optional.\n"
+                    + "ex) " + Constants.DEFAULT_COAP_PORT
+                    + " 127.0.0.1 27017 0\n");
             return;
         }
-
-        if (args.length == 4) {
-            Log.InitWebLog(args[2], args[3],
+        if (webLogHost != null)
+            Log.InitWebLog(webLogHost,
                     AccountServer.class.getSimpleName().toString());
-        }
+
+        AccountDBManager.createInstance(databaseHost);
 
         ServerSystem serverSystem = new ServerSystem();
 
+        serverSystem.addResource(new CloudPingResource());
         serverSystem.addResource(new AccountResource());
-
         serverSystem.addResource(new SessionResource());
-
         serverSystem.addResource(new TokenRefreshResource());
-
         serverSystem.addResource(new GroupResource());
-
         serverSystem.addResource(new AclResource());
-
         serverSystem.addResource(new AclVerifyResource());
-
         serverSystem.addResource(new CertificateResource());
-
         serverSystem.addResource(new CrlResource());
-
         serverSystem.addResource(new AclResource());
-
         serverSystem.addResource(new InviteResource());
 
-        serverSystem.addServer(new CoapServer(
-                new InetSocketAddress(Integer.parseInt(args[0]))));
-
-        boolean tlsMode = Integer.parseInt(args[1]) == 1;
+        serverSystem.addServer(
+                new CoapServer(new InetSocketAddress(coapServerPort)));
 
         serverSystem.startSystem(tlsMode);
 
@@ -104,4 +100,27 @@ public class AccountServer {
 
         System.out.println("Terminated");
     }
+
+    private static boolean parseConfiguration(String[] args) {
+        // configuration provided by arguments
+        if (args.length == 4 || args.length == 6) {
+            coapServerPort = Integer.parseInt(args[0]);
+            databaseHost = args[1] + ":" + args[2];
+            tlsMode = Integer.parseInt(args[3]) == 1;
+            if (args.length == 6)
+                webLogHost = args[4] + ":" + args[5];
+            return true;
+        }
+        // configuration provided by docker env
+        String tlsModeEnv = System.getenv("TLS_MODE");
+        if (tlsModeEnv != null) {
+
+            coapServerPort = Constants.DEFAULT_COAP_PORT;
+            databaseHost = System.getenv("MONGODB_ADDRESS") + ":"
+                    + System.getenv("MONGODB_PORT");
+            tlsMode = Integer.parseInt(tlsModeEnv) == 1;
+            return true;
+        }
+        return false;
+    }
 }
index e75c2b6..00b579d 100644 (file)
@@ -27,6 +27,8 @@ import org.iotivity.cloud.base.OICConstants;
 
 public class Constants extends OICConstants {
 
+    public static final int    DEFAULT_COAP_PORT          = 5685;
+
     public static final String PROPERTIES_FILE_NAME       = "properties"
             + File.separator + "config.properties";
 
index ef7f33f..d10b845 100644 (file)
@@ -21,6 +21,7 @@
  */
 package org.iotivity.cloud.accountserver.db;
 
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -39,24 +40,20 @@ import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorExce
  */
 public class AccountDBManager {
 
-    private static AccountDBManager            accoutDBManager = new AccountDBManager();
-
+    private static AccountDBManager            accountDBManager;
     private MongoDB                            mongoDB;
-
     private HashMap<String, ArrayList<String>> keyField        = new HashMap<String, ArrayList<String>>();
 
-    private AccountDBManager() {
-
-        createDatabase();
+    private AccountDBManager(String dbHost) {
+        createDatabase(dbHost);
         createTables();
         createIndexes();
     }
 
-    private void createDatabase() {
+    private void createDatabase(String dbHost) {
 
         try {
-
-            mongoDB = new MongoDB(Constants.DB_NAME);
+            mongoDB = new MongoDB(dbHost, Constants.DB_NAME);
         } catch (Exception e) {
             e.printStackTrace();
             throw new InternalServerErrorException(
@@ -151,8 +148,20 @@ public class AccountDBManager {
      * @return account DB manager
      */
     public static AccountDBManager getInstance() {
+        if (accountDBManager == null)
+            accountDBManager = new AccountDBManager("127.0.0.1");
+        return accountDBManager;
+    }
 
-        return accoutDBManager;
+    /**
+     * API to create DBManager instance with specific host
+     *
+     * @return created DB manager
+     */
+    public static AccountDBManager createInstance(String dbHost) {
+        if (accountDBManager == null)
+            accountDBManager = new AccountDBManager(dbHost);
+        return accountDBManager;
     }
 
     /**
index 9e522d5..69862e8 100644 (file)
@@ -21,6 +21,7 @@
  */
 package org.iotivity.cloud.accountserver.db;
 
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -57,8 +58,22 @@ public class MongoDB {
      * @throws Exception
      */
     public MongoDB(String dbname) throws Exception {
+        mongoClient = new MongoClient(InetAddress.getLocalHost().getHostAddress());
+        mongoClient.dropDatabase(dbname);
+        db = mongoClient.getDatabase(dbname);
+    }
 
-        mongoClient = new MongoClient();
+    /**
+     * API creating MongoClient and initializing MongoDatabase
+     *
+     * @param host
+     *            host of MongoDatabase
+     * @param dbname
+     *            database name to create MongoDatabase
+     * @throws Exception
+     */
+    public MongoDB(String host, String dbname) throws Exception {
+        mongoClient = new MongoClient(host);
         mongoClient.dropDatabase(dbname);
         db = mongoClient.getDatabase(dbname);
     }
diff --git a/cloud/docker-compose.yml b/cloud/docker-compose.yml
new file mode 100644 (file)
index 0000000..885176e
--- /dev/null
@@ -0,0 +1,22 @@
+version: '3.1'
+
+services:
+  mongodb:
+    image: mongo
+
+  kafka-zookeeper:
+    image: spotify/kafka
+
+  iotivity-messagequeue:
+    image: iotivity/messagequeue
+
+  iotivity-accountserver:
+    image: iotivity/accountserver
+
+  iotivity-resourcedirectory:
+    image: iotivity/resourcedirectory
+
+  iotivity-interface:
+    image: iotivity/interface
+    ports:
+      - "5683:5683"
\ No newline at end of file
diff --git a/cloud/interface/Dockerfile b/cloud/interface/Dockerfile
new file mode 100644 (file)
index 0000000..9926069
--- /dev/null
@@ -0,0 +1,18 @@
+FROM openjdk:8-jre
+
+ADD ./target/CloudInterface-0.0.1-SNAPSHOT.jar iotivity/CloudInterface.jar
+ADD ./target/lib/* iotivity/lib/
+
+WORKDIR iotivity/
+
+ENV TLS_MODE=0
+ENV KEEPALIVE_CLOUD=1
+ENV HC_PROXY_MODE=0
+ENV WEBSOCKET_MODE=0
+ENV RESOURCE_DIRECTORY_ADDRESS iotivity-resourcedirectory
+ENV ACCOUNT_SERVER_ADDRESS iotivity-accountserver
+ENV MESSAGE_QUEUE_ADDRESS iotivity-messagequeue
+
+EXPOSE 5683
+EXPOSE 80
+ENTRYPOINT ["java", "-jar", "CloudInterface.jar"]
index e33bb82..3f0f3c5 100644 (file)
                        <version>1.10.19</version>
                        <scope>test</scope>
                </dependency>
+               <dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-api-mockito</artifactId>
+                       <version>1.6.5</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-core</artifactId>
+                       <version>1.6.5</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-module-junit4</artifactId>
+                       <version>1.6.5</version>
+                       <scope>test</scope>
+               </dependency>
                <dependency>
                        <groupId>com.jayway.awaitility</groupId>
                        <artifactId>awaitility</artifactId>
index 619c57b..5699b31 100755 (executable)
@@ -47,46 +47,50 @@ import org.iotivity.cloud.util.Log;
 
 public class CloudInterfaceServer {
 
-    public static void main(String[] args) throws Exception {
-
-        Log.Init();
+    private static int     coapServerPort;
+    private static boolean tlsMode;
+    private static boolean keepAlive = false;
+    private static boolean hcProxyMode;
+    private static int     hcProxyPort;
+    private static boolean websocketMode;
+    private static int     websocketPort;
+    private static String  resourceDirectoryAddress;
+    private static int     resourceDirectoryPort;
+    private static String  accountServerAddress;
+    private static int     accountServerPort;
+    private static String  messageQueueAddress;
+    private static int     messageQueuePort;
+    private static String  webLogHost;
 
+    public static void main(String[] args) throws Exception {
         System.out.println("-----CI SERVER-------");
+        Log.Init();
 
-        if (!(args.length == 10 || args.length == 12)) {
-            Log.e("\nCoAP-server <Port> and RD-server <Address> <Port> Account-server <Address> <Port> "
-                    + "MQ-broker <Address> <Port> HC-proxy <HTTP-port> Websocket-server <Port> and TLS-mode <0|1> are required.\n"
-                    + "and WebSocketLog-Server <Address> <Port> (optional)\n"
-                    + "ex) 5683 127.0.0.1 5684 127.0.0.1 5685 127.0.0.1 5686 80 8000 0 127.0.0.1 8080\n");
+        if (!parseConfiguration(args)) {
+            Log.e("\nCoAP-server <Port> RD-server <Address> <Port> Account-server <Address> <Port> MQ-broker <Address> <Port> HC-proxy [HTTP-port] "
+                    + "Websocket-server <Port> and TLS-mode <0|1> are required. WebSocketLog-Server <Addres> <Port> "
+                    + "and KeepAlive for cloud components <0|1> are optional.\n"
+                    + "ex) " + Constants.DEFAULT_COAP_PORT
+                    + " 127.0.0.1 " + Constants.DEFAULT_RESOURCE_DIRECTORY_PORT
+                    + " 127.0.0.1 " + Constants.DEFAULT_ACCOUNT_SERVER_PORT
+                    + " 127.0.0.1 " + Constants.DEFAULT_MESSAGE_QUEUE_PORT
+                    + " " + Constants.DEFAULT_HC_PROXY_PORT
+                    + " " + Constants.DEFAULT_WEBSOCKET_PORT + " 0\n");
             return;
         }
-
-        // CoAP-TCP server port
-        int coapPort = Integer.parseInt(args[0]);
-        // HTTP-CoAP proxy server port
-        int hcProxyPort = Integer.parseInt(args[7]);
-        // CoAP-Websocket server port
-        int websocketPort = Integer.parseInt(args[8]);
-
-        boolean hcProxyMode = hcProxyPort > 0;
-        boolean websocketMode = websocketPort > 0;
-
-        boolean tlsMode = Integer.parseInt(args[9]) == 1;
-
-        if (args.length >= 11) {
-            Log.InitWebLog(args[10], args[11],
+        if (webLogHost != null)
+            Log.InitWebLog(webLogHost,
                     CloudInterfaceServer.class.getSimpleName().toString());
-        }
 
-        ConnectorPool.addConnection("rd",
-                new InetSocketAddress(args[1], Integer.parseInt(args[2])),
-                tlsMode);
-        ConnectorPool.addConnection("account",
-                new InetSocketAddress(args[3], Integer.parseInt(args[4])),
-                tlsMode);
-        ConnectorPool.addConnection("mq",
-                new InetSocketAddress(args[5], Integer.parseInt(args[6])),
-                tlsMode);
+        ConnectorPool.requestConnection("rd",
+                new InetSocketAddress(resourceDirectoryAddress, resourceDirectoryPort),
+                tlsMode, keepAlive);
+        ConnectorPool.requestConnection("account",
+                new InetSocketAddress(accountServerAddress, accountServerPort),
+                tlsMode, keepAlive);
+        ConnectorPool.requestConnection("mq",
+                new InetSocketAddress(messageQueueAddress, messageQueuePort),
+                tlsMode, keepAlive);
 
         DeviceServerSystem deviceServer = new DeviceServerSystem();
 
@@ -135,18 +139,16 @@ public class CloudInterfaceServer {
 
         deviceServer.addResource(new RouteResource(devicePool));
 
-        deviceServer.addServer(new CoapServer(new InetSocketAddress(coapPort)));
+        deviceServer.addServer(
+                new CoapServer(new InetSocketAddress(coapServerPort)));
 
-        // Add HTTP Server for HTTP-to-CoAP Proxy
-        if (hcProxyMode) {
+        if (hcProxyMode)
             deviceServer.addServer(
                     new HttpServer(new InetSocketAddress(hcProxyPort)));
-        }
 
-        if (websocketMode) {
+        if (websocketMode)
             deviceServer.addServer(
                     new WebSocketServer(new InetSocketAddress(websocketPort)));
-        }
 
         deviceServer.startSystem(tlsMode);
 
@@ -168,4 +170,49 @@ public class CloudInterfaceServer {
 
         System.out.println("Terminated");
     }
+
+    private static boolean parseConfiguration(String[] args) {
+        // configuration provided by arguments
+        if (args.length == 10 || args.length == 13) {
+            coapServerPort = Integer.parseInt(args[0]);
+            resourceDirectoryAddress = args[1];
+            resourceDirectoryPort = Integer.parseInt(args[2]);
+            accountServerAddress = args[3];
+            accountServerPort = Integer.parseInt(args[4]);
+            messageQueueAddress = args[5];
+            messageQueuePort = Integer.parseInt(args[6]);
+            hcProxyPort = Integer.parseInt(args[7]);
+            hcProxyMode = hcProxyPort != 0;
+            websocketPort = Integer.parseInt(args[8]);
+            websocketMode = websocketPort != 0;
+            tlsMode = Integer.parseInt(args[9]) == 1;
+            if (args.length == 13) {
+                webLogHost = args[10] + ":" + args[11];
+                keepAlive = Integer.parseInt(args[12]) == 1;
+            }
+
+            return true;
+        }
+
+        // configuration provided by docker env
+        String tlsModeEnv = System.getenv("TLS_MODE");
+        if (tlsModeEnv != null) {
+            coapServerPort = Constants.DEFAULT_COAP_PORT;
+            resourceDirectoryAddress = System.getenv("RESOURCE_DIRECTORY_ADDRESS");
+            resourceDirectoryPort = Constants.DEFAULT_RESOURCE_DIRECTORY_PORT;
+            accountServerAddress = System.getenv("ACCOUNT_SERVER_ADDRESS");
+            accountServerPort = Constants.DEFAULT_ACCOUNT_SERVER_PORT;
+            messageQueueAddress = System.getenv("MESSAGE_QUEUE_ADDRESS");
+            messageQueuePort = Constants.DEFAULT_MESSAGE_QUEUE_PORT;
+            hcProxyMode = Integer.parseInt(System.getenv("HC_PROXY_MODE")) == 1;
+            hcProxyPort = Constants.DEFAULT_HC_PROXY_PORT;
+            websocketMode = Integer.parseInt(System.getenv("WEBSOCKET_MODE")) == 1;
+            websocketPort = Constants.DEFAULT_WEBSOCKET_PORT;
+            keepAlive = Integer.parseInt(System.getenv("KEEPALIVE_CLOUD")) == 1;
+            tlsMode = Integer.parseInt(tlsModeEnv) == 1;
+
+            return true;
+        }
+        return false;
+    }
 }
index ac921ae..59164a4 100644 (file)
@@ -24,41 +24,47 @@ package org.iotivity.cloud.ciserver;
 import org.iotivity.cloud.base.OICConstants;
 
 public class Constants extends OICConstants {
+    public static final int    DEFAULT_COAP_PORT               = 5683;
+    public static final int    DEFAULT_RESOURCE_DIRECTORY_PORT = 5684;
+    public static final int    DEFAULT_ACCOUNT_SERVER_PORT     = 5685;
+    public static final int    DEFAULT_MESSAGE_QUEUE_PORT      = 5686;
+    public static final int    DEFAULT_HC_PROXY_PORT           = 80;
+    public static final int    DEFAULT_WEBSOCKET_PORT          = 8000;
 
-    public static final int    MIN_TO_LIVE         = 10;
+    public static final int    MIN_TO_LIVE                     = 10;
 
-    public static final String USER_ID             = "uid";
-    public static final String DEVICE_ID           = "di";
-    public static final String PRESENCE_STATE      = "state";
+    public static final String USER_ID                         = "uid";
+    public static final String DEVICE_ID                       = "di";
+    public static final String PRESENCE_STATE                  = "state";
 
-    public static final String REQ_LOGIN           = "login";
+    public static final String REQ_LOGIN                       = "login";
 
-    public static final String ACCESS_TOKEN        = "accesstoken";
-    public static final String REFRESH_TOKEN       = "refreshtoken";
-    public static final String AUTH_CODE           = "authcode";
-    public static final String AUTH_PROVIDER       = "authprovider";
-    public static final String EXPIRES_IN          = "expiresin";
+    public static final String ACCESS_TOKEN                    = "accesstoken";
+    public static final String REFRESH_TOKEN                   = "refreshtoken";
+    public static final String AUTH_CODE                       = "authcode";
+    public static final String AUTH_PROVIDER                   = "authprovider";
+    public static final String EXPIRES_IN                      = "expiresin";
 
-    public static final String REQ_GROUP_MASTER_ID = "gmid";
-    public static final String REQ_MEMBER_ID       = "mid";
-    public static final String REQ_MEMBER_LIST     = "members";
-    public static final String REQ_DEVICE_ID       = "di";
-    public static final String REQ_DEVICE_ROUTE    = "route";
-    public static final String REQ_DEVICE_LIST     = "devices";
-    public static final String REQ_INVITE          = "invite";
-    public static final String REQ_PING_ARRAY      = "inarray";
-    public static final String REQ_PING            = "in";
-    public static final String REQ_SEARCH_USER_ID  = "sid";
-    public static final String REQ_REQUEST_METHOD  = "rm";
-    public static final String REQ_REQUEST_URI     = "uri";
+    public static final String REQ_GROUP_MASTER_ID             = "gmid";
+    public static final String REQ_MEMBER_ID                   = "mid";
+    public static final String REQ_MEMBER_LIST                 = "members";
+    public static final String REQ_DEVICE_ID                   = "di";
+    public static final String REQ_DEVICE_ROUTE                = "route";
+    public static final String REQ_DEVICE_LIST                 = "devices";
+    public static final String REQ_INVITE                      = "invite";
+    public static final String REQ_PING_ARRAY                  = "inarray";
+    public static final String REQ_PING                        = "in";
+    public static final String REQ_SEARCH_USER_ID              = "sid";
+    public static final String REQ_REQUEST_METHOD              = "rm";
+    public static final String REQ_REQUEST_URI                 = "uri";
 
-    public static final String REQ_GROUP_DEVICES   = "devices";
+    public static final String REQ_GROUP_DEVICES               = "devices";
 
-    public static final String RESP_GRANT_POLICY   = "gp";
-    public static final String RESP_ACL_ALLOWED    = "Allowed";
-    public static final String RESP_ACL_DENIED     = "Denied";
+    public static final String RESP_GRANT_POLICY               = "gp";
+    public static final String RESP_ACL_ALLOWED                = "Allowed";
+    public static final String RESP_ACL_DENIED                 = "Denied";
 
-    public static final String REQ_LINKS           = "links";
-    public static final String REQ_HREF            = "href";
-    public static final String REQ_CRL             = "crl";
+    public static final String REQ_LINKS                       = "links";
+    public static final String REQ_HREF                        = "href";
+    public static final String REQ_CRL                         = "crl";
 }
index 13d5cba..88ccf47 100644 (file)
@@ -70,12 +70,6 @@ public class DeviceServerSystem extends ServerSystem {
     private Cbor<HashMap<String, Object>>                 mCbor     = new Cbor<HashMap<String, Object>>();
     private HashMap<ChannelHandlerContext, CoapSignaling> mCsmMap   = new HashMap<>();
 
-    IRequestChannel                                       mRDServer = null;
-
-    public DeviceServerSystem() {
-        mRDServer = ConnectorPool.getConnection("rd");
-    }
-
     /**
      *
      * This class provides a set of APIs to manage device pool.
@@ -297,7 +291,7 @@ public class DeviceServerSystem extends ServerSystem {
             StringBuffer uriPath = new StringBuffer();
             uriPath.append("/" + Constants.PREFIX_OIC);
             uriPath.append("/" + Constants.DEVICE_PRESENCE_URI);
-            mRDServer.sendRequest(MessageBuilder.createRequest(
+            ConnectorPool.getConnection("rd").sendRequest(MessageBuilder.createRequest(
                     RequestMethod.POST, uriPath.toString(), null,
                     ContentFormat.APPLICATION_CBOR,
                     cbor.encodingPayloadToCbor(payload)), null);
index cb474f6..2ceac25 100644 (file)
@@ -55,12 +55,10 @@ import org.iotivity.cloud.util.Cbor;
 public class RouteResource extends Resource {
 
     private CoapDevicePool                mDevicePool = null;
-    private IRequestChannel               mASServer   = null;
     private Cbor<HashMap<String, Object>> mCbor       = new Cbor<>();
 
     public RouteResource(CoapDevicePool devicePool) {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.REQ_DEVICE_ROUTE));
-        mASServer = ConnectorPool.getConnection("account");
         mDevicePool = devicePool;
     }
 
@@ -279,7 +277,7 @@ public class RouteResource extends Resource {
         IRequest verifyRequest = MessageBuilder.createRequest(RequestMethod.GET,
                 OICConstants.ACL_VERIFY_FULL_URI, uriQuery.toString());
 
-        mASServer.sendRequest(verifyRequest,
+        ConnectorPool.getConnection("account").sendRequest(verifyRequest,
                 new AccountReceiveHandler(srcDevice, request));
     }
 }
\ No newline at end of file
index 1fd22b7..1d40572 100644 (file)
@@ -44,12 +44,9 @@ import org.iotivity.cloud.ciserver.Constants;
  */
 
 public class Account extends Resource {
-    IRequestChannel mASServer = null;
 
     public Account() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
-
-        mASServer = ConnectorPool.getConnection("account");
     }
 
     class RDReceiveHandler implements IResponseEventHandler {
@@ -81,12 +78,10 @@ public class Account extends Resource {
 
     class AccountReceiveHandler implements IResponseEventHandler {
 
-        IRequestChannel  mRDServer = null;
         private Device   mSrcDevice;
         private IRequest mRequest;
 
         public AccountReceiveHandler(IRequest request, Device srcDevice) {
-            mRDServer = ConnectorPool.getConnection("rd");
             mSrcDevice = srcDevice;
             mRequest = request;
         }
@@ -99,7 +94,7 @@ public class Account extends Resource {
                     StringBuffer uriPath = new StringBuffer();
                     uriPath.append(Constants.PREFIX_OIC + "/");
                     uriPath.append(Constants.RD_URI);
-                    mRDServer.sendRequest(
+                    ConnectorPool.getConnection("rd").sendRequest(
                             MessageBuilder.createRequest(RequestMethod.DELETE,
                                     uriPath.toString(), mRequest.getUriQuery()),
                             new RDReceiveHandler(mRequest, response,
@@ -140,7 +135,7 @@ public class Account extends Resource {
             default:
                 break;
         }
-        mASServer.sendRequest(request,
+        ConnectorPool.getConnection("account").sendRequest(request,
                 new AccountReceiveHandler(request, srcDevice));
     }
 }
index 595164a..f3d36bb 100644 (file)
@@ -42,14 +42,11 @@ import org.iotivity.cloud.util.Cbor;
  */
 
 public class AccountSession extends Resource {
-    IRequestChannel                       mAuthServer = null;
     private Cbor<HashMap<String, Object>> mCbor       = new Cbor<>();
 
     public AccountSession() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI,
                 Constants.SESSION_URI));
-
-        mAuthServer = ConnectorPool.getConnection("account");
     }
 
     @Override
@@ -71,6 +68,6 @@ public class AccountSession extends Resource {
                     ContentFormat.APPLICATION_CBOR,
                     mCbor.encodingPayloadToCbor(payloadData));
         }
-        mAuthServer.sendRequest(request, srcDevice);
+        ConnectorPool.getConnection("account").sendRequest(request, srcDevice);
     }
 }
\ No newline at end of file
index e16aaf5..37a284c 100644 (file)
@@ -38,18 +38,15 @@ import org.iotivity.cloud.ciserver.Constants;
  */
 
 public class Acl extends Resource {
-    IRequestChannel mAuthServer = null;
 
     public Acl() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI));
-
-        mAuthServer = ConnectorPool.getConnection("account");
     }
 
     @Override
     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
             throws ServerException {
         // Token exchange is done by CoapClient
-        mAuthServer.sendRequest(request, srcDevice);
+        ConnectorPool.getConnection("account").sendRequest(request, srcDevice);
     }
 }
\ No newline at end of file
index 429e537..e342aaa 100644 (file)
@@ -43,14 +43,11 @@ import org.iotivity.cloud.util.Cbor;
 
 public class AclGroup extends Resource {
 
-    private IRequestChannel               mAuthServer = null;
     private Cbor<HashMap<String, Object>> mCbor       = new Cbor<>();
 
     public AclGroup() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
                 Constants.GROUP_URI));
-
-        mAuthServer = ConnectorPool.getConnection("account");
     }
 
     @Override
@@ -79,7 +76,8 @@ public class AclGroup extends Resource {
         String uriQuery = additionalQuery.toString()
                 + (request.getUriQuery() != null ? (";" + request.getUriQuery())
                         : "");
-        mAuthServer.sendRequest(MessageBuilder.modifyRequest(request, null,
-                uriQuery, null, null), srcDevice);
+        ConnectorPool.getConnection("account").sendRequest(
+                MessageBuilder.modifyRequest(request, null,
+                    uriQuery, null, null), srcDevice);
     }
 }
\ No newline at end of file
index 86e53f1..5c49feb 100644 (file)
@@ -43,14 +43,9 @@ import org.iotivity.cloud.util.Cbor;
 
 public class AclInvite extends Resource {
 
-    IRequestChannel mAuthServer = null;
-
     public AclInvite() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
                 Constants.INVITE_URI));
-
-        mAuthServer = ConnectorPool.getConnection("account");
-
     }
 
     @Override
@@ -84,7 +79,7 @@ public class AclInvite extends Resource {
         request = MessageBuilder.modifyRequest(request, null, uriQuery, null,
                 null);
 
-        mAuthServer.sendRequest(request, srcDevice);
+        ConnectorPool.getConnection("account").sendRequest(request, srcDevice);
     }
 
 }
\ No newline at end of file
index 0bbf181..64255cc 100644 (file)
@@ -39,17 +39,14 @@ import org.iotivity.cloud.ciserver.Constants;
  */
 
 public class Certificate extends Resource {
-    IRequestChannel mAuthServer = null;
-
     public Certificate() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.CREDPROV_URI,
                 Constants.CERT_URI));
-        mAuthServer = ConnectorPool.getConnection("account");
     }
 
     @Override
     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
             throws ServerException {
-        mAuthServer.sendRequest(request, srcDevice);
+        ConnectorPool.getConnection("account").sendRequest(request, srcDevice);
     }
 }
index 0f679b8..5e7ed4d 100644 (file)
@@ -12,19 +12,15 @@ import org.iotivity.cloud.ciserver.Constants;
 
 public class Crl extends Resource {
 
-    private IRequestChannel mAuthServer = null;
-
     public Crl() {
         super(Arrays.asList(Constants.PREFIX_OIC,
                 Constants.CREDPROV_URI, Constants.REQ_CRL));
-
-        mAuthServer = ConnectorPool.getConnection("account");
     }
 
     @Override
     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
             throws ServerException {
         // Token exchange is done by CoapClient
-        mAuthServer.sendRequest(request, srcDevice);
+        ConnectorPool.getConnection("account").sendRequest(request, srcDevice);
     }
 }
\ No newline at end of file
index 4286902..7199a41 100644 (file)
@@ -39,12 +39,8 @@ import org.iotivity.cloud.ciserver.Constants;
  */
 
 public class MessageQueue extends Resource {
-    IRequestChannel mPSServer = null;
-
     public MessageQueue() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.MQ_BROKER_URI));
-
-        mPSServer = ConnectorPool.getConnection("mq");
     }
 
     @Override
@@ -52,6 +48,6 @@ public class MessageQueue extends Resource {
             throws ServerException {
 
         // Token exchange is done by CoapClient
-        mPSServer.sendRequest(request, srcDevice);
+        ConnectorPool.getConnection("mq").sendRequest(request, srcDevice);
     }
 }
index 17b4076..ab74400 100644 (file)
@@ -49,24 +49,19 @@ import org.iotivity.cloud.util.Cbor;
  */
 
 public class DevicePresence extends Resource {
-    IRequestChannel                       mASServer = null;
     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
 
     public DevicePresence() {
         super(Arrays.asList(Constants.PREFIX_OIC,
                 Constants.DEVICE_PRESENCE_URI));
-
-        mASServer = ConnectorPool.getConnection("account");
     }
 
     class AccountReceiveHandler implements IResponseEventHandler {
 
-        IRequestChannel  mRDServer = null;
         private Device   mSrcDevice;
         private IRequest mRequest;
 
         public AccountReceiveHandler(IRequest request, Device srcDevice) {
-            mRDServer = ConnectorPool.getConnection("rd");
             mSrcDevice = srcDevice;
             mRequest = request;
         }
@@ -105,7 +100,7 @@ public class DevicePresence extends Resource {
                         }
                     }
 
-                    mRDServer.sendRequest(mRequest, mSrcDevice);
+                    ConnectorPool.getConnection("rd").sendRequest(mRequest, mSrcDevice);
                     break;
 
                 default:
@@ -165,7 +160,7 @@ public class DevicePresence extends Resource {
         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
                 uriPath.toString(), uriQuery);
 
-        mASServer.sendRequest(requestToAS,
+        ConnectorPool.getConnection("account").sendRequest(requestToAS,
                 new AccountReceiveHandler(request, srcDevice));
     }
 }
\ No newline at end of file
index b71df9e..2f066c7 100644 (file)
@@ -50,13 +50,9 @@ import org.iotivity.cloud.util.Cbor;
 
 public class ResourceDirectory extends Resource {
     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
-    IRequestChannel                       mRDServer = null;
-    IRequestChannel                       mASServer = null;
 
     public ResourceDirectory() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.RD_URI));
-        mRDServer = ConnectorPool.getConnection("rd");
-        mASServer = ConnectorPool.getConnection("account");
     }
 
     @Override
@@ -87,12 +83,12 @@ public class ResourceDirectory extends Resource {
                         query.toString(), ContentFormat.APPLICATION_CBOR,
                         mCbor.encodingPayloadToCbor(requestPayload));
 
-                mASServer.sendRequest(requestToAS,
+                ConnectorPool.getConnection("account").sendRequest(requestToAS,
                         new AccountReceiveHandler(request, srcDevice));
                 break;
 
             case DELETE:
-                mRDServer.sendRequest(request, srcDevice);
+                ConnectorPool.getConnection("rd").sendRequest(request, srcDevice);
                 break;
 
             default:
@@ -123,7 +119,7 @@ public class ResourceDirectory extends Resource {
                             null, ContentFormat.APPLICATION_CBOR,
                             convertedPayload);
 
-                    mRDServer.sendRequest(mRequest,
+                    ConnectorPool.getConnection("rd").sendRequest(mRequest,
                             new PublishResponseHandler(mSrcDevice));
                     break;
 
index e13382a..e79c8c9 100644 (file)
@@ -48,15 +48,10 @@ import org.iotivity.cloud.util.Cbor;
  */
 
 public class ResourceFind extends Resource {
-    IRequestChannel                       mASServer = null;
-    IRequestChannel                       mRDServer = null;
     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
 
     public ResourceFind() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.WELL_KNOWN_URI));
-
-        mASServer = ConnectorPool.getConnection("account");
-        mRDServer = ConnectorPool.getConnection("rd");
     }
 
     class AccountReceiveHandler implements IResponseEventHandler {
@@ -91,7 +86,7 @@ public class ResourceFind extends Resource {
                     mRequest = MessageBuilder.modifyRequest(mRequest, null,
                             uriQuery, null, null);
 
-                    mRDServer.sendRequest(mRequest, mSrcDevice);
+                    ConnectorPool.getConnection("rd").sendRequest(mRequest, mSrcDevice);
                     break;
 
                 default:
@@ -140,7 +135,7 @@ public class ResourceFind extends Resource {
         if (request.getUriQuery() != null && request.getUriQueryMap()
                 .containsKey(Constants.REQ_DEVICE_ID)) {
 
-            mRDServer.sendRequest(request, srcDevice);
+            ConnectorPool.getConnection("rd").sendRequest(request, srcDevice);
 
         } else {
             StringBuffer additionalQuery = new StringBuffer();
@@ -162,7 +157,7 @@ public class ResourceFind extends Resource {
             IRequest requestToAS = MessageBuilder.createRequest(
                     RequestMethod.GET, uriPath.toString(), uriQuery);
 
-            mASServer.sendRequest(requestToAS,
+            ConnectorPool.getConnection("account").sendRequest(requestToAS,
                     new AccountReceiveHandler(request, srcDevice));
         }
     }
index 95dd639..ffaaae0 100644 (file)
@@ -47,23 +47,18 @@ import org.iotivity.cloud.util.Cbor;
  */
 
 public class ResourcePresence extends Resource {
-    IRequestChannel                       mASServer = null;
     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
 
     public ResourcePresence() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.RES_PRESENCE_URI));
-
-        mASServer = ConnectorPool.getConnection("account");
     }
 
     class AccountReceiveHandler implements IResponseEventHandler {
 
-        IRequestChannel  mRDServer = null;
         private Device   mSrcDevice;
         private IRequest mRequest;
 
         public AccountReceiveHandler(IRequest request, Device srcDevice) {
-            mRDServer = ConnectorPool.getConnection("rd");
             mSrcDevice = srcDevice;
             mRequest = request;
         }
@@ -86,7 +81,7 @@ public class ResourcePresence extends Resource {
                         mRequest = MessageBuilder.modifyRequest(mRequest, null,
                                 uriQuery, null, null);
 
-                        mRDServer.sendRequest(mRequest, mSrcDevice);
+                        ConnectorPool.getConnection("rd").sendRequest(mRequest, mSrcDevice);
                     }
                     break;
 
@@ -147,7 +142,7 @@ public class ResourcePresence extends Resource {
         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
                 uriPath.toString(), uriQuery);
 
-        mASServer.sendRequest(requestToAS,
+        ConnectorPool.getConnection("account").sendRequest(requestToAS,
                 new AccountReceiveHandler(request, srcDevice));
     }
 }
\ No newline at end of file
index 8ab871e..05523da 100644 (file)
@@ -31,6 +31,7 @@ import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.Device;
 import org.iotivity.cloud.base.device.IRequestChannel;
@@ -58,6 +59,7 @@ import org.iotivity.cloud.util.Cbor;
 import org.iotivity.cloud.util.Log;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
@@ -70,7 +72,12 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelId;
 import io.netty.channel.ChannelPromise;
 import io.netty.util.Attribute;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class DeviceServerSystemTest {
     private ChannelHandlerContext                   mCtx                  = null;
     private ChannelHandlerContext                   mCtxSignal            = null;
@@ -178,6 +185,8 @@ public class DeviceServerSystemTest {
             }
         }).when(mCtx).writeAndFlush(Mockito.any());
 
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index 63c715c..2d84792 100644 (file)
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -50,13 +51,19 @@ import org.iotivity.cloud.ciserver.resources.RouteResource.LinkInterfaceHandler;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class RouteResourceTest {
     private static final String           RELAY_URI                          = "/di";
     private static final String           RESOURCE_URI                       = "/a/light/0";
@@ -168,6 +175,9 @@ public class RouteResourceTest {
             }
         }).when(mRequestChannelASServer).sendRequest(
                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannelASServer);
     }
 
     @Test
index a6ad127..ebc1d37 100644 (file)
@@ -44,13 +44,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class AccountSessionTest {
     private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
     public static final String   SESSION_URI         = Constants.ACCOUNT_SESSION_FULL_URI;
@@ -94,6 +100,9 @@ public class AccountSessionTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index 0726aad..a95d6b8 100644 (file)
@@ -45,13 +45,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class AccountTest {
     private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
     public static final String   ACCOUNT_URI         = Constants.ACCOUNT_FULL_URI;
@@ -118,6 +124,10 @@ public class AccountTest {
             }
         }).when(mRequestChannelASServer).sendRequest(
                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection("account")).thenReturn(mRequestChannelASServer);
+        PowerMockito.when(ConnectorPool.getConnection("rd")).thenReturn(mRequestChannelRDServer);
     }
 
     @Test
index 89c6a5b..416c2bc 100644 (file)
@@ -31,6 +31,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
@@ -44,13 +45,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class AclGroupTest {
     private static final String   TEST_RESOURCE_GROUP_URI = Constants.GROUP_FULL_URI;
 
@@ -93,6 +100,9 @@ public class AclGroupTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index b6bfe2d..8d8a8d3 100644 (file)
@@ -32,6 +32,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -44,13 +45,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class AclInviteTest {
     private static final String  TEST_RESOURCE_INVITE_URI = Constants.INVITE_FULL_URI;
 
@@ -92,6 +99,9 @@ public class AclInviteTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index a8b1921..57d643e 100644 (file)
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.mock;
 import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -39,13 +40,19 @@ import org.iotivity.cloud.base.protocols.enums.RequestMethod;
 import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class AclTest {
     private static final String TEST_RESOURCE_ACI_URI = "/"
             + OICConstants.PREFIX_OIC + "/" + OICConstants.ACL_URI;
@@ -84,6 +91,9 @@ public class AclTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index 2fa8bc5..208970a 100644 (file)
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.mock;
 import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -39,13 +40,19 @@ import org.iotivity.cloud.base.protocols.enums.RequestMethod;
 import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class CertificateTest {
     private static final String TEST_RESOURCE_CERTI_URI = "/"
             + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/"
@@ -86,6 +93,9 @@ public class CertificateTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index 3bab49f..dbb8fae 100644 (file)
@@ -8,6 +8,7 @@ import static org.mockito.Mockito.mock;
 import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -18,13 +19,19 @@ import org.iotivity.cloud.ciserver.Constants;
 import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class CrlTest {
 
     private static final String TEST_RESOURCE_CRL_URI = "/"
@@ -65,6 +72,9 @@ public class CrlTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index db46b22..6a3581e 100644 (file)
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.mock;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -42,13 +43,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class MessageQueueTest {
     private static final String TEST_MQ_BROKER_URI = Constants.MQ_BROKER_FULL_URI;
     private CoapDevice           mMockDevice        = null;
@@ -80,6 +87,9 @@ public class MessageQueueTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
index e2b52a7..d68c536 100644 (file)
@@ -32,6 +32,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.exception.ClientException;
@@ -48,13 +49,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class DevicePresenceTest {
     private static final String DEVICE_PRS_REQ_URI  = Constants.DEVICE_PRESENCE_FULL_URI;
     private String              mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
@@ -91,6 +98,9 @@ public class DevicePresenceTest {
             }
         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     // @InjectMocks for testSpecificDeviceonResponseReceived
index 763891f..08a5ac4 100644 (file)
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -46,13 +47,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class ResourceDirectoryTest {
     private static final String TEST_RD_URI         = Constants.RD_FULL_URI;
     public static final String  DEVICE_LIST_KEY     = "devices";
@@ -120,6 +127,9 @@ public class ResourceDirectoryTest {
         }).when(mRequestChannelASServer).sendRequest(
                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
 
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection("account")).thenReturn(mRequestChannelASServer);
+        PowerMockito.when(ConnectorPool.getConnection("rd")).thenReturn(mRequestChannelRDServer);
     }
 
     @Test
index 26a5efb..a7db07a 100644 (file)
@@ -32,6 +32,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.exception.ClientException;
@@ -48,13 +49,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class ResourceFindTest {
     private static final String TEST_RESOURCE_FIND_URI = Constants.WELL_KNOWN_FULL_URI;
     private String              di                     = "B371C481-38E6-4D47-8320-7688D8A5B58C";
@@ -128,6 +135,10 @@ public class ResourceFindTest {
             }
         }).when(mRequestChannelASServer).sendRequest(
                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection("account")).thenReturn(mRequestChannelASServer);
+        PowerMockito.when(ConnectorPool.getConnection("rd")).thenReturn(mRequestChannelRDServer);
     }
 
     // @InjectMocks for testSpecificDeviceonResponseReceived
index 93ef37a..5c363aa 100644 (file)
@@ -32,6 +32,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 
+import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.CoapDevice;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.exception.ClientException;
@@ -47,13 +48,19 @@ import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConnectorPool.class)
 public class ResourcePresenceTest {
     public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
     public static final String RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
@@ -64,7 +71,7 @@ public class ResourcePresenceTest {
     private DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
     final CountDownLatch       latch              = new CountDownLatch(1);
     @Mock
-    private IRequestChannel    requestChannel;
+    private IRequestChannel    mRequestChannel;
     @InjectMocks
     private ResourcePresence   adHandler          = new ResourcePresence();
 
@@ -92,8 +99,11 @@ public class ResourcePresenceTest {
                 latch.countDown();
                 return request;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
+
+        PowerMockito.mockStatic(ConnectorPool.class);
+        PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
     }
 
     @Test
diff --git a/cloud/messagequeue/Dockerfile b/cloud/messagequeue/Dockerfile
new file mode 100644 (file)
index 0000000..c8943df
--- /dev/null
@@ -0,0 +1,15 @@
+FROM openjdk:8-jre
+
+ADD ./target/CloudMessageQueue-0.0.1-SNAPSHOT.jar iotivity/MessageQueue.jar
+ADD ./target/lib/* iotivity/lib/
+
+WORKDIR iotivity/
+
+ENV TLS_MODE=0
+ENV ZOOKEEPER_ADDRESS kafka-zookeeper
+ENV ZOOKEEPER_PORT 2181
+ENV KAFKA_ADDRESS kafka-zookeeper
+ENV KAFKA_PORT 9092
+
+EXPOSE 5686
+ENTRYPOINT ["java", "-jar", "MessageQueue.jar"]
\ No newline at end of file
index d724703..ee86ed3 100644 (file)
@@ -24,6 +24,7 @@ package org.iotivity.cloud.mqserver;
 import org.iotivity.cloud.base.OICConstants;
 
 public class Constants extends OICConstants {
+       public static final int    DEFAULT_COAP_PORT       = 5686;
 
     public static final String MQ_TOPICLIST            = "topiclist";
 
index 787d2a6..b3ce4a9 100644 (file)
@@ -25,45 +25,44 @@ import java.net.InetSocketAddress;
 import java.util.Scanner;
 
 import org.iotivity.cloud.base.ServerSystem;
+import org.iotivity.cloud.base.resource.CloudPingResource;
 import org.iotivity.cloud.base.server.CoapServer;
 import org.iotivity.cloud.mqserver.resources.MQBrokerResource;
 import org.iotivity.cloud.util.Log;
 
 public class MessageQueueServer {
 
-    public static void main(String[] args) throws Exception {
-        Log.Init();
+    private static int     coapServerPort;
+    private static boolean tlsMode;
+    private static String  zookeeperHost;
+    private static String  kafkaHost;
+    private static String  webLogHost;
 
+    public static void main(String[] args) throws Exception {
         System.out.println("-----MQ SERVER-----");
+        Log.Init();
 
-        if (!(args.length == 6 || args.length == 8)) {
-            Log.e("coap server port, Kafka_zookeeper_Address port"
-                    + " and Kafka_broker_Address Port and TLS mode required\n"
-                    + " and WebSocketLog-Server <Address> <Port> (optional)\n"
-                    + "ex) 5686 127.0.0.1 2181 127.0.0.1 9092 0 127.0.0.1 8080\n");
-
+        if (!parseConfiguration(args)) {
+            Log.e("\nCoAP-server <Port> Zookeeper <Address> <Port> Kafka <Address> <Port> TLS-mode <0|1> are required. "
+                    + "WebSocketLog-Server <Addres> <Port> is optional.\n"
+                    + "ex) " + Constants.DEFAULT_COAP_PORT
+                    + " 127.0.0.1 2181 127.0.0.1 9092 0\n");
             return;
         }
-
-        if (args.length == 8) {
-            Log.InitWebLog(args[6], args[7],
+        if (webLogHost != null)
+            Log.InitWebLog(webLogHost,
                     MessageQueueServer.class.getSimpleName().toString());
-        }
 
         ServerSystem serverSystem = new ServerSystem();
 
         MQBrokerResource MQBroker = new MQBrokerResource();
-
-        String kafka_zookeeper = args[1] + ":" + args[2];
-        String kafka_broker = args[3] + ":" + args[4];
-        MQBroker.setKafkaInformation(kafka_zookeeper, kafka_broker);
+        MQBroker.setKafkaInformation(zookeeperHost, kafkaHost);
 
         serverSystem.addResource(MQBroker);
+        serverSystem.addResource(new CloudPingResource());
 
-        serverSystem.addServer(new CoapServer(
-                new InetSocketAddress(Integer.parseInt(args[0]))));
-
-        boolean tlsMode = Integer.parseInt(args[5]) == 1;
+        serverSystem.addServer(
+                new CoapServer(new InetSocketAddress(coapServerPort)));
 
         serverSystem.startSystem(tlsMode);
 
@@ -81,4 +80,29 @@ public class MessageQueueServer {
 
         System.out.println("Terminated");
     }
+
+    private static boolean parseConfiguration(String[] args) {
+        // configuration provided by arguments
+        if (args.length == 6 || args.length == 8) {
+            coapServerPort = Integer.parseInt(args[0]);
+            zookeeperHost = args[1] + ":" + args[2];
+            kafkaHost = args[3] + ":" + args[4];
+            tlsMode = Integer.parseInt(args[5]) == 1;
+            if (args.length == 8)
+                webLogHost = args[6] + ":" + args[7];
+            return true;
+        }
+        // configuration provided by docker env
+        String tlsModeEnv = System.getenv("TLS_MODE");
+        if (tlsModeEnv != null) {
+            coapServerPort = Constants.DEFAULT_COAP_PORT;
+            tlsMode = Integer.parseInt(tlsModeEnv) == 1;
+            zookeeperHost = System.getenv("ZOOKEEPER_ADDRESS") + ":"
+                    + System.getenv("ZOOKEEPER_PORT");
+            kafkaHost = System.getenv("KAFKA_ADDRESS") + ":"
+                    + System.getenv("KAFKA_PORT");
+            return true;
+        }
+        return false;
+    }
 }
\ No newline at end of file
diff --git a/cloud/resourcedirectory/Dockerfile b/cloud/resourcedirectory/Dockerfile
new file mode 100644 (file)
index 0000000..04d7005
--- /dev/null
@@ -0,0 +1,13 @@
+FROM openjdk:8-jre
+
+ADD ./target/CloudResourceDirectory-0.0.1-SNAPSHOT.jar iotivity/ResourceDirectory.jar
+ADD ./target/lib/* iotivity/lib/
+
+WORKDIR iotivity/
+
+ENV TLS_MODE=0
+ENV MONGODB_ADDRESS mongodb
+ENV MONGODB_PORT 27017
+
+EXPOSE 5684
+ENTRYPOINT ["java", "-jar", "ResourceDirectory.jar"]
\ No newline at end of file
index f4789bf..ae8d1fb 100644 (file)
@@ -25,6 +25,8 @@ import org.iotivity.cloud.base.OICConstants;
 
 public class Constants extends OICConstants {
 
+    public static final int    DEFAULT_COAP_PORT       = 5684;
+
     /** Database, Table name */
     public static final String RD_DB_NAME              = "RD_DB";
     public static final String RD_TABLE                = "RD_TABLE";
index d22cb38..0d0bcef 100644 (file)
@@ -25,7 +25,9 @@ import java.net.InetSocketAddress;
 import java.util.Scanner;
 
 import org.iotivity.cloud.base.ServerSystem;
+import org.iotivity.cloud.base.resource.CloudPingResource;
 import org.iotivity.cloud.base.server.CoapServer;
+import org.iotivity.cloud.rdserver.db.DBManager;
 import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
 import org.iotivity.cloud.rdserver.resources.directory.res.DiscoveryResource;
 import org.iotivity.cloud.rdserver.resources.presence.device.DevicePresenceResource;
@@ -39,32 +41,37 @@ import org.iotivity.cloud.util.Log;
  */
 public class ResourceDirectoryServer {
 
-    public static void main(String[] args) throws Exception {
-        Log.Init();
+    private static int     coapServerPort;
+    private static boolean tlsMode;
+    private static String  databaseHost;
+    private static String  webLogHost;
 
+    public static void main(String[] args) throws Exception {
         System.out.println("-----RD SERVER-----");
+        Log.Init();
 
-        if (!(args.length != 2 || args.length != 4)) {
-            Log.e("coap server port and TLS mode required\n" + "ex) 5684 0\n");
+        if (!parseConfiguration(args)) {
+            Log.e("\nCoAP-server <Port> Database <Address> <Port> TLS-mode <0|1> are required. WebSocketLog-Server <Addres> <Port> is optional.\n"
+                    + "ex) " + Constants.DEFAULT_COAP_PORT
+                    + " 127.0.0.1 27017 0\n");
             return;
         }
-
-        if (args.length == 4) {
-            Log.InitWebLog(args[2], args[3],
+        if (webLogHost != null)
+            Log.InitWebLog(webLogHost,
                     ResourceDirectoryServer.class.getSimpleName().toString());
-        }
+
+        DBManager.createInstance(databaseHost);
 
         ServerSystem serverSystem = new ServerSystem();
 
+        serverSystem.addResource(new CloudPingResource());
         serverSystem.addResource(new ResourceDirectoryResource());
         serverSystem.addResource(new DiscoveryResource());
         serverSystem.addResource(new DevicePresenceResource());
         serverSystem.addResource(new ResPresenceResource());
 
-        serverSystem.addServer(new CoapServer(
-                new InetSocketAddress(Integer.parseInt(args[0]))));
-
-        boolean tlsMode = Integer.parseInt(args[1]) == 1;
+        serverSystem.addServer(
+                new CoapServer(new InetSocketAddress(coapServerPort)));
 
         serverSystem.startSystem(tlsMode);
 
@@ -82,4 +89,26 @@ public class ResourceDirectoryServer {
 
         System.out.println("Terminated");
     }
+
+    private static boolean parseConfiguration(String[] args) {
+        // configuration provided by arguments
+        if (args.length == 4 || args.length == 6) {
+            coapServerPort = Integer.parseInt(args[0]);
+            databaseHost = args[1] + ":" + args[2];
+            tlsMode = Integer.parseInt(args[3]) == 1;
+            if (args.length == 6)
+                webLogHost = args[4] + ":" + args[5];
+            return true;
+        }
+        // configuration provided by docker env
+        String tlsModeEnv = System.getenv("TLS_MODE");
+        if (tlsModeEnv != null) {
+            coapServerPort = Constants.DEFAULT_COAP_PORT;
+            databaseHost = System.getenv("MONGODB_ADDRESS") + ":"
+                    + System.getenv("MONGODB_PORT");
+            tlsMode = Integer.parseInt(tlsModeEnv) == 1;
+            return true;
+        }
+        return false;
+    }
 }
index 02ea023..a28f519 100644 (file)
@@ -39,12 +39,12 @@ import org.iotivity.cloud.rdserver.Constants;
  */
 public class DBManager {
 
-    private static DBManager                   mDBManager = new DBManager();
+    private static DBManager                   mDBManager;
     private MongoDB                            mMongoDB   = null;
     private HashMap<String, ArrayList<String>> mKeyField  = new HashMap<>();
 
-    private DBManager() {
-        createDatabase();
+    private DBManager(String dbHost) {
+        createDatabase(dbHost);
         createTables();
         createIndexes();
     }
@@ -55,14 +55,25 @@ public class DBManager {
      * @return DBManager DBManager object
      */
     public static DBManager getInstance() {
+        if (mDBManager == null)
+            mDBManager = new DBManager("127.0.0.1");
         return mDBManager;
     }
 
-    private void createDatabase() {
+    /**
+     * API to create DBManager instance with specific host
+     *
+     * @return created DB manager
+     */
+    public static DBManager createInstance(String dbHost) {
+        if (mDBManager == null)
+            mDBManager = new DBManager(dbHost);
+        return mDBManager;
+    }
 
+    private void createDatabase(String dbHost) {
         try {
-
-            mMongoDB = new MongoDB(Constants.RD_DB_NAME);
+            mMongoDB = new MongoDB(dbHost, Constants.RD_DB_NAME);
         } catch (Exception e) {
             e.printStackTrace();
             throw new InternalServerErrorException("Database create failed!");
index 509e9ec..8955edc 100644 (file)
@@ -50,13 +50,15 @@ public class MongoDB {
     /**
      * API creating MongoClient and initializing MongoDatabase
      *
+     * @param host
+     *            host of MongoDatabase
      * @param dbname
      *            database name to create MongoDatabase
      * @throws Exception
      */
-    public MongoDB(String dbname) throws Exception {
+    public MongoDB(String host, String dbname) throws Exception {
 
-        mongoClient = new MongoClient();
+        mongoClient = new MongoClient(host);
         mongoClient.dropDatabase(dbname);
         db = mongoClient.getDatabase(dbname);
     }
index 4b06246..78e08fa 100644 (file)
@@ -49,7 +49,7 @@ public class RDServerTestUtils {
     }
 
     public static void resetRDDatabase() throws Exception {
-        MongoDB mongoDB = new MongoDB(Constants.RD_DB_NAME);
+        MongoDB mongoDB = new MongoDB("127.0.0.1", Constants.RD_DB_NAME);
         mongoDB.createTable(Constants.RD_TABLE);
         mongoDB.createTable(Constants.PRESENCE_TABLE);
     }
index 26dbb53..f230829 100755 (executable)
@@ -85,13 +85,15 @@ public class OICConstants {
 
     /* resource uri for message queue */
     public static final String MQ_BROKER_URI                 = "ps";
-
+    
     /* file path for tls communication - Modify filePath to yours */
-    public static final String ROOT_CERT_FILE                = "../certificate/rootca.crt";
+    public static final String CERTS_PATH                    = "../certificate";
+
+    public static final String ROOT_CERT_FILE                = CERTS_PATH + "/rootca.crt";
 
-    public static final String CLOUD_CERT_FILE               = "../certificate/iotivitycloud.crt";
+    public static final String CLOUD_CERT_FILE               = CERTS_PATH + "/iotivitycloud.crt";
 
-    public static final String CLOUD_KEY_FILE                = "../certificate/iotivitycloud.key";
+    public static final String CLOUD_KEY_FILE                = CERTS_PATH + "/iotivitycloud.key";
 
     /* cloud uuid */
     public static final String CLOUD_UUID                    = "2a6085d1-815d-4277-baba-4e4e4df91308";
@@ -150,4 +152,4 @@ public class OICConstants {
 
     public static final String CREDPROV_CERT_FULL_URI        = "/" + PREFIX_OIC
             + "/" + CREDPROV_URI + "/" + CERT_URI;
-}
+}
\ No newline at end of file
index 1efeb46..281082a 100644 (file)
  */
 package org.iotivity.cloud.base.connector;
 
-import java.io.File;
-import java.net.InetSocketAddress;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-
-import javax.net.ssl.SSLException;
-
-import org.iotivity.cloud.base.OICConstants;
-import org.iotivity.cloud.base.protocols.coap.CoapDecoder;
-import org.iotivity.cloud.base.protocols.coap.CoapEncoder;
-import org.iotivity.cloud.base.protocols.coap.CoapLogHandler;
-import org.iotivity.cloud.base.protocols.coap.CoapResponse;
-
 import io.netty.bootstrap.Bootstrap;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.ChannelHandler;
+import io.netty.channel.*;
 import io.netty.channel.ChannelHandler.Sharable;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInitializer;
-import io.netty.channel.ChannelOption;
-import io.netty.channel.ChannelPipeline;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.SimpleChannelInboundHandler;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioSocketChannel;
 import io.netty.handler.ssl.SslContext;
 import io.netty.handler.ssl.SslContextBuilder;
 import io.netty.handler.ssl.SslProvider;
+import io.netty.handler.timeout.IdleState;
+import io.netty.handler.timeout.IdleStateEvent;
+import io.netty.handler.timeout.IdleStateHandler;
+import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.protocols.coap.*;
+import org.iotivity.cloud.base.protocols.coap.PingMessage;
+import org.iotivity.cloud.util.Log;
+
+import javax.net.ssl.SSLException;
+import java.io.File;
+import java.net.InetSocketAddress;
+import java.util.*;
 
 public class CoapConnector {
 
@@ -75,12 +65,30 @@ public class CoapConnector {
         }
     }
 
+    public static class KeepAliveHandler extends ChannelDuplexHandler {
+        @Override
+        public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
+                throws Exception {
+            if (evt instanceof IdleStateEvent) {
+                IdleStateEvent event = (IdleStateEvent) evt;
+                if (event.state() == IdleState.WRITER_IDLE) {
+                    ctx.writeAndFlush(PingMessage.build());
+                }
+                if (event.state() == IdleState.READER_IDLE) {
+                    Log.d("Connection with" +  ctx.channel().remoteAddress().toString() + "is idle. Closing connection.");
+                    ctx.close();
+                }
+            }
+        }
+    }
+
     public static class CoapConnectorInitializer
             extends ChannelInitializer<SocketChannel> {
 
         private List<ChannelHandler> additionalHandlers = new ArrayList<>();
 
         private Boolean              mTlsMode           = false;
+        private Boolean              mKeepAlive         = false;
         InetSocketAddress            mInetSocketAddress = null;
         String                       mRootCertFiePath   = null;
 
@@ -88,6 +96,10 @@ public class CoapConnector {
             this.mTlsMode = tlsMode;
         }
 
+        public void setKeepAlive(Boolean keepAlive) {
+            this.mKeepAlive = keepAlive;
+        }
+
         public void setInetSocketAddress(InetSocketAddress address) {
             this.mInetSocketAddress = address;
         }
@@ -127,6 +139,12 @@ public class CoapConnector {
             p.addLast(new CoapDecoder());
             p.addLast(new CoapEncoder());
             p.addLast(new CoapLogHandler());
+
+            if (mKeepAlive.equals(true)) {
+                p.addLast(new IdleStateHandler(100, 45, 0));
+                p.addLast(new KeepAliveHandler());
+            }
+
             for (ChannelHandler handler : additionalHandlers) {
                 p.addLast(handler);
             }
@@ -134,12 +152,12 @@ public class CoapConnector {
     }
 
     HashMap<Channel, CoapClient> mChannelMap     = new HashMap<>();
-
     Bootstrap                    mBootstrap      = new Bootstrap();
     EventLoopGroup               mConnectorGroup = new NioEventLoopGroup();
+    Timer                        mTimer          = new Timer();
 
-    public CoapClient connect(final InetSocketAddress inetSocketAddress,
-            boolean tlsMode) throws InterruptedException {
+    public void connect(final String connectionName, final InetSocketAddress inetSocketAddress,
+            boolean tlsMode, boolean keepAlive) {
 
         CoapConnectorInitializer initializer = new CoapConnectorInitializer();
 
@@ -149,17 +167,47 @@ public class CoapConnector {
             initializer.setRootCertFilePath(OICConstants.ROOT_CERT_FILE);
         }
 
+        initializer.setKeepAlive(keepAlive);
         initializer.addHandler(new CoapPacketHandler());
         mBootstrap.handler(initializer);
+        doConnect(connectionName, inetSocketAddress, tlsMode);
+    }
+
+    private void doConnect(final String connectionName, final InetSocketAddress inetSocketAddress, final boolean tlsMode) {
+        mBootstrap.connect(inetSocketAddress).addListener(new ChannelFutureListener() {
+                @Override public void operationComplete(ChannelFuture future) throws Exception {
+                    if(!future.isSuccess()) {
+                        Log.d("Connection to " + inetSocketAddress.getHostString() + " was not successful. Retrying...");
+                        future.channel().close();
+                        scheduleConnect(connectionName, inetSocketAddress, tlsMode, 5000);
+                    } else {
+                        connectionEstablished(connectionName, future.channel());
+                        addCloseDetectListener(future.channel());
+                    }
+                }
 
-        ChannelFuture channelFuture = null;
-        channelFuture = mBootstrap.connect(inetSocketAddress).sync();
+            private void addCloseDetectListener(Channel channel) {
+                channel.closeFuture().addListener((ChannelFutureListener) future -> {
+                    Log.d("Connection to " + inetSocketAddress.getHostString() + " was lost. Retrying...");
+                    scheduleConnect(connectionName, inetSocketAddress, tlsMode, 5);
+                });
+            }
+        });
+    }
 
-        CoapClient coapClient = null;
-        coapClient = new CoapClient(channelFuture.channel());
-        mChannelMap.put(channelFuture.channel(), coapClient);
+    private void scheduleConnect(String connectionName, InetSocketAddress inetSocketAddress, boolean tlsMode, long millis) {
+        mTimer.schedule( new TimerTask() {
+            @Override
+            public void run() {
+                doConnect(connectionName, inetSocketAddress, tlsMode);
+            }
+        }, millis );
+    }
 
-        return coapClient;
+    public void connectionEstablished(String connectionName, Channel channel) {
+        CoapClient coapClient = new CoapClient(channel);
+        mChannelMap.put(channel, coapClient);
+        ConnectorPool.addConnection(connectionName, coapClient);
     }
 
     public void disconenct() throws Exception {
index bf57799..84f4eac 100644 (file)
@@ -23,13 +23,13 @@ package org.iotivity.cloud.base.connector;
 
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.iotivity.cloud.base.device.IRequestChannel;
 
 public class ConnectorPool {
 
-    static HashMap<String, IRequestChannel> mConnection = new HashMap<>();
+    static ConcurrentHashMap<String, IRequestChannel> mConnection = new ConcurrentHashMap<>();
 
     static CoapConnector                    mConnector  = new CoapConnector();
 
@@ -37,9 +37,9 @@ public class ConnectorPool {
 
     }
 
-    public static void addConnection(String name, InetSocketAddress inetAddr,
-            boolean tlsMode) throws InterruptedException {
-        mConnection.put(name, mConnector.connect(inetAddr, tlsMode));
+    public static void requestConnection(String connectionName, InetSocketAddress inetAddr,
+         boolean tlsMode, boolean keepAlive) throws InterruptedException {
+        mConnector.connect(connectionName, inetAddr, tlsMode, keepAlive);
     }
 
     public static IRequestChannel getConnection(String name) {
@@ -49,4 +49,10 @@ public class ConnectorPool {
     public static ArrayList<IRequestChannel> getConnectionList() {
         return new ArrayList<IRequestChannel>(mConnection.values());
     }
+
+    public static void addConnection(String name, IRequestChannel requestChannel) {
+        if (mConnection.containsKey(name))
+            mConnection.remove(name);
+        mConnection.put(name, requestChannel);
+    }
 }
diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/PingMessage.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/PingMessage.java
new file mode 100644 (file)
index 0000000..287c442
--- /dev/null
@@ -0,0 +1,16 @@
+package org.iotivity.cloud.base.protocols.coap;
+
+import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+
+public final class PingMessage extends CoapRequest {
+    public PingMessage() {
+        super(RequestMethod.PUT);
+        setUriPath(OICConstants.KEEP_ALIVE_FULL_URI);
+    }
+
+    public static PingMessage build() {
+        return new PingMessage();
+    }
+}
+
diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/resource/CloudPingResource.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/resource/CloudPingResource.java
new file mode 100644 (file)
index 0000000..bee2f04
--- /dev/null
@@ -0,0 +1,34 @@
+package org.iotivity.cloud.base.resource;
+
+import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.device.Device;
+import org.iotivity.cloud.base.exception.ServerException;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.IResponse;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+
+import java.util.Arrays;
+
+public class CloudPingResource extends Resource {
+    public CloudPingResource() {
+        super(Arrays.asList(OICConstants.PREFIX_OIC, OICConstants.KEEP_ALIVE_URI));
+    }
+
+    @Override
+    public void onDefaultRequestReceived(Device srcDevice, IRequest request)
+            throws ServerException {
+
+        IResponse response;
+        switch (request.getMethod()) {
+            case PUT:
+                response = MessageBuilder.createResponse(request, ResponseStatus.VALID);
+                break;
+
+            default:
+                throw new ServerException.BadRequestException(
+                        request.getMethod() + " request type is not support");
+        }
+        srcDevice.sendResponse(response);
+    }
+}
index 737d4cd..c054fa3 100644 (file)
@@ -56,14 +56,11 @@ public class Log {
         createfile();
     }
 
-    public static void InitWebLog(String weblogHostname, String port,
-            String serverName) {
+    public static void InitWebLog(String webLogHost, String serverName) {
         mServerName = serverName;
-
-        if (weblogHostname != null) {
+        if (webLogHost != null) {
             try {
-                String LogServerAddr = "ws://" + weblogHostname + ":" + port;
-                websocketpoint = new WebsocketLog(new URI(LogServerAddr));
+                websocketpoint = new WebsocketLog(new URI("ws://" + webLogHost));
                 websocketpoint.start();
             } catch (Exception e) {
                 System.out.println(e.getMessage());