Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / OCLib / InProcServerWrapper.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 #include <random>
7 #include <cstring>
8 #include <cstdlib>
9 #include <iostream>
10 #include <algorithm>
11
12 #include "InProcServerWrapper.h"
13 #include <InitializeException.h>
14 #include <OCReflect.h>
15 #include <ocstack.h>
16
17
18 using namespace OC::OCReflect;
19
20 using namespace std;
21
22 void entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest* eHandlerReq)
23 {
24     cout << "Resource Handler: " << eHandlerReq->resource << endl;
25     cout << "Method: "           << eHandlerReq->method << endl;
26     cout << "reqJSONPayLoad: "   << eHandlerReq->reqJSONPayload << endl;
27 }
28
29 namespace OC
30 {
31     InProcServerWrapper::InProcServerWrapper(PlatformConfig cfg)
32     {
33         OCStackResult result = OCInit(cfg.ipAddress.c_str(), cfg.port, OC_SERVER);
34
35         if(OC_STACK_OK != result)
36         {
37             throw InitializeException("Error Initializing Stack", result);
38         }
39                 
40                 m_threadRun = true;
41         m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
42     }
43         
44         void InProcServerWrapper::processFunc()
45     {
46         while(m_threadRun)
47         {
48                         OCStackResult result;
49                         {
50                                 std::lock_guard<std::mutex> lock(m_csdkLock);
51                                 result = OCProcess();
52                         }
53
54             if(result != OC_STACK_OK)
55             {
56                 cout << "Something wrong in OCProcess" << endl;
57                 // TODO: SASHI
58             }
59
60             std::this_thread::yield();
61             // To minimize CPU utilization we may wish to do this with sleep
62                         //std::this_thread::sleep_for(std::chrono::milliseconds(1));
63         }
64     }
65
66     void InProcServerWrapper::registerResource(const std::string& resourceURI,
67                                 const std::string& resourceTypeName,
68                                 named_property_binding_vector properties)
69     {
70         using OC::OCReflect::property_type;
71         using OC::OCReflect::named_property_binding;
72         using namespace OC::OCReflect::to_OCStack;
73
74         std::vector<std::string> reps { convert(properties) };
75
76         for(const auto& r : reps)
77             std::cout << r << '\n';
78
79         char *resourTypeRepresentation = flatten(reps);
80
81         std::cout << resourTypeRepresentation << "\n";
82
83         OCResourceHandle resourceHandle;
84
85                 {
86                         std::lock_guard<std::mutex> lock(m_csdkLock);
87
88             cout << "Creating a resource" << endl;
89
90                         OCStackResult  result;
91
92                         result = OCCreateResource(&resourceHandle, // OCResourceHandle *handl
93                                                         resourceTypeName.c_str(), // const char * resourceTypeName
94                                                         resourTypeRepresentation, //const char * resourceTypeRepresentation
95                                                         "core.rw", //const char * resourceInterfaceName
96                                                         OC_REST_GET | OC_REST_PUT, // uint8_t allowedMethods
97                                                         resourceURI.c_str(), // const char * uri
98                                                         entityHandler, // OCEntityHandler entityHandler
99                                                         OC_DISCOVERABLE | OC_OBSERVABLE // uint8_t resourceProperties
100                                                         );
101
102             if(result != OC_STACK_OK)
103             {
104                 cout << "Something wrong in OCCreateResource" << endl;
105                 // TODO: SASHI
106             }
107             else
108             {
109                 cout << "Resource creation is successful" << endl;
110             }
111             
112                 }
113
114     }
115
116     InProcServerWrapper::~InProcServerWrapper()
117     {
118                 if(m_processThread.joinable())
119         {
120             m_threadRun = false;
121             m_processThread.join();
122         }
123         
124         OCStop();
125     }
126 }