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