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