77e66b251e4ff676ebea0295a648933ebfc51c27
[platform/core/api/maps-service.git] / src / session / command.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
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 "command.h"
18 #include "empty_module.h"
19 #include "maps_util.h"
20
21 extern plugin::plugin_s *__extract_plugin(maps_service_h maps);
22
23 volatile int session::command::command_request_id = 1;
24 session::command session::command::empty_instance;
25
26 session::command::command(maps_service_h ms)
27         : m(ms), my_req_id(0), error(MAPS_ERROR_NONE), is_merged(false)
28 {
29 }
30
31 session::command::command(const command &src)
32 {
33         *this = src;
34 }
35
36 session::command::~command()
37 {
38 }
39
40 session::command &session::command::operator =(const command &src)
41 {
42         if (this != (&src)) {
43                 m = src.m;
44                 my_req_id = src.my_req_id;
45                 error = src.error;
46                 is_merged = src.is_merged;
47         }
48         return *this;
49 }
50
51 int session::command::run()
52 {
53         return MAPS_ERROR_NONE;
54 }
55
56 void session::command::destroy()
57 {
58         if (this != empty_ptr())
59                 delete this;
60 }
61
62 plugin::interface_s *session::command::interface() const
63 {
64         if (!m)
65                 return plugin::get_empty_interface_ptr();
66
67         plugin::plugin_s *p = __extract_plugin(m);
68         if (!p)
69                 return plugin::get_empty_interface_ptr();
70
71         return &p->interface;
72 }
73
74 maps_plugin_h session::command::handle() const
75 {
76         return (maps_plugin_h) plugin();
77 }
78
79 plugin::plugin_s *session::command::plugin() const
80 {
81         return __extract_plugin(m);
82 }
83
84 session::command_type_e session::command::get_type() const
85 {
86         /* Default command type */
87         return MAPS_DATA_COMMAND;
88 }
89
90 int session::command::get_priority() const
91 {
92         /* Default command priority */
93         return 1;
94 }
95
96 void session::command::merge(const command *c)
97 {
98         /* empty */
99 }
100
101 bool session::command::merged() const
102 {
103         return is_merged;
104 }
105
106
107 void session::command::set_merged()
108 {
109         is_merged = true;
110 }
111 /*----------------------------------------------------------------------------*/
112
113 session::command_handler::command_handler(plugin::plugin_s *p, void *ud,
114                                           int urid)
115  : plg(p), user_data(ud), user_req_id(urid), plg_req_id(-1)
116 {
117 }
118
119 session::command_handler::~command_handler()
120 {
121 }
122
123 void session::command_handler::destroy(void *p)
124 {
125         if (!p)
126                 return;
127         command_handler *c = (command_handler *) p;
128         delete c;
129 }
130
131 /*----------------------------------------------------------------------------*/
132
133 session::pending_request::pending_request(plugin::plugin_s *p) : plg(p)
134 {
135 }
136
137 bool session::pending_request::add(const int user_req_id)
138 {
139         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
140                 return false;
141         plugin::scope_mutex(&plg->pending_request_mutex);
142
143         MAPS_LOGD("session::pending_request::add: %d", user_req_id);
144
145         if (contains(user_req_id))
146                 return false;   /* Attempt to add another instance of the id */
147         g_hash_table_insert(plg->pending_request_maps, int_dup(user_req_id),
148                 NULL);
149         return true;
150 }
151
152 void session::pending_request::update(int user_req_id,
153                                       command_handler *handler)
154 {
155         if (!plg || !plg->pending_request_maps || (user_req_id < 0) || !handler)
156                 return;
157         plugin::scope_mutex(&plg->pending_request_mutex);
158
159         MAPS_LOGD("session::pending_request::update: %d, %d", user_req_id,
160                 handler->plg_req_id);
161
162         if (!contains(user_req_id)) {   /* Attempt to update not existing id */
163                 MAPS_LOGD("\t not updated session::pending_request: %d, %d",
164                         user_req_id, handler->plg_req_id);
165                 delete handler; /* This handler must be deleted */
166                 return;
167         }
168
169         g_hash_table_insert(plg->pending_request_maps, int_dup(user_req_id),
170                 handler);
171 }
172
173 void session::pending_request::remove(int user_req_id)
174 {
175         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
176                 return;
177         plugin::scope_mutex(&plg->pending_request_mutex);
178
179         MAPS_LOGD("session::pending_request::remove: %d", user_req_id);
180
181         if (!contains(user_req_id))
182                 return;         /* Attempt to remove not existing id */
183         g_hash_table_remove(plg->pending_request_maps, &user_req_id);
184 }
185
186 session::command_handler *session::pending_request::look_up(const int
187                                                             user_req_id)
188 {
189         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
190                 return NULL;
191         plugin::scope_mutex(&plg->pending_request_mutex);
192
193         MAPS_LOGD("session::pending_request::look_up: %d", user_req_id);
194
195         if (!contains(user_req_id))
196                 return NULL;    /* Attempt to query not existing id */
197         return (command_handler*) g_hash_table_lookup(plg->
198                 pending_request_maps, &user_req_id);
199 }
200
201 int session::pending_request::extract_plg_id(const int user_req_id)
202 {
203         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
204                 return -1;
205         plugin::scope_mutex(&plg->pending_request_mutex);
206
207         command_handler* ch = look_up(user_req_id);
208         if (!ch)
209                 return -1;
210         const int plg_req_id = ch->plg_req_id;
211         remove(user_req_id);
212         return plg_req_id;
213 }
214
215 bool session::pending_request::contains(const int user_req_id)
216 {
217         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
218                 return false;
219         plugin::scope_mutex(&plg->pending_request_mutex);
220
221         return g_hash_table_contains(plg->pending_request_maps, &user_req_id);
222 }
223
224 int *session::pending_request::int_dup(const int n)
225 {
226         int *clone = g_new0(int, 1);
227         *clone = n;
228         return clone;
229 }