Imported Upstream version 1.2.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.connector.ConnectorPool;
28 import org.iotivity.cloud.base.server.CoapServer;
29 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
30 import org.iotivity.cloud.ciserver.resources.DiResource;
31 import org.iotivity.cloud.ciserver.resources.KeepAliveResource;
32 import org.iotivity.cloud.ciserver.resources.proxy.account.Account;
33 import org.iotivity.cloud.ciserver.resources.proxy.account.AccountSession;
34 import org.iotivity.cloud.ciserver.resources.proxy.account.Acl;
35 import org.iotivity.cloud.ciserver.resources.proxy.account.AclGroup;
36 import org.iotivity.cloud.ciserver.resources.proxy.account.AclInvite;
37 import org.iotivity.cloud.ciserver.resources.proxy.account.Certificate;
38 import org.iotivity.cloud.ciserver.resources.proxy.account.Crl;
39 import org.iotivity.cloud.ciserver.resources.proxy.mq.MessageQueue;
40 import org.iotivity.cloud.ciserver.resources.proxy.rd.DevicePresence;
41 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceDirectory;
42 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceFind;
43 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourcePresence;
44 import org.iotivity.cloud.util.Log;
45
46 public class CloudInterfaceServer {
47
48     public static void main(String[] args) throws Exception {
49         Log.Init();
50
51         System.out.println("-----CI SERVER-------");
52
53         if (args.length != 8) {
54             Log.e("coap server port and RDServer_Address port AccountServer_Address Port MQBroker_Address Port and TLS mode required\n"
55                     + "ex) 5683 127.0.0.1 5684 127.0.0.1 5685 127.0.0.1 5686 0\n");
56             return;
57         }
58
59         boolean tlsMode = Integer.parseInt(args[7]) == 1;
60
61         ConnectorPool.addConnection("rd",
62                 new InetSocketAddress(args[1], Integer.parseInt(args[2])),
63                 tlsMode);
64         ConnectorPool.addConnection("account",
65                 new InetSocketAddress(args[3], Integer.parseInt(args[4])),
66                 tlsMode);
67         ConnectorPool.addConnection("mq",
68                 new InetSocketAddress(args[5], Integer.parseInt(args[6])),
69                 tlsMode);
70
71         DeviceServerSystem deviceServer = new DeviceServerSystem();
72
73         Account acHandler = new Account();
74         AccountSession acSessionHandler = new AccountSession();
75         ResourceDirectory rdHandler = new ResourceDirectory();
76         ResourceFind resHandler = new ResourceFind();
77         ResourcePresence adHandler = new ResourcePresence();
78         DevicePresence prsHandler = new DevicePresence();
79         MessageQueue mqHandler = new MessageQueue();
80         Acl aclHandler = new Acl();
81         AclGroup aclGroupHandler = new AclGroup();
82         Certificate certHandler = new Certificate();
83         AclInvite aclInviteHandler = new AclInvite();
84         Crl crlHandler = new Crl();
85         CoapDevicePool devicePool = deviceServer.getDevicePool();
86
87         deviceServer.addResource(acHandler);
88
89         deviceServer.addResource(acSessionHandler);
90
91         deviceServer.addResource(rdHandler);
92
93         deviceServer.addResource(resHandler);
94
95         deviceServer.addResource(adHandler);
96
97         deviceServer.addResource(prsHandler);
98
99         deviceServer.addResource(mqHandler);
100
101         deviceServer.addResource(aclHandler);
102
103         deviceServer.addResource(aclGroupHandler);
104
105         deviceServer.addResource(certHandler);
106
107         deviceServer.addResource(aclInviteHandler);
108
109         deviceServer.addResource(crlHandler);
110
111         KeepAliveResource resKeepAlive = new KeepAliveResource(
112                 new int[] { 1, 2, 4, 8 });
113
114         deviceServer.addResource(resKeepAlive);
115
116         deviceServer.addResource(new DiResource(devicePool));
117
118         deviceServer.addServer(new CoapServer(
119                 new InetSocketAddress(Integer.parseInt(args[0]))));
120
121         // deviceServer.addServer(new HttpServer(new InetSocketAddress(8080)));
122
123         deviceServer.startSystem(tlsMode);
124
125         resKeepAlive.startSessionChecker(3000, 6000);
126
127         Scanner in = new Scanner(System.in);
128
129         System.out.println("press 'q' to terminate");
130
131         while (!in.nextLine().equals("q"));
132
133         in.close();
134
135         System.out.println("Terminating...");
136
137         resKeepAlive.stopSessionChecker();
138
139         deviceServer.stopSystem();
140
141         System.out.println("Terminated");
142     }
143 }