]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/cbroker/fcb.c
a1d6014bde8297bf0d219e624740104e0c39458f
[frescor/frsh.git] / fres / cbroker / fcb.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FRSH is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FRSH; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   fcb.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Mon Oct 20 18:00:18 2008
51  * 
52  * @brief  FRESCOR Contract Broker
53  * 
54  * 
55  */
56 #define WITH_C99                /* For ul_gsa.h iterators */
57 #include <semaphore.h>
58 #include <getopt.h>
59 #include <forb.h>
60 #include <forb/config.h>
61 #include <fcb.h>
62 #include <fcb_contact_info.h>
63 #include <error.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <ul_gavlcust.h>
67 #include <ul_gsacust.h>
68 #include <string.h>
69 #include <ul_log.h>
70 #include <ul_logreg.h>
71 #include <forb/server_id.h>
72 #include <fres_contract.h>
73 #include "fcb_config.h"
74 #include <fosa_clocks_and_timers.h>
75 #include "contract_log.h"
76 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
77 #include <forb/proto_inet.h>
78 #endif
79
80 #define COMPILE_TIME_ASSERT(cond, msg) \
81         typedef char msg[(cond) ? 1 : -1]
82
83
84 UL_LOG_CUST(ulogd_fcb);
85 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
86 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
87         
88 bool opt_daemon = false;
89 char *opt_pidfile = NULL;
90 fosa_abs_time_t start_time;
91
92 /** List of contracts to be newly reserved or changed by resource manager */
93 struct reservation_list {
94         ul_list_head_t fcb_contracts;
95         unsigned length;
96 };
97
98 /**
99  * Resource identification 
100  */
101 struct res_key {
102         frsh_resource_type_t type;
103         frsh_resource_id_t id;
104 };
105
106 /**
107  * Registered resource
108  */
109 struct resource {
110         gavl_node_t node;
111         struct res_key key;
112         char *name;
113         fres_resource_manager mng; /**< Object reference of the resource manager */
114         gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
115         ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
116         struct reservation_list rl; /**< Temporary list of contracts to be reserved on this resource */
117 };
118
119 /**
120  * Resource allocator registered in different nodes/applications 
121  */
122 struct res_alloc {
123         gavl_node_t node;
124         forb_server_id app;
125         fres_resource_allocator ra;
126 };
127
128 struct fcb_contract {
129         fres_contract_id_t id;
130         struct res_alloc *ra;   /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
131         gavl_node_t node_fcb;
132         ul_list_node_t node_sc;
133         ul_list_node_t node_reservation;
134         struct {
135                 int initial;
136                 int try;
137         } sc_variant;
138         fosa_abs_time_t end_of_stability_period;
139         struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
140         struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
141         struct fres_contract *to_be_reserved_contract;
142         struct fres_contract *schedulable_contract;
143 };
144
145 /**
146  * Contract broker data
147  */
148 struct fcb {
149         fres_contract_id_t contract_counter;
150         gavl_cust_root_field_t resources; /**< Registered resources */
151         gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
152 };
153
154 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
155 {
156         struct fcb_contract *fcb_contract;
157         
158         fcb_contract = malloc(sizeof(*fcb_contract));
159         if (!fcb_contract) {
160                 return NULL;
161         }
162         memset(fcb_contract, 0, sizeof(*fcb_contract));
163         fcb_contract->id = *id;
164
165         return fcb_contract;
166 }
167
168 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
169 {
170         if (fcb_contract->user_contract) {
171                 fres_contract_destroy(fcb_contract->user_contract);
172         }
173         if (fcb_contract->to_be_reserved_contract) {
174                 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
175         }
176         if (fcb_contract->requested_contract) {
177                 fres_contract_destroy(fcb_contract->requested_contract);
178         }
179         if (fcb_contract->schedulable_contract) {
180                 fres_contract_destroy(fcb_contract->requested_contract);
181         }
182         free(fcb_contract);
183 }
184
185 static inline int res_key_cmp(const struct res_key *a,
186                               const struct res_key *b)
187 {
188         if (a->type < b->type) {
189                 return -1;
190         } else if (a->type > b->type) {
191                 return +1;
192         } else if (a->id < b->id) {
193                 return -1;
194         } else if (a->id > b->id) {
195                 return +1;
196         } else {
197                 return 0;
198         }
199 }
200
201 /* Container for registered resources */
202 GAVL_CUST_NODE_INT_DEC(fcb_resource   /* cust_prefix */,                \
203                        struct fcb     /* cust_root_t */,                \
204                        struct resource/* cust_item_t */,                \
205                        struct res_key /* cust_key_t */,                 \
206                        resources      /* cust_root_node */,             \
207                        node           /* cust_item_node */,             \
208                        key            /* cust_item_key */,              \
209                        res_key_cmp    /* cust_cmp_fnc */);
210
211 GAVL_CUST_NODE_INT_IMP(fcb_resource   /* cust_prefix */,                \
212                        struct fcb     /* cust_root_t */,                \
213                        struct resource/* cust_item_t */,                \
214                        struct res_key /* cust_key_t */,                 \
215                        resources      /* cust_root_node */,             \
216                        node           /* cust_item_node */,             \
217                        key            /* cust_item_key */,              \
218                        res_key_cmp    /* cust_cmp_fnc */);
219
220 /* Container for allocators registered for a given resource */
221 GAVL_CUST_NODE_INT_DEC(fcb_alloc        /* cust_prefix */,              \
222                        struct resource  /* cust_root_t */,              \
223                        struct res_alloc /* cust_item_t */,              \
224                        forb_server_id   /* cust_key_t */,               \
225                        allocators       /* cust_root_node */,           \
226                        node             /* cust_item_node */,           \
227                        app              /* cust_item_key */,            \
228                        fres_contract_id_cmp /* cust_cmp_fnc */);
229
230 GAVL_CUST_NODE_INT_IMP(fcb_alloc        /* cust_prefix */,              \
231                        struct resource   /* cust_root_t */,             \
232                        struct res_alloc /* cust_item_t */,              \
233                        forb_server_id   /* cust_key_t */,               \
234                        allocators       /* cust_root_node */,           \
235                        node             /* cust_item_node */,           \
236                        app              /* cust_item_key */,            \
237                        forb_server_id_cmp /* cust_cmp_fnc */);
238
239 /* Container for contracts with spare capacity negotiated for a given resource */
240 UL_LIST_CUST_DEC(sc_contracts        /* cust_prefix */,                 \
241                  struct resource  /* cust_head_t */,                    \
242                  struct fcb_contract /* cust_item_t */,                 \
243                  sc_contracts   /* cust_head_field */,                  \
244                  node_sc        /* cust_node_field */);
245
246 UL_LIST_CUST_DEC(reservation_list        /* cust_prefix */,             \
247                  struct reservation_list  /* cust_head_t */,            \
248                  struct fcb_contract /* cust_item_t */,                 \
249                  fcb_contracts  /* cust_head_field */,                  \
250                  node_reservation /* cust_node_field */);
251
252 /* Container for negotiated contracts */
253 GAVL_CUST_NODE_INT_DEC(fcb_contract         /* cust_prefix */,          \
254                        struct fcb           /* cust_root_t */,          \
255                        struct fcb_contract  /* cust_item_t */,          \
256                        fres_contract_id_t   /* cust_key_t */,           \
257                        contracts            /* cust_root_node */,       \
258                        node_fcb             /* cust_item_node */,       \
259                        id                   /* cust_item_key */,        \
260                        fres_contract_id_cmp /* cust_cmp_fnc */);
261
262 #if 1
263 GAVL_CUST_NODE_INT_IMP(fcb_contract         /* cust_prefix */,          \
264                        struct fcb           /* cust_root_t */,          \
265                        struct fcb_contract  /* cust_item_t */,          \
266                        fres_contract_id_t   /* cust_key_t */,           \
267                        contracts            /* cust_root_node */,       \
268                        node_fcb             /* cust_item_node */,       \
269                        id                   /* cust_item_key */,        \
270                        fres_contract_id_cmp /* cust_cmp_fnc */);
271 #else
272 #include "fcb_contract_gavl.inc"
273 #endif
274
275 struct res_array {
276         gsa_array_field_t array;
277 };
278
279 GSA_CUST_DEC(res_array          /* cust_prefix */,
280              struct res_array   /* cust_array_t */,
281              struct resource    /* cust_item_t */,
282              struct res_key     /* cust_key_t */,
283              array              /* cust_array_field */,
284              key                /* cust_item_key */,
285              res_key_cmp        /* cust_cmp_fnc */);
286
287 GSA_CUST_IMP(res_array          /* cust_prefix */,
288              struct res_array   /* cust_array_t */,
289              struct resource    /* cust_item_t */,
290              struct res_key     /* cust_key_t */,
291              array              /* cust_array_field */,
292              key                /* cust_item_key */,
293              res_key_cmp        /* cust_cmp_fnc */,
294              true               /* cust_ins_fl */);
295
296
297
298 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
299
300 static struct res_key*
301 get_res_key(const struct fres_contract *contract, struct res_key *key)
302 {
303         fres_block_resource *block_res;
304         
305         block_res = fres_contract_get_resource(contract);
306         if (!block_res) {
307                 ul_logerr("No resource specified\n");
308                 return NULL;
309         } else {
310                 key->type = block_res->resource_type;
311                 key->id = block_res->resource_id;
312         }
313         return key;
314 }
315
316 static struct res_key*
317 get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
318 {
319         if (fc->user_contract)
320                 get_res_key(fc->user_contract, key);
321         else
322                 get_res_key(fc->requested_contract, key);
323         return key;
324 }
325
326 /** 
327  * Fills in an array of fcb_contracts according to requests submited
328  * by an application through negotiate_contracts() or
329  * negotiate_transaction(). For every contract in @a contracts, there
330  * is allocated one fcb_contract in @a fcb_contracts array.
331  * 
332  * @param fcb
333  * @param fcb_contracts Where to store pointers to fcb_contracts
334  * @param contracts Contracts submited for negotiation 
335  * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
336  * 
337  * @return Zero on success, non-zero error code on error.
338  */
339 static int
340 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
341                       struct fres_contract *contracts[], int num)
342 {
343         unsigned i;
344         struct fcb_contract *fc;
345
346         for (i=0; i<num; i++) {
347                 struct fres_contract *c = contracts[i];
348
349                 if (fres_contract_id_is_empty(&c->id)) {
350                         /* Normal negotiation request */
351                         COMPILE_TIME_ASSERT(sizeof(c->id) == sizeof(fcb->contract_counter),
352                                             wrong_size_of_contract_id);
353                         c->id = ++fcb->contract_counter;
354                         fc = fcb_contract_new(&c->id);
355                         if (!fc)
356                                 return errno;
357                         fcb_contracts[i] = fc;
358                         log_contract("Negotiation request", i, c);
359                 } else {
360                         fc = fcb_contract_find(fcb, &c->id);
361                         if (!fc) {
362                                 char str[60];
363                                 fres_contract_id_to_string(str, &c->id, sizeof(str));
364                                 ul_logerr("Attempt to change unknown contract %s\n", str);
365                                 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
366                         } else {
367                                 fcb_contracts[i] = fc;
368                                 if (fres_contract_get_num_blocks(c) == 0) {
369                                         /* Cancelation */
370                                         log_contract("Cancelation request", i, fc->user_contract);
371                                 } else {
372                                         /* Renegotiation */
373                                         log_contract("Renegotiation request", i, fc->user_contract);
374                                 }
375                         }
376                 }
377                 fc->requested_contract = c;
378         }
379         return 0;
380 }
381
382 /**
383  * Ensure that all contracts belong to the same resource and find
384  * resource allocators for the contracts.
385  *
386  * @param fcb 
387  * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
388  * @param num Number of contracts in @a fcb_contracts
389  * @param resource Resource for which negotiation is being done.
390  * @param app Application which requests negotiation
391  */
392 static int
393 check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
394                          struct resource **resource,
395                          forb_server_id *app,
396                          int num)
397 {
398         unsigned i;
399         struct res_alloc *ra;
400         struct res_key key, key2 = {-1,-1};
401
402         for (i=0; i<num; i++) {
403                 struct fcb_contract *fc = fcb_contracts[i];
404
405                 if (!get_fc_res_key(fc, &key))
406                         return FRSH_ERR_RESOURCE_ID_INVALID;
407
408                 /* Check that all contracts are for the same resource */
409                 if (i==0) {
410                         key2 = key;
411                         *resource = fcb_resource_find(fcb, &key);
412                         if (!*resource) {
413                                 ul_logerr("No resource manager for %d.%d registered\n",
414                                           key.type, key.id);
415                                 return FRES_ERR_NO_RESOURCE_MANAGER;
416                         }
417                         /* Find allocator for this request */
418                         ra = fcb_alloc_find(*resource, app);
419                         if (!ra) {
420                                 char str[60];
421                                 forb_server_id_to_string(str, app, sizeof(str));
422                                 ul_logerr("No resource allocator found for %d.%d and %s\n",
423                                           (*resource)->key.type, (*resource)->key.id, str);
424                                 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
425                         }
426                 }
427                 else if (key.type != key2.type ||
428                          key.id   != key2.id) {
429                         ul_logerr("Contract #%d differs in resource!\n", i);
430                         return FRSH_ERR_RESOURCE_ID_INVALID;
431                 }
432                 fc->ra = ra;
433         }       
434         return 0;
435 }
436
437 /**
438  * Prepares a list of contracts to pass to resource manager for (re)reserving.
439  *
440  * @todo Needs to be changed for compatibility with transactions.
441  * 
442  * @param resource Resource for which to rebalance capacity and negotiate new contracts
443  * @param fcb_contract New requests to negotiate
444  * @param num The number of elements in @a fcb_contract
445  * @param[out] rl List with changed contracts to be commited
446  */
447 static void
448 prepare_reservation_list(struct resource *resource,
449                          struct fcb_contract *fcb_contract[], int num)
450 {
451         int i;
452         fosa_abs_time_t now;
453         struct fcb_contract *fc;
454
455         reservation_list_init_head(&resource->rl);
456         resource->rl.length = 0;
457         for (i=0; i<num; i++) {
458                 assert(fcb_contract[i]->requested_contract != NULL);
459                 reservation_list_insert(&resource->rl, fcb_contract[i]);
460                 resource->rl.length++;
461         }
462         fosa_clock_get_time(CLOCK_REALTIME, &now);
463         ul_list_for_each(sc_contracts, resource, fc) {
464                 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
465                     fc->requested_contract == NULL) /* Do not insert contract inserted above */
466                 {
467                         reservation_list_insert(&resource->rl, fc);
468                         resource->rl.length++;
469                 }
470         }
471 }
472
473 static int
474 reserve_resource(struct resource *resource)
475 {
476         int ret, i;
477         fres_contract_ptr_seq contracts;
478         struct fres_contract *c;
479         struct fcb_contract *fc;
480         CORBA_Environment ev;
481         
482         if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
483                 ret = errno;
484                 goto err;
485         }
486         forb_sequence_length(&contracts) = resource->rl.length;
487
488         i=0;
489         /* Prepare FORB sequence  */
490         ul_list_for_each(reservation_list, &resource->rl, fc) {
491                 c = NULL;
492                 if (fc->to_be_reserved_contract)
493                         /* Contract without spare capacity */
494                         c = fc->to_be_reserved_contract;
495                 else if (fc->requested_contract)
496                         c = fc->requested_contract;
497                 assert(c);
498                 forb_sequence_elem(&contracts, i++) = c;
499         }
500         
501         /* Reserve contract */
502         ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
503         if (forb_exception_occurred(&ev)) {
504                 ret = fres_forbex2err(&ev);
505                 ul_logerr("FORB exception when reserving contracts\n");
506                 goto err_free;
507         }
508         if (ret < 0) {
509                 ul_logerr("Contract reservation error %d\n", ret);
510                 ret = FRES_ERR_ADMISSION_TEST;
511                 goto err_free;
512         } else if (ret == 0)
513                 ret = FRSH_NO_ERROR;
514         else if (ret == 1)
515                 ret = FRSH_ERR_CONTRACT_REJECTED;
516         else
517                 assert(false);
518 err_free:
519         forb_sequence_free_buf(&contracts, forb_no_destructor);
520 err:
521         return ret;
522 }
523
524 /** 
525  * 
526  * 
527  * @param resource Resource for which to rebalance capacity and negotiate new contracts
528  * @param rl List with changed contracts to be commited
529  * 
530  * @return Zero on success, non-zero error code on error
531  */
532 static int
533 rebalance_spare_capacity_and_reserve(struct resource *resource)
534 {
535         int ret;
536         struct reservation_list *rl = &resource->rl;
537         struct fcb_contract *fc;
538         fres_block_spare_capacity *s;
539
540         /* Initialize optimization */
541         ul_list_for_each(reservation_list, rl, fc) {
542                 fc->to_be_reserved_contract =
543                         fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
544                         fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
545                         NULL;
546                 assert(fc->to_be_reserved_contract != NULL);
547
548                 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
549                 if (s && s->granularity == FRSH_GR_DISCRETE) {
550                         fc->sc_variant.initial = s->variants._length - 1;
551                         fc->sc_variant.try = fc->sc_variant.initial;
552                 }
553         }
554
555         bool all_combinations_tried;
556         int criterion, best_criterion = -1;
557         struct fcb_contract *fcb_changed;
558         /* Exhaustive search. Try all combinations of spare capacity
559          * variants and find the one with best creterion. */
560         do {
561                 all_combinations_tried = true;
562                 fcb_changed = NULL;
563                 criterion = 0;
564                 /* Prepare spare capacity variant */
565                 ul_list_for_each(reservation_list, rl, fc) {
566                         s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
567                         /* TODO: Simulate continuous granularity by discretization */
568                         if (s && s->granularity == FRSH_GR_DISCRETE) {
569                                 fres_container_copy(fc->to_be_reserved_contract->container,
570                                                     forb_sequence_elem(&s->variants, fc->sc_variant.try));
571                                 criterion += fc->sc_variant.try;
572
573                                 if (fcb_changed == NULL) {
574                                         /* Chnage tried variant for the next round */
575                                         fc->sc_variant.try = fc->sc_variant.try > 0 ?
576                                                 fc->sc_variant.try - 1 :
577                                                 s->variants._length - 1;
578                                         /* Do not change other
579                                          * contracts unless we have
580                                          * tried all variants */
581                                         if (fc->sc_variant.try != fc->sc_variant.initial) {
582                                                 fcb_changed = fc;
583                                         }
584                                 }
585                                 if (fc->sc_variant.try != fc->sc_variant.initial)
586                                         all_combinations_tried = false;
587                         }
588                 }
589
590                 if (criterion > best_criterion) {
591                         ret = reserve_resource(resource);
592                         switch (ret) {
593                         case FRSH_NO_ERROR:
594                                 best_criterion = criterion;
595                                 break;
596                         case FRSH_ERR_CONTRACT_REJECTED:
597                                 break;
598                         default:
599                                 goto err;
600                         }
601                 }
602         } while (!all_combinations_tried);
603
604         if (best_criterion == -1) {
605                 ret = FRSH_ERR_CONTRACT_REJECTED;
606         } else {
607                 /* At least some variant succeeded */
608                 ret = 0;
609                 sc_contracts_init_head(resource);
610                 ul_list_for_each(reservation_list, rl, fc) {
611                         s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
612                         if (s && s->granularity == FRSH_GR_DISCRETE) {
613                                 sc_contracts_insert(resource, fc);
614                         }
615                 }
616         }
617 err:
618         ul_list_for_each(reservation_list, rl, fc) {
619                 fres_contract_destroy(fc->to_be_reserved_contract);
620                 fc->to_be_reserved_contract = NULL;
621         }
622         return ret;
623 }
624
625 /**
626  * Create/change VReses according to @a schedulable_contracts.
627  *
628  * There might be more allocators for schedulable contracts, so merge
629  * adjacent vreses with the same allocator together and create/change
630  * them in one step. Also the order of schedulable contracts might be
631  * different from the initial order od ids in commit_contracts()
632  * therefore we have to search for every contract.
633  */
634 static int
635 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
636 {
637         struct res_alloc *last_ra = NULL;
638         fres_contract_ptr_seq vreses;
639         int i, ret = 0;
640         CORBA_Environment ev;
641         
642         if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
643                 return errno;
644         }
645         vreses._length = 0;
646         
647         for (i=0; i<schedulable_contracts->_length; i++) {
648                 struct fcb_contract *fc;
649                 struct fres_contract *sc = schedulable_contracts->_buffer[i];
650
651                 log_contract("Changing VRES", i, sc);
652
653                 fc = fcb_contract_find(fcb, &sc->id);
654                 assert(fc != NULL);
655
656                 if (true /* TODO: if the schedulable contract is changed */) {
657                         if (last_ra != fc->ra) {
658                                 if (vreses._length) {
659                                         ret = fres_resource_allocator_change_vreses(last_ra->ra,
660                                                                                     &vreses, &ev);
661                                         if (forb_exception_occurred(&ev)) {
662                                                 ret = fres_forbex2err(&ev);
663                                                 goto err;
664                                         }
665                                         if (ret) goto err;
666                                 }
667                                 vreses._length = 0;
668                         }
669                         vreses._buffer[vreses._length] = sc;
670                         vreses._length++;
671                         fres_contract_destroy(fc->schedulable_contract);
672                         fc->schedulable_contract = fres_contract_duplicate(sc);
673                         last_ra = fc->ra;
674                 
675                 }
676         }
677         if (last_ra) {
678                 ret = fres_resource_allocator_change_vreses(last_ra->ra,
679                                                             &vreses, &ev);
680                 if (forb_exception_occurred(&ev))
681                         ret = fres_forbex2err(&ev);
682         }
683 err:
684         forb_sequence_free_buf(&vreses, forb_no_destructor);
685         return ret;
686 }
687
688 void
689 free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
690 {
691         int i;
692         struct fcb_contract *fc;
693         for (i=0; i<num; i++) {
694                 fc = fcb_contracts[i];
695                 if (fc && !fc->user_contract) {
696                         fc->requested_contract = NULL; /* Destroyed by FORB */
697                         fcb_contract_destroy(fc);
698                 }
699         }
700         free(fcb_contracts);
701 }
702
703 int
704 commit_resource(struct resource *resource,
705                 fres_contract_ptr_seq **schedulable_contracts)
706 {
707         int ret;
708         fres_contract_id_seq commit_ids;
709         int i;
710         struct fcb_contract *fc;
711         CORBA_Environment ev;
712         
713         if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
714                 ret = errno;
715                 goto err;
716         }
717
718         commit_ids._length = resource->rl.length;
719         i=0;
720         ul_list_for_each(reservation_list, &resource->rl, fc) {
721                 forb_sequence_elem(&commit_ids, i) = fc->id;
722                 i++;
723         }
724         
725         fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
726                                                schedulable_contracts, &ev);
727         if (forb_exception_occurred(&ev)) {
728                 ret = fres_forbex2err(&ev);
729                 goto err_free;
730         }
731         return 0;
732 err_free:
733         forb_sequence_free_buf(&commit_ids, forb_no_destructor);
734 err:
735         return ret;
736 }
737
738 int cancel_reservations(struct resource *resource)
739 {
740         int ret;
741         fres_contract_id_seq commit_ids;
742         int i;
743         struct fcb_contract *fc;
744         CORBA_Environment ev;
745         
746         if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
747                 ret = errno;
748                 goto err;
749         }
750
751         commit_ids._length = resource->rl.length;
752         i=0;
753         ul_list_for_each(reservation_list, &resource->rl, fc) {
754                 forb_sequence_elem(&commit_ids, i) = fc->id;
755                 i++;
756         }
757         
758         fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
759         if (forb_exception_occurred(&ev)) {
760                 ret = fres_forbex2err(&ev);
761                 goto err_free;
762         }
763         return 0;
764 err_free:
765         forb_sequence_free_buf(&commit_ids, forb_no_destructor);
766 err:
767         return ret;
768 }
769
770
771 CORBA_long
772 negotiate_contracts(fres_contract_broker obj,
773                     const fres_contract_ptr_seq* contracts,
774                     fres_contract_id_seq** ids_out,
775                     CORBA_Environment *ev)
776 {
777         struct fcb *fcb = o2fcb(obj);
778         struct resource *resource = NULL;
779         int ret = 0;
780         forb_server_id app;
781         fres_contract_ptr_seq *schedulable_contracts;
782         struct fcb_contract **fcb_contracts, *fc;
783         unsigned i;
784         int num = contracts->_length;
785
786         /* Prepare output sequence for the case we return eariler with
787          * an error */
788         forb_sequence_alloc(*ids_out, 0);
789         if (!*ids_out) {
790                 ev->major = FORB_EX_NO_MEMORY;
791                 goto err;
792         }
793         CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
794         
795         forb_get_req_source(obj, &app);
796         
797         fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
798         if (!fcb_contracts) {
799                 ret = errno;
800                 goto err;
801         }
802         memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
803
804         ret = prepare_fcb_contracts(fcb, fcb_contracts,
805                                     contracts->_buffer, num);
806         if (ret)
807                 goto err_free_fcb_contracts;
808         ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
809                                        &app, num);
810         if (ret)
811                 goto err_free_fcb_contracts;
812
813         prepare_reservation_list(resource, fcb_contracts, num);
814
815         /* Reserve contracts */
816         ret = rebalance_spare_capacity_and_reserve(resource);
817         if (ret) {
818                 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
819                         ul_logmsg("Contract(s) was/were rejected\n");
820                 } else {
821                         char msg[100];
822                         fres_strerror(ret, msg, sizeof(msg));
823                         ul_logerr("Reservation error: %s\n", msg);
824                 }
825                 goto err_free_fcb_contracts;
826         }
827
828         /* Commit contracts */
829         ret = commit_resource(resource, &schedulable_contracts);
830         if (ret)
831                 goto err_cancel_reservation;
832
833         /* Add new contracts to our fcb database for later
834          * reference. Canceled contracts are removed below. */
835         for (i=0; i<num; i++) {
836                 fc =  fcb_contracts[i];
837
838                 if (fc->user_contract) {
839                         if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
840                                 /* Renegotiation */
841                                 fres_contract_destroy(fc->user_contract);
842                                 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
843                                 /* Note: requested_contract is also
844                                  * pointed by contracts parameter and
845                                  * will be freed by FORB. */
846                                 fc->requested_contract = NULL;
847                         }
848                 } else {
849                         /* Insert new contracts */
850                         fcb_contract_insert(fcb, fcb_contracts[i]);
851                         fc->user_contract = fres_contract_duplicate(fc->requested_contract);
852                         fc->requested_contract = NULL;
853                         /* See the note above. */
854                 }
855         }
856
857         ret = change_vreses(fcb, schedulable_contracts);
858         if (ret)
859                 goto err_cancel_reservation;
860         
861         forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
862         if (ret != 0) {
863                 ul_logerr("VRes was not created\n");
864                 goto err_cancel_contracts;
865         }
866
867
868         /* Return IDs and delete canceled contracts from out database */
869         if (!forb_sequence_alloc_buf(*ids_out, num)) {
870                 ev->major = FORB_EX_NO_MEMORY;
871                 goto err_cancel_contracts;
872         }
873         (*ids_out)->_length = num;
874         for (i=0; i<num; i++) {
875                 struct fcb_contract *fc =  fcb_contracts[i];
876                 forb_sequence_elem(*ids_out, i) = fc->id;
877
878                 if (fc->requested_contract &&
879                     fres_contract_get_num_blocks(fc->requested_contract) == 0) {
880                         fcb_contract_delete(fcb, fc);
881                         /* Note: requested_contract is also pointed by
882                          * contracts parameter and will be freed by FORB. */
883                         fc->requested_contract = NULL;
884                         fcb_contract_destroy(fc);
885                 }
886         }
887         return 0;
888
889 err_cancel_contracts:
890         /* TODO */
891         goto err_free_fcb_contracts;
892 err_cancel_reservation:
893         cancel_reservations(resource);
894 err_free_fcb_contracts:
895         free_fcb_contracts(fcb_contracts, num);
896 err:
897         return ret;
898 }
899
900 void redistribute_spare_capacity(fres_contract_broker obj,
901                                  const frsh_resource_type_t restype,
902                                  const frsh_resource_id_t resid,
903                                  CORBA_Environment *ev)
904 {
905         struct fcb *fcb = o2fcb(obj);
906         struct res_key key = {restype, resid };
907         struct resource *resource;
908         
909         resource = fcb_resource_find(fcb, &key);
910
911         prepare_reservation_list(resource, NULL, 0);
912
913 /*      forb_sequence_alloc(ids, rl.length); */
914 /*      if (!ids || !ids->_buffer) { */
915 /*              ev->major = FORB_EX_NO_MEMORY; */
916 /*              goto err_free_fcb_contracts; */
917 /*      } */
918 /*      CORBA_sequence_set_release(ids, CORBA_TRUE); */
919 /*      *ids_out = ids;         /\* ids is freed by FORB *\/ */
920         
921         
922         rebalance_spare_capacity_and_reserve(resource);
923         /* Commit */
924 }
925
926 CORBA_long register_resource(fres_contract_broker obj,
927                             const frsh_resource_type_t restype,
928                             const frsh_resource_id_t resid,
929                             const fres_resource_desc *desc,
930                             CORBA_Environment *ev)
931 {
932         struct fcb *fcb = o2fcb(obj);
933         struct resource *res, *res2;
934
935         res = malloc(sizeof(*res));
936         if (!res) goto err;
937         memset(res, 0, sizeof(*res));
938         res->key.type = restype;
939         res->key.id = resid;
940         res2 = fcb_resource_find(fcb, &res->key);
941         if (res2) {
942                 if (forb_object_is_stale(res2->mng)) {
943                         ul_logmsg("Removing stale manager for resource %d.%d\n",
944                                   restype, resid);
945                         forb_object_release(res2->mng);
946                         fcb_resource_delete(fcb, res2);
947                         /* TODO: Delete also all allocators associated
948                          * with this stale resource manager. */
949                         free(res);
950                         res = res2;
951                 } else {
952                         ul_logerr("Resource manager %d.%d already registered\n",
953                                   restype, resid);
954                         goto free_err;
955                 }
956         }
957         res->mng = forb_object_duplicate(desc->manager);
958         res->name = desc->name;
959
960         fcb_alloc_init_root_field(res);
961         sc_contracts_init_head(res);
962         ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
963                   res->name, restype, resid);
964         fcb_resource_insert(fcb, res);
965         return 0;
966 free_err:
967         free(res);
968 err:
969         return -1;
970 }
971
972
973 CORBA_long register_allocator(fres_contract_broker obj,
974                               const frsh_resource_type_t restype,
975                               const frsh_resource_id_t resid,
976                               const fres_resource_allocator ra_obj,
977                               CORBA_Environment *ev)
978 {
979         struct fcb *fcb = o2fcb(obj);
980         struct resource *res;
981         struct res_alloc *ra;
982         struct res_key resource;
983         forb_server_id server_id;
984         char server_id_str[40];
985         int ret;
986
987         forb_get_server_id(ra_obj, &server_id);
988         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
989         ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
990                   restype, resid, server_id_str);
991         
992         resource.type = restype;
993         resource.id = resid;
994         res = fcb_resource_find(fcb, &resource);
995         if (!res) {
996                 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
997                           restype, resid);
998                 ret = FRES_ERR_NO_RESOURCE_MANAGER;
999                 goto err;
1000         }
1001         ra = fcb_alloc_find(res, &server_id);
1002         if (ra) {
1003                 char *str = forb_object_to_string(ra_obj);
1004                 ul_logerr("Allocator from already registered (%s)\n",
1005                           str);
1006                 forb_free(str);
1007                 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
1008                 goto err;
1009         }
1010         ra = malloc(sizeof(*ra));
1011         if (!ra) {
1012                 ret = ENOMEM;
1013                 goto err;
1014         }
1015         ra->app = server_id;
1016         ra->ra = forb_object_duplicate(ra_obj);
1017         fcb_alloc_insert(res, ra);
1018         return 0;
1019 err:
1020         return ret;
1021 }
1022
1023 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1024 {
1025         struct fcb *fcb = o2fcb(obj);
1026         fres_resource_seq *seq;
1027         struct resource *res;
1028         int n;
1029
1030         seq = malloc(sizeof(*seq));
1031         if (!seq) {
1032                 ev->major = FORB_EX_NO_MEMORY;
1033                 return;
1034         }
1035         memset(seq, 0, sizeof(*seq));
1036
1037         n=0;
1038         gavl_cust_for_each(fcb_resource, fcb, res) {
1039                 n++;
1040         }
1041
1042         seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1043         seq->_maximum = n;
1044         seq->_length = n;
1045
1046         n = 0;
1047         gavl_cust_for_each(fcb_resource, fcb, res) {
1048                 seq->_buffer[n].restype = res->key.type;
1049                 seq->_buffer[n].resid = res->key.id;
1050                 seq->_buffer[n].desc.manager = res->mng;
1051                 seq->_buffer[n].desc.name = res->name;
1052                 n++;
1053         }
1054
1055         *resources = seq;
1056 }
1057
1058 static bool
1059 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1060 {
1061         struct fres_contract **c;
1062         forb_sequence_foreach(&transaction->contracts, c)
1063                 if (fres_contract_get_spare_capacity(*c))
1064                         return true;
1065         return false;
1066 }
1067
1068 static int
1069 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1070                           int num, struct res_array *res_array)
1071 {
1072         int i;
1073         struct resource *resource;
1074         struct res_key key;
1075         int ret;
1076
1077         for (i = 0; i < num; i++) {
1078                 get_fc_res_key(fc[i], &key);
1079                 resource = fcb_resource_find(fcb, &key);
1080                 ret = res_array_insert(res_array, resource);
1081                 if (ret == -1) {
1082                         ret = FRSH_ERR_INTERNAL_ERROR;
1083                         goto err;
1084                 }
1085         }
1086         ret = 0;
1087 err:
1088         return ret;
1089 }
1090                                  
1091 CORBA_long
1092 negotiate_transaction(fres_contract_broker _obj,
1093                       const fres_transaction_t* transaction,
1094                       CORBA_Environment *ev)
1095 {
1096         struct fcb *fcb = o2fcb(_obj);
1097         struct fcb_contract **fcb_contracts, *fc;
1098         const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1099         int i, ret, num = user_contracts->_length;
1100         struct res_array res_array;
1101         struct res_key key;
1102         struct resource *resource;
1103
1104         ul_logmsg("Negotiating transaction of %d contracts\n", num);
1105         if (transaction_has_spare_capacity(transaction)) {
1106                 ret =  FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1107                 goto err;
1108         }
1109
1110         fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1111         if (!fcb_contracts) {
1112                 ret = errno;
1113                 goto err;
1114         }
1115         memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1116
1117         ret = prepare_fcb_contracts(fcb, fcb_contracts,
1118                                     user_contracts->_buffer, num);
1119         if (ret)
1120                 goto err_free_fcb_contracts;
1121
1122         res_array_init_array_field(&res_array);
1123         ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1124
1125         gsa_cust_for_each(res_array, &res_array, resource) {
1126                 reservation_list_init_head(&resource->rl);
1127                 resource->rl.length = 0;
1128                 for (i = 0; i < num; i++) {
1129                         fc = fcb_contracts[i];
1130                         get_fc_res_key(fc, &key);
1131                         if (res_key_cmp(&key, &resource->key) == 0) {
1132                                 reservation_list_insert(&resource->rl, fc);
1133                                 resource->rl.length++;
1134                         }
1135                 }
1136         }
1137         gsa_cust_for_each(res_array, &res_array, resource) {
1138                 ret = reserve_resource(resource);
1139                 if (ret) {
1140                         ul_logerr("Reservation failed\n");
1141                         goto err_cancel_reservation;
1142                 }
1143         }
1144         gsa_cust_for_each(res_array, &res_array, resource) {
1145                 ret = commit_resource(resource);
1146                 if (ret) {
1147                         ul_logerr("Commit failed\n");
1148                         goto err_cancel_reservation;
1149                 }
1150         }
1151         
1152         res_array_delete_all(&res_array);
1153         return 0;
1154 err_cancel_reservation:
1155         gsa_cust_for_each(res_array, &res_array, resource) {
1156                 cancel_reservations(resource);
1157         }
1158         res_array_delete_all(&res_array);
1159 err_free_fcb_contracts:
1160         free_fcb_contracts(fcb_contracts, num);
1161 err:
1162         return ret;
1163 }
1164
1165 CORBA_long
1166 wait_transaction(fres_contract_broker _obj,
1167                  const CORBA_char * name,
1168                  fres_transaction_t** transaction,
1169                  CORBA_Environment *ev)
1170 {
1171         return FRSH_ERR_NOT_IMPLEMENTED;
1172 }
1173
1174 CORBA_long
1175 allocate_transaction_vres(fres_contract_broker _obj,
1176                           const CORBA_long id,
1177                           CORBA_Environment *ev)
1178 {
1179         return FRSH_ERR_NOT_IMPLEMENTED;
1180 }
1181
1182
1183 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1184 static int register_inet_port(forb_orb orb)
1185 {
1186         forb_port_t *port = forb_malloc(sizeof(*port));
1187         int ret;
1188         struct in_addr listen_on;
1189         
1190         if (!port)
1191                 return -1;
1192         memset(port, 0, sizeof(*port));
1193         listen_on.s_addr = INADDR_ANY;
1194         ret = forb_inet_port_init(&port->desc, listen_on, 0);
1195         if (ret)
1196                 return ret;
1197         ret = forb_register_port(orb, port);
1198         return ret;
1199 }
1200 #endif
1201
1202 struct forb_fres_contract_broker_impl impl = {
1203         .negotiate_contracts = negotiate_contracts,
1204         .register_resource = register_resource,
1205         .register_allocator = register_allocator,
1206         .redistribute_spare_capacity = redistribute_spare_capacity,
1207         .get_resources = get_resources,
1208         .negotiate_transaction = negotiate_transaction,
1209         .wait_transaction = wait_transaction,
1210         .allocate_transaction_vres = allocate_transaction_vres,
1211 };
1212
1213 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1214 {
1215         forb_server_id server_id;
1216         char server_id_str[sizeof(forb_server_id)*2+1];
1217
1218         forb_get_server_id(peer_orb, &server_id);
1219         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1220 #if 0
1221         ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1222 #endif
1223
1224         if (orb_id == NULL)
1225                 return;
1226
1227         if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1228                 fosa_abs_time_t now;
1229                 fosa_rel_time_t delay;
1230                 fosa_clock_get_time(CLOCK_REALTIME, &now);
1231                 delay = fosa_abs_time_extract_interval(start_time, now);
1232                 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1233                           fosa_rel_time_to_msec(delay));
1234         }
1235 }
1236
1237 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1238 {
1239 }
1240
1241 static struct option long_opts[] = {
1242     { "daemon",   optional_argument, NULL, 'd' },
1243     { "loglevel", required_argument, NULL, 'l' },
1244     { 0, 0, 0, 0}
1245 };
1246
1247 static void
1248 usage(void)
1249 {
1250         printf("usage: fcb [ options ]\n");
1251         printf("  -d, --daemon [pid-file]   go to background after FORB initialization\n");
1252         printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
1253 }
1254
1255 int print_log_domain(ul_log_domain_t *domain, void *context)
1256 {
1257         printf("%s = %d\n", domain->name, domain->level);
1258         return 0;
1259 }
1260
1261 int main(int argc, char *argv[])
1262 {
1263         forb_orb orb;
1264         struct fcb fcb_data;
1265         fres_contract_broker fcb;
1266         forb_executor_t executor;
1267         int ret;
1268         forb_init_attr_t attr = {
1269                 .orb_id = "org.frescor.fcb",
1270                 .peer_discovery_callback = peer_discovery_callback,
1271                 .peer_dead_callback = peer_dead_callback,
1272                 .fixed_tcp_port = FCB_TCP_PORT,
1273 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT           
1274                 .fixed_server_id = FCB_SERVER_ID,
1275                 .redistribute_hellos = true,
1276 #endif
1277         };
1278         int  opt;
1279
1280         while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1281                 switch (opt) {
1282                         case 'l':
1283                                 if (*optarg == '?') {
1284                                         ul_logreg_for_each_domain(print_log_domain, NULL);
1285                                         exit(0);
1286                                 }
1287                                 {
1288                                         int ret;
1289                                         ret = ul_log_domain_arg2levels(optarg);
1290                                         if (ret) 
1291                                                 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1292                                 }
1293                                 break;
1294                         case 'd':
1295                                 opt_daemon = true;
1296                                 opt_pidfile = optarg;
1297                                 break;
1298                         case 'h':
1299                         /*default:*/
1300                                 usage();
1301                                 exit(opt == 'h' ? 0 : 1);
1302                 }
1303         }
1304
1305         memset(&fcb_data, 0, sizeof(fcb_data));
1306         fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1307
1308         if (opt_daemon)
1309                 forb_daemon_prepare(opt_pidfile);
1310
1311         orb = forb_init(&argc, &argv, &attr);
1312         if (!orb) error(1, errno, "FORB initialization failed");
1313
1314 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1315         ret = register_inet_port(orb);
1316         if (ret) error(0, errno, "INET port registration failed");
1317 #endif
1318
1319         fcb_resource_init_root_field(&fcb_data);
1320         fcb_contract_init_root_field(&fcb_data);
1321
1322         fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1323         if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1324
1325         /* Prepare executor before we register the fcb reference,
1326          * so that no reuqests are lost */
1327         ret = forb_executor_init(&executor);
1328         if (ret) error(1, errno, "forb_executor_init failed");
1329         
1330         ret = forb_executor_register_object(&executor, fcb);
1331         if (ret) error(1, errno, "forb_executor_register_object failed");
1332
1333         ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1334         if (ret) error(1, errno, "forb_register_reference() failed");
1335
1336         ul_logmsg("Waiting for requests\n");
1337         if (opt_daemon)
1338                 forb_daemon_ready();
1339
1340         ret = forb_executor_run(&executor);
1341         if (ret) error(1, errno, "forb_executor_run failed");
1342         
1343         return 0;
1344 }