Migrate from 2.4 code repo
[platform/core/context/context-service.git] / src / context_trigger / fact_reader.h
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __CONTEXT_FACT_READER_H__
18 #define __CONTEXT_FACT_READER_H__
19
20 #include <list>
21 #include <json.h>
22 #include "../context_mgr_impl.h"
23 #include "fact.h"
24
25 namespace ctx {
26
27         class context_trigger;
28
29         class fact_reader {
30                 public:
31                         fact_reader(context_manager_impl* mgr, context_trigger* trigger);
32                         ~fact_reader();
33
34                         bool is_supported(const char* subject, const char* zone);
35
36                         int subscribe(const char* subject, json* option, const char* zone, bool wait_response = false);
37                         void unsubscribe(const char* subject, json* option, const char* zone);
38                         void unsubscribe(int subscription_id);
39                         bool read(const char* subject, json* option, const char* zone, context_fact& fact);
40
41                         void reply_result(int req_id, int error, const char* zone, json* request_result = NULL, json* fact = NULL);
42                         void publish_fact(int req_id, int error, const char* zone, const char* subject, json* option, json* fact);
43
44                 private:
45                         static context_manager_impl* _context_mgr;
46                         static context_trigger* _trigger;
47
48                         struct subscr_info_s {
49                                 int sid;
50                                 std::string subject;
51                                 ctx::json option;
52                                 std::string zone_name;
53                                 subscr_info_s(int id, const char* subj, ctx::json* opt, const char* zone)
54                                         : sid(id), subject(subj)
55                                 {
56                                         if (opt)
57                                                 option = *opt;
58
59                                         if (zone)
60                                                 zone_name = zone;
61                                 }
62                         };
63
64                         typedef std::list<subscr_info_s*> subscr_list_t;
65                         subscr_list_t subscr_list;
66
67                         int find_sub(const char* subject, json* option, const char* zone);
68                         bool add_sub(int sid, const char* subject, json* option, const char* zone);
69                         void remove_sub(const char* subject, json* option, const char* zone);
70                         void remove_sub(int sid);
71
72                         static gboolean send_request(gpointer data);
73         };
74
75 }       /* namespace ctx */
76
77 #endif  /* End of __CONTEXT_FACT_READER_H__ */