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