Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / examples / OCWrapper / testServerApp.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 ///
7 /// This sample provides steps to define an interface for a resource
8 /// (properties and methods) and host this resource on the server. 
9 ///
10
11 #include <functional>
12
13 #include <OCServer.h>
14 #include <OCReflect.h>
15 #include <OCPlatform.h>
16 #include <OCApi.h>
17 #include <iostream>
18
19 using namespace std;
20
21 using namespace OC;
22 using namespace OC::OCReflect;
23
24 /// This class represents a single resource named 'light'. This resource has 
25 /// two simple properties named 'state' and 'power' and they have respective setter
26 /// and getter methods.
27
28 class light
29 {
30 private:
31         /// Access this property from a TB client 
32         bool m_state;
33         int m_power;
34
35 public:
36         light()
37                 : m_power(0),
38                 m_state(false)
39         {}
40
41 public:
42         /// Setter method for the setting the power of this light resource
43         void setPower(int powerValue)             
44         { 
45                 m_power = powerValue; 
46         }
47
48         /// Getter method for the getting the power of this light resource
49         int getPower() const                           
50         { 
51                 return m_power; 
52         }
53
54         /// Setter method for the setting the state of this light resource
55         void setState(bool state)             
56         { 
57                 m_state = state; 
58         }
59
60         /// Getter method for the getting the state of this light resource
61         bool getState() const                           
62         { 
63                 return m_state; 
64         }
65         
66 public:
67         /* Note that this does not need to be a member function: for classes you do not have
68         access to, you can accomplish this with a free function: */
69         
70         /// This function binds the properties and methods to the server. 
71         void bindTo(OC::OCPlatform& platform)
72         {
73                 using OC::OCReflect::property_type;
74                 using OC::OCReflect::named_property_binding;
75
76                 named_property_binding_vector properties {
77                         named_property_binding("state", property_type::boolean),
78                         named_property_binding("power", property_type::integer)
79                 };
80
81                 std::string resourceURI = "/a/light";
82                 std::string resourceTypeName = "light";
83                 platform.registerResource(resourceURI, resourceTypeName, properties);
84         }
85 };
86
87 int main()
88 {
89         // Step1: Create a OCPlatform instance. 
90         // Step1.1: The constructor of OCPlatform class takes PlatformConfig object.
91         // Note: Platform creation is synchronous call. 
92
93         PlatformConfig cfg;
94         cfg.ipAddress = "192.168.1.5";
95         cfg.port = 8080;
96         cfg.mode = ModeType::Server;
97         cfg.serviceType = ServiceType::InProc;
98
99     cout << "Creating OCPlatform .. \n";
100         
101         OCPlatform platform(cfg);
102         
103         // Step2: Create the instance of the resource class (in this case instance of class 'light'). 
104         // Step2.1: Invoke bindTo function of class light. 
105         
106         light myLight;
107         myLight.bindTo(platform);
108
109         while(true)
110         {
111                 //some tasks
112         }
113
114         // No explicit call to stop the platform. 
115         // When OCPlatform destructor is invoked, internally we do platform cleanup
116 }