Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / CloudInterfaceServer.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.ciserver;
23
24 import java.net.InetSocketAddress;
25 import java.util.Scanner;
26
27 import org.iotivity.cloud.base.CoapServer;
28 import org.iotivity.cloud.base.ResourceManager;
29 import org.iotivity.cloud.base.SessionManager;
30 import org.iotivity.cloud.ciserver.protocols.CoapAuthHandler;
31 import org.iotivity.cloud.ciserver.protocols.CoapRelayHandler;
32 import org.iotivity.cloud.ciserver.resources.KeepAliveResource;
33 import org.iotivity.cloud.util.CoapLogHandler;
34 import org.iotivity.cloud.util.Logger;
35
36 public class CloudInterfaceServer {
37
38     public static void main(String[] args) throws Exception {
39
40         System.out.println("-----CI SERVER-------");
41
42         if (args.length != 5) {
43             Logger.e(
44                     "coap server port and RDServer_Address port AccountServer_Address Port required\n"
45                             + "ex) 5683 127.0.0.1 5684 127.0.0.1 5685\n");
46             return;
47         }
48
49         ResourceManager resourceManager = null;
50         SessionManager sessionManager = null;
51         CoapServer coapServer = null;
52
53         CoapRelayHandler relayHandler = null;
54         CoapAuthHandler authHandler = null;
55
56         KeepAliveResource keepAliveResource = null;
57
58         coapServer = new CoapServer();
59
60         sessionManager = new SessionManager();
61
62         resourceManager = new ResourceManager();
63
64         relayHandler = new CoapRelayHandler(sessionManager);
65
66         authHandler = new CoapAuthHandler();
67
68         keepAliveResource = new KeepAliveResource(sessionManager,
69                 new int[] { 1, 2, 4, 8 });
70
71         coapServer.addHandler(new CoapLogHandler());
72
73         coapServer.addHandler(authHandler);
74
75         coapServer.addHandler(relayHandler);
76
77         coapServer.addHandler(resourceManager);
78
79         resourceManager.registerResource(keepAliveResource);
80
81         authHandler.startHandler(args[3], Integer.parseInt(args[4]));
82
83         relayHandler.startHandler(args[1], Integer.parseInt(args[2]), args[3],
84                 Integer.parseInt(args[4]));
85
86         coapServer
87                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
88
89         keepAliveResource.startSessionChecker();
90
91         Scanner in = new Scanner(System.in, "UTF-8");
92
93         System.out.println("press 'q' to terminate");
94
95         while (!in.nextLine().equals("q"));
96
97         in.close();
98
99         System.out.println("Terminating...");
100
101         keepAliveResource.stopSessionChecker();
102
103         coapServer.stopServer();
104
105         relayHandler.stopHandler();
106
107         authHandler.stopHandler();
108
109         System.out.println("Terminated");
110     }
111 }