Merge branch '1.1-rel'
[platform/upstream/iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / CloudInterfaceServer.java
index 31513e6..a165393 100644 (file)
@@ -22,6 +22,7 @@
 package org.iotivity.cloud.ciserver;
 
 import java.net.InetSocketAddress;
+import java.util.Scanner;
 
 import org.iotivity.cloud.base.CoapServer;
 import org.iotivity.cloud.base.ResourceManager;
@@ -31,18 +32,12 @@ import org.iotivity.cloud.ciserver.protocols.CoapRelayHandler;
 import org.iotivity.cloud.ciserver.resources.KeepAliveResource;
 import org.iotivity.cloud.util.CoapLogHandler;
 import org.iotivity.cloud.util.Logger;
-import org.iotivity.cloud.util.Net;
 
 public class CloudInterfaceServer {
 
     public static void main(String[] args) throws Exception {
 
         System.out.println("-----CI SERVER-------");
-        String hostAddress = Net.getMyIpAddress();
-        if (hostAddress.equals("") == true) {
-            Logger.e("cannot find host address.");
-            return;
-        }
 
         if (args.length != 5) {
             Logger.e(
@@ -55,27 +50,62 @@ public class CloudInterfaceServer {
         SessionManager sessionManager = null;
         CoapServer coapServer = null;
 
+        CoapRelayHandler relayHandler = null;
+        CoapAuthHandler authHandler = null;
+
+        KeepAliveResource keepAliveResource = null;
+
         coapServer = new CoapServer();
 
         sessionManager = new SessionManager();
 
-        resourceManager = new ResourceManager(sessionManager);
+        resourceManager = new ResourceManager();
+
+        relayHandler = new CoapRelayHandler(sessionManager);
 
-        coapServer.addHandler(
-                new CoapAuthHandler(args[3], Integer.parseInt(args[4])));
+        authHandler = new CoapAuthHandler();
+
+        keepAliveResource = new KeepAliveResource(sessionManager,
+                new int[] { 1, 2, 4, 8 });
 
         coapServer.addHandler(new CoapLogHandler());
 
-        // Comment the following one line to make CI server run alone
-        coapServer.addHandler(new CoapRelayHandler(sessionManager, args[1],
-                Integer.parseInt(args[2]), args[3], Integer.parseInt(args[4])));
+        coapServer.addHandler(authHandler);
+
+        coapServer.addHandler(relayHandler);
 
         coapServer.addHandler(resourceManager);
 
-        resourceManager.registerResource(new KeepAliveResource(sessionManager,
-                new int[] { 1, 2, 4, 8 }));
+        resourceManager.registerResource(keepAliveResource);
+
+        authHandler.startHandler(args[3], Integer.parseInt(args[4]));
+
+        relayHandler.startHandler(args[1], Integer.parseInt(args[2]), args[3],
+                Integer.parseInt(args[4]));
 
         coapServer
                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
+
+        keepAliveResource.startSessionChecker();
+
+        Scanner in = new Scanner(System.in, "UTF-8");
+
+        System.out.println("press 'q' to terminate");
+
+        while (!in.nextLine().equals("q"));
+
+        in.close();
+
+        System.out.println("Terminating...");
+
+        keepAliveResource.stopSessionChecker();
+
+        coapServer.stopServer();
+
+        relayHandler.stopHandler();
+
+        authHandler.stopHandler();
+
+        System.out.println("Terminated");
     }
 }