]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/cbroker/fcb.c
FCB measures time elapsed time before other nodes are discovered
[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 #include <forb.h>
57 #include <fcb.h>
58 #include <error.h>
59 #include <errno.h>
60 #include <stdio.h>
61 #include <ul_gavlcust.h>
62 #include <string.h>
63 #include <ul_log.h>
64 #include <forb/server_id.h>
65 #include <fres_contract.h>
66 #include "fcb_config.h"
67 #include <fosa_clocks_and_timers.h>
68 #ifdef CONFIG_FCB_INET
69 #include <forb/proto_inet.h>
70 #endif
71
72 UL_LOG_CUST(ulogd_fcb);
73 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
74
75 fosa_abs_time_t start_time;
76
77
78 /**
79  * Resource identification 
80  */
81 struct res_key {
82         frsh_resource_type_t type;
83         frsh_resource_id_t id;
84 };
85
86 /**
87  * Registered resource
88  */
89 struct resource {
90         gavl_node_t node;
91         struct res_key key;
92         fres_resource_manager mng; /**< Object reference of the resource manager */
93         gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
94 };
95
96 /**
97  * Resource allocator registered in different nodes/applications 
98  */
99 struct res_alloc {
100         gavl_node_t node;
101         forb_server_id app;
102         fres_resource_allocator ra;
103 };
104
105 struct fcb_contract {
106         fres_contract_id_t id;
107         gavl_node_t node;
108         struct fres_contract *user_contract;
109 };
110
111 /**
112  * Contract broker data
113  */
114 struct fcb {
115         gavl_cust_root_field_t resources; /**< Registered resources */
116         gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
117 };
118
119 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
120 {
121         struct fcb_contract *fcb_contract;
122         
123         fcb_contract = malloc(sizeof(*fcb_contract));
124         if (!fcb_contract) {
125                 return NULL;
126         }
127         memset(fcb_contract, 0, sizeof(*fcb_contract));
128         fcb_contract->id = *id;
129
130         return fcb_contract;
131 }
132
133 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
134 {
135         if (fcb_contract->user_contract) {
136                 fres_contract_destroy(fcb_contract->user_contract);
137         }
138         free(fcb_contract);
139 }
140
141 static inline int res_key_cmp(const struct res_key *a,
142                               const struct res_key *b)
143 {
144         if (a->type < b->type) {
145                 return -1;
146         } else if (a->type > b->type) {
147                 return +1;
148         } else if (a->id < b->id) {
149                 return -1;
150         } else if (a->id > b->id) {
151                 return +1;
152         } else {
153                 return 0;
154         }
155 }
156
157 /* Container for registered resources */
158 GAVL_CUST_NODE_INT_DEC(fcb_resource   /* cust_prefix */,                \
159                        struct fcb     /* cust_root_t */,                \
160                        struct resource/* cust_item_t */,                \
161                        struct res_key /* cust_key_t */,                 \
162                        resources      /* cust_root_node */,             \
163                        node           /* cust_item_node */,             \
164                        key            /* cust_item_key */,              \
165                        res_key_cmp    /* cust_cmp_fnc */);
166
167 GAVL_CUST_NODE_INT_IMP(fcb_resource   /* cust_prefix */,                \
168                        struct fcb     /* cust_root_t */,                \
169                        struct resource/* cust_item_t */,                \
170                        struct res_key /* cust_key_t */,                 \
171                        resources      /* cust_root_node */,             \
172                        node           /* cust_item_node */,             \
173                        key            /* cust_item_key */,              \
174                        res_key_cmp    /* cust_cmp_fnc */);
175
176 /* Container for allocators registered for a given resource */
177 GAVL_CUST_NODE_INT_DEC(fcb_alloc        /* cust_prefix */,              \
178                        struct resource  /* cust_root_t */,              \
179                        struct res_alloc /* cust_item_t */,              \
180                        forb_server_id   /* cust_key_t */,               \
181                        allocators       /* cust_root_node */,           \
182                        node             /* cust_item_node */,           \
183                        app              /* cust_item_key */,            \
184                        fres_contract_id_cmp /* cust_cmp_fnc */);
185
186 GAVL_CUST_NODE_INT_IMP(fcb_alloc        /* cust_prefix */,              \
187                        struct resource   /* cust_root_t */,             \
188                        struct res_alloc /* cust_item_t */,              \
189                        forb_server_id   /* cust_key_t */,               \
190                        allocators       /* cust_root_node */,           \
191                        node             /* cust_item_node */,           \
192                        app              /* cust_item_key */,            \
193                        forb_server_id_cmp /* cust_cmp_fnc */);
194
195 /* Container for negotiated contracts */
196 GAVL_CUST_NODE_INT_DEC(fcb_contract         /* cust_prefix */,          \
197                        struct fcb           /* cust_root_t */,          \
198                        struct fcb_contract  /* cust_item_t */,          \
199                        fres_contract_id_t   /* cust_key_t */,           \
200                        contracts            /* cust_root_node */,       \
201                        node                 /* cust_item_node */,       \
202                        id                   /* cust_item_key */,        \
203                        fres_contract_id_cmp /* cust_cmp_fnc */);
204
205 #if 1
206 GAVL_CUST_NODE_INT_IMP(fcb_contract         /* cust_prefix */,          \
207                        struct fcb           /* cust_root_t */,          \
208                        struct fcb_contract  /* cust_item_t */,          \
209                        fres_contract_id_t   /* cust_key_t */,           \
210                        contracts            /* cust_root_node */,       \
211                        node                 /* cust_item_node */,       \
212                        id                   /* cust_item_key */,        \
213                        fres_contract_id_cmp /* cust_cmp_fnc */);
214 #else
215 #include "fcb_contract_gavl.inc"
216 #endif
217
218
219 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
220
221 struct res_key *
222 get_res_key(const struct fcb *fcb, const struct fres_contract *contract, struct res_key *key)
223 {
224         fres_block_resource *block_res;
225         
226         block_res = fres_contract_get_resource(contract);
227         if (!block_res && !fres_contract_id_is_empty(&contract->id)) {
228                 /* If the contract doesn't have resource information,
229                  * this might be cancelation or renegotiation request,
230                  * so look at our database for formerly submited
231                  * contract.  */
232                 struct fcb_contract *fcb_contract;
233                 fcb_contract = fcb_contract_find(fcb, &contract->id);
234                 if (fcb_contract) {
235                         block_res = fres_contract_get_resource(fcb_contract->user_contract);
236                 }
237         }
238         if (!block_res) {
239                 ul_logerr("No resource specified\n");
240                 return NULL;
241         } else {
242                 key->type = block_res->resource_type;
243                 key->id = block_res->resource_id;
244         }
245         return key;
246 }
247
248 /** 
249  * Checks whether all contracts refers to a single resource.
250  * 
251  * @param fcb 
252  * @param contracts Array of contract pointers.
253  * @param num Number of contracts.
254  * 
255  * @return If all contracts refer to a signle resource, pointer to the
256  * coresponding resource structure is returned. Otherwise, NULL is
257  * returned.
258  */
259 struct resource *
260 check_single_resource(struct fcb *fcb, struct fres_contract *contracts[], int num)
261 {
262         struct resource *resource = NULL;
263         unsigned i;
264         struct res_key key, key2 = {-1,-1};
265
266         for (i=0; i<num; i++) {
267                 if (!get_res_key(fcb, contracts[i], &key)) {
268                         return NULL;
269                 }
270                 if (i==0) key2 = key;
271                 else if (key.type != key2.type ||
272                          key.id   != key2.id) {
273                         return NULL;
274                 }
275         }
276         
277         resource = fcb_resource_find(fcb, &key);
278         if (!resource) {
279                 ul_logerr("No resource manager for %d.%d registered\n",
280                           key.type, key.id);
281         }
282         return resource;
283 }
284
285 /** 
286  * 
287  * 
288  * @param fcb 
289  * @param contracts 
290  * @param num 
291  * 
292  * @return Zero on success, non-zero error code on error.
293  */
294 static int
295 prepare_reservation_contracts(struct fcb *fcb, struct fres_contract *contracts[], int num)
296 {
297         unsigned i;
298         struct fcb_contract *fcb_contract;
299         
300         for (i=0; i<num; i++) {
301                 struct fres_contract *c = contracts[i];
302                 
303                 if (fres_contract_id_is_empty(&c->id)) {
304                         forb_uuid_generate((forb_uuid_t *)&c->id);
305                         continue;
306                 }
307                 if (fres_contract_get_num_blocks(c) == 0) {
308                         /* Nothing to do for deletion requesst */
309                         continue;
310                 }
311
312                 /* Renegotiation */
313                 fcb_contract = fcb_contract_find(fcb, &c->id);
314                 if (fcb_contract) {
315                         /* Copy missing blocks from fcb_contract to contract */
316                         fres_contract_merge(c, fcb_contract->user_contract);
317                 } else {
318                         char str[60];
319                         fres_contract_id_to_string(str, &c->id, sizeof(str));
320                         ul_logerr("Attempt to renegotiate unknown contract %s\n", str);
321                         return FRES_ERR_NOTHING_TO_RENEGOTIATE;
322                 }
323
324         }       
325         return 0;
326 }
327
328 CORBA_long
329 negotiate_contracts(fres_contract_broker obj,
330                     const fres_contract_ptr_seq* contracts,
331                     fres_contract_id_seq** ids_out,
332                     CORBA_Environment *ev)
333 {
334         struct fcb *fcb = o2fcb(obj);
335         struct resource *resource;
336         struct res_alloc *ra;
337         int ret;
338         forb_server_id app;
339         fres_contract_ptr_seq *schedulable_contracts;
340         struct fcb_contract **fcb_contracts;
341         unsigned i;
342         fres_contract_id_seq* ids;
343
344         resource = check_single_resource(fcb, contracts->_buffer, contracts->_length);
345         if (!resource) {
346                 ret = FRSH_ERR_RESOURCE_ID_INVALID;
347                 goto err;
348         }
349
350         ret = prepare_reservation_contracts(fcb, contracts->_buffer, contracts->_length);
351         if (ret)
352                 goto err;
353         
354         forb_get_req_source(obj, &app);
355         ra = fcb_alloc_find(resource, &app);
356         if (!ra) {
357                 char str[60];
358                 forb_server_id_to_string(str, &app, sizeof(str));
359                 ul_logerr("No resource allocator found for %d.%d and %s\n",
360                           resource->key.type, resource->key.id, str);
361                 ret = FRES_ERR_NO_RESOURCE_ALLOCATOR;
362                 goto err;
363         }
364
365         /* Allocate all the needed memory before doing reservation. If
366          * there is no enough memory, it has no sense to call resource
367          * manager. */
368         ids = malloc(sizeof(*ids));
369         if (!ids) {
370                 ev->major = FORB_EX_NO_MEMORY;
371                 goto err;
372         }
373         memset(ids, 0, sizeof(*ids));
374         CORBA_sequence_set_release(ids, CORBA_TRUE);
375         *ids_out = ids;
376         ids->_buffer = malloc(contracts->_length*sizeof(ids->_buffer[0]));
377         if (!ids->_buffer) {
378                 ret = errno;
379                 goto err;
380         }
381         ids->_length = ids->_maximum = contracts->_length;
382         for (i=0; i<contracts->_length; i++) {
383                 ids->_buffer[i] = contracts->_buffer[i]->id;
384         }
385         
386         fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
387         if (!fcb_contracts) {
388                 ret = errno;
389                 goto err;
390         }
391         memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
392
393         for (i=0; i<contracts->_length; i++) {
394                 struct fres_contract *c = contracts->_buffer[i];
395                 if (fres_contract_get_num_blocks(c) > 0) {
396                         fcb_contracts[i] = fcb_contract_new(&c->id);
397                         if (!fcb_contracts[i]) {
398                                 ret = errno ? errno : -1;
399                                 goto err_free_fcb_contracts;
400                         }
401                         fcb_contracts[i]->user_contract = fres_contract_duplicate(c);
402                         if (!fcb_contracts[i]->user_contract) {
403                                 ret = errno ? errno : -1;
404                                 goto err_free_fcb_contracts;
405                         }
406                 }
407         }
408
409         /* TODO: Optimize the following by introducing
410          * reserve_and_commit FRM method. */
411         
412         /* Reserve contract */
413         ret = fres_resource_manager_reserve_contracts(resource->mng, contracts, ev);
414         if (forb_exception_occurred(ev) || ret < 0) {
415                 goto err_free_fcb_contracts;
416         }
417         if (ret == 1) {
418                 ul_logmsg("Contract was not accepted\n");
419                 goto err_free_fcb_contracts;
420         }
421
422         /* Commit contract */
423         fres_resource_manager_commit_contracts(resource->mng, ids,
424                                                &schedulable_contracts, ev);
425         if (forb_exception_occurred(ev)) {
426                 ret = FRES_ERR_FORB_EXCEPTION;
427                 goto err_cancel_reservation;
428         }
429
430         /* Create VRes */
431         ret = fres_resource_allocator_change_vreses(ra->ra, schedulable_contracts, ev);
432         if (CORBA_sequence_get_release(schedulable_contracts)) {
433                 int i;
434                 for (i=0; i<schedulable_contracts->_length; i++) {
435                         fres_contract_destroy(schedulable_contracts->_buffer[i]);
436                 }
437                 forb_free(schedulable_contracts->_buffer);
438         }
439         forb_free(schedulable_contracts);
440         if (forb_exception_occurred(ev)) {
441                 ret = FRES_ERR_FORB_EXCEPTION;
442                 goto err_cancel_reservation;
443         }
444         if (ret != 0) {
445                 ul_logmsg("VRes was not created\n");
446                 goto err_cancel_reservation;
447         }
448
449         /* Update database of negotiated contracts stored for later reference */
450         for (i=0; i<contracts->_length; i++) {
451                 struct fcb_contract *fcb_contract;
452                 fcb_contract = fcb_contract_find(fcb, &contracts->_buffer[i]->id);
453                 /* Delete canceled or renegotiated user contract */
454                 if (fcb_contract) {
455                         fcb_contract_delete(fcb, fcb_contract);
456                         fcb_contract_destroy(fcb_contract);
457                 }
458                 if (fcb_contracts[i]) {
459                         /* Insert new contracts */
460                         fcb_contract_insert(fcb, fcb_contracts[i]);
461                 }
462         }
463
464         return 0;
465
466 err_cancel_reservation:
467         fres_resource_manager_cancel_reservations(resource->mng, ids, ev);
468 err_free_fcb_contracts:
469         for (i=0; i<contracts->_length; i++) 
470                 fcb_contract_destroy(fcb_contracts[i]);
471         free(fcb_contracts);
472 err:
473         return ret;
474 }
475
476 CORBA_long register_resource(fres_contract_broker obj,
477                             const frsh_resource_type_t restype,
478                             const frsh_resource_id_t resid,
479                             const fres_resource_desc *desc,
480                             CORBA_Environment *ev)
481 {
482         struct fcb *fcb = o2fcb(obj);
483         struct resource *res, *res2;
484
485         res = malloc(sizeof(*res));
486         if (!res) goto err;
487         memset(res, 0, sizeof(*res));
488         res->key.type = restype;
489         res->key.id = resid;
490         res2 = fcb_resource_find(fcb, &res->key);
491         if (res2) {
492                 if (forb_object_is_stale(res2->mng)) {
493                         ul_logmsg("Removing stale manager for resource %d.%d\n",
494                                   restype, resid);
495                         forb_object_release(res2->mng);
496                         fcb_resource_delete(fcb, res2);
497                         /* TODO: Delete also all allocators associated
498                          * with this stale resource manager. */
499                         free(res);
500                         res = res2;
501                 } else {
502                         ul_logerr("Resource manager %d.%d already registered\n",
503                                   restype, resid);
504                         goto free_err;
505                 }
506         }
507         res->mng = forb_object_duplicate(desc->manager);
508
509         fcb_alloc_init_root_field(res);
510         ul_logmsg("Registering manager for resource %d.%d\n",
511                   restype, resid);
512         fcb_resource_insert(fcb, res);
513         return 0;
514 free_err:
515         free(res);
516 err:
517         return -1;
518 }
519
520
521 CORBA_long register_allocator(fres_contract_broker obj,
522                               const frsh_resource_type_t restype,
523                               const frsh_resource_id_t resid,
524                               const fres_resource_allocator ra_obj,
525                               CORBA_Environment *ev)
526 {
527         struct fcb *fcb = o2fcb(obj);
528         struct resource *res;
529         struct res_alloc *ra;
530         struct res_key resource;
531         forb_server_id server_id;
532         char server_id_str[40];
533
534         forb_get_server_id(ra_obj, &server_id);
535         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
536         ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
537                   restype, resid, server_id_str);
538         
539         resource.type = restype;
540         resource.id = resid;
541         res = fcb_resource_find(fcb, &resource);
542         if (!res) {
543                 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
544                           restype, resid);
545                 goto err;
546         }
547         ra = fcb_alloc_find(res, &server_id);
548         if (ra) {
549                 char *str = forb_object_to_string(ra_obj);
550                 ul_logerr("Allocator from already registered (%s)\n",
551                           str);
552                 forb_free(str);
553                 goto err;
554         }
555         ra = malloc(sizeof(*ra));
556         if (!ra) {
557                 goto err;
558         }
559         ra->app = server_id;
560         ra->ra = forb_object_duplicate(ra_obj);
561         fcb_alloc_insert(res, ra);
562         return 0;
563 err:
564         return -1;      
565 }
566
567 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
568 {
569         struct fcb *fcb = o2fcb(obj);
570         fres_resource_seq *seq;
571         struct resource *res;
572         int n;
573
574         seq = malloc(sizeof(*seq));
575         if (!seq) {
576                 ev->major = FORB_EX_NO_MEMORY;
577                 return;
578         }
579         memset(seq, 0, sizeof(*seq));
580
581         n=0;
582         gavl_cust_for_each(fcb_resource, fcb, res) {
583                 n++;
584         }
585
586         seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
587         seq->_maximum = n;
588         seq->_length = n;
589
590         n = 0;
591         gavl_cust_for_each(fcb_resource, fcb, res) {
592                 seq->_buffer[n].restype = res->key.type;
593                 seq->_buffer[n].resid = res->key.id;
594                 seq->_buffer[n].desc.manager = res->mng;
595                 n++;
596         }
597
598         *resources = seq;
599 }
600
601 #ifdef CONFIG_FCB_INET
602 static int register_inet_port(forb_orb orb)
603 {
604         forb_port_t *port = forb_malloc(sizeof(*port));
605         int ret;
606         struct in_addr listen_on;
607         
608         if (!port)
609                 return -1;
610         memset(port, 0, sizeof(*port));
611         listen_on.s_addr = INADDR_ANY;
612         ret = forb_inet_port_init(&port->desc, listen_on);
613         if (ret)
614                 return ret;
615         ret = forb_register_port(orb, port);
616         return ret;
617 }
618 #endif
619
620 struct forb_fres_contract_broker_impl impl = {
621         .negotiate_contracts = negotiate_contracts,
622         .register_resource = register_resource,
623         .register_allocator = register_allocator,
624         .get_resources = get_resources,
625 };
626
627 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
628 {
629         forb_server_id server_id;
630         char server_id_str[sizeof(forb_server_id)*2+1];
631
632         forb_get_server_id(peer_orb, &server_id);
633         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
634 #if 0
635         ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
636 #endif
637         if (strcmp(orb_id, "org.frescor.fcb") == 0) {
638                 fosa_abs_time_t now;
639                 fosa_rel_time_t delay;
640                 fosa_clock_get_time(CLOCK_REALTIME, &now);
641                 delay = fosa_abs_time_extract_interval(start_time, now);
642                 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
643                           fosa_rel_time_to_msec(delay));
644         }
645 }
646
647 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
648 {
649 }
650
651 int main(int argc, char *argv[])
652 {
653         forb_orb orb;
654         struct fcb fcb_data;
655         fres_contract_broker fcb;
656         forb_executor_t executor;
657         int ret;
658         forb_init_attr_t attr = {
659                 .orb_id = "org.frescor.fcb",
660                 .peer_discovery_callback = peer_discovery_callback,
661                 .peer_dead_callback = peer_dead_callback,
662         };
663
664
665         fosa_clock_get_time(CLOCK_REALTIME, &start_time);
666
667         orb = forb_init(&argc, &argv, &attr);
668         if (!orb) error(1, errno, "FORB initialization failed");
669
670 #ifdef CONFIG_FCB_INET
671         ret = register_inet_port(orb);
672         if (ret) error(0, errno, "INET port registration failed");
673 #endif
674
675         fcb_resource_init_root_field(&fcb_data);
676         fcb_contract_init_root_field(&fcb_data);
677
678         fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
679         if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
680
681         /* Prepare executor before we register the fcb reference,
682          * so that no reuqests are lost */
683         ret = forb_executor_init(&executor);
684         if (ret) error(1, errno, "forb_executor_init failed");
685         
686         ret = forb_executor_register_object(&executor, fcb);
687         if (ret) error(1, errno, "forb_executor_register_object failed");
688
689         ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
690         if (ret) error(1, errno, "forb_register_reference() failed");
691
692         ul_logmsg("Waiting for requests\n");
693         ret = forb_executor_run(&executor);
694         if (ret) error(1, errno, "forb_executor_run failed");
695         
696         return 0;
697 }