e84c67b21293500e8190276546cc5096ebadec45
[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, util_uri_to_path(id), 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_period_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 ret;
437         double period;
438         int status;
439
440         ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
441         if (ret != 4) {
442                 ErrPrint("Invalid argument\n");
443                 ret = -EINVAL;
444                 goto out;
445         }
446
447         handler = lb_find_livebox(pkgname, id);
448         if (!handler) {
449                 ErrPrint("Livebox(%s - %s) is not found\n", pkgname, id);
450                 ret = -ENOENT;
451                 goto out;
452         }
453
454         if (handler->state != CREATE) {
455                 ret = -EPERM;
456                 goto out;
457         }
458
459         if (status == 0)
460                 lb_set_period(handler, period);
461
462         if (handler->period_changed_cb) {
463                 handler->period_changed_cb(handler, status, handler->group_cbdata);
464
465                 handler->period_changed_cb = NULL;
466                 handler->period_cbdata = NULL;
467         } else {
468                 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
469         }
470
471         ret = 0;
472
473 out:
474         return NULL;
475 }
476
477 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
478 {
479         struct livebox *handler;
480         const char *pkgname;
481         const char *id;
482         int ret;
483         const char *cluster;
484         const char *category;
485         int status;
486
487         ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
488         if (ret != 5) {
489                 ErrPrint("Invalid argument\n");
490                 ret = -EINVAL;
491                 goto out;
492         }
493
494         handler = lb_find_livebox(pkgname, id);
495         if (!handler) {
496                 ret = -ENOENT;
497                 goto out;
498         }
499
500         if (handler->state != CREATE) {
501                 /*!
502                  * \note
503                  * Do no access this handler,
504                  * You cannot believe this handler anymore.
505                  */
506                 ret = -EPERM;
507                 goto out;
508         }
509
510         if (status == 0)
511                 lb_set_group(handler, cluster, category);
512
513         if (handler->group_changed_cb) {
514                 handler->group_changed_cb(handler, status, handler->group_cbdata);
515
516                 handler->group_changed_cb = NULL;
517                 handler->group_cbdata = NULL;
518         } else {
519                 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
520         }
521
522         ret = 0;
523
524 out:
525         return NULL;
526 }
527
528 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
529 {
530         struct livebox *handler;
531
532         int lb_w;
533         int lb_h;
534         int pd_w;
535         int pd_h;
536         const char *pkgname;
537         const char *id;
538
539         const char *content;
540         const char *cluster;
541         const char *category;
542         const char *lb_fname;
543         const char *pd_fname;
544         const char *title;
545
546         double timestamp;
547         int auto_launch;
548         double priority;
549         int size_list;
550         int user;
551         int pinup_supported;
552         enum lb_type lb_type;
553         enum pd_type pd_type;
554         double period;
555         int is_pinned_up;
556
557         int old_state = DESTROYED;
558
559         int ret;
560
561         ret = packet_get(packet, "dsssiiiissssidiiiiidsi",
562                         &timestamp,
563                         &pkgname, &id, &content,
564                         &lb_w, &lb_h, &pd_w, &pd_h,
565                         &cluster, &category, &lb_fname, &pd_fname,
566                         &auto_launch, &priority, &size_list, &user, &pinup_supported,
567                         &lb_type, &pd_type, &period, &title, &is_pinned_up);
568         if (ret != 22) {
569                 ErrPrint("Invalid argument\n");
570                 ret = -EINVAL;
571                 goto out;
572         }
573
574         ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
575                 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
576                 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
577                 "auto_launch: %d, priority: %lf, size_list: %d, user: %d, pinup: %d, "
578                 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
579                 timestamp, pkgname, id, content,
580                 pd_w, pd_h, lb_w, lb_h,
581                 cluster, category, lb_fname, pd_fname,
582                 auto_launch, priority, size_list, user, pinup_supported,
583                 lb_type, pd_type, period, title, is_pinned_up);
584
585         handler = lb_find_livebox_by_timestamp(timestamp);
586         if (!handler) {
587                 handler = lb_new_livebox(pkgname, id, timestamp);
588                 if (!handler) {
589                         ErrPrint("Failed to create a new livebox\n");
590                         ret = -EFAULT;
591                         goto out;
592                 }
593
594                 old_state = handler->state;
595         } else {
596                 if (handler->state != CREATE) {
597                         if (handler->state != DELETE) {
598                                 /*!
599                                  * \note
600                                  * This is not possible!!!
601                                  */
602                                 ErrPrint("Invalid handler\n");
603                                 ret = -EINVAL;
604                                 goto out;
605                         }
606
607                         /*!
608                          * \note
609                          * After get the delete states,
610                          * call the create callback with deleted result.
611                          */
612                 }
613
614                 old_state = handler->state;
615
616                 if (handler->id) {
617                         ErrPrint("Already created: timestamp[%lf] "
618                                 "pkgname[%s], id[%s] content[%s] "
619                                 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
620                                         timestamp, pkgname, id,
621                                         content, cluster, category,
622                                         lb_fname, pd_fname);
623
624                         ret = -EALREADY;
625                         goto out;
626                 }
627
628                 lb_set_id(handler, id);
629         }
630
631         lb_set_size(handler, lb_w, lb_h);
632         handler->lb.type = lb_type;
633         handler->is_pinned_up = is_pinned_up;
634
635         switch (lb_type) {
636         case _LB_TYPE_FILE:
637                 break;
638         case _LB_TYPE_SCRIPT:
639         case _LB_TYPE_BUFFER:
640                 if (!strlen(lb_fname))
641                         break;
642                 lb_set_lb_fb(handler, lb_fname);
643                 ret = fb_sync(lb_get_lb_fb(handler));
644                 if (ret < 0)
645                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
646                 break;
647         case _LB_TYPE_TEXT:
648                 lb_set_text_lb(handler);
649                 break;
650         default:
651                 break;
652         }
653
654         handler->pd.type = pd_type;
655         lb_set_pdsize(handler, pd_w, pd_h);
656         switch (pd_type) {
657         case _PD_TYPE_SCRIPT:
658         case _PD_TYPE_BUFFER:
659                 if (!strlen(pd_fname))
660                         break;
661
662                 lb_set_pd_fb(handler, pd_fname);
663                 ret = fb_sync(lb_get_pd_fb(handler));
664                 if (ret < 0)
665                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
666                 break;
667         case _PD_TYPE_TEXT:
668                 lb_set_text_pd(handler);
669                 break;
670         default:
671                 break;
672         }
673
674         lb_set_priority(handler, priority);
675
676         lb_set_size_list(handler, size_list);
677         lb_set_group(handler, cluster, category);
678
679         lb_set_content(handler, content);
680         lb_set_title(handler, title);
681
682         lb_set_user(handler, user);
683
684         lb_set_auto_launch(handler, auto_launch);
685         lb_set_pinup(handler, pinup_supported);
686
687         lb_set_period(handler, period);
688
689         ret = 0;
690
691         if (handler->state == CREATE) {
692                 /*!
693                  * \note
694                  * These callback can change the handler->state.
695                  * So we have to use the "old_state" which stored state before call these callbacks
696                  */
697
698                 if (handler->created_cb) {
699                         DbgPrint("Invoke the created_cb\n");
700                         handler->created_cb(handler, ret, handler->created_cbdata);
701
702                         handler->created_cb = NULL;
703                         handler->created_cbdata = NULL;
704                 } else {
705                         DbgPrint("Invoke the lb,created\n");
706                         lb_invoke_event_handler(handler, LB_EVENT_CREATED);
707                 }
708         }
709
710 out:
711         if (ret == 0 && old_state == DELETE) {
712                 DbgPrint("Send the delete request\n");
713                 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
714
715                 /*!
716                  * \note
717                  * handler->created_cb = NULL;
718                  * handler->created_cbdata = NULL;
719                  *
720                  * Do not clear this to use this from the deleted event callback.
721                  * if this value is not cleared when the deleted event callback check it,
722                  * it means that the created function is not called yet.
723                  * Then the call the deleted event callback with -ECANCELED errno.
724                  */
725         }
726
727         return NULL;
728 }
729
730 static struct method s_table[] = {
731         {
732                 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
733                 .handler = master_lb_updated,
734         },
735         {
736                 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
737                 .handler = master_pd_updated,
738         },
739         {
740                 .cmd = "pd_created",
741                 .handler = master_pd_created,
742         },
743         {
744                 .cmd = "pd_destroyed",
745                 .handler = master_pd_destroyed,
746         },
747         {
748                 .cmd = "fault_package", /* pkgname, id, function, ret */
749                 .handler = master_fault_package,
750         },
751         {
752                 .cmd = "deleted", /* pkgname, id, timestamp, ret */
753                 .handler = master_deleted,
754         },
755         {
756                 .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 */
757                 .handler = master_created,
758         },
759         {
760                 .cmd = "group_changed",
761                 .handler = master_group_changed,
762         },
763         {
764                 .cmd = "period_changed",
765                 .handler = master_period_changed,
766         },
767         {
768                 .cmd = "pinup",
769                 .handler = master_pinup,
770         },
771         {
772                 .cmd = NULL,
773                 .handler = NULL,
774         },
775 };
776
777 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
778 {
779         if (!result) {
780                 DbgPrint("Result packet is not valid\n");
781         } else {
782                 int ret;
783
784                 if (packet_get(result, "i", &ret) != 1)
785                         ErrPrint("Invalid argument\n");
786                 else
787                         DbgPrint("Acquire returns: %d\n", ret);
788         }
789
790         return;
791 }
792
793 static gboolean connector_cb(gpointer user_data)
794 {
795         s_info.reconnector = 0;
796
797         if (s_info.fd > 0) {
798                 DbgPrint("Connection is already made\n");
799                 return FALSE;
800         }
801
802         make_connection();
803         return FALSE;
804 }
805
806 static inline void make_connection(void)
807 {
808         struct packet *packet;
809         int ret;
810
811         DbgPrint("Let's making connection!\n");
812
813         s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
814         if (s_info.fd < 0) {
815                 /*!< After 10 secs later, try to connect again */
816                 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
817                 if (s_info.reconnector == 0)
818                         ErrPrint("Failed to fire the reconnector\n");
819
820                 ErrPrint("Try this again 10 secs later\n");
821                 return;
822         }
823
824         packet = packet_create("acquire", "d", util_timestamp());
825         if (!packet) {
826                 com_core_packet_client_fini(s_info.fd);
827                 s_info.fd = -1;
828                 return;
829         }
830
831         ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
832         if (ret < 0) {
833                 ErrPrint("Master RPC returns %d\n", ret);
834                 com_core_packet_client_fini(s_info.fd);
835                 s_info.fd = -1;
836         }
837
838 }
839
840 static int connected_cb(int handle, void *data)
841 {
842         master_rpc_check_and_fire_consumer();
843         return 0;
844 }
845
846 static int disconnected_cb(int handle, void *data)
847 {
848         if (s_info.fd != handle) {
849                 /*!< This handle is not my favor */
850                 return 0;
851         }
852
853         s_info.fd = -1; /*!< Disconnected */
854
855         if (s_info.reconnector > 0) {
856                 DbgPrint("Reconnector already fired\n");
857                 return 0;
858         }
859
860         /*!< After 10 secs later, try to connect again */
861         s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
862         if (s_info.reconnector == 0) {
863                 ErrPrint("Failed to fire the reconnector\n");
864                 make_connection();
865         }
866
867         master_rpc_clear_all_request();
868         lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
869
870         lb_delete_all();
871         return 0;
872 }
873
874 int client_init(void)
875 {
876         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
877         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
878         make_connection();
879         return 0;
880 }
881
882 int client_fd(void)
883 {
884         return s_info.fd;
885 }
886
887 const char *client_addr(void)
888 {
889         return CLIENT_SOCKET;
890 }
891
892 int client_fini(void)
893 {
894         com_core_packet_client_fini(s_info.fd);
895         com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
896         com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
897         s_info.fd = -1;
898         return 0;
899 }
900
901 /* End of a file */
902
903