]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/cbroker/fcb.c
26aea51cfb8e1e3aa838171780a902e2fc90d6da
[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         
764         return 0;
765 err_free:
766         forb_sequence_free_buf(&commit_ids, forb_no_destructor);
767 err:
768         return ret;
769 }
770
771 /* Add new contracts to our fcb database for later
772  * reference. Canceled contracts are removed below. */
773 int
774 fcb_remember_contracts(struct fcb *fcb,
775                        struct fcb_contract **fcb_contracts,
776                        int num)
777 {
778         struct fcb_contract *fc;
779         int i;
780         
781         for (i=0; i<num; i++) {
782                 fc =  fcb_contracts[i];
783
784                 if (fc->user_contract) {
785                         if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
786                                 /* Renegotiation */
787                                 fres_contract_destroy(fc->user_contract);
788                                 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
789                                 /* Note: requested_contract is also
790                                  * pointed by contracts parameter and
791                                  * will be freed by FORB. */
792                                 fc->requested_contract = NULL;
793                         }
794                 } else {
795                         /* Insert new contracts */
796                         fcb_contract_insert(fcb, fcb_contracts[i]);
797                         fc->user_contract = fres_contract_duplicate(fc->requested_contract);
798                         fc->requested_contract = NULL;
799                         /* See the note above. */
800                 }
801         }
802         return 0;
803 }
804
805 CORBA_long
806 negotiate_contracts(fres_contract_broker obj,
807                     const fres_contract_ptr_seq* contracts,
808                     fres_contract_id_seq** ids_out,
809                     CORBA_Environment *ev)
810 {
811         struct fcb *fcb = o2fcb(obj);
812         struct resource *resource = NULL;
813         int ret = 0;
814         forb_server_id app;
815         fres_contract_ptr_seq *schedulable_contracts;
816         struct fcb_contract **fcb_contracts;
817         unsigned i;
818         int num = contracts->_length;
819
820         /* Prepare output sequence for the case we return eariler with
821          * an error */
822         forb_sequence_alloc(*ids_out, 0);
823         if (!*ids_out) {
824                 ev->major = FORB_EX_NO_MEMORY;
825                 goto err;
826         }
827         CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
828         
829         forb_get_req_source(obj, &app);
830         
831         fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
832         if (!fcb_contracts) {
833                 ret = errno;
834                 goto err;
835         }
836         memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
837
838         ret = prepare_fcb_contracts(fcb, fcb_contracts,
839                                     contracts->_buffer, num);
840         if (ret)
841                 goto err_free_fcb_contracts;
842         ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
843                                        &app, num);
844         if (ret)
845                 goto err_free_fcb_contracts;
846
847         prepare_reservation_list(resource, fcb_contracts, num);
848
849         /* Reserve contracts */
850         ret = rebalance_spare_capacity_and_reserve(resource);
851         if (ret) {
852                 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
853                         ul_logmsg("Contract(s) was/were rejected\n");
854                 } else {
855                         char msg[100];
856                         fres_strerror(ret, msg, sizeof(msg));
857                         ul_logerr("Reservation error: %s\n", msg);
858                 }
859                 goto err_free_fcb_contracts;
860         }
861
862         /* Commit contracts */
863         ret = commit_resource(resource, &schedulable_contracts);
864         if (ret)
865                 goto err_cancel_reservation;
866
867         fcb_remember_contracts(fcb, fcb_contracts, num);
868
869         ret = change_vreses(fcb, schedulable_contracts);
870         if (ret)
871                 goto err_cancel_reservation;
872         
873         forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
874         if (ret != 0) {
875                 ul_logerr("VRes was not created\n");
876                 goto err_cancel_contracts;
877         }
878
879
880         /* Return IDs and delete canceled contracts from out database */
881         if (!forb_sequence_alloc_buf(*ids_out, num)) {
882                 ev->major = FORB_EX_NO_MEMORY;
883                 goto err_cancel_contracts;
884         }
885         (*ids_out)->_length = num;
886         for (i=0; i<num; i++) {
887                 struct fcb_contract *fc =  fcb_contracts[i];
888                 forb_sequence_elem(*ids_out, i) = fc->id;
889
890                 if (fc->requested_contract &&
891                     fres_contract_get_num_blocks(fc->requested_contract) == 0) {
892                         fcb_contract_delete(fcb, fc);
893                         /* Note: requested_contract is also pointed by
894                          * contracts parameter and will be freed by FORB. */
895                         fc->requested_contract = NULL;
896                         fcb_contract_destroy(fc);
897                 }
898         }
899         return 0;
900
901 err_cancel_contracts:
902         /* TODO */
903         goto err_free_fcb_contracts;
904 err_cancel_reservation:
905         cancel_reservations(resource);
906 err_free_fcb_contracts:
907         free_fcb_contracts(fcb_contracts, num);
908 err:
909         return ret;
910 }
911
912 void redistribute_spare_capacity(fres_contract_broker obj,
913                                  const frsh_resource_type_t restype,
914                                  const frsh_resource_id_t resid,
915                                  CORBA_Environment *ev)
916 {
917         struct fcb *fcb = o2fcb(obj);
918         struct res_key key = {restype, resid };
919         struct resource *resource;
920         
921         resource = fcb_resource_find(fcb, &key);
922
923         prepare_reservation_list(resource, NULL, 0);
924
925 /*      forb_sequence_alloc(ids, rl.length); */
926 /*      if (!ids || !ids->_buffer) { */
927 /*              ev->major = FORB_EX_NO_MEMORY; */
928 /*              goto err_free_fcb_contracts; */
929 /*      } */
930 /*      CORBA_sequence_set_release(ids, CORBA_TRUE); */
931 /*      *ids_out = ids;         /\* ids is freed by FORB *\/ */
932         
933         
934         rebalance_spare_capacity_and_reserve(resource);
935         /* Commit */
936 }
937
938 CORBA_long register_resource(fres_contract_broker obj,
939                             const frsh_resource_type_t restype,
940                             const frsh_resource_id_t resid,
941                             const fres_resource_desc *desc,
942                             CORBA_Environment *ev)
943 {
944         struct fcb *fcb = o2fcb(obj);
945         struct resource *res, *res2;
946
947         res = malloc(sizeof(*res));
948         if (!res) goto err;
949         memset(res, 0, sizeof(*res));
950         res->key.type = restype;
951         res->key.id = resid;
952         res2 = fcb_resource_find(fcb, &res->key);
953         if (res2) {
954                 if (forb_object_is_stale(res2->mng)) {
955                         ul_logmsg("Removing stale manager for resource %d.%d\n",
956                                   restype, resid);
957                         forb_object_release(res2->mng);
958                         fcb_resource_delete(fcb, res2);
959                         /* TODO: Delete also all allocators associated
960                          * with this stale resource manager. */
961                         free(res);
962                         res = res2;
963                 } else {
964                         ul_logerr("Resource manager %d.%d already registered\n",
965                                   restype, resid);
966                         goto free_err;
967                 }
968         }
969         res->mng = forb_object_duplicate(desc->manager);
970         res->name = desc->name;
971
972         fcb_alloc_init_root_field(res);
973         sc_contracts_init_head(res);
974         ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
975                   res->name, restype, resid);
976         fcb_resource_insert(fcb, res);
977         return 0;
978 free_err:
979         free(res);
980 err:
981         return -1;
982 }
983
984
985 CORBA_long register_allocator(fres_contract_broker obj,
986                               const frsh_resource_type_t restype,
987                               const frsh_resource_id_t resid,
988                               const fres_resource_allocator ra_obj,
989                               CORBA_Environment *ev)
990 {
991         struct fcb *fcb = o2fcb(obj);
992         struct resource *res;
993         struct res_alloc *ra;
994         struct res_key resource;
995         forb_server_id server_id;
996         char server_id_str[40];
997         int ret;
998
999         forb_get_server_id(ra_obj, &server_id);
1000         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1001         ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
1002                   restype, resid, server_id_str);
1003         
1004         resource.type = restype;
1005         resource.id = resid;
1006         res = fcb_resource_find(fcb, &resource);
1007         if (!res) {
1008                 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
1009                           restype, resid);
1010                 ret = FRES_ERR_NO_RESOURCE_MANAGER;
1011                 goto err;
1012         }
1013         ra = fcb_alloc_find(res, &server_id);
1014         if (ra) {
1015                 char *str = forb_object_to_string(ra_obj);
1016                 ul_logerr("Allocator from already registered (%s)\n",
1017                           str);
1018                 forb_free(str);
1019                 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
1020                 goto err;
1021         }
1022         ra = malloc(sizeof(*ra));
1023         if (!ra) {
1024                 ret = ENOMEM;
1025                 goto err;
1026         }
1027         ra->app = server_id;
1028         ra->ra = forb_object_duplicate(ra_obj);
1029         fcb_alloc_insert(res, ra);
1030         return 0;
1031 err:
1032         return ret;
1033 }
1034
1035 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1036 {
1037         struct fcb *fcb = o2fcb(obj);
1038         fres_resource_seq *seq;
1039         struct resource *res;
1040         int n;
1041
1042         seq = malloc(sizeof(*seq));
1043         if (!seq) {
1044                 ev->major = FORB_EX_NO_MEMORY;
1045                 return;
1046         }
1047         memset(seq, 0, sizeof(*seq));
1048
1049         n=0;
1050         gavl_cust_for_each(fcb_resource, fcb, res) {
1051                 n++;
1052         }
1053
1054         seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1055         seq->_maximum = n;
1056         seq->_length = n;
1057
1058         n = 0;
1059         gavl_cust_for_each(fcb_resource, fcb, res) {
1060                 seq->_buffer[n].restype = res->key.type;
1061                 seq->_buffer[n].resid = res->key.id;
1062                 seq->_buffer[n].desc.manager = res->mng;
1063                 seq->_buffer[n].desc.name = res->name;
1064                 n++;
1065         }
1066
1067         *resources = seq;
1068 }
1069
1070 static bool
1071 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1072 {
1073         struct fres_contract **c;
1074         forb_sequence_foreach(&transaction->contracts, c)
1075                 if (fres_contract_get_spare_capacity(*c))
1076                         return true;
1077         return false;
1078 }
1079
1080 static int
1081 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1082                           int num, struct res_array *res_array)
1083 {
1084         int i;
1085         struct resource *resource;
1086         struct res_key key;
1087         int ret;
1088
1089         for (i = 0; i < num; i++) {
1090                 get_fc_res_key(fc[i], &key);
1091                 resource = fcb_resource_find(fcb, &key);
1092                 ret = res_array_insert(res_array, resource);
1093                 if (ret == -1) {
1094                         ret = FRSH_ERR_INTERNAL_ERROR;
1095                         goto err;
1096                 }
1097         }
1098         ret = 0;
1099 err:
1100         return ret;
1101 }
1102                                  
1103 CORBA_long
1104 negotiate_transaction(fres_contract_broker _obj,
1105                       const fres_transaction_t* transaction,
1106                       CORBA_Environment *ev)
1107 {
1108         struct fcb *fcb = o2fcb(_obj);
1109         struct fcb_contract **fcb_contracts, *fc;
1110         const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1111         int i, ret, num = user_contracts->_length;
1112         struct res_array res_array;
1113         struct res_key key;
1114         struct resource *resource;
1115         fres_contract_ptr_seq *schedulable_contracts;
1116
1117         ul_logmsg("Negotiating transaction with %d contracts\n", num);
1118         if (transaction_has_spare_capacity(transaction)) {
1119                 ret =  FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1120                 goto err;
1121         }
1122
1123         fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1124         if (!fcb_contracts) {
1125                 ret = errno;
1126                 goto err;
1127         }
1128         memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1129
1130         ret = prepare_fcb_contracts(fcb, fcb_contracts,
1131                                     user_contracts->_buffer, num);
1132         if (ret)
1133                 goto err_free_fcb_contracts;
1134
1135         res_array_init_array_field(&res_array);
1136         ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1137
1138         gsa_cust_for_each(res_array, &res_array, resource) {
1139                 reservation_list_init_head(&resource->rl);
1140                 resource->rl.length = 0;
1141                 for (i = 0; i < num; i++) {
1142                         fc = fcb_contracts[i];
1143                         get_fc_res_key(fc, &key);
1144                         if (res_key_cmp(&key, &resource->key) == 0) {
1145                                 reservation_list_insert(&resource->rl, fc);
1146                                 resource->rl.length++;
1147                         }
1148                 }
1149         }
1150         gsa_cust_for_each(res_array, &res_array, resource) {
1151                 ret = reserve_resource(resource);
1152                 if (ret) {
1153                         ul_logerr("Reservation failed\n");
1154                         goto err_cancel_reservation;
1155                 }
1156         }
1157         gsa_cust_for_each(res_array, &res_array, resource) {
1158                 ret = commit_resource(resource, &schedulable_contracts);
1159                 if (ret) {
1160                         ul_logerr("Commit failed\n");
1161                         goto err_cancel_reservation;
1162                 }
1163         }
1164         fcb_remember_contracts(fcb, fcb_contracts, num);
1165         forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
1166         
1167         res_array_delete_all(&res_array);
1168         return 0;
1169 err_cancel_reservation:
1170         gsa_cust_for_each(res_array, &res_array, resource) {
1171                 cancel_reservations(resource);
1172         }
1173         res_array_delete_all(&res_array);
1174 err_free_fcb_contracts:
1175         free_fcb_contracts(fcb_contracts, num);
1176 err:
1177         return ret;
1178 }
1179
1180 CORBA_long
1181 wait_transaction(fres_contract_broker _obj,
1182                  const CORBA_char * name,
1183                  fres_transaction_t** transaction,
1184                  CORBA_Environment *ev)
1185 {
1186         return FRSH_ERR_NOT_IMPLEMENTED;
1187 }
1188
1189 CORBA_long
1190 allocate_transaction_vres(fres_contract_broker _obj,
1191                           const CORBA_long id,
1192                           CORBA_Environment *ev)
1193 {
1194         return FRSH_ERR_NOT_IMPLEMENTED;
1195 }
1196
1197
1198 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1199 static int register_inet_port(forb_orb orb)
1200 {
1201         forb_port_t *port = forb_malloc(sizeof(*port));
1202         int ret;
1203         struct in_addr listen_on;
1204         
1205         if (!port)
1206                 return -1;
1207         memset(port, 0, sizeof(*port));
1208         listen_on.s_addr = INADDR_ANY;
1209         ret = forb_inet_port_init(&port->desc, listen_on, 0);
1210         if (ret)
1211                 return ret;
1212         ret = forb_register_port(orb, port);
1213         return ret;
1214 }
1215 #endif
1216
1217 struct forb_fres_contract_broker_impl impl = {
1218         .negotiate_contracts = negotiate_contracts,
1219         .register_resource = register_resource,
1220         .register_allocator = register_allocator,
1221         .redistribute_spare_capacity = redistribute_spare_capacity,
1222         .get_resources = get_resources,
1223         .negotiate_transaction = negotiate_transaction,
1224         .wait_transaction = wait_transaction,
1225         .allocate_transaction_vres = allocate_transaction_vres,
1226 };
1227
1228 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1229 {
1230         forb_server_id server_id;
1231         char server_id_str[sizeof(forb_server_id)*2+1];
1232
1233         forb_get_server_id(peer_orb, &server_id);
1234         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1235 #if 0
1236         ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1237 #endif
1238
1239         if (orb_id == NULL)
1240                 return;
1241
1242         if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1243                 fosa_abs_time_t now;
1244                 fosa_rel_time_t delay;
1245                 fosa_clock_get_time(CLOCK_REALTIME, &now);
1246                 delay = fosa_abs_time_extract_interval(start_time, now);
1247                 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1248                           fosa_rel_time_to_msec(delay));
1249         }
1250 }
1251
1252 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1253 {
1254 }
1255
1256 static struct option long_opts[] = {
1257     { "daemon",   optional_argument, NULL, 'd' },
1258     { "loglevel", required_argument, NULL, 'l' },
1259     { 0, 0, 0, 0}
1260 };
1261
1262 static void
1263 usage(void)
1264 {
1265         printf("usage: fcb [ options ]\n");
1266         printf("  -d, --daemon [pid-file]   go to background after FORB initialization\n");
1267         printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
1268 }
1269
1270 int print_log_domain(ul_log_domain_t *domain, void *context)
1271 {
1272         printf("%s = %d\n", domain->name, domain->level);
1273         return 0;
1274 }
1275
1276 int main(int argc, char *argv[])
1277 {
1278         forb_orb orb;
1279         struct fcb fcb_data;
1280         fres_contract_broker fcb;
1281         forb_executor_t executor;
1282         int ret;
1283         forb_init_attr_t attr = {
1284                 .orb_id = "org.frescor.fcb",
1285                 .peer_discovery_callback = peer_discovery_callback,
1286                 .peer_dead_callback = peer_dead_callback,
1287                 .fixed_tcp_port = FCB_TCP_PORT,
1288 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT           
1289                 .fixed_server_id = FCB_SERVER_ID,
1290                 .redistribute_hellos = true,
1291 #endif
1292         };
1293         int  opt;
1294
1295         while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1296                 switch (opt) {
1297                         case 'l':
1298                                 if (*optarg == '?') {
1299                                         ul_logreg_for_each_domain(print_log_domain, NULL);
1300                                         exit(0);
1301                                 }
1302                                 {
1303                                         int ret;
1304                                         ret = ul_log_domain_arg2levels(optarg);
1305                                         if (ret) 
1306                                                 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1307                                 }
1308                                 break;
1309                         case 'd':
1310                                 opt_daemon = true;
1311                                 opt_pidfile = optarg;
1312                                 break;
1313                         case 'h':
1314                         /*default:*/
1315                                 usage();
1316                                 exit(opt == 'h' ? 0 : 1);
1317                 }
1318         }
1319
1320         memset(&fcb_data, 0, sizeof(fcb_data));
1321         fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1322
1323         if (opt_daemon)
1324                 forb_daemon_prepare(opt_pidfile);
1325
1326         orb = forb_init(&argc, &argv, &attr);
1327         if (!orb) error(1, errno, "FORB initialization failed");
1328
1329 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1330         ret = register_inet_port(orb);
1331         if (ret) error(0, errno, "INET port registration failed");
1332 #endif
1333
1334         fcb_resource_init_root_field(&fcb_data);
1335         fcb_contract_init_root_field(&fcb_data);
1336
1337         fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1338         if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1339
1340         /* Prepare executor before we register the fcb reference,
1341          * so that no reuqests are lost */
1342         ret = forb_executor_init(&executor);
1343         if (ret) error(1, errno, "forb_executor_init failed");
1344         
1345         ret = forb_executor_register_object(&executor, fcb);
1346         if (ret) error(1, errno, "forb_executor_register_object failed");
1347
1348         ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1349         if (ret) error(1, errno, "forb_register_reference() failed");
1350
1351         ul_logmsg("Waiting for requests\n");
1352         if (opt_daemon)
1353                 forb_daemon_ready();
1354
1355         ret = forb_executor_run(&executor);
1356         if (ret) error(1, errno, "forb_executor_run failed");
1357         
1358         return 0;
1359 }