ed72158060b37fbd1738f3003a29e56d892fb76c
[profile/ivi/download-provider.git] / src / download-provider-receiver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/socket.h>
4 #include <sys/un.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <sys/time.h>
9
10 #include <glib.h>
11
12 #include "download-provider-config.h"
13 #include "download-provider-log.h"
14 #include "download-provider-pthread.h"
15 #include "download-provider-notification.h"
16 #include "download-provider-ipc.h"
17 #include "download-provider-db.h"
18 #include "download-provider-utils.h"
19
20 #include "download-agent-defs.h"
21 #include "download-agent-interface.h"
22
23 int _init_agent(void);
24 void _deinit_agent(void);
25 static void __downloading_info_cb(user_downloading_info_t *download_info,
26                                         void *user_data);
27 static void __download_info_cb(user_download_info_t *download_info,
28                                         void *user_data);
29 static void __notify_cb(user_notify_info_t *notify_info, void *user_data);
30 static int __change_error(int err);
31 static int __change_state(da_state state);
32
33 void TerminateDaemon(int signo);
34
35 pthread_attr_t g_download_provider_thread_attr;
36 fd_set g_download_provider_socket_readset;
37 fd_set g_download_provider_socket_exceptset;
38
39 int _change_pended_download(download_clientinfo *clientinfo)
40 {
41         if (!clientinfo)
42                 return -1;
43         clientinfo->state = DOWNLOAD_STATE_PENDED;
44         clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
45         download_provider_db_requestinfo_update_column(clientinfo, DOWNLOAD_DB_STATE);
46         ipc_send_request_stateinfo(clientinfo);
47         return 0;
48 }
49
50 void *_start_download(void *args)
51 {
52         int da_ret = -1;
53         int req_dl_id = -1;
54
55         download_clientinfo_slot *clientinfoslot =
56                 (download_clientinfo_slot *) args;
57         if (!clientinfoslot) {
58                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo_slot");
59                 return 0;
60         }
61         download_clientinfo *clientinfo =
62                 (download_clientinfo *) clientinfoslot->clientinfo;
63         if (!clientinfo) {
64                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo");
65                 return 0;
66         }
67
68         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
69         clientinfo->state = DOWNLOAD_STATE_READY;
70         clientinfo->err = DOWNLOAD_ERROR_NONE;
71         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
72
73         // call start_download() of download-agent
74         if (clientinfo->requestinfo->headers.rows > 0) {
75                 int len = 0;
76                 int i = 0;
77                 char **req_header = NULL;
78                 len = clientinfo->requestinfo->headers.rows;
79                 req_header = calloc(len, sizeof(char *));
80                 if (!req_header) {
81                         TRACE_DEBUG_MSG("fail to calloc");
82                         return 0;
83                 }
84                 for (i = 0; i < len; i++)
85                         req_header[i] =
86                                 strdup(clientinfo->requestinfo->headers.str[i].str);
87                 if (clientinfo->requestinfo->install_path.length > 1) {
88                         if (clientinfo->requestinfo->filename.length > 1)
89                                 da_ret =
90                                         da_start_download_with_extension(clientinfo->requestinfo->
91                                                         url.str, &req_dl_id,
92                                                         DA_FEATURE_REQUEST_HEADER,
93                                                         req_header, &len,
94                                                         DA_FEATURE_INSTALL_PATH,
95                                                         clientinfo->requestinfo->install_path.str,
96                                                         DA_FEATURE_FILE_NAME,
97                                                         clientinfo->requestinfo->filename.str,
98                                                         DA_FEATURE_USER_DATA,
99                                                         (void *)clientinfoslot,
100                                                         NULL);
101                         else
102                                 da_ret =
103                                         da_start_download_with_extension(clientinfo->requestinfo->
104                                                         url.str, &req_dl_id,
105                                                         DA_FEATURE_REQUEST_HEADER,
106                                                         req_header, &len,
107                                                         DA_FEATURE_INSTALL_PATH,
108                                                         clientinfo->requestinfo->install_path.str,
109                                                         DA_FEATURE_USER_DATA,
110                                                         (void *)clientinfoslot,
111                                                         NULL);
112                 } else {
113                         if (clientinfo->requestinfo->filename.length > 1)
114                                 da_ret =
115                                         da_start_download_with_extension(clientinfo->requestinfo->
116                                                         url.str, &req_dl_id,
117                                                         DA_FEATURE_REQUEST_HEADER,
118                                                         req_header, &len,
119                                                         DA_FEATURE_FILE_NAME,
120                                                         clientinfo->requestinfo->filename.str,
121                                                         DA_FEATURE_USER_DATA,
122                                                         (void *)clientinfoslot,
123                                                         NULL);
124                         else
125                                 da_ret =
126                                         da_start_download_with_extension(clientinfo->requestinfo->
127                                                         url.str, &req_dl_id,
128                                                         DA_FEATURE_REQUEST_HEADER,
129                                                         req_header, &len,
130                                                         DA_FEATURE_USER_DATA,
131                                                         (void *)clientinfoslot,
132                                                         NULL);
133                 }
134                 for (i = 0; i < len; i++) {
135                         if (req_header[i])
136                                 free(req_header[i]);
137                 }
138         } else {
139                 if (clientinfo->requestinfo->install_path.length > 1) {
140                         if (clientinfo->requestinfo->filename.length > 1)
141                                 da_ret =
142                                         da_start_download_with_extension(clientinfo->requestinfo->
143                                                         url.str, &req_dl_id,
144                                                         DA_FEATURE_INSTALL_PATH,
145                                                         clientinfo->requestinfo->install_path.str,
146                                                         DA_FEATURE_FILE_NAME,
147                                                         clientinfo->requestinfo->filename.str,
148                                                         DA_FEATURE_USER_DATA,
149                                                         (void *)clientinfoslot,
150                                                         NULL);
151                         else
152                                 da_ret =
153                                         da_start_download_with_extension(clientinfo->requestinfo->
154                                                         url.str, &req_dl_id,
155                                                         DA_FEATURE_INSTALL_PATH,
156                                                         clientinfo->requestinfo->install_path.str,
157                                                         DA_FEATURE_USER_DATA,
158                                                         (void *)clientinfoslot,
159                                                         NULL);
160                 } else {
161                         if (clientinfo->requestinfo->filename.length > 1)
162                                 da_ret =
163                                         da_start_download_with_extension(clientinfo->requestinfo->
164                                                         url.str, &req_dl_id,
165                                                         DA_FEATURE_FILE_NAME,
166                                                         clientinfo->requestinfo->filename.str,
167                                                         DA_FEATURE_USER_DATA,
168                                                         (void *)clientinfoslot,
169                                                         NULL);
170                         else
171                                 da_ret =
172                                         da_start_download_with_extension(clientinfo->requestinfo->
173                                                         url.str, &req_dl_id,
174                                                         DA_FEATURE_USER_DATA,
175                                                         (void *)clientinfoslot,
176                                                         NULL);
177                 }
178         }
179
180         // if start_download() return error cause of maximun download limitation,
181         // set state to DOWNLOAD_STATE_PENDED.
182         if (da_ret == DA_ERR_ALREADY_MAX_DOWNLOAD) {
183                 TRACE_DEBUG_INFO_MSG("change to pended request [%d]", da_ret);
184                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
185                 _change_pended_download(clientinfo);
186                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
187                 return 0;
188         } else if (da_ret != DA_RESULT_OK) {
189                 TRACE_DEBUG_INFO_MSG("Fail to request start [%d]", da_ret);
190                 /* FIXME : need to seperate in detail according to error return values */
191                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
192                 clientinfo->state = DOWNLOAD_STATE_FAILED;
193                 clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
194                 download_provider_db_requestinfo_remove(clientinfo->
195                                                         requestinfo->requestid);
196                 ipc_send_request_stateinfo(clientinfo);
197                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
198                 return 0;
199         }
200
201         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
202
203         TRACE_DEBUG_INFO_MSG("started download [%d]", da_ret);
204
205         clientinfo->req_id = req_dl_id;
206         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
207         clientinfo->err = DOWNLOAD_ERROR_NONE;
208
209         download_provider_db_requestinfo_update_column(clientinfo,
210                                                                 DOWNLOAD_DB_STATE);
211
212         // sync return  // client should be alive till this line at least.
213         ipc_send_request_stateinfo(clientinfo);
214
215         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
216         return 0;
217 }
218
219 int _create_download_thread(download_clientinfo_slot *clientinfo_slot)
220 {
221         if (!clientinfo_slot)
222                 return -1;
223
224         // create thread for receiving the reqeust info from client.
225         // and if possible, it will create the thread for listening the event.
226         clientinfo_slot->clientinfo->state = DOWNLOAD_STATE_READY;
227         clientinfo_slot->clientinfo->err = DOWNLOAD_ERROR_NONE;
228         if (pthread_create
229                 (&clientinfo_slot->clientinfo->thread_pid,
230                 &g_download_provider_thread_attr, _start_download,
231                 clientinfo_slot) != 0) {
232                 TRACE_DEBUG_INFO_MSG("failed to call pthread_create for client");
233                 TRACE_DEBUG_INFO_MSG("Change to pended job");
234                 _change_pended_download(clientinfo_slot->clientinfo);
235                 return -1;
236         }
237         return 0;
238 }
239
240 int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_clientinfo *request_clientinfo)
241 {
242         int searchslot = 0;
243         unsigned active_count = 0;
244
245         // NULL - checking
246         if (!clientinfo_list || !request_clientinfo ) {
247                 TRACE_DEBUG_MSG("NULL-CHECK");
248                 return -1;
249         }
250
251         CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
252
253 #ifdef SO_PEERCRED
254         socklen_t cr_len =
255                 sizeof(request_clientinfo->credentials);
256         if (getsockopt
257                 (request_clientinfo->clientfd, SOL_SOCKET,
258                 SO_PEERCRED, &request_clientinfo->credentials,
259                 &cr_len) == 0) {
260                 TRACE_DEBUG_INFO_MSG
261                         ("Client Info : pid=%d, uid=%d, gid=%d\n",
262                         request_clientinfo->credentials.pid,
263                         request_clientinfo->credentials.uid,
264                         request_clientinfo->credentials.gid);
265         }
266 #endif
267
268         download_controls type =
269                 ipc_receive_header(request_clientinfo->clientfd);
270         TRACE_DEBUG_INFO_MSG("[ACCEPT] HEADER : [%d] ", type);
271         // first of all, receive requestinfo .
272         if (type <= 0 || ipc_receive_request_msg(request_clientinfo) < 0) {
273                 TRACE_DEBUG_MSG("Ignore this connection, Invalid command");
274                 clear_clientinfo(request_clientinfo);
275                 return -1;
276         }
277
278         if (type == DOWNLOAD_CONTROL_STOP
279                 || type == DOWNLOAD_CONTROL_GET_STATE_INFO
280                 || type == DOWNLOAD_CONTROL_RESUME
281                 || type == DOWNLOAD_CONTROL_PAUSE) {
282                 // get requestid from socket.
283                 if (request_clientinfo->requestinfo
284                         && request_clientinfo->requestinfo->requestid > 0) {
285                         // search requestid in slots.
286                         int searchindex = get_same_request_slot_index
287                                                         (clientinfo_list,
288                                                         request_clientinfo->requestinfo->requestid);
289                         if (type == DOWNLOAD_CONTROL_STOP) {
290                                 TRACE_DEBUG_INFO_MSG("Request : DOWNLOAD_CONTROL_STOP");
291                                 if (searchindex >= 0) {
292                                         if (da_cancel_download
293                                                 (clientinfo_list[searchindex].clientinfo->req_id)
294                                                 == DA_RESULT_OK) {
295                                                 request_clientinfo->state = DOWNLOAD_STATE_STOPPED;
296                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
297                                                 if (clientinfo_list[searchindex].clientinfo->requestinfo
298                                                         && clientinfo_list[searchindex].clientinfo->requestinfo->notification)
299                                                         set_downloadedinfo_appfw_notification(clientinfo_list[searchindex].clientinfo);
300                                                 download_provider_db_requestinfo_remove(request_clientinfo->requestinfo->requestid);
301                                                 download_provider_db_history_new(clientinfo_list[searchindex].clientinfo);
302                                         } else {
303                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
304                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
305                                         }
306                                 } else { // no found
307                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
308                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
309                                 }
310                                 ipc_send_stateinfo(request_clientinfo);
311                         } else if (type == DOWNLOAD_CONTROL_GET_STATE_INFO) {
312                                 // search slots/downloading db/history db
313                                 if (searchindex > 0) { // exist in slots (memory)
314                                         request_clientinfo->state =
315                                                 clientinfo_list[searchindex].clientinfo->state;
316                                         request_clientinfo->err =
317                                                 clientinfo_list[searchindex].clientinfo->err;
318                                 } else {  //search downloading db or history db
319                                         download_dbinfo* dbinfo =
320                                                 download_provider_db_get_info(
321                                                         request_clientinfo->requestinfo->requestid);
322                                         if (dbinfo) { // found in downloading db..it means crashed job
323                                                 request_clientinfo->state = DOWNLOAD_STATE_PENDED;
324                                                 request_clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
325                                         } else { // no exist in downloading db
326                                                 dbinfo = download_provider_db_history_get_info(
327                                                         request_clientinfo->requestinfo->requestid);
328                                                 if (!dbinfo) { // no info anywhere
329                                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
330                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
331                                                 } else { //history info
332                                                         request_clientinfo->state = dbinfo->state;
333                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
334                                                 }
335                                         }
336                                         download_provider_db_info_free(dbinfo);
337                                         free(dbinfo);
338                                 }
339                                 ipc_send_stateinfo(request_clientinfo);
340                                 // estabilish the spec of return value.
341                         } else if (type == DOWNLOAD_CONTROL_PAUSE) {
342                                 if (searchindex >= 0) {
343                                         if (da_suspend_download
344                                                 (clientinfo_list[searchindex].clientinfo->req_id)
345                                                 == DA_RESULT_OK) {
346                                                 request_clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
347                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
348                                         } else {
349                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
350                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
351                                         }
352                                 } else { // no found
353                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
354                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
355                                 }
356                                 ipc_send_stateinfo(request_clientinfo);
357                         } else if (type == DOWNLOAD_CONTROL_RESUME) {
358                                 if (searchindex >= 0) {
359                                         if (da_resume_download
360                                                 (clientinfo_list[searchindex].clientinfo->req_id)
361                                                 == DA_RESULT_OK) {
362                                                 request_clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
363                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
364                                         } else {
365                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
366                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
367                                         }
368                                 } else { // no found
369                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
370                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
371                                 }
372                                 ipc_send_stateinfo(request_clientinfo);
373                         }
374                         ipc_receive_header(request_clientinfo->clientfd);
375                 }
376                 clear_clientinfo(request_clientinfo);
377                 return 0;
378         }
379
380         if (type != DOWNLOAD_CONTROL_START) {
381                 TRACE_DEBUG_MSG
382                         ("Now, DOWNLOAD_CONTROL_START is only supported");
383                 clear_clientinfo(request_clientinfo);
384                 return -1;
385         }
386
387         // check whether requestinfo has requestid or not.
388         if (request_clientinfo->requestinfo
389                 && request_clientinfo->requestinfo->requestid > 0) {
390                 // search same request id.
391                 int searchindex = get_same_request_slot_index(clientinfo_list,
392                                                 request_clientinfo->requestinfo->requestid);
393                 if (searchindex < 0) {
394                         TRACE_DEBUG_INFO_MSG("Not Found Same Request ID");
395                         // Invalid id
396                         request_clientinfo->state = DOWNLOAD_STATE_FAILED;
397                         request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
398                         ipc_send_request_stateinfo(request_clientinfo);
399                         clear_clientinfo(request_clientinfo);
400                         return 0;
401                 } else {        // found request id. // how to deal etag ?
402                         // connect to slot.
403                         TRACE_DEBUG_INFO_MSG("Found Same Request ID slot[%d]", searchindex);
404                         CLIENT_MUTEX_LOCK(&(request_clientinfo->client_mutex));
405                         // close previous socket.
406                         if (clientinfo_list[searchindex].clientinfo->clientfd > 0)
407                                 close(clientinfo_list[searchindex].clientinfo->clientfd);
408                         // change to new socket.
409                         clientinfo_list[searchindex].clientinfo->clientfd =
410                                 request_clientinfo->clientfd;
411                         // update some info.
412                         clientinfo_list[searchindex].clientinfo->requestinfo->callbackinfo =
413                                 request_clientinfo->requestinfo->callbackinfo;
414                         clientinfo_list[searchindex].clientinfo->requestinfo->notification =
415                                 request_clientinfo->requestinfo->notification;
416                         request_clientinfo->clientfd = 0;       // prevent to not be disconnected.
417                         CLIENT_MUTEX_UNLOCK(&(request_clientinfo->client_mutex));
418                         clear_clientinfo(request_clientinfo);
419
420                         if (clientinfo_list[searchindex].clientinfo->state
421                                 == DOWNLOAD_STATE_READY
422                                 || clientinfo_list[searchindex].clientinfo->state
423                                 >= DOWNLOAD_STATE_FINISHED) {
424                                 active_count = get_downloading_count(clientinfo_list);
425                                 if (active_count >= DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
426                                         // deal as pended job.
427                                         _change_pended_download(clientinfo_list[searchindex].clientinfo);
428                                         TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
429                                         searchslot, MAX_CLIENT);
430                                 } else
431                                         _create_download_thread(&clientinfo_list[searchindex]);
432                         } else
433                                 ipc_send_request_stateinfo(clientinfo_list[searchindex].clientinfo);
434                         return 0;
435                 }
436         }
437
438         // new request.
439         searchslot = get_empty_slot_index(clientinfo_list);
440         if (searchslot < 0) {
441                 TRACE_DEBUG_MSG("download-provider is busy, try later");
442                 clear_clientinfo(request_clientinfo);
443                 sleep(5);       // provider need the time of refresh.
444                 return -1;
445         }
446         // create new unique id, and insert info to DB.
447         if (request_clientinfo->requestinfo
448                 && request_clientinfo->requestinfo->requestid <= 0) {
449                 request_clientinfo->requestinfo->requestid =
450                         get_download_request_id();
451                 if (download_provider_db_requestinfo_new
452                         (request_clientinfo) < 0) {
453                         clear_clientinfo(request_clientinfo);
454                         sleep(5);       // provider need the time of refresh.
455                         return -1;
456                 }
457         }
458
459         clientinfo_list[searchslot].clientinfo = request_clientinfo;
460
461         active_count = get_downloading_count(clientinfo_list);
462
463         TRACE_DEBUG_INFO_MSG("New Connection slot [%d/%d] active [%d/%d]",
464                                                                                         searchslot,
465                                                                                         MAX_CLIENT,
466                                                                                         active_count,
467                                                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
468
469         if (active_count >= DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
470                 // deal as pended job.
471                 _change_pended_download(clientinfo_list[searchslot].clientinfo);
472                 TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
473                         searchslot, MAX_CLIENT);
474         } else {
475                 // Pending First
476                 unsigned free_slot_count
477                         = DA_MAX_DOWNLOAD_REQ_AT_ONCE - active_count;
478                 int pendedslot = get_pended_slot_index(clientinfo_list);
479                 if(pendedslot >= 0) {
480                         TRACE_DEBUG_INFO_MSG ("Free Space [%d]", free_slot_count);
481                         for (;free_slot_count > 0 && pendedslot >= 0; free_slot_count--) {
482                                 TRACE_DEBUG_INFO_MSG ("Start pended job [%d]", pendedslot);
483                                 _create_download_thread(&clientinfo_list[pendedslot]);
484                                 pendedslot = get_pended_slot_index(clientinfo_list);
485                         }
486                 }
487
488                 if (free_slot_count <= 0) { // change to PENDED
489                         // start pended job, deal this job to pended
490                         _change_pended_download(clientinfo_list[searchslot].clientinfo);
491                         TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
492                                 searchslot, MAX_CLIENT);
493                 } else
494                         _create_download_thread(&clientinfo_list[searchslot]);
495         }
496         return 0;
497 }
498
499 int _handle_client_request(download_clientinfo* clientinfo)
500 {
501         int da_ret = 0;
502         int msgType = 0;
503
504         // NULL - checking
505         if (!clientinfo) {
506                 TRACE_DEBUG_MSG("NULL-CHECK");
507                 return -1;
508         }
509
510         switch (msgType = ipc_receive_header(clientinfo->clientfd)) {
511         case DOWNLOAD_CONTROL_STOP:
512                 if (clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
513                         // clear slot requested by client after finished download
514                         TRACE_DEBUG_INFO_MSG("request Free slot to main thread");
515                         clear_socket(clientinfo);
516                         break;
517                 }
518                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_STOP");
519                 da_ret = da_cancel_download(clientinfo->req_id);
520                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
521                 if (da_ret != DA_RESULT_OK) {
522                         /* FIXME : need to seperate in detail according to error return values */
523                         clientinfo->state = DOWNLOAD_STATE_FAILED;
524                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
525                         TRACE_DEBUG_MSG("Fail to request cancel [%d]", da_ret);
526                 } else {
527                         clientinfo->state = DOWNLOAD_STATE_STOPPED;
528                         clientinfo->err = DOWNLOAD_ERROR_NONE;
529                         if (clientinfo->requestinfo) {
530                                 if (clientinfo->requestinfo->notification)
531                                         set_downloadedinfo_appfw_notification(clientinfo);
532                                 download_provider_db_requestinfo_remove(clientinfo->
533                                         requestinfo->requestid);
534                         }
535                         download_provider_db_history_new(clientinfo);
536                 }
537                 ipc_send_stateinfo(clientinfo);
538                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
539                 break;
540         case DOWNLOAD_CONTROL_PAUSE:
541                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_PAUSE");
542                 da_ret = da_suspend_download(clientinfo->req_id);
543                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
544                 if (da_ret != DA_RESULT_OK) {
545                         /* FIXME : need to seperate in detail according to error return values */
546                         clientinfo->state = DOWNLOAD_STATE_FAILED;
547                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
548                         TRACE_DEBUG_MSG("Fail to request suspend [%d]", da_ret);
549                 } else {
550                         clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
551                         clientinfo->err = DOWNLOAD_ERROR_NONE;
552                 }
553                 ipc_send_stateinfo(clientinfo);
554                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
555                 break;
556         case DOWNLOAD_CONTROL_RESUME:
557                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_RESUME");
558                 da_ret = da_resume_download(clientinfo->req_id);
559                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
560                 if (da_ret != DA_RESULT_OK) {
561                         /* FIXME : need to seperate in detail according to error return values */
562                         clientinfo->state = DOWNLOAD_STATE_FAILED;
563                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
564                         TRACE_DEBUG_MSG("Fail to request resume [%d]", da_ret);
565                 } else {
566                         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
567                         clientinfo->err = DOWNLOAD_ERROR_NONE;
568                 }
569                 ipc_send_stateinfo(clientinfo);
570                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
571                 break;
572         case DOWNLOAD_CONTROL_GET_STATE_INFO:   // sync call
573                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_STATE_INFO");
574                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
575                 ipc_send_stateinfo(clientinfo);
576                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
577                 break;
578         case DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO:        // sync call
579                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO");
580                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
581                 ipc_send_downloadinfo(clientinfo);
582                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
583                 break;
584         case -1:
585         case 0:
586                 TRACE_DEBUG_MSG("(Closed Socket ) terminate event thread (%d)",
587                         msgType);
588                 // bloken socket... it seems the client is dead or closed by agent thread.
589                 // close socket, this will break the loop. and terminate this thread.
590                 clear_socket(clientinfo);
591                 return -1;
592         default:
593                 TRACE_DEBUG_MSG("Unknow message [%d]", msgType);
594                 return -1;
595         }
596         return 0;
597 }
598
599 void *run_manage_download_server(void *args)
600 {
601         int listenfd = 0;       // main socket to be albe to listen the new connection
602         int maxfd;
603         int ret = 0;
604         fd_set rset, exceptset;
605         struct timeval timeout;
606         long flexible_timeout;
607         download_clientinfo_slot *clientinfo_list;
608         int searchslot = 0;
609         unsigned active_count = 0;
610         download_clientinfo *request_clientinfo;
611         int check_retry = 1;
612         int i = 0;
613         int is_timeout = 0;
614
615         socklen_t clientlen;
616         struct sockaddr_un listenaddr, clientaddr;
617
618         GMainLoop *mainloop = (GMainLoop *) args;
619
620         ret = _init_agent();
621         if (ret != DOWNLOAD_ERROR_NONE) {
622                 TRACE_DEBUG_MSG("failed to init agent");
623                 TerminateDaemon(SIGTERM);
624                 return 0;
625         }
626         clear_downloadinginfo_appfw_notification();
627
628         if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
629                 TRACE_DEBUG_MSG("failed to create socket");
630                 TerminateDaemon(SIGTERM);
631                 return 0;
632         }
633
634         bzero(&listenaddr, sizeof(listenaddr));
635         listenaddr.sun_family = AF_UNIX;
636         strcpy(listenaddr.sun_path, DOWNLOAD_PROVIDER_IPC);
637
638         if (bind(listenfd, (struct sockaddr *)&listenaddr, sizeof listenaddr) !=
639                 0) {
640                 TRACE_DEBUG_MSG("failed to call bind");
641                 TerminateDaemon(SIGTERM);
642                 return 0;
643         }
644
645         if (chmod(listenaddr.sun_path, 0777) < 0) {
646                 TRACE_DEBUG_MSG
647                         ("failed to change the permission of socket file");
648                 TerminateDaemon(SIGTERM);
649                 return 0;
650         }
651
652         if (listen(listenfd, MAX_CLIENT) != 0) {
653                 TRACE_DEBUG_MSG("failed to call listen");
654                 TerminateDaemon(SIGTERM);
655                 return 0;
656         }
657
658         maxfd = listenfd;
659         TRACE_DEBUG_INFO_MSG("Ready to listen IPC [%d][%s]", listenfd,
660                         DOWNLOAD_PROVIDER_IPC);
661
662         // allocation the array structure for managing the clients.
663         clientinfo_list =
664                 (download_clientinfo_slot *) calloc(MAX_CLIENT,
665                                                         sizeof(download_clientinfo_slot));
666         if (clientinfo_list == NULL) {
667                 TRACE_DEBUG_MSG("failed to allocate the memory for client list");
668                 TerminateDaemon(SIGTERM);
669                 return 0;
670         }
671
672         if (pthread_attr_init(&g_download_provider_thread_attr) != 0) {
673                 TRACE_DEBUG_MSG("failed to call pthread_attr_init for client");
674                 TerminateDaemon(SIGTERM);
675                 return 0;
676         }
677         if (pthread_attr_setdetachstate(&g_download_provider_thread_attr,
678                                                                         PTHREAD_CREATE_DETACHED) != 0) {
679                 TRACE_DEBUG_MSG("failed to set detach option");
680                 TerminateDaemon(SIGTERM);
681                 return 0;
682         }
683
684         flexible_timeout = DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
685
686         FD_ZERO(&g_download_provider_socket_readset);
687         FD_ZERO(&g_download_provider_socket_exceptset);
688         FD_SET(listenfd, &g_download_provider_socket_readset);
689         FD_SET(listenfd, &g_download_provider_socket_exceptset);
690
691         while (g_main_loop_is_running(mainloop)) {
692
693                 // clean slots
694                 for (i = 0; i < MAX_CLIENT; i++) {
695                         if (!clientinfo_list[i].clientinfo)
696                                 continue;
697                         // clear slot.
698                         if (clientinfo_list[i].clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
699                                 if (clientinfo_list[i].clientinfo->clientfd <= 0)
700                                         clear_clientinfoslot(&clientinfo_list[i]);
701                                 continue;
702                         }
703                 }
704
705                 is_timeout = 1;
706                 rset = g_download_provider_socket_readset;
707                 exceptset = g_download_provider_socket_exceptset;
708
709                 memset(&timeout, 0x00, sizeof(struct timeval));
710                 timeout.tv_sec = flexible_timeout;
711
712                 if (select((maxfd + 1), &rset, 0, &exceptset, &timeout) < 0) {
713                         TRACE_DEBUG_MSG
714                                 ("select error, provider can't receive any request from client.");
715                         TerminateDaemon(SIGTERM);
716                         break;
717                 }
718
719                 for (i = 0; i < MAX_CLIENT; i++) {  // find the socket received the packet.
720                         if (!clientinfo_list[i].clientinfo)
721                                 continue;
722                         //Even if no socket, downloading should be progressed.
723                         if (clientinfo_list[i].clientinfo->clientfd <= 0)
724                                 continue;
725                         if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &rset) > 0) {
726                                 // ignore it is not started yet.
727                                 if (clientinfo_list[i].clientinfo->state <= DOWNLOAD_STATE_READY)
728                                         continue;
729                                 TRACE_DEBUG_INFO_MSG("FD_ISSET [%d] readset slot[%d]",
730                                         clientinfo_list[i].clientinfo->clientfd, i);
731                                 _handle_client_request(clientinfo_list[i].clientinfo);
732                                 if (is_timeout)
733                                         is_timeout = 0;
734                         } else if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &exceptset) > 0) {
735                                 TRACE_DEBUG_MSG("FD_ISSET [%d] exceptset slot[%d]", clientinfo_list[i].clientinfo->clientfd, i);
736                                 clear_clientinfoslot(&clientinfo_list[i]);
737                         }
738                 } // MAX_CLIENT
739
740                 if (FD_ISSET(listenfd, &exceptset) > 0) {
741                         TRACE_DEBUG_MSG("meet listenfd Exception of socket");
742                         TerminateDaemon(SIGTERM);
743                         break;
744                 } else if (FD_ISSET(listenfd, &rset) > 0) { // new connection
745                         TRACE_DEBUG_INFO_MSG("FD_ISSET listenfd rset");
746                         if (is_timeout)
747                                 is_timeout = 0;
748                         // reset timeout.
749                         flexible_timeout =
750                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
751                         // ready the buffer.
752                         request_clientinfo =
753                                 (download_clientinfo *) calloc(1,
754                                                         sizeof(download_clientinfo));
755                         if (!request_clientinfo) {
756                                 TRACE_DEBUG_MSG
757                                         ("download-provider can't allocate the memory, try later");
758                                 clientlen = sizeof(clientaddr);
759                                 int clientfd = accept(listenfd,
760                                         (struct sockaddr *)&clientaddr, &clientlen);
761                                 close(clientfd);        // disconnect.
762                                 sleep(5);       // provider need the time of refresh.
763                                 continue;
764                         }
765                         // accept client.
766                         clientlen = sizeof(clientaddr);
767                         request_clientinfo->clientfd = accept(listenfd,
768                                                                 (struct sockaddr*)&clientaddr,
769                                                                 &clientlen);
770                         if (request_clientinfo->clientfd < 0) {
771                                 clear_clientinfo(request_clientinfo);
772                                 sleep(5);       // provider need the time of refresh.
773                                 continue;
774                         }
775                         if (_handle_new_connection(clientinfo_list, request_clientinfo) < 0) {
776                                 sleep(1);
777                                 continue;
778                         }
779                         // after starting the download by DA, event thread will start to get the event from client.
780                         if (request_clientinfo && request_clientinfo->clientfd > 0) {
781                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_readset);      // add new descriptor to set
782                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_exceptset);
783                                 if (request_clientinfo->clientfd > maxfd )
784                                         maxfd = request_clientinfo->clientfd;   /* for select */
785                         }
786                 }
787
788                 if (is_timeout) { // timeout
789                         // If there is better solution to be able to know
790                         // the number of downloading threads, replace below rough codes.
791                         active_count = get_downloading_count(clientinfo_list);
792                         // check whether the number of downloading is already maximum.
793                         if (active_count >= DA_MAX_DOWNLOAD_REQ_AT_ONCE)
794                                 continue;
795
796                         unsigned free_slot_count
797                                         = DA_MAX_DOWNLOAD_REQ_AT_ONCE - active_count;
798                         int pendedslot = get_pended_slot_index(clientinfo_list);
799                         if(pendedslot >= 0) {
800                                 TRACE_DEBUG_INFO_MSG ("Free Space [%d]", free_slot_count);
801                                 for (;free_slot_count > 0 && pendedslot >= 0; free_slot_count--) {
802                                         TRACE_DEBUG_INFO_MSG ("start pended job [%d]", pendedslot);
803                                         if (_create_download_thread(&clientinfo_list[pendedslot]) > 0)
804                                                 active_count++;
805                                         pendedslot = get_pended_slot_index(clientinfo_list);
806                                 }
807                         }
808
809                         if (check_retry && free_slot_count > 0) {
810                                 // Auto re-download feature. 
811                                 // ethernet may be connected with other downloading items.
812                                 if (get_network_status() < 0)
813                                         continue;
814
815                                 // check auto-retrying list regardless state. pended state is also included to checking list.
816                                 int i = 0;
817                                 download_dbinfo_list *db_list =
818                                         download_provider_db_get_list(DOWNLOAD_STATE_NONE);
819                                 if (!db_list || db_list->count <= 0) {
820                                         TRACE_DEBUG_INFO_MSG
821                                                 ("provider does not need to check DB anymore. in this life.");
822                                         // provider does not need to check DB anymore. in this life.
823                                         check_retry = 0;
824                                         if (db_list)
825                                                 download_provider_db_list_free(db_list);
826                                         continue;
827                                 }
828                                 for (i = 0;
829                                         active_count <
830                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE
831                                         && i < db_list->count; i++) {
832                                         if (db_list->item[i].requestid <= 0)
833                                                 continue;
834                                         if (get_same_request_slot_index
835                                                 (clientinfo_list,db_list->item[i].requestid) < 0) {
836                                                 // not found requestid in memory
837                                                 TRACE_DEBUG_INFO_MSG
838                                                         ("Retry download [%d]",
839                                                         db_list->item[i].requestid);
840                                                 //search empty slot. copy db info to slot.
841                                                 searchslot =
842                                                         get_empty_slot_index(clientinfo_list);
843                                                 if (searchslot < 0) {
844                                                         TRACE_DEBUG_INFO_MSG
845                                                                 ("download-provider is busy, try later");
846                                                         flexible_timeout =
847                                                                 flexible_timeout * 2;
848                                                         break;
849                                                 }
850                                                 // allocte requestinfo to empty slot.
851                                                 request_clientinfo =
852                                                         (download_clientinfo *)
853                                                         calloc(1, sizeof(download_clientinfo));
854                                                 if (!request_clientinfo)
855                                                         continue;
856                                                 request_clientinfo->requestinfo
857                                                         =
858                                                         download_provider_db_get_requestinfo
859                                                         (&db_list->item[i]);
860                                                 if (!request_clientinfo->requestinfo) {
861                                                         free(request_clientinfo);
862                                                         request_clientinfo = NULL;
863                                                         continue;
864                                                 }
865
866                                                 CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
867                                                 request_clientinfo->state = DOWNLOAD_STATE_READY;
868                                                 clientinfo_list[searchslot].clientinfo =
869                                                         request_clientinfo;
870
871                                                 TRACE_DEBUG_INFO_MSG
872                                                         ("Retry download [%d/%d][%d/%d]",
873                                                         searchslot, MAX_CLIENT,
874                                                         active_count,
875                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
876                                                 if (_create_download_thread(&clientinfo_list[searchslot]) > 0)
877                                                         active_count++;
878                                         }
879                                 }
880                                 if (i >= db_list->count)        // do not search anymore.
881                                         check_retry = 0;
882                                 download_provider_db_list_free(db_list);
883                         }
884
885                         // save system resource (CPU)
886                         if (check_retry == 0 && active_count == 0
887                                 && flexible_timeout <
888                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
889                                 flexible_timeout = flexible_timeout * 2;
890                         if (flexible_timeout >
891                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
892                                 flexible_timeout =
893                                         DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL;
894                         TRACE_DEBUG_INFO_MSG("Next Timeout after [%ld] sec",
895                                         flexible_timeout);
896
897                 } // if (i >= MAX_CLIENT) { // timeout
898         }
899
900         FD_CLR(listenfd, &rset);
901         FD_CLR(listenfd, &exceptset);
902         FD_CLR(listenfd, &g_download_provider_socket_readset);
903         FD_CLR(listenfd, &g_download_provider_socket_exceptset);
904
905         // close accept socket.
906         if (listenfd)
907                 close(listenfd);
908
909         _deinit_agent();
910
911         // close all sockets for client. .. 
912         // client thread will terminate by itself through catching this closing.
913         for (searchslot = 0; searchslot < MAX_CLIENT; searchslot++)
914                 if (clientinfo_list[searchslot].clientinfo)
915                         clear_clientinfoslot(&clientinfo_list[searchslot]);
916
917         if (clientinfo_list)
918                 free(clientinfo_list);
919
920         pthread_exit(NULL);
921         return 0;
922 }
923
924 void __download_info_cb(user_download_info_t *download_info, void *user_data)
925 {
926         int len = 0;
927         if (!user_data) {
928                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
929                 return;
930         }
931         download_clientinfo_slot *clientinfoslot =
932                 (download_clientinfo_slot *) user_data;
933         download_clientinfo *clientinfo =
934                 (download_clientinfo *) clientinfoslot->clientinfo;
935         if (!clientinfo) {
936                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
937                 return;
938         }
939         TRACE_DEBUG_INFO_MSG("id[%d],size[%lu]",
940                         download_info->da_dl_req_id, download_info->file_size);
941
942         if (clientinfo->req_id != download_info->da_dl_req_id) {
943                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
944                                 clientinfo->req_id,
945                                 download_info->da_dl_req_id);
946                 return;
947         }
948         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
949         if (!clientinfo->downloadinfo)
950                 clientinfo->downloadinfo =
951                         (download_content_info *) calloc(1, sizeof(download_content_info));
952         if (clientinfo->downloadinfo)
953                 clientinfo->downloadinfo->file_size = download_info->file_size;
954         if (download_info->file_type) {
955                 TRACE_DEBUG_INFO_MSG("mime[%s]", download_info->file_type);
956
957                 len = strlen(download_info->file_type);
958                 if (len > (DP_MAX_STR_LEN - 1))
959                         len = DP_MAX_STR_LEN - 1;
960                 if (clientinfo->downloadinfo) {
961                         strncpy(clientinfo->downloadinfo->mime_type,
962                                 download_info->file_type, len);
963                         download_provider_db_requestinfo_update_column
964                                 (clientinfo, DOWNLOAD_DB_MIMETYPE);
965                 }
966         }
967         if (download_info->tmp_saved_path) {
968                 char *str = NULL;
969                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->tmp_saved_path);
970                 clientinfo->tmp_saved_path =
971                         strdup(download_info->tmp_saved_path);
972                 download_provider_db_requestinfo_update_column(clientinfo,
973                                                                         DOWNLOAD_DB_SAVEDPATH);
974                 str = strrchr(download_info->tmp_saved_path, '/');
975                 if (str) {
976                         str++;
977                         len = strlen(str);
978                         if (len > (DP_MAX_STR_LEN - 1))
979                                 len = DP_MAX_STR_LEN - 1;
980                         if (clientinfo->downloadinfo) {
981                                 strncpy(clientinfo->downloadinfo->content_name,
982                                         str, len);
983                                 download_provider_db_requestinfo_update_column
984                                         (clientinfo, DOWNLOAD_DB_FILENAME);
985                                 TRACE_DEBUG_INFO_MSG("content_name[%s]",
986                                                 clientinfo->downloadinfo->
987                                                 content_name);
988                         }
989                 }
990         }
991
992         if (clientinfo->requestinfo->callbackinfo.started)
993                 ipc_send_downloadinfo(clientinfo);
994
995         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
996 }
997
998 void __downloading_info_cb(user_downloading_info_t *download_info,
999                            void *user_data)
1000 {
1001         int len = 0;
1002         if (!user_data) {
1003                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1004                 return;
1005         }
1006         download_clientinfo_slot *clientinfoslot =
1007                 (download_clientinfo_slot *) user_data;
1008         download_clientinfo *clientinfo =
1009                 (download_clientinfo *) clientinfoslot->clientinfo;
1010         if (!clientinfo) {
1011                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1012                 return;
1013         }
1014         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1015         if (clientinfo->req_id != download_info->da_dl_req_id) {
1016                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1017                                 clientinfo->req_id,
1018                                 download_info->da_dl_req_id);
1019                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1020                 return;
1021         }
1022         if (!clientinfo->downloadinginfo)
1023                 clientinfo->downloadinginfo = (downloading_state_info *) calloc(1,
1024                                                                         sizeof(downloading_state_info));
1025         if (clientinfo->downloadinginfo)
1026                 clientinfo->downloadinginfo->received_size =
1027                         download_info->total_received_size;
1028         if (download_info->saved_path) {
1029                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->saved_path);
1030                 len = strlen(download_info->saved_path);
1031                 if (len > (DP_MAX_PATH_LEN - 1))
1032                         len = DP_MAX_PATH_LEN - 1;
1033                 if (clientinfo->downloadinginfo)
1034                         strncpy(clientinfo->downloadinginfo->saved_path,
1035                                 download_info->saved_path, len);
1036                 /* FIXME : This should be reviewd again after smack rules is applied */
1037                 if (chown(clientinfo->downloadinginfo->saved_path,
1038                                 clientinfo->credentials.uid,
1039                                 clientinfo->credentials.gid) < 0)
1040                         TRACE_DEBUG_INFO_MSG("Fail to chown [%s]", strerror(errno));
1041         }
1042
1043         static size_t updated_second;
1044         time_t tt = time(NULL);
1045         struct tm *localTime = localtime(&tt);
1046
1047         if (updated_second != localTime->tm_sec || download_info->saved_path) { // every 1 second.
1048                 if (clientinfo->requestinfo
1049                         && clientinfo->requestinfo->notification)
1050                         set_downloadinginfo_appfw_notification(clientinfo);
1051                 if (clientinfo->requestinfo->callbackinfo.progress)
1052                         ipc_send_downloadinginfo(clientinfo);
1053                 updated_second = localTime->tm_sec;
1054         }
1055         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1056 }
1057
1058 void __notify_cb(user_notify_info_t *notify_info, void *user_data)
1059 {
1060         if (!user_data) {
1061                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1062                 return;
1063         }
1064         download_clientinfo_slot *clientinfoslot =
1065                 (download_clientinfo_slot *) user_data;
1066         download_clientinfo *clientinfo =
1067                 (download_clientinfo *) clientinfoslot->clientinfo;
1068         if (!clientinfo) {
1069                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1070                 return;
1071         }
1072
1073         TRACE_DEBUG_INFO_MSG("id[%d],state[%d],err[%d]",
1074                         notify_info->da_dl_req_id,
1075                         notify_info->state, notify_info->err);
1076         if (clientinfo->req_id != notify_info->da_dl_req_id) {
1077                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1078                                 clientinfo->req_id, notify_info->da_dl_req_id);
1079                 return;
1080         }
1081
1082         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1083
1084         clientinfo->state = __change_state(notify_info->state);
1085         clientinfo->err = __change_error(notify_info->err);
1086         if (clientinfo->state == DOWNLOAD_STATE_FINISHED ||
1087                         clientinfo->state == DOWNLOAD_STATE_FAILED) {
1088                 if (clientinfo->requestinfo) {
1089                         if (clientinfo->requestinfo->notification)
1090                                 set_downloadedinfo_appfw_notification(clientinfo);
1091                         download_provider_db_requestinfo_remove(clientinfo->
1092                                 requestinfo->requestid);
1093                 }
1094                 download_provider_db_history_new(clientinfo);
1095                 TRACE_DEBUG_INFO_MSG("[TEST]Finish clientinfo[%p], fd[%d]",
1096                         clientinfo, clientinfo->clientfd);
1097         }
1098
1099         TRACE_DEBUG_INFO_MSG("state[%d]", clientinfo->state);
1100         ipc_send_stateinfo(clientinfo);
1101
1102         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1103 }
1104
1105 int __change_state(da_state state)
1106 {
1107         int ret = DOWNLOAD_STATE_NONE;
1108         switch (state) {
1109         case DA_STATE_WAITING:
1110         case DA_STATE_DOWNLOAD_STARTED:
1111                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOAD_STARTED");
1112                 ret = DOWNLOAD_STATE_READY;
1113                 break;
1114         case DA_STATE_DOWNLOADING:
1115                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOADING");
1116                 ret = DOWNLOAD_STATE_DOWNLOADING;
1117                 break;
1118         case DA_STATE_DOWNLOAD_COMPLETE:
1119                 TRACE_DEBUG_INFO_MSG("DA_STATE_COMPLETE");
1120                 ret = DOWNLOAD_STATE_INSTALLING;
1121                 break;
1122         case DA_STATE_CANCELED:
1123                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED");
1124                 ret = DOWNLOAD_STATE_STOPPED;
1125                 break;
1126         case DA_STATE_CANCELED_ALL:
1127                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED_ALL");
1128                 break;
1129         case DA_STATE_SUSPENDED:
1130                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED");
1131                 ret = DOWNLOAD_STATE_PAUSED;
1132                 break;
1133         case DA_STATE_SUSPENDED_ALL:
1134                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED_ALL");
1135                 break;
1136         case DA_STATE_RESUMED:
1137                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED");
1138                 ret = DOWNLOAD_STATE_DOWNLOADING;
1139                 break;
1140         case DA_STATE_RESUMED_ALL:
1141                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED_ALL");
1142                 break;
1143         case DA_STATE_FINISHED:
1144                 TRACE_DEBUG_INFO_MSG("DA_STATE_FINISHED");
1145                 ret = DOWNLOAD_STATE_FINISHED;
1146                 break;
1147         case DA_STATE_FAILED:
1148                 TRACE_DEBUG_INFO_MSG("DA_STATE_FAILED");
1149                 ret = DOWNLOAD_STATE_FAILED;
1150                 break;
1151         default:
1152                 break;
1153         }
1154         return ret;
1155 }
1156
1157 int __change_error(int err)
1158 {
1159         int ret = DOWNLOAD_ERROR_UNKOWN;
1160         switch (err) {
1161         case DA_RESULT_OK:
1162                 ret = DOWNLOAD_ERROR_NONE;
1163                 break;
1164         case DA_ERR_INVALID_ARGUMENT:
1165                 ret = DOWNLOAD_ERROR_INVALID_PARAMETER;
1166                 break;
1167         case DA_ERR_FAIL_TO_MEMALLOC:
1168                 ret = DOWNLOAD_ERROR_OUT_OF_MEMORY;
1169                 break;
1170         case DA_ERR_UNREACHABLE_SERVER:
1171                 ret = DOWNLOAD_ERROR_NETWORK_UNREACHABLE;
1172                 break;
1173         case DA_ERR_HTTP_TIMEOUT:
1174                 ret = DOWNLOAD_ERROR_CONNECTION_TIMED_OUT;
1175                 break;
1176         case DA_ERR_DISK_FULL:
1177                 ret = DOWNLOAD_ERROR_NO_SPACE;
1178                 break;
1179         case DA_ERR_INVALID_STATE:
1180                 ret = DOWNLOAD_ERROR_INVALID_STATE;
1181                 break;
1182         case DA_ERR_NETWORK_FAIL:
1183                 ret = DOWNLOAD_ERROR_CONNECTION_FAILED;
1184                 break;
1185         case DA_ERR_INVALID_URL:
1186                 ret = DOWNLOAD_ERROR_INVALID_URL;
1187                 break;
1188         case DA_ERR_INVALID_INSTALL_PATH:
1189                 ret = DOWNLOAD_ERROR_INVALID_DESTINATION;
1190                 break;
1191         case DA_ERR_ALREADY_MAX_DOWNLOAD:
1192                 ret = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
1193                 break;
1194         case DA_ERR_FAIL_TO_INSTALL_FILE:
1195                 ret = DOWNLOAD_ERROR_INSTALL_FAIL;
1196                 break;
1197         case DA_ERR_FAIL_TO_CREATE_THREAD:
1198         case DA_ERR_FAIL_TO_OBTAIN_MUTEX:
1199         case DA_ERR_FAIL_TO_ACCESS_FILE:
1200         case DA_ERR_FAIL_TO_GET_CONF_VALUE:
1201         case DA_ERR_FAIL_TO_ACCESS_STORAGE:
1202         case DA_ERR_DLOPEN_FAIL:
1203                 ret = DOWNLOAD_ERROR_IO_ERROR;
1204                 break;
1205         }
1206         return ret;
1207 }
1208
1209 int _init_agent()
1210 {
1211         int da_ret = 0;
1212         da_client_cb_t da_cb = {
1213                 __notify_cb,
1214                 __download_info_cb,
1215                 __downloading_info_cb
1216         };
1217         da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
1218         if (da_ret != DA_RESULT_OK) {
1219                 return DOWNLOAD_ERROR_FAIL_INIT_AGENT;
1220         }
1221         return DOWNLOAD_ERROR_NONE;
1222 }
1223
1224 void _deinit_agent()
1225 {
1226         da_deinit();
1227 }