Add CoAP over Websocket interface in cloud
[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 < 8 || 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], CloudInterfaceServer.class
78                     .getSimpleName().toString());
79         }
80
81         ConnectorPool.addConnection("rd", new InetSocketAddress(args[1],
82                 Integer.parseInt(args[2])), tlsMode);
83         ConnectorPool.addConnection("account", new InetSocketAddress(args[3],
84                 Integer.parseInt(args[4])), tlsMode);
85         ConnectorPool.addConnection("mq", new InetSocketAddress(args[5],
86                 Integer.parseInt(args[6])), tlsMode);
87
88         DeviceServerSystem deviceServer = new DeviceServerSystem();
89
90         Account acHandler = new Account();
91         AccountSession acSessionHandler = new AccountSession();
92         ResourceDirectory rdHandler = new ResourceDirectory();
93         ResourceFind resHandler = new ResourceFind();
94         ResourcePresence adHandler = new ResourcePresence();
95         DevicePresence prsHandler = new DevicePresence();
96         MessageQueue mqHandler = new MessageQueue();
97         Acl aclHandler = new Acl();
98         AclGroup aclGroupHandler = new AclGroup();
99         Certificate certHandler = new Certificate();
100         AclInvite aclInviteHandler = new AclInvite();
101         Crl crlHandler = new Crl();
102         CoapDevicePool devicePool = deviceServer.getDevicePool();
103
104         deviceServer.addResource(acHandler);
105
106         deviceServer.addResource(acSessionHandler);
107
108         deviceServer.addResource(rdHandler);
109
110         deviceServer.addResource(resHandler);
111
112         deviceServer.addResource(adHandler);
113
114         deviceServer.addResource(prsHandler);
115
116         deviceServer.addResource(mqHandler);
117
118         deviceServer.addResource(aclHandler);
119
120         deviceServer.addResource(aclGroupHandler);
121
122         deviceServer.addResource(certHandler);
123
124         deviceServer.addResource(aclInviteHandler);
125
126         deviceServer.addResource(crlHandler);
127
128         KeepAliveResource resKeepAlive = new KeepAliveResource(
129                 new int[] { 1, 2, 4, 8 });
130
131         deviceServer.addResource(resKeepAlive);
132
133         deviceServer.addResource(new RouteResource(devicePool));
134
135         deviceServer.addServer(new CoapServer(new InetSocketAddress(coapPort)));
136
137         // Add HTTP Server for HTTP-to-CoAP Proxy
138         if (hcProxyMode) {
139             deviceServer.addServer(new HttpServer(new InetSocketAddress(
140                     hcProxyPort)));
141         }
142
143         if (websocketMode) {
144             deviceServer.addServer(new WebSocketServer(new InetSocketAddress(
145                     websocketPort)));
146         }
147
148         deviceServer.startSystem(tlsMode);
149
150         resKeepAlive.startSessionChecker(3000, 6000);
151
152         Scanner in = new Scanner(System.in);
153
154         System.out.println("press 'q' to terminate");
155
156         while (!in.nextLine().equals("q"));
157
158         in.close();
159
160         System.out.println("Terminating...");
161
162         resKeepAlive.stopSessionChecker();
163
164         deviceServer.stopSystem();
165
166         System.out.println("Terminated");
167     }
168 }