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