]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/iop.c
95c0443f54fd485070a092b1e39d3ec14910f056
[frescor/forb.git] / src / iop.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 FORB (Frescor Object Request Broker)             */
26 /*                                                                        */
27 /* FORB 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.  FORB 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 FORB; 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 FORB header files in a file,         */
39 /* instantiating FORB generics or templates, or linking other files       */
40 /* with FORB 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   iop.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Mon Sep  1 21:51:58 2008
51  * 
52  * @brief  Implementation of Inter-ORB protocol.
53  * 
54  * 
55  */
56 #include "iop.h"
57 #include <forb/cdr.h>
58 #include <forb/object.h>
59 #include "proto.h"
60 #include <ul_log.h>
61 #include "exec_req.h"
62 #include <errno.h>
63 #include "peer.h"
64 #include "discovery.h"
65
66 /** Version of the protocol */
67 #define VER_MAJOR 0
68 #define VER_MINOR 0
69
70 #define VER(major, minor) ((major)<<8 || (minor))
71
72 extern UL_LOG_CUST(ulogd_forb_iop);
73
74
75 CORBA_boolean
76 forb_iop_prepend_message_header(FORB_CDR_Codec *codec, forb_iop_message_type mt)
77 {
78         CORBA_boolean ret;
79         forb_iop_message_header mh;
80         mh.proto_version.major = VER_MAJOR;
81         mh.proto_version.minor = VER_MINOR;
82         mh.message_type = mt;
83         mh.flags = (codec->data_endian == LittleEndian) ? forb_iop_LITTLE_ENDIAN : 0;
84         mh.message_size = FORB_CDR_data_size(codec);
85         ret = FORB_CDR_buffer_prepend(codec, forb_iop_MESSAGE_HEADER_SIZE);
86         if (ret) {
87                 ret = forb_iop_message_header_serialize(codec, &mh);
88         }
89         return ret;
90 }
91
92 CORBA_boolean
93 forb_iop_prepare_request(forb_request_t *req,
94                          char *iface,
95                          unsigned method_ind,
96                          CORBA_Environment *env)
97 {
98         CORBA_boolean ret;
99         forb_iop_request_header rh;
100
101         rh.request_id = req->request_id;
102         rh.iface = iface;
103         rh.objkey = forb_object_to_key(req->obj);
104         rh.method_index = method_ind;
105         rh.source = forb_object_to_forb(req->obj)->server_id;
106         ret = forb_iop_request_header_serialize(&req->cdr_request, &rh);
107         if (ret) {
108                 /* Request body is 8 byte aligned */
109                 ret = FORB_CDR_put_align(&req->cdr_request, 8);
110                 char str[50];
111                 ul_logdeb("preparing request: id=%d  dest=%s  iface=%s method=%d\n", req->request_id,
112                           forb_server_id_to_string(str, &req->obj->server, sizeof(str)),
113                           iface, method_ind);
114         }
115         return ret;
116 }
117
118 static CORBA_boolean
119 forb_iop_prepare_hello(FORB_CDR_Codec *codec,
120                        const forb_server_id *server_id,
121                        const void *src_addr,
122                        CORBA_boolean (*serialize_addr)(FORB_CDR_Codec *codec, const void *addr),
123                        const CORBA_char *orb_id)
124 {
125         if (!forb_server_id_serialize(codec, server_id)) return CORBA_FALSE;
126         if (serialize_addr) {
127                 if (!serialize_addr(codec, src_addr)) return CORBA_FALSE;
128         }
129         if (!CORBA_string_serialize(codec, &orb_id)) return CORBA_FALSE;
130         if (!forb_iop_prepend_message_header(codec, forb_iop_HELLO)) return CORBA_FALSE;
131         return CORBA_TRUE;
132 }
133
134 int forb_iop_send_hello_to(forb_peer_t *peer)
135 {
136         forb_port_t *port = peer->port;
137         FORB_CDR_Codec codec;
138         int ret;
139         FORB_CDR_codec_init_static(&codec, port->forb->orb);
140         if (!FORB_CDR_buffer_init(&codec, 1024, 0))
141                 return -1;              
142         if (!FORB_CDR_buffer_reset(&codec, forb_iop_MESSAGE_HEADER_SIZE)) {
143                 ret = -1;
144                 goto free;
145         }
146         if (!forb_iop_prepare_hello(&codec, &port->forb->server_id,
147                                     port->desc.addr,
148                                     port->desc.proto->serialize_addr,
149                                     port->forb->attr.orb_id)) {
150                 ret = -1;
151                 goto free;
152         }
153         ret = forb_proto_send(peer, &codec);
154         if (ret > 0) ret = 0;
155 free:
156         FORB_CDR_codec_release_buffer(&codec);
157         return ret;
158 }
159
160 bool
161 forb_iop_process_message_header(forb_iop_message_header *mh, FORB_CDR_Codec *codec)
162 {
163         /* FIXME: If we have multiple protocol versions, use different
164          * type (independent from version) for return value instead of
165          * mh */
166         forb_iop_version_deserialize(codec, &mh->proto_version);
167         switch (VER(mh->proto_version.major, mh->proto_version.minor)) {
168                 case VER(VER_MAJOR, VER_MINOR):
169                         forb_iop_message_type_deserialize(codec, &mh->message_type);
170                         forb_iop_message_flags_deserialize(codec, &mh->flags);
171                         /* Check whwther the type is meaningfull */
172                         switch (mh->message_type) {
173                                 case forb_iop_REQUEST:
174                                 case forb_iop_REPLY:
175                                 case forb_iop_HELLO:
176                                         break;
177                                 default:
178                                         ul_logerr("rcvd wrong message type: %d\n",
179                                                   mh->message_type);
180                                         return false;
181                         }
182                         codec->data_endian = (mh->flags && forb_iop_LITTLE_ENDIAN) ?
183                                 LittleEndian : BigEndian;                       
184                         CORBA_unsigned_long_deserialize(codec, &mh->message_size);
185                         return true;
186                         break;                  
187                 default:
188                         ul_logerr("rcvd wrong protocol versio: %d.%d\n",
189                                   mh->proto_version.major, mh->proto_version.minor);
190                         return false;
191         }
192 }
193
194 static inline CORBA_boolean
195 forb_exception_serialize(FORB_CDR_Codec *codec, struct forb_env *env)
196 {
197         return CORBA_long_serialize(codec, &env->major);
198 }
199
200 static inline CORBA_boolean
201 forb_exception_deserialize(FORB_CDR_Codec *codec, struct forb_env *env)
202 {
203         /* TODO: Declare exceptions in IDL and don't typecast here. */
204         return CORBA_long_deserialize(codec, (CORBA_long*)&env->major);
205 }
206
207 void
208 forb_iop_send_reply(forb_t *forb,
209            forb_server_id *dest,
210            FORB_CDR_Codec *codec,
211            CORBA_long request_id,
212            struct forb_env *env)
213 {
214         forb_iop_reply_header reply_header;
215         CORBA_boolean ret;
216         forb_peer_t *peer;
217         fosa_abs_time_t timeout;
218         
219         reply_header.request_id = request_id;
220         reply_header.flags = 0;
221         if (forb_exception_occurred(env)) {
222                 reply_header.flags |= forb_iop_FLAG_EXCEPTION;
223                 FORB_CDR_buffer_reset(codec, forb_iop_MESSAGE_HEADER_SIZE +
224                                              forb_iop_REPLY_HEADER_SIZE);
225                 forb_exception_serialize(codec, env);
226         }
227         /* forb_iop_REPLY_HEADER_SIZE equals to 8 even if the real
228          * header is shorter. We want reply data to be 8 byte
229          * aligned */
230         ret = FORB_CDR_buffer_prepend(codec, forb_iop_REPLY_HEADER_SIZE);
231         if (!ret) {
232                 goto err;
233         }
234         ret = forb_iop_reply_header_serialize(codec, &reply_header);
235         if (!ret) {
236                 goto err;
237         }
238         forb_iop_prepend_message_header(codec, forb_iop_REPLY);
239
240         fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
241         timeout = fosa_abs_time_incr(timeout,
242                                      fosa_msec_to_rel_time(1000));
243
244         peer = forb_get_next_hop(forb, dest, &timeout);
245         if (!peer) {
246                 char str[60];
247                 forb_server_id_to_string(str, dest, sizeof(str));
248                 ul_logerr("Reply destination not found: %s\n", str);
249                 goto err;
250         } else {
251                 char str[60];
252                 forb_server_id_to_string(str, dest, sizeof(str));
253                 ul_logdeb("sending reply: dest=%s, id=%u\n", str, request_id);
254         }
255         forb_proto_send(peer, codec);
256         forb_peer_put(peer);
257 err:
258         ;
259 }
260
261 static void
262 process_request(forb_port_t *port, FORB_CDR_Codec *codec, uint32_t message_size)
263 {
264         forb_iop_request_header request_header;
265         CORBA_boolean ret;
266         forb_object obj;
267         size_t n;
268         forb_t *forb = port->forb;
269         struct forb_env env;
270         FORB_CDR_Codec reply_codec;
271         forb_exec_req_t *exec_req;
272         uint32_t req_size, data_size, header_size;
273         char str[32];
274         forb_peer_t *peer;
275
276         data_size = FORB_CDR_data_size(codec);
277         ret = forb_iop_request_header_deserialize(codec, &request_header);
278         if (!ret) {
279                 ul_logerr("Malformed request recevied\n");
280                 env.major = FORB_EX_COMM_FAILURE;
281                 goto out;
282         }
283         ret = FORB_CDR_get_align(codec, 8);
284         if (!ret) {
285                 ul_logerr("Malformed request recevied\n");
286                 env.major = FORB_EX_COMM_FAILURE;
287                 goto out;
288         }
289
290         header_size = data_size - FORB_CDR_data_size(codec);
291         req_size = message_size - header_size;
292
293         ul_logdeb("rcvd request: src=%s, id=%u, iface=%s, method=%hd\n",
294                   forb_server_id_to_string(str, &request_header.source, sizeof(str)),
295                   request_header.request_id,
296                   request_header.iface,
297                   request_header.method_index);
298
299         if (port->new_peer) {
300                 /* Request from a new peer was reported by the underlaying protocol */
301                 peer = forb_peer_find(port->forb, &request_header.source);
302                 if (peer) {
303                         /* We already know this peer */
304                         /* TODO: Can it be in FORB_PEER_WANTED state?
305                          * If yes, we cannot simply igore this. */
306                         ul_logmsg("new_peer was already known\n");
307                         forb_peer_put(port->new_peer);
308                         port->new_peer = NULL;
309                 } else {
310                         ul_logdeb("discovered new_peer from incomming connection\n");
311                         peer = port->new_peer;
312                         port->new_peer = NULL;
313                         forb_new_peer_discovered(port, peer, request_header.source,
314                                                  peer->addr, peer->orb_id);
315                 }
316                 
317         }
318         
319         obj = forb_key_to_object(forb, request_header.objkey);
320         if (!obj) {
321                 ul_logerr("Nonexistent object key\n");
322                 env.major = FORB_EX_OBJECT_NOT_EXIST;
323                 goto send_execption;
324         }
325
326         n = strlen(request_header.iface);
327         ret = strncmp(request_header.iface, obj->interface->name, n);
328         forb_free(request_header.iface);
329         request_header.iface = NULL;
330         if (ret != 0) {
331                 env.major = FORB_EX_INV_OBJREF;
332                 ul_logerr("Object reference has incorrect type\n");
333                 goto send_execption;
334         }
335         
336         if (request_header.method_index >= obj->interface->num_methods) {
337                 env.major = FORB_EX_INV_IDENT;
338                 ul_logerr("To high method number\n");
339                 goto send_execption;
340         }
341
342         if (!obj->executor) {
343                 env.major = FORB_EX_NO_IMPLEMENT;
344                 ul_logerr("No executor for object\n");
345                 goto send_execption;
346
347         }
348
349         /* Enqueue execution request */
350         exec_req = forb_malloc(sizeof(*exec_req));
351         if (exec_req) {
352                 memset(exec_req, 0, sizeof(exec_req));
353                 exec_req->request_id = request_header.request_id;
354                 exec_req->source = request_header.source;
355                 exec_req->obj = obj;
356                 exec_req->method_index = request_header.method_index;
357                 /* Copy the request to exec_req */
358                 FORB_CDR_codec_init_static(&exec_req->codec, codec->orb);
359                 ret = FORB_CDR_buffer_init(&exec_req->codec,
360                                       req_size,
361                                       0);
362                 if (!ret) {
363                         env.major = FORB_EX_NO_MEMORY;
364                         ul_logerr("No memory for executor request bufer of size %d (header_size=%d)\n",
365                                   req_size, header_size);
366                         goto send_execption;
367                 }
368                 exec_req->codec.data_endian = codec->data_endian;
369                 FORB_CDR_buffer_gets(codec, exec_req->codec.buffer, req_size);
370                 /* TODO: Use better data structure for incomming
371                    buffer to achieve zero-copy behaviour. */
372                 forb_exec_req_ins_tail(obj->executor, exec_req);
373         } else {
374                 env.major = FORB_EX_NO_MEMORY;
375                 ul_logerr("No memory for executor request\n");
376                 goto send_execption;
377         }
378         
379 out:
380         return;
381         
382 send_execption:
383         FORB_CDR_codec_init_static(&reply_codec, codec->orb);   
384         ret = FORB_CDR_buffer_init(&reply_codec, 4096,
385                               forb_iop_MESSAGE_HEADER_SIZE +
386                               forb_iop_REPLY_HEADER_SIZE);
387         if (!ret) {
388                 ul_logerr("No memory for exception reply buffer\n");
389                 return;
390         }
391
392         forb_iop_send_reply(port->forb, &request_header.source,
393                             &reply_codec, request_header.request_id, &env);
394         FORB_CDR_codec_release_buffer(&reply_codec);
395         /* TODO: relese exec_req etc. */
396 }
397
398 static void
399 process_reply(forb_port_t *port, FORB_CDR_Codec *codec)
400 {
401         forb_iop_reply_header rh;
402         forb_t *forb = port->forb;
403         forb_request_t *req;
404
405         forb_iop_reply_header_deserialize(codec, &rh);
406         /* Reply data are 8 byte aligned */
407         FORB_CDR_get_align(codec, 8);
408         ul_logdeb("rcvd reply: id=%u\n", rh.request_id);
409         req = forb_request_find(forb, &rh.request_id);
410         if (!req) {
411                 ul_logerr("Received reply to unknown request_id %u\n", rh.request_id);
412                 return;
413         }
414         forb_request_delete(forb, req); /* Deregister request from forb */
415
416         if (rh.flags & forb_iop_FLAG_EXCEPTION) {
417                 forb_exception_deserialize(codec, req->env);
418         } else {
419                 req->cdr_reply = codec;
420         }
421
422         /* Tell the stub where to signal that reply processing is
423          * finished */
424         req->reply_processed = &port->reply_processed;
425
426         /* Resume the stub waiting in forb_wait_for_reply() */
427         forb_syncobj_signal(&req->reply_ready);
428
429         /* Wait for stub to process the results from the codec's buffer */
430         forb_syncobj_wait(&port->reply_processed);
431 }
432
433 /** 
434  * Process incomming HELLO messages.
435  *
436  * For every incomming HELLO message the peer table is searched
437  * whether it already contains a record for that peer or not. If not,
438  * the new peer is added to the table and another hello message is
439  * sent so that the new peer discovers us quickly.
440  * 
441  * @param port Port, where hello was received
442  * @param codec Buffer with the hello message
443  */
444 static void
445 process_hello(forb_port_t *port, FORB_CDR_Codec *codec)
446 {
447         forb_server_id server_id;
448         void *addr = NULL;
449         forb_peer_t *peer;
450         forb_t *forb = port->forb;
451         CORBA_string peer_orb_id = NULL;
452
453 /*      printf("Hello received at port %p\n", port); */
454
455         forb_server_id_deserialize(codec, &server_id);
456         if (port->desc.proto->deserialize_addr) {
457                 port->desc.proto->deserialize_addr(codec, &addr);
458         }
459         {
460                 char str[60], addrstr[60];
461                 if (port->desc.proto->addr2str) {
462                         port->desc.proto->addr2str(addrstr, sizeof(addrstr), addr);
463                 } else
464                         addrstr[0] = 0;
465                 ul_logdeb("rcvd hello from %s (addr %s)\n",
466                           forb_server_id_to_string(str, &server_id, sizeof(str)),
467                           addrstr);
468         }
469         CORBA_string_deserialize(codec, &peer_orb_id);
470         if (forb_server_id_cmp(&server_id, &forb->server_id) != 0) {
471                 peer = forb_peer_find(forb, &server_id);
472                 if (peer && peer->state == FORB_PEER_DISCOVERED) {
473                         /* TODO: Update last hello receive time */
474                         if (addr)
475                                 forb_free(addr);
476                         if (peer_orb_id)
477                                 forb_free(peer_orb_id);
478                         forb_peer_put(peer);
479                 } else {
480                         if (port->new_peer) {
481                                 if (peer)
482                                         ul_logerr("Unahandled case - FORB_PEER_WANTED && port->new_peer\n");
483                                 peer = port->new_peer;
484                                 port->new_peer = NULL;
485                         }
486
487                         forb_new_peer_discovered(port, peer, server_id, addr, peer_orb_id);
488                 }
489         }
490 }
491
492 static void
493 process_message(forb_port_t *port, const forb_iop_message_header *mh,
494                 FORB_CDR_Codec *codec)
495 {
496         CORBA_long data_size = FORB_CDR_data_size(codec);
497         /* TODO: Check destination address, whether the message is for
498          * us or should be routed. */
499
500         /* TODO: If there is some processing error, skip the rest of
501          * the message according mh.message_size. */
502         switch (mh->message_type) {
503                 case forb_iop_REQUEST:
504                         process_request(port, codec, mh->message_size);
505                         break;
506                 case forb_iop_REPLY:
507                         process_reply(port, codec);
508                         break;
509                 case forb_iop_HELLO:
510                         process_hello(port, codec);
511                         break;
512                 default:
513                         ul_logmsg("rcvd unknown message type\n");
514                         break;
515         }
516         if (port->new_peer) {
517                 /* If for some reaseon the new_peer was not processed so free it here. */
518                 ul_logmsg("Forgotten new_peer\n");
519                 forb_peer_put(port->new_peer);
520                 port->new_peer = NULL;
521         }
522         if (FORB_CDR_data_size(codec) != data_size - mh->message_size) {
523                 size_t processed = data_size - FORB_CDR_data_size(codec);
524                 ul_logmsg("Message of type %d handled incorrectly (size=%d, processed=%d); fixing\n",
525                           mh->message_type, mh->message_size, processed);
526                 ;
527                 codec->rptr += mh->message_size - processed;
528         }
529 }
530
531 /** 
532  * Thread run for every port to receive FORB messages from that port. 
533  * 
534  * @param arg Pointer to ::forb_port_t typecasted to void *.
535  * 
536  * @return Always NULL
537  */
538 void *forb_iop_receiver_thread(void *arg)
539 {
540         forb_port_t *port = arg;
541         const forb_proto_t *proto = port->desc.proto;
542         FORB_CDR_Codec *c = &port->codec;
543         ssize_t rcvd;
544         size_t len;
545         forb_iop_message_header mh;
546         bool header_received = false;
547
548         while (!port->finish) {
549                 if (c->rptr == c->wptr) {
550                         /* The buffer is empty now - start writing from begining*/
551                         FORB_CDR_buffer_reset(c, 0);
552                 }
553                 /* TODO: If there is not enough space for reception,
554                  * we should shift the already received data to the
555                  * beginning of the buffer. */
556                 rcvd = proto->recv(port,
557                                    &c->buffer[c->wptr],
558                                    c->wptr_max - c->wptr);
559                 if (rcvd < 0) {
560                         ul_logerr("recv returned error %d (%s), exiting\n", rcvd, strerror(errno));
561                         return NULL;
562                 }
563                 c->wptr += rcvd;
564                 c->wptr_last = c->wptr;
565
566                 /* While there are some data in the buffer, process them. */
567                 while (FORB_CDR_data_size(c) > 0) {
568                         len = FORB_CDR_data_size(c);
569                         /* Wait for and then process message header */
570                         if (!header_received) {
571                                 if (len >= forb_iop_MESSAGE_HEADER_SIZE) {
572                                         if (c->rptr % 8 != 0) {
573                                                 ul_logerr("Header doesn't start at 8 byte bounday\n");
574                                         }
575                                         header_received = forb_iop_process_message_header(&mh, c);
576                                         if (!header_received) {
577                                                 ul_logerr("Wrong header received\n");
578                                                 /* TODO: We should probably reset the buffer here */
579                                         }
580                                         len = FORB_CDR_data_size(c);
581                                 } else {
582                                         break; /* Wait for more data to arrive*/
583                                 }
584                         }
585                         
586                         /* Wait for and then process the message body */
587                         if (header_received) {
588                                 if (len >= mh.message_size) {
589                                         process_message(port, &mh, c);
590                                         /* Wait for the next message */
591                                         header_received = false;
592                                 } else {
593                                         break; /* Wait for more data to arrive*/
594                                 }
595                         }
596                 }
597         }
598         return NULL;
599 }
600
601 static void
602 discovery_cleanup(void *codec)
603 {
604         FORB_CDR_codec_release_buffer((FORB_CDR_Codec*)codec);
605         /* TODO: Broadcast some kind of bye bye message */
606 }
607
608
609 /** 
610  * Thread run for every port to broadcast HELLO messages. These
611  * messages are used for a FORB to discover all peers (and in future
612  * also to detect their disconnection).
613  * 
614  * @param arg Pointer to ::forb_port_t typecasted to void *.
615  * 
616  * @return Always NULL
617  */
618 void *forb_iop_discovery_thread(void *arg)
619 {
620         forb_port_t *port = arg;
621         const forb_proto_t *proto = port->desc.proto;
622         FORB_CDR_Codec codec;
623         fosa_abs_time_t hello_time;
624         fosa_rel_time_t hello_interval = fosa_msec_to_rel_time(1000*proto->hello_interval);
625         int ret;
626
627         FORB_CDR_codec_init_static(&codec, port->forb->orb);
628         FORB_CDR_buffer_init(&codec, 1024, 0);
629
630         pthread_cleanup_push(discovery_cleanup, &codec);
631         
632         /* Next hello interval is now */
633         fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &hello_time);
634         
635         while (!port->finish) {
636                 /* Wait for next hello interval or until somebody
637                  * signals us. */
638                 ret = forb_syncobj_timedwait(&port->hello, &hello_time);
639                 /* sem_timedwait would be more appropriate */
640                 if (ret == FOSA_ETIMEDOUT) {
641                         hello_time = fosa_abs_time_incr(hello_time, hello_interval);
642                 } else if (ret != 0) {
643                         ul_logerr("hello syncobj error: %s\n", strerror(ret));
644                 }
645
646                 if (port->finish) break;
647
648                 FORB_CDR_buffer_reset(&codec, forb_iop_MESSAGE_HEADER_SIZE);
649                 forb_iop_prepare_hello(&codec, &port->forb->server_id, port->desc.addr,
650                                        proto->serialize_addr, port->forb->attr.orb_id);
651 /*              printf("Broadcasting hello from port %p\n", port);  */
652                 proto->broadcast(port, &codec.buffer[codec.rptr],
653                                  FORB_CDR_data_size(&codec));
654         }
655
656         pthread_cleanup_pop(1);
657         return NULL;
658 }
659
660 /** 
661  * Sends REQUEST message to another FORB.
662  *
663  * The request @a req has to be prepared by
664  * forb_iop_prepare_request(). Then, this function adds a message
665  * header, connects to the destination FORB and sends the request.
666  *
667  * If no exception is reported, then the caller must wait for response
668  * by calling forb_wait_for_reply().
669  * 
670  * @param req A request prepared by forb_iop_prepare_request()
671  * @param env Environment for returning exceptions
672  */
673 void
674 forb_request_send(forb_request_t *req, CORBA_Environment *env)
675 {
676         CORBA_boolean ret;
677         forb_peer_t *peer;
678         ssize_t size;
679         size_t len;
680         fosa_abs_time_t timeout;
681         forb_t *forb = forb_object_to_forb(req->obj);
682
683         if (!forb) {
684                 env->major = FORB_EX_INTERNAL;
685                 return;
686         }
687
688         req->env = env;     /* Remember, where to return exceptions */
689
690         ret = forb_iop_prepend_message_header(&req->cdr_request, forb_iop_REQUEST);
691         if (!ret) {
692                 /* This should never happen */
693                 env->major = FORB_EX_INTERNAL;
694                 return;
695         }
696
697         fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
698         timeout = fosa_abs_time_incr(timeout,
699                                      fosa_msec_to_rel_time(1000));
700         peer = forb_get_next_hop(forb, &req->obj->server, &timeout);
701         if (!peer) {
702                 char str[50];
703                 ul_logerr("Cannot find peer to send request for server %s\n",
704                           forb_server_id_to_string(str, &req->obj->server, sizeof(str)));
705                 env->major = FORB_EX_COMM_FAILURE;
706                 return;
707         }
708         /* Register the request with forb so we can match incomming
709          * reply to this request. */
710         ret = forb_request_insert(forb, req);
711         if (ret <= 0) {
712                 ul_logerr("Insert request error %d\n", ret);
713                 env->major = FORB_EX_INTERNAL;
714                 goto err_peer_put;
715         }
716
717         {
718                 char str[50];
719                 ul_logdeb("sending request: id=%d  dest=%s\n", req->request_id,
720                           forb_server_id_to_string(str, &req->obj->server, sizeof(str)));
721         }
722         len = FORB_CDR_data_size(&req->cdr_request);
723         fosa_mutex_lock(&peer->send_lock);
724         size = forb_proto_send(peer, &req->cdr_request);
725         fosa_mutex_unlock(&peer->send_lock);
726         if (size <= 0 || size != len) {
727                 env->major = FORB_EX_COMM_FAILURE;
728                 /* Request is deleted when the stub calls forb_request_destroy() */
729         }
730  err_peer_put:
731         forb_peer_put(peer);
732 }