Update Resource Broker Initial Version
[platform/upstream/iotivity.git] / service / basis / resourceBroker / src / ResourceBroker.cpp
1 /*
2  * ResourceBroker.cpp
3  *
4  */
5
6 #include "ResourceBroker.h"
7
8 #define OIC_COAP "coap://"
9 #define DEFAULT_CONTEXT_VALUE 0x99
10
11 typedef std::function<OCStackApplicationResult(void *, OCDoHandle, OCClientResponse *)> FindCB;
12
13 ResourceBroker * ResourceBroker::s_instance = null;
14
15 class DiscoverResource: public PrimitiveResource {
16 public:
17     DiscoverResource();
18     DiscoverResource(PrimitiveResource & pResource, DiscoverCB _cb) :
19             Uri(pResource.getUri()), Address(pResource.getAddress()), discovercb(_cb) {
20     pFindCB = (FindCB)(std::bind(&DiscoverResource::findCB,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
21         }
22     DiscoverResource(std::string _Uri, std::string _Address, DiscoverCB _cb) :
23             Uri(_Uri), Address(_Address), discovercb(_cb) {
24     pFindCB = (FindCB)(std::bind(&DiscoverResource::findCB, this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
25         }
26     ~DiscoverResource();
27     const DiscoverCB discovercb;
28     const FindCB pFindCB;
29
30 private:
31     OCStackApplicationResult findCB(void * ctx, OCDoHandle handle,OCClientResponse * clientResponse)
32     {
33         OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION;
34         // parse about resource info
35
36         // create primitiveResource
37         PrimitiveResource * retResource = new PrimitiveResource(this->Uri, this->Address);
38         // callback
39         this->discovercb(retResource);
40         //why using auto_ptr?
41         std::auto_ptr bye_bye(this);
42         /////////////////////////////////////////////////////////////////////////////
43         std::cout << "Is Possible?? : " << this->getAddress() << std::endl;
44         /////////////////////////////////////////////////////////////////////////////
45         return ret;
46      }
47 };
48
49         ResourceBroker::ResourceBroker() {
50             // TODO Auto-generated constructor stub
51
52         }
53
54         ResourceBroker::~ResourceBroker() {
55             // TODO Auto-generated destructor stub
56         }
57
58         ResourceBroker * ResourceBroker::getInstance() {
59             if (!s_instance) {
60                 s_mutexForCreation.lock();
61                 if (!s_instance) {
62                     s_instance = new ResourceBroker();
63                 }
64                 s_mutexForCreation.unlock();
65             }
66             return s_instance;
67         }
68
69         OCStackResult ResourceBroker::discoverResource(const std::string _uri,
70                 const std::string _address, DiscoverCB _cb) {
71             OCStackResult ret = OC_STACK_INVALID_PARAM;
72
73             if (_uri.empty() || _address.empty()) {
74                 return ret;
75             }
76             if (!_cb) {
77                 ret = OC_STACK_INVALID_CALLBACK;
78                 return ret;
79             }
80
81             DiscoverResource * dResource = new DiscoverResource(_uri, _address, _cb);
82             OCDoHandle handle;
83             OCCallbackData cbData;
84             std::string fullAddress = OIC_COAP + _address;
85
86             cbData.cb = dResource->pFindCB;
87             cbData.context = (void *) DEFAULT_CONTEXT_VALUE;
88             cbData.cd = NULL;
89
90             return OCDoResource(&handle, OC_REST_GET, fullAddress.c_str(), _uri.c_str(), 0, OC_ALL,
91                     OC_LOW_QOS, &cbData, NULL, 0);
92         }
93
94         OCStackResult ResourceBroker::discoverResource(PrimitiveResource & pResource,
95                 DiscoverCB _cb) {
96             return this->discoverResource(pResource.getUri(), pResource.getAddress(), _cb);
97         }
98
99         OCStackResult ResourceBroker::hostResource(const std::string _uri,const std::string _address, BrokerCB _cb)
100         {
101             OCStackResult ret = OC_STACK_INVALID_PARAM;
102             if (uri.empty() || address.empty()) {
103                 return ret;
104             }
105             if (!cb) {
106                 ret = OC_STACK_INVALID_CALLBACK;
107                 return ret;
108             }
109
110             return ret;
111         }
112         OCStackResult ResourceBroker::hostResource(PrimitiveResource & pResource, BrokerCB cb)
113         {
114             OCStackResult ret = OC_STACK_INVALID_PARAM;
115             if (pResource.getUri().empty() || pResource.getAddress().empty()) {
116                 return ret;
117             }
118             if (!cb) {
119                 ret = OC_STACK_INVALID_CALLBACK;
120                 return ret;
121             }
122
123             return ret;
124         }
125         bool ResourceBroker::isPrimitiveResource(PrimitiveResource& pResource, BrokerCB cb)
126         {
127             if(s_presenceInfo.empty())
128                 return false;
129             else
130             {
131                 std::list<PresenceInfo*>::iterator it;
132                 PresenceInfo item = new PresenceInfo(pResource.getUri(), pResource.getAddress(), cb);
133                 item.PrimitiveResource(pResource.getUri(),pResource.getAddress());
134                 it = std::find(s_presenceInfo.begin(),s_presenceInfo.end(),item);
135                 if(it==s_presenceInfo.end())
136                     return false;
137                 else
138                     return true;
139             }
140         }