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