iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / examples / old_tests / OCWrapper / testServerApp.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 ///
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server. 
24 ///
25
26 #include <functional>
27
28 #include <OCServer.h>
29 #include <OCReflect.h>
30 #include <OCPlatform.h>
31 #include <OCApi.h>
32 #include <iostream>
33
34 using namespace std;
35
36 using namespace OC;
37 using namespace OC::OCReflect;
38
39 /// This class represents a single resource named 'light'. This resource has 
40 /// two simple properties named 'state' and 'power' and they have respective setter
41 /// and getter methods.
42
43 class light
44 {
45 private:
46         /// Access this property from a TB client 
47         bool m_state;
48         int m_power;
49
50 public:
51         light()
52                 : m_power(0),
53                 m_state(false)
54         {}
55
56 public:
57         /// Setter method for the setting the power of this light resource
58         void setPower(int powerValue)             
59         { 
60                 m_power = powerValue; 
61         }
62
63         /// Getter method for the getting the power of this light resource
64         int getPower() const                           
65         { 
66                 return m_power; 
67         }
68
69         /// Setter method for the setting the state of this light resource
70         void setState(bool state)             
71         { 
72                 m_state = state; 
73         }
74
75         /// Getter method for the getting the state of this light resource
76         bool getState() const                           
77         { 
78                 return m_state; 
79         }
80         
81 public:
82         /* Note that this does not need to be a member function: for classes you do not have
83         access to, you can accomplish this with a free function: */
84         
85         /// This function binds the properties and methods to the server. 
86         void bindTo(OC::OCPlatform& platform)
87         {
88                 using OC::OCReflect::property_type;
89                 using OC::OCReflect::property_binding;
90
91                 property_binding_vector properties {
92                         property_binding("state", property_type::boolean),
93                         property_binding("power", property_type::integer)
94                 };
95
96                 std::string resourceURI = "/a/light";
97                 std::string resourceTypeName = "light";
98                 platform.registerResource(resourceURI, resourceTypeName, properties);
99         }
100 };
101
102 int main()
103 {
104         // Step1: Create a OCPlatform instance. 
105         // Step1.1: The constructor of OCPlatform class takes PlatformConfig object.
106         // Note: Platform creation is synchronous call. 
107
108         PlatformConfig cfg;
109         cfg.ipAddress = "192.168.1.5";
110         cfg.port = 8080;
111         cfg.mode = ModeType::Server;
112         cfg.serviceType = ServiceType::InProc;
113
114     cout << "Creating OCPlatform .. \n";
115         
116         OCPlatform platform(cfg);
117         
118         // Step2: Create the instance of the resource class (in this case instance of class 'light'). 
119         // Step2.1: Invoke bindTo function of class light. 
120         
121         light myLight;
122         myLight.bindTo(platform);
123
124         while(true)
125         {
126                 //some tasks
127         }
128
129         // No explicit call to stop the platform. 
130         // When OCPlatform destructor is invoked, internally we do platform cleanup
131 }