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