Tizen 2.4.0 rev3 SDK Public Release
[framework/location/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)
28           , my_req_id(0)
29           , error(MAPS_ERROR_NONE)
30 {
31 }
32
33 session::command::command(const command &src)
34 {
35         *this = src;
36 }
37
38 session::command::~command()
39 {
40 }
41
42 session::command &session::command::operator =(const command &src)
43 {
44         if (this != (&src)) {
45                 m = src.m;
46                 my_req_id = src.my_req_id;
47                 error = src.error;
48         }
49         return *this;
50 }
51
52 int session::command::run()
53 {
54         return MAPS_ERROR_NONE;
55 }
56
57 void session::command::destroy()
58 {
59         if (this != empty_ptr())
60                 delete this;
61 }
62
63 plugin::interface_s *session::command::interface() const
64 {
65         if (!m)
66                 return plugin::get_empty_interface_ptr(); /* PROBLEM!!! Why have
67                         no maps service!! Returning default empty interface */
68
69         plugin::plugin_s *p = __extract_plugin(m);
70         if (!p)
71                 return plugin::get_empty_interface_ptr(); /* PROBLEM!!! Why have
72                                 no plugin!! Returning default empty interface */
73
74         return &p->interface;
75 }
76
77 maps_plugin_h session::command::handle() const
78 {
79         return (maps_plugin_h) plugin();
80 }
81
82 plugin::plugin_s *session::command::plugin() const
83 {
84         return __extract_plugin(m);
85 }
86
87 /*----------------------------------------------------------------------------*/
88
89 session::command_handler::command_handler(plugin::plugin_s *p, void *ud,
90                                           int urid)
91  : plg(p), user_data(ud), user_req_id(urid), plg_req_id(-1)
92 {
93 }
94
95 session::command_handler::~command_handler()
96 {
97 }
98
99 void session::command_handler::destroy(void *p)
100 {
101         if (!p)
102                 return;
103         command_handler *c = (command_handler *) p;
104         delete c;
105 }
106
107 /*----------------------------------------------------------------------------*/
108
109 session::pending_request::pending_request(plugin::plugin_s *p) : plg(p)
110 {
111 }
112
113 bool session::pending_request::add(const int user_req_id)
114 {
115         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
116                 return false;
117         plugin::scope_mutex(&plg->pending_request_mutex);
118
119         MAPS_LOGD("session::pending_request::add: %d", user_req_id);
120
121         if (contains(user_req_id))
122                 return false;   /* Attempt to add another instance of the id */
123         g_hash_table_insert(plg->pending_request_maps, int_dup(user_req_id),
124                 NULL);
125         return true;
126 }
127
128 void session::pending_request::update(int user_req_id,
129                                       command_handler *handler)
130 {
131         if (!plg || !plg->pending_request_maps || (user_req_id < 0) || !handler)
132                 return;
133         plugin::scope_mutex(&plg->pending_request_mutex);
134
135         MAPS_LOGD("session::pending_request::update: %d, %d", user_req_id,
136                 handler->plg_req_id);
137
138         if (!contains(user_req_id)) {   /* Attempt to update not existing id */
139                 MAPS_LOGD("\t not updated session::pending_request: %d, %d",
140                         user_req_id, handler->plg_req_id);
141                 delete handler; /* This handler must be deleted */
142                 return;
143         }
144
145         g_hash_table_insert(plg->pending_request_maps, int_dup(user_req_id),
146                 handler);
147 }
148
149 void session::pending_request::remove(int user_req_id)
150 {
151         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
152                 return;
153         plugin::scope_mutex(&plg->pending_request_mutex);
154
155         MAPS_LOGD("session::pending_request::remove: %d", user_req_id);
156
157         if (!contains(user_req_id))
158                 return;         /* Attempt to remove not existing id */
159         g_hash_table_remove(plg->pending_request_maps, &user_req_id);
160 }
161
162 session::command_handler *session::pending_request::look_up(const int
163                                                             user_req_id)
164 {
165         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
166                 return NULL;
167         plugin::scope_mutex(&plg->pending_request_mutex);
168
169         MAPS_LOGD("session::pending_request::look_up: %d", user_req_id);
170
171         if (!contains(user_req_id))
172                 return NULL;    /* Attempt to query not existing id */
173         return (command_handler*) g_hash_table_lookup(plg->
174                 pending_request_maps, &user_req_id);
175 }
176
177 int session::pending_request::extract_plg_id(const int user_req_id)
178 {
179         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
180                 return -1;
181         plugin::scope_mutex(&plg->pending_request_mutex);
182
183         command_handler* ch = look_up(user_req_id);
184         if (!ch)
185                 return -1;
186         const int plg_req_id = ch->plg_req_id;
187         remove(user_req_id);
188         return plg_req_id;
189 }
190
191 bool session::pending_request::contains(const int user_req_id)
192 {
193         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
194                 return false;
195         plugin::scope_mutex(&plg->pending_request_mutex);
196
197         return g_hash_table_contains(plg->pending_request_maps, &user_req_id);
198 }
199
200 int *session::pending_request::int_dup(const int n)
201 {
202         int *clone = g_new0(int, 1);
203         *clone = n;
204         return clone;
205 }