1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of FORB (Frescor Object Request Broker) */
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. */
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 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Mon Sep 1 21:51:58 2008
52 * @brief Implementation of Inter-ORB protocol.
58 #include <forb/object.h>
64 #include "discovery.h"
66 /** Version of the protocol */
70 #define VER(major, minor) ((major)<<8 || (minor))
72 extern UL_LOG_CUST(ulogd_forb_iop);
76 forb_iop_prepend_message_header(FORB_CDR_Codec *codec, forb_iop_message_type mt)
79 forb_iop_message_header mh;
80 mh.proto_version.major = VER_MAJOR;
81 mh.proto_version.minor = VER_MINOR;
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);
87 ret = forb_iop_message_header_serialize(codec, &mh);
93 forb_iop_prepare_request(forb_request_t *req,
96 CORBA_Environment *env)
99 forb_iop_request_header rh;
101 rh.request_id = req->request_id;
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);
108 /* Request body is 8 byte aligned */
109 ret = FORB_CDR_put_align(&req->cdr_request, 8);
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)),
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)
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;
129 if (!CORBA_string_serialize(codec, &orb_id)) return CORBA_FALSE;
130 /* All headers must be 8 byte aligned so align the length of
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;
137 int forb_iop_send_hello_to(forb_peer_t *peer)
139 forb_port_t *port = peer->port;
140 FORB_CDR_Codec codec;
142 FORB_CDR_codec_init_static(&codec, port->forb->orb);
143 if (!FORB_CDR_buffer_init(&codec, 1024, 0))
145 if (!FORB_CDR_buffer_reset(&codec, forb_iop_MESSAGE_HEADER_SIZE)) {
149 if (!forb_iop_prepare_hello(&codec, &port->forb->server_id,
151 port->desc.proto->serialize_addr,
152 port->forb->attr.orb_id)) {
156 ret = forb_proto_send(peer, &codec);
157 if (ret > 0) ret = 0;
159 FORB_CDR_codec_release_buffer(&codec);
164 forb_iop_process_message_header(forb_iop_message_header *mh, FORB_CDR_Codec *codec)
166 /* FIXME: If we have multiple protocol versions, use different
167 * type (independent from version) for return value instead of
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:
181 ul_logerr("rcvd wrong message type: %d\n",
185 codec->data_endian = (mh->flags && forb_iop_LITTLE_ENDIAN) ?
186 LittleEndian : BigEndian;
187 CORBA_unsigned_long_deserialize(codec, &mh->message_size);
191 ul_logerr("rcvd wrong protocol versio: %d.%d\n",
192 mh->proto_version.major, mh->proto_version.minor);
197 static inline CORBA_boolean
198 forb_exception_serialize(FORB_CDR_Codec *codec, struct forb_env *env)
200 return CORBA_long_serialize(codec, &env->major);
203 static inline CORBA_boolean
204 forb_exception_deserialize(FORB_CDR_Codec *codec, struct forb_env *env)
206 /* TODO: Declare exceptions in IDL and don't typecast here. */
207 return CORBA_long_deserialize(codec, (CORBA_long*)&env->major);
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)
217 forb_iop_reply_header reply_header;
220 fosa_abs_time_t timeout;
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);
230 /* All headers must be 8 byte aligned so align the length of
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)*/
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
239 ret = FORB_CDR_buffer_prepend(codec, forb_iop_REPLY_HEADER_SIZE);
243 ret = forb_iop_reply_header_serialize(codec, &reply_header);
247 forb_iop_prepend_message_header(codec, forb_iop_REPLY);
249 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
250 timeout = fosa_abs_time_incr(timeout,
251 fosa_msec_to_rel_time(1000));
253 peer = forb_get_next_hop(forb, dest, &timeout);
256 forb_server_id_to_string(str, dest, sizeof(str));
257 ul_logerr("Reply destination not found: %s\n", str);
261 forb_server_id_to_string(str, dest, sizeof(str));
262 ul_logdeb("sending reply: dest=%s, id=%u\n", str, request_id);
264 forb_proto_send(peer, codec);
271 process_request(forb_port_t *port, FORB_CDR_Codec *codec, uint32_t message_size)
273 forb_iop_request_header request_header;
277 forb_t *forb = port->forb;
279 FORB_CDR_Codec reply_codec;
280 forb_exec_req_t *exec_req;
281 uint32_t req_size, data_size, header_size;
285 data_size = FORB_CDR_data_size(codec);
286 ret = forb_iop_request_header_deserialize(codec, &request_header);
288 ul_logerr("Malformed request recevied\n");
289 env.major = FORB_EX_COMM_FAILURE;
292 ret = FORB_CDR_get_align(codec, 8);
294 ul_logerr("Malformed request recevied\n");
295 env.major = FORB_EX_COMM_FAILURE;
299 header_size = data_size - FORB_CDR_data_size(codec);
300 req_size = message_size - header_size;
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);
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);
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;
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);
328 obj = forb_key_to_object(forb, request_header.objkey);
330 ul_logerr("Nonexistent object key\n");
331 env.major = FORB_EX_OBJECT_NOT_EXIST;
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;
340 env.major = FORB_EX_INV_OBJREF;
341 ul_logerr("Object reference has incorrect type\n");
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");
351 if (!obj->executor) {
352 env.major = FORB_EX_NO_IMPLEMENT;
353 ul_logerr("No executor for object\n");
358 /* Enqueue execution request */
359 exec_req = forb_malloc(sizeof(*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;
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,
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);
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);
383 env.major = FORB_EX_NO_MEMORY;
384 ul_logerr("No memory for executor request\n");
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);
397 ul_logerr("No memory for exception reply buffer\n");
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. */
408 process_reply(forb_port_t *port, FORB_CDR_Codec *codec)
410 forb_iop_reply_header rh;
411 forb_t *forb = port->forb;
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);
420 ul_logerr("Received reply to unknown request_id %u\n", rh.request_id);
423 forb_request_delete(forb, req); /* Deregister request from forb */
425 if (rh.flags & forb_iop_FLAG_EXCEPTION) {
426 forb_exception_deserialize(codec, req->env);
428 req->cdr_reply = codec;
431 /* Tell the stub where to signal that reply processing is
433 req->reply_processed = &port->reply_processed;
435 /* Resume the stub waiting in forb_wait_for_reply() */
436 forb_syncobj_signal(&req->reply_ready);
438 /* Wait for stub to process the results from the codec's buffer */
439 forb_syncobj_wait(&port->reply_processed);
443 * Process incomming HELLO messages.
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.
450 * @param port Port, where hello was received
451 * @param codec Buffer with the hello message
454 process_hello(forb_port_t *port, FORB_CDR_Codec *codec)
456 forb_server_id server_id;
459 forb_t *forb = port->forb;
460 CORBA_string peer_orb_id = NULL;
462 /* printf("Hello received at port %p\n", port); */
464 forb_server_id_deserialize(codec, &server_id);
465 if (port->desc.proto->deserialize_addr) {
466 port->desc.proto->deserialize_addr(codec, &addr);
469 char str[60], addrstr[60];
470 if (port->desc.proto->addr2str) {
471 port->desc.proto->addr2str(addrstr, sizeof(addrstr), addr);
474 ul_logdeb("rcvd hello from %s (addr %s)\n",
475 forb_server_id_to_string(str, &server_id, sizeof(str)),
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 */
486 forb_free(peer_orb_id);
489 if (port->new_peer) {
491 ul_logerr("Unahandled case - FORB_PEER_WANTED && port->new_peer\n");
492 peer = port->new_peer;
493 port->new_peer = NULL;
496 forb_new_peer_discovered(port, peer, server_id, addr, peer_orb_id);
502 process_message(forb_port_t *port, const forb_iop_message_header *mh,
503 FORB_CDR_Codec *codec)
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. */
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);
516 process_reply(port, codec);
519 process_hello(port, codec);
522 ul_logmsg("rcvd unknown message type\n");
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;
532 FORB_CDR_get_align(codec, 8);
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);
539 codec->rptr += mh->message_size - processed;
544 * Thread run for every port to receive FORB messages from that port.
546 * @param arg Pointer to ::forb_port_t typecasted to void *.
548 * @return Always NULL
550 void *forb_iop_receiver_thread(void *arg)
552 forb_port_t *port = arg;
553 const forb_proto_t *proto = port->desc.proto;
554 FORB_CDR_Codec *c = &port->codec;
557 forb_iop_message_header mh;
558 bool header_received = false;
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);
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,
570 c->wptr_max - c->wptr);
572 ul_logerr("recv returned error %d (%s), exiting\n", rcvd, strerror(errno));
576 c->wptr_last = c->wptr;
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");
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 */
592 len = FORB_CDR_data_size(c);
594 break; /* Wait for more data to arrive*/
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;
605 break; /* Wait for more data to arrive*/
614 discovery_cleanup(void *codec)
616 FORB_CDR_codec_release_buffer((FORB_CDR_Codec*)codec);
617 /* TODO: Broadcast some kind of bye bye message */
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).
626 * @param arg Pointer to ::forb_port_t typecasted to void *.
628 * @return Always NULL
630 void *forb_iop_discovery_thread(void *arg)
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);
639 FORB_CDR_codec_init_static(&codec, port->forb->orb);
640 FORB_CDR_buffer_init(&codec, 1024, 0);
642 pthread_cleanup_push(discovery_cleanup, &codec);
644 /* Next hello interval is now */
645 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &hello_time);
647 while (!port->finish) {
648 /* Wait for next hello interval or until somebody
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));
658 if (port->finish) break;
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));
668 pthread_cleanup_pop(1);
673 * Sends REQUEST message to another FORB.
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.
679 * If no exception is reported, then the caller must wait for response
680 * by calling forb_wait_for_reply().
682 * @param req A request prepared by forb_iop_prepare_request()
683 * @param env Environment for returning exceptions
686 forb_request_send(forb_request_t *req, CORBA_Environment *env)
692 fosa_abs_time_t timeout;
693 forb_t *forb = forb_object_to_forb(req->obj);
696 env->major = FORB_EX_INTERNAL;
700 req->env = env; /* Remember, where to return exceptions */
702 /* All headers must be 8 byte aligned so align the length of
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");
710 ret = forb_iop_prepend_message_header(&req->cdr_request, forb_iop_REQUEST);
712 /* This should never happen */
713 env->major = FORB_EX_INTERNAL;
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);
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;
728 /* Register the request with forb so we can match incomming
729 * reply to this request. */
730 ret = forb_request_insert(forb, req);
732 ul_logerr("Insert request error %d\n", ret);
733 env->major = FORB_EX_INTERNAL;
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)));
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() */