Implements OIC/OCF resource models for cloud.
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / ResourceDirectoryServer.java
index 732e4d4..24dbb9a 100644 (file)
@@ -1,64 +1,80 @@
 /*
- *******************************************************************
- *
- * Copyright 2016 Samsung Electronics All Rights Reserved.
- *
- *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  */
 package org.iotivity.cloud.rdserver;
 
 import java.net.InetSocketAddress;
+import java.util.Scanner;
 
-import org.iotivity.cloud.base.CoapServer;
-import org.iotivity.cloud.base.ResourceManager;
-import org.iotivity.cloud.rdserver.resources.ResourceDirectoryResource;
+import org.iotivity.cloud.base.ServerSystem;
+import org.iotivity.cloud.base.server.CoapServer;
+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;
+import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresenceResource;
+import org.iotivity.cloud.util.ErrorLogger;
+import org.iotivity.cloud.util.FileLogger;
 import org.iotivity.cloud.util.Logger;
-import org.iotivity.cloud.util.Net;
 
 public class ResourceDirectoryServer {
 
     public static void main(String[] args) throws Exception {
+        System.setOut(FileLogger.createLoggingProxy(System.out));
 
         System.out.println("-----RD SERVER-----");
-        String hostAddress = Net.getMyIpAddress();
-        if (hostAddress.equals("") == true) {
-            Logger.e("cannot find host address.");
-            return;
-        }
 
-        if (args.length != 1) {
-            Logger.e("coap server port required");
+        if (args.length != 2) {
+            Logger.e("coap server port and TLS mode required\n"
+                    + "ex) 5684 0\n");
             return;
         }
 
-        ResourceManager resourceManager = null;
+        ErrorLogger.Init();
 
-        CoapServer coapServer = null;
+        ServerSystem serverSystem = new ServerSystem();
 
-        coapServer = new CoapServer();
+        serverSystem.addResource(new ResourceDirectoryResource());
+        serverSystem.addResource(new DiscoveryResource());
+        serverSystem.addResource(new DevicePresenceResource());
+        serverSystem.addResource(new ResPresenceResource());
 
-        resourceManager = new ResourceManager();
+        serverSystem.addServer(new CoapServer(
+                new InetSocketAddress(Integer.parseInt(args[0]))));
 
-        coapServer.addHandler(resourceManager);
+        boolean tlsMode = Integer.parseInt(args[1]) == 1;
 
-        resourceManager.registerResource(new ResourceDirectoryResource());
+        serverSystem.startSystem(tlsMode);
 
-        coapServer
-                .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
-    }
+        Scanner in = new Scanner(System.in);
+
+        System.out.println("press 'q' to terminate");
+
+        while (!in.nextLine().equals("q"));
 
+        in.close();
+
+        System.out.println("Terminating...");
+
+        serverSystem.stopSystem();
+
+        System.out.println("Terminated");
+    }
 }