619c57b40303153e7ec0c5aa6101f2e7ebfdad05
[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.connector.ConnectorPool;
28 import org.iotivity.cloud.base.server.CoapServer;
29 import org.iotivity.cloud.base.server.HttpServer;
30 import org.iotivity.cloud.base.server.WebSocketServer;
31 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
32 import org.iotivity.cloud.ciserver.resources.KeepAliveResource;
33 import org.iotivity.cloud.ciserver.resources.RouteResource;
34 import org.iotivity.cloud.ciserver.resources.proxy.account.Account;
35 import org.iotivity.cloud.ciserver.resources.proxy.account.AccountSession;
36 import org.iotivity.cloud.ciserver.resources.proxy.account.Acl;
37 import org.iotivity.cloud.ciserver.resources.proxy.account.AclGroup;
38 import org.iotivity.cloud.ciserver.resources.proxy.account.AclInvite;
39 import org.iotivity.cloud.ciserver.resources.proxy.account.Certificate;
40 import org.iotivity.cloud.ciserver.resources.proxy.account.Crl;
41 import org.iotivity.cloud.ciserver.resources.proxy.mq.MessageQueue;
42 import org.iotivity.cloud.ciserver.resources.proxy.rd.DevicePresence;
43 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceDirectory;
44 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceFind;
45 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourcePresence;
46 import org.iotivity.cloud.util.Log;
47
48 public class CloudInterfaceServer {
49
50     public static void main(String[] args) throws Exception {
51
52         Log.Init();
53
54         System.out.println("-----CI SERVER-------");
55
56         if (!(args.length == 10 || args.length == 12)) {
57             Log.e("\nCoAP-server <Port> and RD-server <Address> <Port> Account-server <Address> <Port> "
58                     + "MQ-broker <Address> <Port> HC-proxy <HTTP-port> Websocket-server <Port> and TLS-mode <0|1> are required.\n"
59                     + "and WebSocketLog-Server <Address> <Port> (optional)\n"
60                     + "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");
61             return;
62         }
63
64         // CoAP-TCP server port
65         int coapPort = Integer.parseInt(args[0]);
66         // HTTP-CoAP proxy server port
67         int hcProxyPort = Integer.parseInt(args[7]);
68         // CoAP-Websocket server port
69         int websocketPort = Integer.parseInt(args[8]);
70
71         boolean hcProxyMode = hcProxyPort > 0;
72         boolean websocketMode = websocketPort > 0;
73
74         boolean tlsMode = Integer.parseInt(args[9]) == 1;
75
76         if (args.length >= 11) {
77             Log.InitWebLog(args[10], args[11],
78                     CloudInterfaceServer.class.getSimpleName().toString());
79         }
80
81         ConnectorPool.addConnection("rd",
82                 new InetSocketAddress(args[1], Integer.parseInt(args[2])),
83                 tlsMode);
84         ConnectorPool.addConnection("account",
85                 new InetSocketAddress(args[3], Integer.parseInt(args[4])),
86                 tlsMode);
87         ConnectorPool.addConnection("mq",
88                 new InetSocketAddress(args[5], Integer.parseInt(args[6])),
89                 tlsMode);
90
91         DeviceServerSystem deviceServer = new DeviceServerSystem();
92
93         Account acHandler = new Account();
94         AccountSession acSessionHandler = new AccountSession();
95         ResourceDirectory rdHandler = new ResourceDirectory();
96         ResourceFind resHandler = new ResourceFind();
97         ResourcePresence adHandler = new ResourcePresence();
98         DevicePresence prsHandler = new DevicePresence();
99         MessageQueue mqHandler = new MessageQueue();
100         Acl aclHandler = new Acl();
101         AclGroup aclGroupHandler = new AclGroup();
102         Certificate certHandler = new Certificate();
103         AclInvite aclInviteHandler = new AclInvite();
104         Crl crlHandler = new Crl();
105         CoapDevicePool devicePool = deviceServer.getDevicePool();
106
107         deviceServer.addResource(acHandler);
108
109         deviceServer.addResource(acSessionHandler);
110
111         deviceServer.addResource(rdHandler);
112
113         deviceServer.addResource(resHandler);
114
115         deviceServer.addResource(adHandler);
116
117         deviceServer.addResource(prsHandler);
118
119         deviceServer.addResource(mqHandler);
120
121         deviceServer.addResource(aclHandler);
122
123         deviceServer.addResource(aclGroupHandler);
124
125         deviceServer.addResource(certHandler);
126
127         deviceServer.addResource(aclInviteHandler);
128
129         deviceServer.addResource(crlHandler);
130
131         KeepAliveResource resKeepAlive = new KeepAliveResource(
132                 new int[] { 1, 2, 4, 8 });
133
134         deviceServer.addResource(resKeepAlive);
135
136         deviceServer.addResource(new RouteResource(devicePool));
137
138         deviceServer.addServer(new CoapServer(new InetSocketAddress(coapPort)));
139
140         // Add HTTP Server for HTTP-to-CoAP Proxy
141         if (hcProxyMode) {
142             deviceServer.addServer(
143                     new HttpServer(new InetSocketAddress(hcProxyPort)));
144         }
145
146         if (websocketMode) {
147             deviceServer.addServer(
148                     new WebSocketServer(new InetSocketAddress(websocketPort)));
149         }
150
151         deviceServer.startSystem(tlsMode);
152
153         resKeepAlive.startSessionChecker(3000, 6000);
154
155         Scanner in = new Scanner(System.in);
156
157         System.out.println("press 'q' to terminate");
158
159         while (!in.nextLine().equals("q"));
160
161         in.close();
162
163         System.out.println("Terminating...");
164
165         resKeepAlive.stopSessionChecker();
166
167         deviceServer.stopSystem();
168
169         System.out.println("Terminated");
170     }
171 }