2 *******************************************************************
4 * Copyright 2015 Intel Corporation.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base.examples;
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;
32 import java.util.LinkedList;
33 import java.util.List;
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.
42 public class SimpleServer {
44 static List<Light> lights = new LinkedList<>();
47 * A local method to configure and initialize platform, and then create a light resource.
49 private static void startSimpleServer() {
51 PlatformConfig platformConfig = new PlatformConfig(
54 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
55 0, // Uses randomly available port
59 msg("Configuring platform.");
60 OcPlatform.Configure(platformConfig);
62 createNewLightResource("/a/light", "John's light");
64 msg("Waiting for the requests...");
68 public static void createNewLightResource(String resourceUri, String resourceName){
69 msg("Creating a light");
70 Light light = new Light(
76 msg(light.toString());
78 msg("Registering light as a resource");
80 light.registerResource();
81 } catch (OcException e) {
82 msgError(TAG, e.toString());
83 msg("Failed to register a light resource");
88 private static void stopSimpleServer() {
89 for (Light light : lights) {
91 light.unregisterResource();
92 } catch (OcException e) {
93 msgError(TAG, e.toString());
94 msg("Failed to unregister a light resource");
99 msg("All created resources have been unregistered");
103 //******************************************************************************
104 // End of the OIC specific code
105 //******************************************************************************
107 private final static String TAG = SimpleServer.class.getSimpleName();
109 public static void main(String[] args) {
113 public void sleep(int seconds) {
115 Thread.sleep(seconds * 1000);
116 } catch (InterruptedException e) {
118 msgError(TAG, e.toString());
122 public static void msg(final String text) {
123 System.out.println("[O]" + TAG + " | " + text);
126 public static void msg(final String tag, final String text) {
127 System.out.println("[O]" + tag + " | " + text);
130 public static void msgError(final String tag ,final String text) {
131 System.out.println("[E]" + tag + " | " + text);
134 public static void printLine() {
135 msg("------------------------------------------------------------------------");