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