merge with master
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include <glib.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <errno.h>
30
31 static __thread GQueue *g_request_queue = NULL;
32 typedef struct {
33         GIOChannel *channel;
34         char *path;
35         int source_id;
36 } thumbReq;
37
38 int
39 _media_thumb_create_socket(int sock_type, int *sock)
40 {
41         int sock_fd = 0;
42
43         if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
44                 thumb_err("socket failed: %s", strerror(errno));
45                 return MEDIA_THUMB_ERROR_NETWORK;
46         }
47
48         if (sock_type == CLIENT_SOCKET) {
49
50 #ifdef _USE_MEDIA_UTIL_
51                 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
52 #else
53                 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
54 #endif
55
56                 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
57                         thumb_err("setsockopt failed: %s", strerror(errno));
58                         close(sock_fd);
59                         return MEDIA_THUMB_ERROR_NETWORK;
60                 }
61         } else if (sock_type == SERVER_SOCKET) {
62
63                 int n_reuse = 1;
64
65                 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
66                         thumb_err("setsockopt failed: %s", strerror(errno));
67                         close(sock_fd);
68                         return MEDIA_THUMB_ERROR_NETWORK;
69                 }
70         }
71
72         *sock = sock_fd;
73
74         return MEDIA_THUMB_ERROR_NONE;
75 }
76
77
78 int
79 _media_thumb_create_udp_socket(int *sock)
80 {
81         int sock_fd = 0;
82
83         if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
84                 thumb_err("socket failed: %s", strerror(errno));
85                 return MEDIA_THUMB_ERROR_NETWORK;
86         }
87
88 #ifdef _USE_MEDIA_UTIL_
89         struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
90 #else
91         struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
92 #endif
93
94         if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
95                 thumb_err("setsockopt failed: %s", strerror(errno));
96                 close(sock_fd);
97                 return MEDIA_THUMB_ERROR_NETWORK;
98         }
99
100         *sock = sock_fd;
101
102         return MEDIA_THUMB_ERROR_NONE;
103 }
104
105 int _media_thumb_get_error()
106 {
107         if (errno == EWOULDBLOCK) {
108                 thumb_err("Timeout. Can't try any more");
109                 return MEDIA_THUMB_ERROR_TIMEOUT;
110         } else {
111                 thumb_err("recvfrom failed : %s", strerror(errno));
112                 return MEDIA_THUMB_ERROR_NETWORK;
113         }
114 }
115
116 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
117 {
118         int req_len = 0, i;
119
120         if (g_request_queue == NULL) return -1;
121         req_len = g_queue_get_length(g_request_queue);
122
123 //      thumb_dbg("Queue length : %d", req_len);
124 //      thumb_dbg("Queue path : %s", path);
125
126         if (req_len <= 0) {
127 //              thumb_dbg("There is no request in the queue");
128         } else {
129
130                 for (i = 0; i < req_len; i++) {
131                         thumbReq *req = NULL;
132                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
133                         if (req == NULL) continue;
134
135                         if (strncmp(path, req->path, strlen(path)) == 0) {
136                                 //thumb_dbg("Popped %s", path);
137                                 if (shutdown_channel) {
138                                         g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
139
140                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
141                                         g_io_channel_unref(req->channel);
142                                 }
143                                 g_queue_pop_nth(g_request_queue, i);
144
145                                 SAFE_FREE(req->path);
146                                 SAFE_FREE(req);
147
148                                 break;
149                         }
150                 }
151                 if (i == req_len) {
152 //                      thumb_dbg("There's no %s in the queue", path);
153                 }
154         }
155
156         return MEDIA_THUMB_ERROR_NONE;
157 }
158
159 int __media_thumb_check_req_queue(const char *path)
160 {
161         int req_len = 0, i;
162
163         req_len = g_queue_get_length(g_request_queue);
164
165 //      thumb_dbg("Queue length : %d", req_len);
166 //      thumb_dbg("Queue path : %s", path);
167
168         if (req_len <= 0) {
169 //              thumb_dbg("There is no request in the queue");
170         } else {
171
172                 for (i = 0; i < req_len; i++) {
173                         thumbReq *req = NULL;
174                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
175                         if (req == NULL) continue;
176
177                         if (strncmp(path, req->path, strlen(path)) == 0) {
178                                 //thumb_dbg("Same Request - %s", path);
179                                 return -1;
180
181                                 break;
182                         }
183                 }
184         }
185
186         return MEDIA_THUMB_ERROR_NONE;
187 }
188
189 int
190 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
191 {
192         int recv_msg_len = 0;
193         unsigned char *buf = NULL;
194
195         buf = (unsigned char*)malloc(header_size);
196
197         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
198                 thumb_err("recvfrom failed : %s", strerror(errno));
199                 SAFE_FREE(buf);
200                 return _media_thumb_get_error();
201         }
202
203         memcpy(msg, buf, header_size);
204         //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
205
206         SAFE_FREE(buf);
207
208         buf = (unsigned char*)malloc(msg->origin_path_size);
209
210         if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
211                 thumb_err("recvfrom failed : %s", strerror(errno));
212                 SAFE_FREE(buf);
213                 return _media_thumb_get_error();
214         }
215
216         strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
217         //thumb_dbg("original path : %s", msg->org_path);
218
219         SAFE_FREE(buf);
220         buf = (unsigned char*)malloc(msg->dest_path_size);
221
222         if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
223                 thumb_err("recvfrom failed : %s", strerror(errno));
224                 SAFE_FREE(buf);
225                 return _media_thumb_get_error();
226         }
227
228         strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
229         //thumb_dbg("destination path : %s", msg->dst_path);
230
231         SAFE_FREE(buf);
232         return MEDIA_THUMB_ERROR_NONE;
233 }
234
235
236 int
237 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
238 {
239         int recv_msg_len = 0;
240         unsigned int from_addr_size = sizeof(struct sockaddr_in);
241         unsigned char *buf = NULL;
242
243         buf = (unsigned char*)malloc(sizeof(thumbMsg));
244
245         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
246                 thumb_err("recvfrom failed : %s", strerror(errno));
247                 SAFE_FREE(buf);
248                 return _media_thumb_get_error();
249         }
250
251         memcpy(msg, buf, header_size);
252
253         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
254                 SAFE_FREE(buf);
255                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
256                 return MEDIA_THUMB_ERROR_NETWORK;
257         }
258
259         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
260         //thumb_dbg("original path : %s", msg->org_path);
261
262         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
263                 SAFE_FREE(buf);
264                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
265                 return MEDIA_THUMB_ERROR_NETWORK;
266         }
267
268         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
269         //thumb_dbg("destination path : %s", msg->dst_path);
270
271         SAFE_FREE(buf);
272         *from_size = from_addr_size;
273
274         return MEDIA_THUMB_ERROR_NONE;
275 }
276
277 int
278 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
279 {
280         if (req_msg == NULL || buf == NULL) {
281                 return -1;
282         }
283
284         int org_path_len = 0;
285         int dst_path_len = 0;
286         int size = 0;
287         int header_size = 0;
288
289         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
290         org_path_len = strlen(req_msg->org_path) + 1;
291         dst_path_len = strlen(req_msg->dst_path) + 1;
292
293         //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
294
295         size = header_size + org_path_len + dst_path_len;
296         *buf = malloc(size);
297         memcpy(*buf, req_msg, header_size);
298         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
299         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
300
301         *buf_size = size;
302
303         return 0;
304 }
305
306 int
307 _media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info)
308 {
309         int sock;
310         const char *serv_ip = "127.0.0.1";
311         struct sockaddr_in serv_addr;
312
313         int recv_str_len = 0;
314         int err;
315         int pid;
316
317
318 #ifdef _USE_MEDIA_UTIL_
319         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
320                 thumb_err("ms_ipc_create_client_socket failed");
321                 return MEDIA_THUMB_ERROR_NETWORK;
322         }
323 #else
324         /* Creaete a TCP socket */
325         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
326                 thumb_err("_media_thumb_create_socket failed");
327                 return MEDIA_THUMB_ERROR_NETWORK;
328         }
329 #endif
330
331         memset(&serv_addr, 0, sizeof(serv_addr));
332         serv_addr.sin_family = AF_INET;
333         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
334 #ifdef _USE_MEDIA_UTIL_
335         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
336 #else
337         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
338 #endif
339
340         /* Connecting to the thumbnail server */
341         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
342                 thumb_err("connect error : %s", strerror(errno));
343                 return MEDIA_THUMB_ERROR_NETWORK;
344         }
345
346         thumbMsg req_msg;
347         thumbMsg recv_msg;
348
349         memset((void *)&req_msg, 0, sizeof(thumbMsg));
350         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
351
352         /* Get PID of client*/
353         pid = getpid();
354         req_msg.pid = pid;
355
356         /* Set requset message */
357         req_msg.msg_type = msg_type;
358         req_msg.thumb_type = thumb_type;
359         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
360         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
361
362         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
363                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
364                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
365         }
366
367         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
368         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
369
370         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
371                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
372                 close(sock);
373                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
374         }
375
376         unsigned char *buf = NULL;
377         int buf_size = 0;
378         int header_size = 0;
379
380         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
381         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
382
383         if (send(sock, buf, buf_size, 0) != buf_size) {
384                 thumb_err("sendto failed: %d\n", errno);
385                 SAFE_FREE(buf);
386                 close(sock);
387                 return MEDIA_THUMB_ERROR_NETWORK;
388         }
389
390         thumb_dbg("Sending msg to thumbnail daemon is successful");
391
392         SAFE_FREE(buf);
393
394         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
395                 thumb_err("_media_thumb_recv_msg failed ");
396                 close(sock);
397                 return err;
398         }
399
400         recv_str_len = strlen(recv_msg.org_path);
401         thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
402
403         close(sock);
404
405         if (recv_str_len > max_length) {
406                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
407                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
408         }
409
410         if (recv_msg.status == THUMB_FAIL) {
411                 thumb_err("Failed to make thumbnail");
412                 return -1;
413         }
414
415         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
416                 strncpy(thumb_path, recv_msg.dst_path, max_length);
417         }
418
419         thumb_info->origin_width = recv_msg.origin_width;
420         thumb_info->origin_height = recv_msg.origin_height;
421
422         return 0;
423 }
424
425 int
426 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
427 {
428         int err = -1;
429         unsigned char *data = NULL;
430         int thumb_size = 0;
431         int thumb_w = 0;
432         int thumb_h = 0;
433         int origin_w = 0;
434         int origin_h = 0;
435         int max_length = 0;
436         char *thumb_path = NULL;
437         int need_update_db = 0;
438         int alpha = 0;
439
440         if (req_msg == NULL || res_msg == NULL) {
441                 thumb_err("Invalid msg!");
442                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
443         }
444
445         int msg_type = req_msg->msg_type;
446         media_thumb_type thumb_type = req_msg->thumb_type;
447         const char *origin_path = req_msg->org_path;
448
449         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
450
451         thumb_path = res_msg->dst_path;
452         thumb_path[0] = '\0';
453         max_length = sizeof(res_msg->dst_path);
454
455         err = _media_thumb_db_connect();
456         if (err < 0) {
457                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
458                 return MEDIA_THUMB_ERROR_DB;
459         }
460
461         if (msg_type == THUMB_REQUEST_DB_INSERT) {
462                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
463                 if (err == 0) {
464                         res_msg->origin_width = origin_w;
465                         res_msg->origin_height = origin_h;
466                         _media_thumb_db_disconnect();
467                         return MEDIA_THUMB_ERROR_NONE;
468                 } else {
469                         if (strlen(thumb_path) == 0) {
470                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
471                                 if (err < 0) {
472                                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
473                                         strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
474                                         _media_thumb_db_disconnect();
475                                         return err;
476                                 }
477
478                                 thumb_path[strlen(thumb_path)] = '\0';
479                         }
480                 }
481
482         } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
483                 strncpy(thumb_path, req_msg->dst_path, max_length);
484
485         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
486                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
487                 if (err < 0) {
488                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
489                         strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
490                         _media_thumb_db_disconnect();
491                         return err;
492                 }
493
494                 thumb_path[strlen(thumb_path)] = '\0';
495         }
496
497         thumb_dbg("Thumb path : %s", thumb_path);
498
499         if (g_file_test(thumb_path, 
500                                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
501                 thumb_warn("thumb path already exists in file system.. remove the existed file");
502                 _media_thumb_remove_file(thumb_path);
503         }
504
505         err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha);
506         if (err < 0) {
507                 thumb_err("_thumbnail_get_data failed - %d\n", err);
508                 SAFE_FREE(data);
509
510                 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
511                 _media_thumb_db_disconnect();
512                 return err;
513         }
514
515         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
516         //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
517         //thumb_dbg("Thumb : %s", thumb_path);
518
519         res_msg->msg_type = THUMB_RESPONSE;
520         res_msg->thumb_size = thumb_size;
521         res_msg->thumb_width = thumb_w;
522         res_msg->thumb_height = thumb_h;
523         res_msg->origin_width = origin_w;
524         res_msg->origin_height = origin_h;
525
526         /* If the image is transparent PNG format, make png file as thumbnail of this image */
527         if (alpha) {
528                 char file_ext[10];
529                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
530                 if (strncasecmp(file_ext, "png", 3) == 0) {
531                         int len = strlen(thumb_path);
532                         thumb_path[len - 3] = 'p';
533                         thumb_path[len - 2] = 'n';
534                         thumb_path[len - 1] = 'g';
535                 }
536                 thumb_dbg("Thumb path is changed : %s", thumb_path);
537         }
538
539         err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
540         if (err < 0) {
541                 thumb_err("save_to_file_with_evas failed - %d\n", err);
542                 SAFE_FREE(data);
543
544                 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
545                         strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
546
547                 _media_thumb_db_disconnect();
548                 return err;
549         } else {
550                 thumb_dbg("file save success\n");
551         }
552
553         /* fsync */
554         int fd = 0;
555         fd = open(thumb_path, O_WRONLY);
556         if (fd < 0) {
557                 thumb_warn("open failed");
558         } else {
559                 err = fsync(fd);
560                 if (err == -1) {
561                         thumb_warn("fsync failed");
562                 }
563
564                 close(fd);
565         }
566         /* End of fsync */
567
568         SAFE_FREE(data);
569
570         /* DB update if needed */
571         if (need_update_db == 1) {
572                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height);
573                 if (err < 0) {
574                         thumb_err("_media_thumb_update_db failed : %d", err);
575                 }
576         }
577
578         _media_thumb_db_disconnect();
579
580         return 0;
581 }
582
583 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
584 {
585         thumbMsg recv_msg;
586         int header_size = 0;
587         int recv_str_len = 0;
588         int sock = 0;
589         int err = MEDIA_THUMB_ERROR_NONE;
590
591         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
592         sock = g_io_channel_unix_get_fd(src);
593
594         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
595
596         thumb_err("_media_thumb_write_socket socket : %d", sock);
597
598         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
599                 thumb_err("_media_thumb_recv_msg failed ");
600                 g_io_channel_shutdown(src, TRUE, NULL);
601                 return FALSE;
602         }
603
604         recv_str_len = strlen(recv_msg.dst_path);
605         thumb_dbg("recv %s(%d) in  [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
606
607         g_io_channel_shutdown(src, TRUE, NULL);
608
609         //thumb_dbg("Completed..%s", recv_msg.org_path);
610         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
611
612         if (recv_msg.status == THUMB_FAIL) {
613                 thumb_err("Failed to make thumbnail");
614                 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
615                 goto callback;
616         }
617
618 callback:
619         if (data) {
620                 thumbUserData* cb = (thumbUserData*)data;
621                 cb->func(err, recv_msg.dst_path, cb->user_data);
622                 free(cb);
623                 cb = NULL;
624         }
625
626         thumb_dbg("Done");
627         return FALSE;
628 }
629
630 int
631 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData)
632 {
633         int sock;
634         const char *serv_ip = "127.0.0.1";
635         struct sockaddr_in serv_addr;
636
637         int pid;
638
639         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
640                 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
641         }
642
643 #ifdef _USE_MEDIA_UTIL_
644         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
645                 thumb_err("ms_ipc_create_client_socket failed");
646                 return MEDIA_THUMB_ERROR_NETWORK;
647         }
648 #else
649         /* Creaete a TCP socket */
650         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
651                 thumb_err("_media_thumb_create_socket failed");
652                 return MEDIA_THUMB_ERROR_NETWORK;
653         }
654 #endif
655
656         GIOChannel *channel = NULL;
657         channel = g_io_channel_unix_new(sock);
658         int source_id = -1;
659
660
661         memset(&serv_addr, 0, sizeof(serv_addr));
662         serv_addr.sin_family = AF_INET;
663         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
664 #ifdef _USE_MEDIA_UTIL_
665         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
666 #else
667         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
668 #endif
669
670         /* Connecting to the thumbnail server */
671         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
672                 thumb_err("connect error : %s", strerror(errno));
673                 g_io_channel_shutdown(channel, TRUE, NULL);
674                 return MEDIA_THUMB_ERROR_NETWORK;
675         }
676
677         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
678                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
679
680                 /* Create new channel to watch udp socket */
681                 GSource *source = NULL;
682                 source = g_io_create_watch(channel, G_IO_IN);
683
684                 /* Set callback to be called when socket is readable */
685                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
686                 source_id = g_source_attach(source, g_main_context_get_thread_default());
687         }
688
689         thumbMsg req_msg;
690         memset((void *)&req_msg, 0, sizeof(thumbMsg));
691
692         pid = getpid();
693         req_msg.pid = pid;
694         req_msg.msg_type = msg_type;
695         req_msg.thumb_type = thumb_type;
696         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
697         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
698
699         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
700         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
701
702         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
703                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
704                 g_io_channel_shutdown(channel, TRUE, NULL);
705                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
706         }
707
708         unsigned char *buf = NULL;
709         int buf_size = 0;
710         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
711
712         //thumb_dbg("buffer size : %d", buf_size);
713
714         if (send(sock, buf, buf_size, 0) != buf_size) {
715                 thumb_err("sendto failed: %d\n", errno);
716                 SAFE_FREE(buf);
717                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
718                 g_io_channel_shutdown(channel, TRUE, NULL);
719                 return MEDIA_THUMB_ERROR_NETWORK;
720         }
721
722         SAFE_FREE(buf);
723         thumb_dbg("Sending msg to thumbnail daemon is successful");
724
725 #if 0
726         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
727                 g_io_channel_shutdown(channel, TRUE, NULL);
728         }
729 #else
730         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
731                 g_io_channel_shutdown(channel, TRUE, NULL);
732
733                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
734                 __media_thumb_pop_req_queue(origin_path, TRUE);
735         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
736                 if (g_request_queue == NULL) {
737                         g_request_queue = g_queue_new();
738                 }
739
740                 thumbReq *thumb_req = NULL;
741                 thumb_req = calloc(1, sizeof(thumbReq));
742                 if (thumb_req == NULL) {
743                         thumb_err("Failed to create request element");
744                         return 0;
745                 }
746
747                 thumb_req->channel = channel;
748                 thumb_req->path = strdup(origin_path);
749                 thumb_req->source_id = source_id;
750
751                 //thumb_dbg("Push : %s [%d]", origin_path, sock);
752                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
753         }
754 #endif
755
756         return 0;
757 }
758