Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / QueryProcessor / QueryEngine.h
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 #ifndef _QueryEngine_H_
21 #define _QueryEngine_H_
22
23 #include "SSMInterface/SSMCore.h"
24 #include "Common/PlatformLayer.h"
25 #include "Common/InternalInterface.h"
26 #include "ContextQuery.h"
27 #include "ConditionedQueryResult.h"
28
29 /**
30 * @class    CQueryEngine
31 * @brief    CQueryEngine Interface
32 *            This class manages ContextQuery.
33 *
34 * @see
35 *           CObject<CQueryEngine>   *pQueryEngine;
36 *           CreateInstance(OID_IQueryEngineInternal, (IBase**)&pQueryEngine);
37 *           pQeuryEngine->ExecuteContextQuery(contextquery,cqid);
38 */
39 class CQueryEngine :
40     public CObjectRoot<CObjectMultiThreadModel>
41     , public IQueryEngineInternal
42     , public IThreadClient
43     , public IConditionedQueryEvent
44 {
45     private:
46         CObjectPtr<IPropagationEngine>      m_pPropagationEngine;
47         std::map<int, IConditionedQuery *>   m_conditionedQueries;
48         CSimpleMutex                        m_mtxQueries;
49         int                                 m_cqid;
50         IQueryEngineEvent                   *m_pQueryEngineEvent;
51         std::map<int, CContextQuery *>       m_contextQueries;
52         CObjectPtr<ITasker>                 m_pTasker;
53
54     private:
55         SSMRESULT processQueryResult(int userTriggerId, std::vector<result_model> *result);
56         SSMRESULT validateQueryResult(IConditionedQueryResult *pConditionedQueryResult,
57                                       std::vector<result_model> *resultData);
58
59     public:
60         enum EventType {EVENT_TYPE_INNER, EVENT_TYPE_OUTER};
61         SSMRESULT finalConstruct();
62         void finalRelease();
63         void onExecute(void *pArg);
64         void onTerminate(void *pArg);
65
66         /**
67         * @fn onConditionedQueryEvent
68         * @brief IConditionedQueryEvent interface's event callbacks for receiving query results
69         *
70         * @param [in] std::string ContextQuery - Entered ContetxQuery
71         * @param [out] int* cqid - ID of ContextQuery
72         * @return SSMRESULT
73         * @warning
74         * @exception
75         * @see
76         */
77         SSMRESULT onConditionedQueryEvent(int userTriggerId,
78                                           IConditionedQueryResult *pConditionedQueryResult);
79
80         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
81         {
82             if (ppObject == NULL)
83                 return SSM_E_POINTER;
84
85             if (IsEqualOID(objectID, OID_IQueryEngineInternal))
86             {
87                 IBase *pBase = (IQueryEngineInternal *)this;
88                 pBase->addRef();
89                 *ppObject = pBase;
90                 return SSM_S_OK;
91             }
92
93             return SSM_E_NOINTERFACE;
94         }
95
96         /**
97         * @fn executeContextQuery
98         * @brief Execute ContextQuery and return ContextQuery ID
99         *
100         * @param [in] std::string ContextQuery - Entered ContetxQuery
101         * @param [out] int* cqid - ID of ContextQuery
102         * @return SSMRESULT
103         * @warning
104         * @exception
105         * @see
106         */
107         SSMRESULT executeContextQuery(std::string contextQuery, int *cqid);
108
109         //TODO: Registration with multiple instance support
110         /**
111         * @fn registerQueryEvent
112         * @brief Register QueryEngineEvent to QueryEngine.
113         *
114         * @param [in] IQueryEngineEvent* pQueryEngineEvent - register QueryEngineEvent
115         * @return SSMRESULT
116         * @warning
117         * @exception
118         * @see
119         */
120         SSMRESULT registerQueryEvent(IQueryEngineEvent *pQueryEngineEvent);
121
122
123         /**
124         * @fn unregisterQueryEvent
125         * @brief Unregister QueryEngineEvent to QueryEngine.
126         *
127         * @param [in] IQueryEngineEvent* pQueryEngineEvent - unregister QueryEngineEvent
128         * @return SSMRESULT
129         * @warning
130         * @exception
131         * @see
132         */
133         SSMRESULT unregisterQueryEvent(IQueryEngineEvent *pQueryEngineEvent);
134
135         /**
136         * @fn killContextQuery
137         * @brief Kill registered ContextQuery according to cqid
138         *
139         * @param [in] int cqid - Context Query corresponding to the cqid will be terminated
140         *
141         * @return SSMRESULT
142         * @warning
143         * @exception
144         * @see
145         */
146         SSMRESULT killContextQuery(int cqid);
147 };
148
149 #endif /*_QueryEngine_H_*/