7a3641860225e45921e382b448049e027eaa1a0d
[platform/core/system/sync-agent.git] / src / framework / engine-controller / task_info_pool.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdlib.h>
19 #include "utility/sync_util.h"
20 #include "engine-controller/task_info_pool.h"
21 #include "engine-controller/queuing_rule_spec.h"
22 #include "engine-controller/task_spec_internal.h"
23
24 #ifndef SYNC_AGENT_LOG
25 #undef LOG_TAG
26 #define LOG_TAG "AF_EC"
27 #endif
28
29 ec_task_info_t *ec_task_info_new(sync_agent_ec_int task_spec_id, sync_agent_ec_task_spec_s * task_spec)
30 {
31         _EXTERN_FUNC_ENTER;
32
33         ec_task_info_t *task_info = (ec_task_info_t *) calloc(1, sizeof(ec_task_info_t));
34         if (task_info == NULL) {
35                 goto error_part;
36         }
37
38         task_info->task_spec_id = task_spec_id;
39         task_info->progress_blocking_entity_list = NULL;
40         task_info->task_spec = ec_task_spec_ref(task_spec);
41
42         _EXTERN_FUNC_EXIT;
43
44         return task_info;
45
46  error_part:
47         ec_task_info_free(task_info);
48         return NULL;
49 }
50
51 void ec_task_info_free(ec_task_info_t * task_info)
52 {
53         _EXTERN_FUNC_ENTER;
54
55         if (task_info != NULL) {
56                 if (task_info->task_spec != NULL) {
57                         ec_task_spec_unref(task_info->task_spec);
58                 }
59
60                 if (task_info->progress_blocking_entity_list != NULL) {
61                         GSList *iter = NULL;
62                         ec_progress_blocking_entity_t *entity = NULL;
63                         for (iter = task_info->progress_blocking_entity_list; iter != NULL; iter = g_slist_next(iter)) {
64                                 entity = (ec_progress_blocking_entity_t *) (iter->data);
65                                 ec_progress_blocking_entity_unref(entity);
66                         }
67                         g_slist_free(task_info->progress_blocking_entity_list);
68                 }
69
70                 free(task_info);
71         }
72
73         _EXTERN_FUNC_EXIT;
74 }
75
76 ec_task_info_t *ec_task_info_ref(ec_task_info_t * task_info)
77 {
78         _EXTERN_FUNC_ENTER;
79
80         retvm_if(task_info == NULL, NULL, "ec_task_info_t is NULL !!");
81
82         /* TODO */
83
84         _EXTERN_FUNC_EXIT;
85         return task_info;
86 }
87
88 void ec_task_info_unref(ec_task_info_t * task_info)
89 {
90         _EXTERN_FUNC_ENTER;
91
92         /* TODO */
93
94         _EXTERN_FUNC_EXIT;
95 }
96
97 sync_agent_ec_boolean ec_task_info_is_progress_blocking_entity_exist(ec_task_info_t * task_info, ec_progress_blocking_entity_t * entity)
98 {
99         _EXTERN_FUNC_ENTER;
100
101         GSList *found_node = g_slist_find(task_info->progress_blocking_entity_list, entity);
102         if (found_node != NULL) {
103                 _EXTERN_FUNC_EXIT;
104                 return true;
105         }
106
107         _EXTERN_FUNC_EXIT;
108         return false;
109 }
110
111 sync_agent_ec_error_e ec_task_info_add_progress_blocking_entity(ec_task_info_t * task_info, ec_progress_blocking_entity_t * entity)
112 {
113         _EXTERN_FUNC_ENTER;
114
115         retvm_if(task_info == NULL, SYNC_AGENT_EC_UNKNOWN_ERROR, "ec_task_info_t is NULL !!");
116         retvm_if(entity == NULL, SYNC_AGENT_EC_UNKNOWN_ERROR, "ec_progress_blocking_entity_t is NULL !!");
117
118         sync_agent_ec_error_e ec_error = SYNC_AGENT_EC_OK;
119
120         if (ec_task_info_is_progress_blocking_entity_exist(task_info, entity)) {
121                 _DEBUG_INFO("task info has already same entity");
122                 goto return_part;
123         }
124
125         task_info->progress_blocking_entity_list = g_slist_prepend(task_info->progress_blocking_entity_list, entity);
126         /* TODO : error handling */
127
128  return_part:
129         _EXTERN_FUNC_EXIT;
130         return ec_error;
131 }
132
133 sync_agent_ec_error_e ec_task_info_remove_blocking_entity(ec_task_info_t * task_info, ec_progress_blocking_entity_t * entity)
134 {
135         _EXTERN_FUNC_ENTER;
136
137         retvm_if(task_info == NULL, SYNC_AGENT_EC_UNKNOWN_ERROR, "ec_task_info_t is NULL !!");
138         retvm_if(entity == NULL, SYNC_AGENT_EC_UNKNOWN_ERROR, "ec_progress_blocking_entity_t is NULL !!");
139
140         sync_agent_ec_error_e ec_error = SYNC_AGENT_EC_OK;
141
142         if (!ec_task_info_is_progress_blocking_entity_exist(task_info, entity)) {
143                 _DEBUG_INFO("task info has no such entity");
144                 goto return_part;
145         }
146
147         task_info->progress_blocking_entity_list = g_slist_remove(task_info->progress_blocking_entity_list, entity);
148
149  return_part:
150         _EXTERN_FUNC_EXIT;
151         return ec_error;
152 }
153
154 ec_task_info_pool_t *ec_task_info_pool_alloc()
155 {
156         _EXTERN_FUNC_ENTER;
157
158         ec_task_info_pool_t *task_info_pool = (ec_task_info_pool_t *) calloc(1, sizeof(ec_task_info_pool_t));
159         if (task_info_pool == NULL) {
160                 goto return_part;
161         }
162
163         task_info_pool->task_info_pool = g_hash_table_new(g_direct_hash, g_direct_equal);
164         if (task_info_pool->task_info_pool == NULL) {
165                 goto error_part;
166         }
167
168  return_part:
169         _EXTERN_FUNC_EXIT;
170         return task_info_pool;
171
172  error_part:
173         ec_task_info_pool_free(task_info_pool);
174         return NULL;
175 }
176
177 void ec_task_info_pool_free(ec_task_info_pool_t * task_info_pool)
178 {
179         _EXTERN_FUNC_ENTER;
180
181         if (task_info_pool != NULL) {
182                 if (task_info_pool->task_info_pool != NULL) {
183                         g_hash_table_destroy(task_info_pool->task_info_pool);
184                         task_info_pool->task_info_pool = NULL;
185                 }
186                 free(task_info_pool);
187         }
188
189         _EXTERN_FUNC_EXIT;
190 }
191
192 sync_agent_ec_boolean ec_task_info_pool_add_task_info(ec_task_info_pool_t * task_info_pool, ec_task_info_t * task_info, sync_agent_ec_boolean replace)
193 {
194         _EXTERN_FUNC_ENTER;
195
196         retvm_if(task_info_pool == NULL, false, "ec_task_info_pool_t is NULL !!");
197         retvm_if(task_info == NULL, false, "ec_task_info_t is NULL !!");
198
199         sync_agent_ec_boolean success = false;
200
201         GHashTable *hash = task_info_pool->task_info_pool;
202         if (replace) {
203                 g_hash_table_replace(hash, (gpointer) (task_info->task_spec_id), task_info);    /* TODO : check free well done */
204                 success = true;
205         } else {
206                 gpointer tmp = g_hash_table_lookup(hash, (gpointer) (task_info->task_spec_id));
207                 if (tmp != NULL) {
208                         success = false;
209                 } else {
210                         g_hash_table_insert(hash, (gpointer) (task_info->task_spec_id), task_info);
211                 }
212         }
213
214         _EXTERN_FUNC_EXIT;
215
216         return success;
217 }
218
219 sync_agent_ec_boolean ec_task_info_pool_remove_task_info(ec_task_info_pool_t * task_info_pool, sync_agent_ec_uint msg_type)
220 {
221         _EXTERN_FUNC_ENTER;
222
223         /* TODO */
224
225         _EXTERN_FUNC_EXIT;
226
227         return true;
228 }
229
230 ec_task_info_t *ec_task_info_pool_search_task_info(ec_task_info_pool_t * task_info_pool, sync_agent_ec_uint msg_type)
231 {
232         _EXTERN_FUNC_ENTER;
233
234         GHashTable *hash = task_info_pool->task_info_pool;
235         ec_task_info_t *task_info = NULL;
236
237         task_info = (ec_task_info_t *) g_hash_table_lookup(hash, (gpointer) msg_type);
238
239         _EXTERN_FUNC_EXIT;
240
241         return task_info;
242 }