Migrate from 2.4 code repo
[platform/core/context/context-service.git] / src / context_trigger / fact_request.cpp
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 #include <types_internal.h>
18 #include "fact_request.h"
19
20 ctx::fact_request::fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, const char* zone, fact_reader* reader)
21         : request_info(type, client, req_id, subj, desc)
22         , _reader(reader)
23         , replied(false)
24 {
25         if (zone)
26                 _zone_name = zone;
27 }
28
29 ctx::fact_request::~fact_request()
30 {
31         reply(ERR_OPERATION_FAILED);
32 }
33
34 bool ctx::fact_request::reply(int error)
35 {
36         IF_FAIL_RETURN(!replied && _reader, true);
37         _reader->reply_result(_req_id, error, _zone_name.c_str());
38         return (replied = true);
39 }
40
41 bool ctx::fact_request::reply(int error, ctx::json& request_result)
42 {
43         IF_FAIL_RETURN(!replied && _reader, true);
44         IF_FAIL_RETURN(_type != REQ_READ_SYNC, true);
45         _reader->reply_result(_req_id, error, _zone_name.c_str(), &request_result);
46         return (replied = true);
47 }
48
49 bool ctx::fact_request::reply(int error, ctx::json& request_result, ctx::json& data_read)
50 {
51         IF_FAIL_RETURN(!replied && _reader, true);
52         _reader->reply_result(_req_id, error, _zone_name.c_str(), &request_result, &data_read);
53         return (replied = true);
54 }
55
56 bool ctx::fact_request::publish(int error, ctx::json& data)
57 {
58         IF_FAIL_RETURN(_reader, true);
59         _reader->publish_fact(_req_id, error, _zone_name.c_str(), _subject.c_str(), &get_description(), &data);
60         return true;
61 }
62
63 void ctx::fact_request::set_zone_name(const char* zone_name)
64 {
65         if (zone_name)
66                 _zone_name = zone_name;
67 }