Implement reconnection and ping-pong between cloud services. Make
[iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / ResourceDirectoryServer.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.rdserver;
23
24 import java.net.InetSocketAddress;
25 import java.util.Scanner;
26
27 import org.iotivity.cloud.base.ServerSystem;
28 import org.iotivity.cloud.base.resource.CloudPingResource;
29 import org.iotivity.cloud.base.server.CoapServer;
30 import org.iotivity.cloud.rdserver.db.DBManager;
31 import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
32 import org.iotivity.cloud.rdserver.resources.directory.res.DiscoveryResource;
33 import org.iotivity.cloud.rdserver.resources.presence.device.DevicePresenceResource;
34 import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresenceResource;
35 import org.iotivity.cloud.util.Log;
36
37 /**
38  *
39  * This class is in charge of running of resource directory server.
40  *
41  */
42 public class ResourceDirectoryServer {
43
44     private static int     coapServerPort;
45     private static boolean tlsMode;
46     private static String  databaseHost;
47     private static String  webLogHost;
48
49     public static void main(String[] args) throws Exception {
50         System.out.println("-----RD SERVER-----");
51         Log.Init();
52
53         if (!parseConfiguration(args)) {
54             Log.e("\nCoAP-server <Port> Database <Address> <Port> TLS-mode <0|1> are required. WebSocketLog-Server <Addres> <Port> is optional.\n"
55                     + "ex) " + Constants.DEFAULT_COAP_PORT
56                     + " 127.0.0.1 27017 0\n");
57             return;
58         }
59         if (webLogHost != null)
60             Log.InitWebLog(webLogHost,
61                     ResourceDirectoryServer.class.getSimpleName().toString());
62
63         DBManager.createInstance(databaseHost);
64
65         ServerSystem serverSystem = new ServerSystem();
66
67         serverSystem.addResource(new CloudPingResource());
68         serverSystem.addResource(new ResourceDirectoryResource());
69         serverSystem.addResource(new DiscoveryResource());
70         serverSystem.addResource(new DevicePresenceResource());
71         serverSystem.addResource(new ResPresenceResource());
72
73         serverSystem.addServer(
74                 new CoapServer(new InetSocketAddress(coapServerPort)));
75
76         serverSystem.startSystem(tlsMode);
77
78         Scanner in = new Scanner(System.in);
79
80         System.out.println("press 'q' to terminate");
81
82         while (!in.nextLine().equals("q"));
83
84         in.close();
85
86         System.out.println("Terminating...");
87
88         serverSystem.stopSystem();
89
90         System.out.println("Terminated");
91     }
92
93     private static boolean parseConfiguration(String[] args) {
94         // configuration provided by arguments
95         if (args.length == 4 || args.length == 6) {
96             coapServerPort = Integer.parseInt(args[0]);
97             databaseHost = args[1] + ":" + args[2];
98             tlsMode = Integer.parseInt(args[3]) == 1;
99             if (args.length == 6)
100                 webLogHost = args[4] + ":" + args[5];
101             return true;
102         }
103         // configuration provided by docker env
104         String tlsModeEnv = System.getenv("TLS_MODE");
105         if (tlsModeEnv != null) {
106             coapServerPort = Constants.DEFAULT_COAP_PORT;
107             databaseHost = System.getenv("MONGODB_ADDRESS") + ":"
108                     + System.getenv("MONGODB_PORT");
109             tlsMode = Integer.parseInt(tlsModeEnv) == 1;
110             return true;
111         }
112         return false;
113     }
114 }