Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / examples / OICMiddle / Client.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
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 #include <map>
22
23 #include "WrapResource.h"
24 #include "Client.h"
25
26 MiddleClient::MiddleClient()
27 {
28     m_findCB = bind(&MiddleClient::foundOCResource, this, placeholders::_1);
29 }
30
31 bool MiddleClient::init()
32 {
33     findResources();
34     return true;
35 }
36
37 void MiddleClient::findResources()
38 {
39     m_resourceMap.clear();
40
41     OC::OCPlatform::findResource("", OC_WELL_KNOWN_QUERY, m_findCB);
42 }
43
44 void MiddleClient::foundOCResource(shared_ptr<OCResource> resource)
45 {
46     WrapResource *wres;
47     string resourceID = formatResourceID(resource);
48
49     m_mutexFoundCB.lock();
50
51     try {
52         wres = m_resourceMap.at(resourceID);
53     } catch (const std::out_of_range) {
54         wres = new WrapResource(resourceID, resource);
55         m_resourceMap[resourceID] = wres;
56     }
57
58     m_mutexFoundCB.unlock();
59
60     wres->findTypes();
61 }
62
63 /*
64  *  I need a unique ID, so I concatenate the host string and resource uri
65  *  It's arbitrary and sufficient.
66  */
67 string MiddleClient::formatResourceID(std::shared_ptr<OCResource> resource)
68 {
69     string host = resource->host();
70     if (host.compare(0, 7, "coap://") == 0)
71         host = host.erase(0, 7);
72     return host + resource->uri();
73 }
74
75 void MiddleClient::addResource(WrapResource *wres)
76 {
77     string resourceID = wres->getResourceID();
78     try {
79         m_resourceMap[resourceID];
80     } catch (const std::out_of_range) {
81         m_resourceMap[resourceID] = wres;
82     }
83 }