Sync to I29fc45cc5bd693eb796d4ea256c0b23afe39b192
[platform/framework/web/livebox-viewer.git] / src / client.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include <dlog.h>
24 #include <glib.h>
25
26 #include <packet.h>
27 #include <com-core.h>
28 #include <com-core_packet.h>
29 #include <livebox-errno.h>
30
31 #include "debug.h"
32 #include "client.h"
33 #include "livebox.h"
34 #include "livebox_internal.h"
35 #include "desc_parser.h"
36 #include "fb.h"
37 #include "util.h"
38 #include "master_rpc.h"
39 #include "conf.h"
40 #include "critical_log.h"
41
42 static inline void make_connection(void);
43
44 static struct info {
45         int fd;
46         guint reconnector;
47 } s_info = {
48         .fd = -1,
49         .reconnector = 0,
50 };
51
52 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
53 {
54         const char *pkgname;
55         const char *id;
56         const char *function;
57
58         if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
59                 ErrPrint("Invalid arguments\n");
60                 return NULL;
61         }
62
63         master_rpc_clear_fault_package(pkgname);
64         lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
65         DbgPrint("%s(%s) is deactivated\n", pkgname, id);
66         return NULL;
67 }
68
69 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
70 {
71         struct livebox *handler;
72         const char *pkgname;
73         const char *id;
74         int seize;
75         int ret;
76
77         ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
78         if (ret != 3) {
79                 ErrPrint("Invalid argument\n");
80                 goto out;
81         }
82
83         handler = lb_find_livebox(pkgname, id);
84         if (!handler) {
85                 ErrPrint("Instance(%s) is not exists\n", id);
86                 goto out;
87         }
88
89         lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
90
91 out:
92         return NULL;
93 }
94
95 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
96 {
97         const char *pkgname;
98         const char *id;
99         const char *content;
100         struct livebox *handler;
101         char *new_content;
102         int ret;
103         int pinup;
104
105         ret = packet_get(packet, "iisss", &ret, &pinup, &pkgname, &id, &content);
106         if (ret != 5) {
107                 ErrPrint("Invalid argument\n");
108                 goto out;
109         }
110
111         handler = lb_find_livebox(pkgname, id);
112         if (!handler) {
113                 ErrPrint("Instance (%s) is not exists\n", id);
114                 goto out;
115         }
116
117         if (ret == 0) {
118                 new_content = strdup(content);
119                 if (new_content) {
120                         free(handler->content);
121                         handler->content = new_content;
122                         handler->is_pinned_up = pinup;
123                 } else {
124                         ErrPrint("Heap: %s\n", strerror(errno));
125                         ret = LB_STATUS_ERROR_MEMORY;
126                 }
127         }
128
129         if (handler->pinup_cb) {
130                 handler->pinup_cb(handler, ret, handler->pinup_cbdata);
131
132                 handler->pinup_cb = NULL; /*!< Reset pinup cb */
133                 handler->pinup_cbdata = NULL;
134         } else if (ret == 0) {
135                 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
136         }
137
138 out:
139         return NULL;
140 }
141
142 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
143 {
144         const char *pkgname;
145         const char *id;
146         double timestamp;
147         struct livebox *handler;
148
149         if (packet_get(packet, "ssd", &pkgname, &id, &timestamp) != 3) {
150                 ErrPrint("Invalid arguemnt\n");
151                 goto out;
152         }
153
154         handler = lb_find_livebox_by_timestamp(timestamp);
155         if (!handler) {
156                 /*!
157                  * \note
158                  * This can be happens only if the user delete a livebox
159                  * right after create it before receive created event.
160                  */
161                 goto out;
162         }
163
164         /*!< Check validity of this "handler" */
165         if (handler->state != CREATE) {
166                 if (handler->state != DELETE) {
167                         /*!
168                          * \note
169                          * This is not possible
170                          */
171                         CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
172                         return NULL;
173                 }
174         }
175
176         if (handler->created_cb) {
177                 /*!
178                  * \note
179                  *
180                  * "if (handler->id == NULL)"
181                  *
182                  * The instance is not created yet.
183                  * But the master forcely destroy it and send destroyed event to this
184                  * without the created event.
185                  *
186                  * It could be destroyed when a slave has critical error(fault)
187                  * before creating an instance successfully.
188                  */
189                 if (handler->created_cb == handler->deleted_cb) {
190                         if (handler->created_cbdata != handler->deleted_cbdata)
191                                 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
192
193                         handler->deleted_cb = NULL;
194                         handler->deleted_cbdata = NULL;
195                 }
196
197                 DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n");
198                 handler->created_cb(handler, LB_STATUS_ERROR_CANCEL, handler->created_cbdata);
199                 handler->created_cb = NULL;
200                 handler->created_cbdata = NULL;
201         } else if (handler->id) {
202                 if (handler->deleted_cb) {
203                         DbgPrint("Call the deleted cb\n");
204                         handler->deleted_cb(handler, LB_STATUS_SUCCESS, handler->deleted_cbdata);
205
206                         handler->deleted_cb = NULL;
207                         handler->deleted_cbdata = NULL;
208                 } else {
209                         DbgPrint("Call the lb,deleted\n");
210                         lb_invoke_event_handler(handler, LB_EVENT_DELETED);
211                 }
212         }
213
214         DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
215
216         /* Just try to delete it, if a user didn't remove it from the live box list */
217         lb_unref(handler);
218
219 out:
220         return NULL;
221 }
222
223 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
224 {
225         const char *pkgname;
226         const char *id;
227         const char *fbfile;
228         const char *content;
229         const char *title;
230         struct livebox *handler;
231         int lb_w;
232         int lb_h;
233         double priority;
234         int ret;
235
236         ret = packet_get(packet, "sssiidss",
237                                 &pkgname, &id,
238                                 &fbfile, &lb_w, &lb_h,
239                                 &priority, &content, &title);
240         if (ret != 8) {
241                 ErrPrint("Invalid argument\n");
242                 goto out;
243         }
244
245         handler = lb_find_livebox(pkgname, id);
246         if (!handler) {
247                 ErrPrint("instance(%s) is not exists\n", id);
248                 goto out;
249         }
250
251         if (handler->state != CREATE) {
252                 /*!
253                  * \note
254                  * Already deleted by the user.
255                  * Don't try to notice anything with this, Just ignore all events
256                  * Beacuse the user doesn't wants know about this anymore
257                  */
258                 DbgPrint("(%s) is not exists, but updated\n", id);
259                 goto out;
260         }
261
262         lb_set_priority(handler, priority);
263         lb_set_content(handler, content);
264         lb_set_title(handler, title);
265         lb_set_size(handler, lb_w, lb_h);
266
267         if (lb_text_lb(handler)) {
268                 (void)parse_desc(handler, livebox_filename(handler), 0);
269                 /*!
270                  * \note
271                  * DESC parser will call the "text event callback".
272                  * Don't need to call global event callback in this case.
273                  */
274                 goto out;
275         } else if (lb_get_lb_fb(handler)) {
276                 lb_set_lb_fb(handler, fbfile);
277                 ret = fb_sync(lb_get_lb_fb(handler));
278                 if (ret < 0)
279                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
280         } else {
281                 ret = 0;
282         }
283
284         if (ret == 0)
285                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
286
287 out:
288         return NULL;
289 }
290
291 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
292 {
293         struct livebox *handler;
294         const char *pkgname;
295         const char *id;
296         const char *buf_id;
297         int width;
298         int height;
299         int ret;
300         int status;
301
302         ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
303         if (ret != 6) {
304                 ErrPrint("Invalid argument\n");
305                 goto out;
306         }
307
308         handler = lb_find_livebox(pkgname, id);
309         if (!handler) {
310                 ErrPrint("Instance(%s) is not exists\n", id);
311                 goto out;
312         }
313
314         if (handler->state != CREATE) {
315                 ErrPrint("Instance(%s) is not created\n", id);
316                 goto out;
317         }
318
319         lb_set_pdsize(handler, width, height);
320         if (lb_text_pd(handler)) {
321                 DbgPrint("Text TYPE does not need to handle this\n");
322         } else {
323                 (void)lb_set_pd_fb(handler, buf_id);
324                 ret = fb_sync(lb_get_pd_fb(handler));
325                 if (ret < 0)
326                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
327         }
328
329         handler->is_pd_created = (status == 0);
330
331         if (handler->pd_created_cb) {
332                 DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
333                 handler->pd_created_cb(handler, status, handler->pd_created_cbdata);
334
335                 handler->pd_created_cb = NULL;
336                 handler->pd_created_cbdata = NULL;
337         } else if (status == 0) {
338                 DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
339                 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
340         }
341
342 out:
343         return NULL;
344 }
345
346 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
347 {
348         struct livebox *handler;
349         const char *pkgname;
350         const char *id;
351         int ret;
352         int status;
353
354         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
355         if (ret != 3) {
356                 ErrPrint("Invalid argument\n");
357                 goto out;
358         }
359
360         handler = lb_find_livebox(pkgname, id);
361         if (!handler) {
362                 ErrPrint("Instance(%s) is not exists\n", id);
363                 goto out;
364         }
365
366         if (handler->state != CREATE) {
367                 ErrPrint("Instance(%s) is not created\n", id);
368                 goto out;
369         }
370
371         handler->is_pd_created = 0;
372
373         if (handler->pd_destroyed_cb) {
374                 DbgPrint("Invoke the PD Destroyed CB\n");
375                 handler->pd_destroyed_cb(handler, status, handler->pd_destroyed_cbdata);
376
377                 handler->pd_destroyed_cb = NULL;
378                 handler->pd_destroyed_cbdata = NULL;
379         } else if (status == 0) {
380                 DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n");
381                 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
382         }
383
384 out:
385         return NULL;
386 }
387
388 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
389 {
390         const char *pkgname;
391         const char *id;
392         const char *descfile;
393         const char *fbfile;
394         int ret;
395         struct livebox *handler;
396         int pd_w;
397         int pd_h;
398
399         ret = packet_get(packet, "ssssii",
400                                 &pkgname, &id,
401                                 &descfile, &fbfile,
402                                 &pd_w, &pd_h);
403         if (ret != 6) {
404                 ErrPrint("Invalid argument\n");
405                 goto out;
406         }
407
408         handler = lb_find_livebox(pkgname, id);
409         if (!handler) {
410                 ErrPrint("Instance(%s) is not exists\n", id);
411                 goto out;
412         }
413
414         if (handler->state != CREATE) {
415                 /*!
416                  * \note
417                  * This handler is already deleted by the user.
418                  * So don't try to notice anything about this anymore.
419                  * Just ignore all events.
420                  */
421                 ErrPrint("Instance(%s) is not created\n", id);
422                 goto out;
423         }
424
425         lb_set_pdsize(handler, pd_w, pd_h);
426
427         if (lb_text_pd(handler)) {
428                 (void)parse_desc(handler, descfile, 1);
429         } else {
430                 (void)lb_set_pd_fb(handler, fbfile);
431
432                 ret = fb_sync(lb_get_pd_fb(handler));
433                 if (ret < 0)
434                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
435                 else
436                         lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
437         }
438
439 out:
440         return NULL;
441 }
442
443 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
444 {
445         struct livebox *handler;
446         const char *pkgname;
447         const char *id;
448         const char *fbfile;
449         int status;
450         int ret;
451         int w;
452         int h;
453         int is_pd;
454
455         if (!packet) {
456                 ErrPrint("Invalid packet\n");
457                 goto out;
458         }
459
460         ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
461         if (ret != 7) {
462                 ErrPrint("Invalid argument\n");
463                 goto out;
464         }
465
466         DbgPrint("Size is changed: %dx%d (%s), fb: [%s]\n", w, h, id, fbfile);
467
468         handler = lb_find_livebox(pkgname, id);
469         if (!handler) {
470                 ErrPrint("Livebox(%s) is not found\n", id);
471                 goto out;
472         }
473
474         if (handler->state != CREATE) {
475                 ErrPrint("Livebox(%s) is not created yet\n", id);
476                 goto out;
477         }
478
479         if (is_pd) {
480                 DbgPrint("PD is resized\n");
481                 /*!
482                  * \NOTE
483                  * PD is not able to resized by the client.
484                  * PD is only can be managed by the provider.
485                  * So the PD has no private resized event handler.
486                  * Notify it via global event handler only.
487                  */
488                 if (status == 0) {
489                         lb_set_pdsize(handler, w, h);
490                         lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
491                 } else {
492                         ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
493                 }
494         } else {
495                 if (status == 0) {
496                         DbgPrint("LB is successfully resized (%dx%d)\n", w, h);
497                         lb_set_size(handler, w, h);
498
499                         /*!
500                          * \NOTE
501                          * If there is a created LB FB, 
502                          * Update it too.
503                          */
504                         if (lb_get_lb_fb(handler)) {
505                                 lb_set_lb_fb(handler, fbfile);
506
507                                 ret = fb_sync(lb_get_lb_fb(handler));
508                                 if (ret < 0)
509                                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
510
511                                 /* Just update the size info only. */
512                         }
513
514                         /*!
515                          * \NOTE
516                          * I cannot believe client.
517                          * So I added some log before & after call the user callback.
518                          */
519                         if (handler->size_changed_cb) {
520                                 handler->size_changed_cb(handler, status, handler->size_cbdata);
521
522                                 handler->size_changed_cb = NULL;
523                                 handler->size_cbdata = NULL;
524                         } else {
525                                 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
526                         }
527                 } else {
528                         DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status);
529                         if (handler->size_changed_cb) {
530                                 handler->size_changed_cb(handler, status, handler->size_cbdata);
531
532                                 handler->size_changed_cb = NULL;
533                                 handler->size_cbdata = NULL;
534                         }
535                 }
536         }
537
538 out:
539         return NULL;
540 }
541
542 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
543 {
544         struct livebox *handler;
545         const char *pkgname;
546         const char *id;
547         int ret;
548         double period;
549         int status;
550
551         ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
552         if (ret != 4) {
553                 ErrPrint("Invalid argument\n");
554                 goto out;
555         }
556
557         handler = lb_find_livebox(pkgname, id);
558         if (!handler) {
559                 ErrPrint("Livebox(%s) is not found\n", id);
560                 goto out;
561         }
562
563         if (handler->state != CREATE) {
564                 ErrPrint("Livebox(%s) is not created\n", id);
565                 goto out;
566         }
567
568         DbgPrint("Update period is changed? %lf (%d)\n", period, status);
569         if (status == 0)
570                 lb_set_period(handler, period);
571
572         if (handler->period_changed_cb) {
573                 handler->period_changed_cb(handler, status, handler->group_cbdata);
574
575                 handler->period_changed_cb = NULL;
576                 handler->period_cbdata = NULL;
577         } else if (status == 0) {
578                 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
579         }
580
581 out:
582         return NULL;
583 }
584
585 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
586 {
587         struct livebox *handler;
588         const char *pkgname;
589         const char *id;
590         int ret;
591         const char *cluster;
592         const char *category;
593         int status;
594
595         ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
596         if (ret != 5) {
597                 ErrPrint("Invalid argument\n");
598                 goto out;
599         }
600
601         handler = lb_find_livebox(pkgname, id);
602         if (!handler) {
603                 ErrPrint("Livebox(%s) is not exists\n", id);
604                 goto out;
605         }
606
607         if (handler->state != CREATE) {
608                 /*!
609                  * \note
610                  * Do no access this handler,
611                  * You cannot believe this handler anymore.
612                  */
613                 ErrPrint("Livebox(%s) is not created\n", id);
614                 goto out;
615         }
616
617         DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status);
618         if (status == 0)
619                 (void)lb_set_group(handler, cluster, category);
620
621         if (handler->group_changed_cb) {
622                 handler->group_changed_cb(handler, status, handler->group_cbdata);
623
624                 handler->group_changed_cb = NULL;
625                 handler->group_cbdata = NULL;
626         } else if (status == 0) {
627                 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
628         }
629
630 out:
631         return NULL;
632 }
633
634 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
635 {
636         struct livebox *handler;
637
638         int lb_w;
639         int lb_h;
640         int pd_w;
641         int pd_h;
642         const char *pkgname;
643         const char *id;
644
645         const char *content;
646         const char *cluster;
647         const char *category;
648         const char *lb_fname;
649         const char *pd_fname;
650         const char *title;
651
652         double timestamp;
653         const char *auto_launch;
654         double priority;
655         int size_list;
656         int user;
657         int pinup_supported;
658         enum lb_type lb_type;
659         enum pd_type pd_type;
660         double period;
661         int is_pinned_up;
662
663         int old_state = DESTROYED;
664
665         int ret;
666
667         ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
668                         &timestamp,
669                         &pkgname, &id, &content,
670                         &lb_w, &lb_h, &pd_w, &pd_h,
671                         &cluster, &category, &lb_fname, &pd_fname,
672                         &auto_launch, &priority, &size_list, &user, &pinup_supported,
673                         &lb_type, &pd_type, &period, &title, &is_pinned_up);
674         if (ret != 22) {
675                 ErrPrint("Invalid argument\n");
676                 ret = LB_STATUS_ERROR_INVALID;
677                 goto out;
678         }
679
680         ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
681                 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
682                 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
683                 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
684                 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
685                 timestamp, pkgname, id, content,
686                 pd_w, pd_h, lb_w, lb_h,
687                 cluster, category, lb_fname, pd_fname,
688                 auto_launch, priority, size_list, user, pinup_supported,
689                 lb_type, pd_type, period, title, is_pinned_up);
690
691         handler = lb_find_livebox_by_timestamp(timestamp);
692         if (!handler) {
693                 handler = lb_new_livebox(pkgname, id, timestamp);
694                 if (!handler) {
695                         ErrPrint("Failed to create a new livebox\n");
696                         ret = LB_STATUS_ERROR_FAULT;
697                         goto out;
698                 }
699
700                 old_state = handler->state;
701         } else {
702                 if (handler->state != CREATE) {
703                         if (handler->state != DELETE) {
704                                 /*!
705                                  * \note
706                                  * This is not possible!!!
707                                  */
708                                 ErrPrint("Invalid handler\n");
709                                 ret = LB_STATUS_ERROR_INVALID;
710                                 goto out;
711                         }
712
713                         /*!
714                          * \note
715                          * After get the delete states,
716                          * call the create callback with deleted result.
717                          */
718                 }
719
720                 old_state = handler->state;
721
722                 if (handler->id) {
723                         ErrPrint("Already created: timestamp[%lf] "
724                                 "pkgname[%s], id[%s] content[%s] "
725                                 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
726                                         timestamp, pkgname, id,
727                                         content, cluster, category,
728                                         lb_fname, pd_fname);
729
730                         ret = LB_STATUS_ERROR_ALREADY;
731                         goto out;
732                 }
733
734                 lb_set_id(handler, id);
735         }
736
737         lb_set_size(handler, lb_w, lb_h);
738         handler->lb.type = lb_type;
739         handler->is_pinned_up = is_pinned_up;
740
741         switch (lb_type) {
742         case _LB_TYPE_FILE:
743                 break;
744         case _LB_TYPE_SCRIPT:
745         case _LB_TYPE_BUFFER:
746                 if (!strlen(lb_fname))
747                         break;
748                 lb_set_lb_fb(handler, lb_fname);
749                 ret = fb_sync(lb_get_lb_fb(handler));
750                 if (ret < 0)
751                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
752                 break;
753         case _LB_TYPE_TEXT:
754                 lb_set_text_lb(handler);
755                 break;
756         default:
757                 break;
758         }
759
760         handler->pd.type = pd_type;
761         lb_set_pdsize(handler, pd_w, pd_h);
762         lb_set_default_pdsize(handler, pd_w, pd_h);
763         switch (pd_type) {
764         case _PD_TYPE_SCRIPT:
765         case _PD_TYPE_BUFFER:
766                 if (!strlen(pd_fname))
767                         break;
768
769                 lb_set_pd_fb(handler, pd_fname);
770                 ret = fb_sync(lb_get_pd_fb(handler));
771                 if (ret < 0)
772                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
773                 break;
774         case _PD_TYPE_TEXT:
775                 lb_set_text_pd(handler);
776                 break;
777         default:
778                 break;
779         }
780
781         lb_set_priority(handler, priority);
782
783         lb_set_size_list(handler, size_list);
784         lb_set_group(handler, cluster, category);
785
786         lb_set_content(handler, content);
787         lb_set_title(handler, title);
788
789         lb_set_user(handler, user);
790
791         lb_set_auto_launch(handler, auto_launch);
792         lb_set_pinup(handler, pinup_supported);
793
794         lb_set_period(handler, period);
795
796         ret = 0;
797
798         if (handler->state == CREATE) {
799                 /*!
800                  * \note
801                  * These callback can change the handler->state.
802                  * So we have to use the "old_state" which stored state before call these callbacks
803                  */
804
805                 if (handler->created_cb) {
806                         DbgPrint("Invoke the created_cb\n");
807                         handler->created_cb(handler, ret, handler->created_cbdata);
808
809                         handler->created_cb = NULL;
810                         handler->created_cbdata = NULL;
811                 } else {
812                         DbgPrint("Invoke the lb,created\n");
813                         lb_invoke_event_handler(handler, LB_EVENT_CREATED);
814                 }
815         }
816
817 out:
818         if (ret == 0 && old_state == DELETE) {
819                 DbgPrint("Send the delete request\n");
820                 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
821
822                 /*!
823                  * \note
824                  * handler->created_cb = NULL;
825                  * handler->created_cbdata = NULL;
826                  *
827                  * Do not clear this to use this from the deleted event callback.
828                  * if this value is not cleared when the deleted event callback check it,
829                  * it means that the created function is not called yet.
830                  * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
831                  */
832         }
833
834         return NULL;
835 }
836
837 static struct method s_table[] = {
838         {
839                 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
840                 .handler = master_lb_updated,
841         },
842         {
843                 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
844                 .handler = master_pd_updated,
845         },
846         {
847                 .cmd = "pd_created",
848                 .handler = master_pd_created,
849         },
850         {
851                 .cmd = "pd_destroyed",
852                 .handler = master_pd_destroyed,
853         },
854         {
855                 .cmd = "fault_package", /* pkgname, id, function, ret */
856                 .handler = master_fault_package,
857         },
858         {
859                 .cmd = "deleted", /* pkgname, id, timestamp, ret */
860                 .handler = master_deleted,
861         },
862         {
863                 .cmd = "created", /* timestamp, pkgname, id, content, lb_w, lb_h, pd_w, pd_h, cluster, category, lb_file, pd_file, auto_launch, priority, size_list, is_user, pinup_supported, text_lb, text_pd, period, ret */
864                 .handler = master_created,
865         },
866         {
867                 .cmd = "group_changed",
868                 .handler = master_group_changed,
869         },
870         {
871                 .cmd = "period_changed",
872                 .handler = master_period_changed,
873         },
874         {
875                 .cmd = "size_changed",
876                 .handler = master_size_changed,
877         },
878         {
879                 .cmd = "pinup",
880                 .handler = master_pinup,
881         },
882         {
883                 .cmd = "scroll",
884                 .handler = master_hold_scroll,
885         },
886         {
887                 .cmd = NULL,
888                 .handler = NULL,
889         },
890 };
891
892 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
893 {
894         if (!result) {
895                 DbgPrint("Result packet is not valid\n");
896         } else {
897                 int ret;
898
899                 if (packet_get(result, "i", &ret) != 1)
900                         ErrPrint("Invalid argument\n");
901                 else
902                         DbgPrint("Acquire returns: %d\n", ret);
903         }
904
905         return;
906 }
907
908 static gboolean connector_cb(gpointer user_data)
909 {
910         s_info.reconnector = 0;
911
912         if (s_info.fd > 0) {
913                 DbgPrint("Connection is already made\n");
914                 return FALSE;
915         }
916
917         make_connection();
918         return FALSE;
919 }
920
921 static inline void make_connection(void)
922 {
923         struct packet *packet;
924         int ret;
925
926         DbgPrint("Let's making connection!\n");
927
928         s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
929         if (s_info.fd < 0) {
930                 /*!< After 10 secs later, try to connect again */
931                 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
932                 if (s_info.reconnector == 0)
933                         ErrPrint("Failed to fire the reconnector\n");
934
935                 ErrPrint("Try this again A sec later\n");
936                 return;
937         }
938
939         packet = packet_create("acquire", "d", util_timestamp());
940         if (!packet) {
941                 com_core_packet_client_fini(s_info.fd);
942                 s_info.fd = -1;
943                 return;
944         }
945
946         ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
947         if (ret < 0) {
948                 ErrPrint("Master RPC returns %d\n", ret);
949                 com_core_packet_client_fini(s_info.fd);
950                 s_info.fd = -1;
951         }
952
953 }
954
955 static int connected_cb(int handle, void *data)
956 {
957         master_rpc_check_and_fire_consumer();
958         return 0;
959 }
960
961 static int disconnected_cb(int handle, void *data)
962 {
963         if (s_info.fd != handle) {
964                 /*!< This handle is not my favor */
965                 return 0;
966         }
967
968         s_info.fd = -1; /*!< Disconnected */
969
970         if (s_info.reconnector > 0) {
971                 DbgPrint("Reconnector already fired\n");
972                 return 0;
973         }
974
975         /*!< After 10 secs later, try to connect again */
976         s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
977         if (s_info.reconnector == 0) {
978                 ErrPrint("Failed to fire the reconnector\n");
979                 make_connection();
980         }
981
982         master_rpc_clear_all_request();
983         lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
984
985         lb_delete_all();
986         return 0;
987 }
988
989 int client_init(void)
990 {
991         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
992         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
993         make_connection();
994         return 0;
995 }
996
997 int client_fd(void)
998 {
999         return s_info.fd;
1000 }
1001
1002 const char *client_addr(void)
1003 {
1004         return CLIENT_SOCKET;
1005 }
1006
1007 int client_fini(void)
1008 {
1009         com_core_packet_client_fini(s_info.fd);
1010         com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1011         com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1012         s_info.fd = -1;
1013         return 0;
1014 }
1015
1016 /* End of a file */