[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / examples-java / simpleserver / src / main / java / org / iotivity / base / examples / SimpleServer.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2015 Intel Corporation.
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
23 package org.iotivity.base.examples;
24
25 import org.iotivity.base.ModeType;
26 import org.iotivity.base.OcException;
27 import org.iotivity.base.OcPlatform;
28 import org.iotivity.base.PlatformConfig;
29 import org.iotivity.base.QualityOfService;
30 import org.iotivity.base.ServiceType;
31
32 import java.util.LinkedList;
33 import java.util.List;
34
35 /**
36  * SimpleServer
37  * <p/>
38  * SimpleServer is a sample OIC server application.
39  * It creates a Light and waits for the incoming client calls to handle
40  * various request scenarios.
41  */
42 public class SimpleServer {
43
44     static List<Light> lights = new LinkedList<>();
45
46     /**
47      * A local method to configure and initialize platform, and then create a light resource.
48      */
49     private static void startSimpleServer() {
50
51         PlatformConfig platformConfig = new PlatformConfig(
52                 ServiceType.IN_PROC,
53                 ModeType.SERVER,
54                 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
55                 0,         // Uses randomly available port
56                 QualityOfService.LOW
57         );
58
59         msg("Configuring platform.");
60         OcPlatform.Configure(platformConfig);
61
62         createNewLightResource("/a/light", "John's light");
63
64         msg("Waiting for the requests...");
65         printLine();
66     }
67
68     public static void createNewLightResource(String resourceUri, String resourceName){
69         msg("Creating a light");
70         Light light = new Light(
71                 resourceUri,     //URI
72                 resourceName,    //name
73                 false,           //state
74                 0                //power
75         );
76         msg(light.toString());
77
78         msg("Registering light as a resource");
79         try {
80             light.registerResource();
81         } catch (OcException e) {
82             msgError(TAG, e.toString());
83             msg("Failed to register a light resource");
84         }
85         lights.add(light);
86     }
87
88     private static void stopSimpleServer() {
89         for (Light light : lights) {
90             try {
91                 light.unregisterResource();
92             } catch (OcException e) {
93                 msgError(TAG, e.toString());
94                 msg("Failed to unregister a light resource");
95             }
96         }
97         lights.clear();
98
99         msg("All created resources have been unregistered");
100         printLine();
101     }
102
103     //******************************************************************************
104     // End of the OIC specific code
105     //******************************************************************************
106
107     private final static String TAG = SimpleServer.class.getSimpleName();
108
109     public static void main(String[] args) {
110         startSimpleServer();
111     }
112
113     public void sleep(int seconds) {
114         try {
115             Thread.sleep(seconds * 1000);
116         } catch (InterruptedException e) {
117             e.printStackTrace();
118             msgError(TAG, e.toString());
119         }
120     }
121
122     public static void msg(final String text) {
123         System.out.println("[O]" + TAG + " | " + text);
124     }
125
126     public static void msg(final String tag, final String text) {
127         System.out.println("[O]" + tag + " | " + text);
128     }
129
130     public static void msgError(final String tag ,final String text) {
131         System.out.println("[E]" + tag + " | " + text);
132     }
133
134     public static void printLine() {
135         msg("------------------------------------------------------------------------");
136     }
137
138 }