Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SDK / cpp / src / InprocSSMCore.cpp
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics 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 #include "SSMInterface.h"
21 #include "SSMInterface/SSMCore.h"
22 #include "Common/PlatformLayer.h"
23
24 class SSMCoreEventReceiver : public IQueryEngineEvent
25 {
26     public:
27         SSMCoreEventReceiver()
28         {
29         }
30
31         SSMRESULT onQueryEngineEvent(int cqid, IDataReader *pResult)
32         {
33             SSMRESULT res = SSM_E_FAIL;
34
35             m_mtxListener.lock();
36
37             if (m_mapListener.find(cqid) == m_mapListener.end())
38             {
39                 SSM_CLEANUP_ASSERT(res);
40             }
41
42             m_mapListener[cqid]->onQueryEngineEvent(cqid, pResult);
43
44             res = SSM_S_OK;
45
46 CLEANUP:
47             m_mtxListener.unlock();
48             return res;
49         }
50
51         void lockListener()
52         {
53             m_mtxListener.lock();
54         }
55
56         void unlockListener()
57         {
58             m_mtxListener.unlock();
59         }
60
61         void addListener(int cqid, IQueryEngineEvent *pEngineEvent)
62         {
63             m_mapListener[cqid] = pEngineEvent;
64         }
65
66         void removeListener(int cqid)
67         {
68             m_mapListener.erase(cqid);
69         }
70
71     private:
72         CSimpleMutex                        m_mtxListener;
73         std::map<int, IQueryEngineEvent *>   m_mapListener;
74 };
75
76 IQueryEngine                        *g_pQueryEngineInstance = NULL;
77 SSMCoreEventReceiver                *g_pEventReceiver = NULL;
78
79 SSMRESULT OIC::InitializeSSM(std::string xmlDescription)
80 {
81     SSMRESULT res = SSM_E_FAIL;
82
83     if (g_pQueryEngineInstance != NULL)
84         SSM_CLEANUP_ASSERT(SSM_E_INITIALIZED);
85
86     g_pEventReceiver = new SSMCoreEventReceiver();
87     SSM_CLEANUP_NULL_ASSERT(g_pEventReceiver);
88     SSM_CLEANUP_ASSERT(InitializeSSMCore(xmlDescription));
89     SSM_CLEANUP_ASSERT(StartSSMCore());
90     SSM_CLEANUP_ASSERT(CreateQueryEngine(&g_pQueryEngineInstance));
91     SSM_CLEANUP_ASSERT(g_pQueryEngineInstance->registerQueryEvent(g_pEventReceiver));
92
93 CLEANUP:
94     if (res != SSM_S_OK &&
95         res != SSM_E_INITIALIZED)
96     {
97         SAFE_DELETE(g_pEventReceiver);
98     }
99
100     return res;
101 }
102
103 SSMRESULT OIC::TerminateSSM()
104 {
105     SSMRESULT res = SSM_E_FAIL;
106
107     if (g_pQueryEngineInstance == NULL)
108         SSM_CLEANUP_ASSERT(SSM_E_NOTINIT);
109
110     SSM_CLEANUP_ASSERT(g_pQueryEngineInstance->unregisterQueryEvent(g_pEventReceiver));
111     ReleaseQueryEngine(g_pQueryEngineInstance);
112     g_pQueryEngineInstance = NULL;
113     SSM_CLEANUP_ASSERT(StopSSMCore());
114     SSM_CLEANUP_ASSERT(TerminateSSMCore());
115
116 CLEANUP:
117     SAFE_DELETE(g_pEventReceiver);
118     return res;
119 }
120
121 SSMRESULT OIC::RegisterQuery(std::string queryString, IQueryEngineEvent *listener,
122                              int &cqid)
123 {
124     SSMRESULT res = SSM_E_FAIL;
125
126     if (g_pQueryEngineInstance == NULL)
127         SSM_CLEANUP_ASSERT(SSM_E_NOTINIT);
128
129     g_pEventReceiver->lockListener();
130     SSM_CLEANUP_ASSERT(g_pQueryEngineInstance->executeContextQuery(queryString, &cqid));
131     g_pEventReceiver->addListener(cqid, listener);
132
133 CLEANUP:
134     if (g_pEventReceiver != NULL)
135         g_pEventReceiver->unlockListener();
136     return res;
137 }
138
139 SSMRESULT OIC::UnregisterQuery(int cqid)
140 {
141     SSMRESULT res = SSM_E_FAIL;
142
143     if (g_pQueryEngineInstance == NULL)
144         SSM_CLEANUP_ASSERT(SSM_E_NOTINIT);
145
146     g_pEventReceiver->lockListener();
147     SSM_CLEANUP_ASSERT(g_pQueryEngineInstance->killContextQuery(cqid));
148     g_pEventReceiver->removeListener(cqid);
149
150 CLEANUP:
151     if (g_pEventReceiver != NULL)
152         g_pEventReceiver->unlockListener();
153     return res;
154 }