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_string 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 if (!forb_iop_prepend_message_header(codec, forb_iop_HELLO)) return CORBA_FALSE;
135 forb_iop_process_message_header(forb_iop_message_header *mh, FORB_CDR_Codec *codec)
137 /* FIXME: If we have multiple protocol versions, use different
138 * type (independent from version) for return value instead of
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:
154 codec->data_endian = (mh->flags && forb_iop_LITTLE_ENDIAN) ?
155 LittleEndian : BigEndian;
156 CORBA_unsigned_long_deserialize(codec, &mh->message_size);
164 static inline CORBA_boolean
165 forb_exception_serialize(FORB_CDR_Codec *codec, struct forb_env *env)
167 return CORBA_long_serialize(codec, &env->major);
170 static inline CORBA_boolean
171 forb_exception_deserialize(FORB_CDR_Codec *codec, struct forb_env *env)
173 /* TODO: Declare exceptions in IDL and don't typecast here. */
174 return CORBA_long_deserialize(codec, (CORBA_long*)&env->major);
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)
184 forb_iop_reply_header reply_header;
187 fosa_abs_time_t timeout;
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);
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
200 ret = FORB_CDR_buffer_prepend(codec, forb_iop_REPLY_HEADER_SIZE);
204 ret = forb_iop_reply_header_serialize(codec, &reply_header);
208 forb_iop_prepend_message_header(codec, forb_iop_REPLY);
210 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
211 timeout = fosa_abs_time_incr(timeout,
212 fosa_msec_to_rel_time(1000));
214 peer = forb_get_next_hop(forb, dest, &timeout);
217 forb_server_id_to_string(str, dest, sizeof(str));
218 ul_logerr("Reply destination not found: %s\n", str);
222 forb_server_id_to_string(str, dest, sizeof(str));
223 ul_logdeb("sending reply: dest=%s, id=%u\n", str, request_id);
225 forb_proto_send(peer, codec);
232 process_request(forb_port_t *port, FORB_CDR_Codec *codec, uint32_t message_size)
234 forb_iop_request_header request_header;
238 forb_t *forb = port->forb;
240 FORB_CDR_Codec reply_codec;
241 forb_exec_req_t *exec_req;
242 uint32_t req_size, data_size, header_size;
246 data_size = FORB_CDR_data_size(codec);
247 ret = forb_iop_request_header_deserialize(codec, &request_header);
249 ul_logerr("Malformed request recevied\n");
250 env.major = FORB_EX_COMM_FAILURE;
253 ret = FORB_CDR_get_align(codec, 8);
255 ul_logerr("Malformed request recevied\n");
256 env.major = FORB_EX_COMM_FAILURE;
260 header_size = data_size - FORB_CDR_data_size(codec);
261 req_size = message_size - header_size;
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);
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);
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;
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);
289 obj = forb_key_to_object(forb, request_header.objkey);
291 ul_logerr("Nonexistent object key\n");
292 env.major = FORB_EX_OBJECT_NOT_EXIST;
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;
301 env.major = FORB_EX_INV_OBJREF;
302 ul_logerr("Object reference has incorrect type\n");
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");
312 if (!obj->executor) {
313 env.major = FORB_EX_NO_IMPLEMENT;
314 ul_logerr("No executor for object\n");
319 /* Enqueue execution request */
320 exec_req = forb_malloc(sizeof(*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;
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,
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);
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);
344 env.major = FORB_EX_NO_MEMORY;
345 ul_logerr("No memory for executor request\n");
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);
358 ul_logerr("No memory for exception reply buffer\n");
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. */
369 process_reply(forb_port_t *port, FORB_CDR_Codec *codec)
371 forb_iop_reply_header rh;
372 forb_t *forb = port->forb;
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);
381 ul_logerr("Received reply to unknown request_id %u\n", rh.request_id);
384 forb_request_delete(forb, req); /* Deregister request from forb */
386 if (rh.flags & forb_iop_FLAG_EXCEPTION) {
387 forb_exception_deserialize(codec, req->env);
389 req->cdr_reply = codec;
392 /* Tell the stub where to signal that reply processing is
394 req->reply_processed = &port->reply_processed;
396 /* Resume the stub waiting in forb_wait_for_reply() */
397 forb_syncobj_signal(&req->reply_ready);
399 /* Wait for stub to process the results from the codec's buffer */
400 forb_syncobj_wait(&port->reply_processed);
404 * Process incomming HELLO messages.
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.
411 * @param port Port, where hello was received
412 * @param codec Buffer with the hello message
415 process_hello(forb_port_t *port, FORB_CDR_Codec *codec)
417 forb_server_id server_id;
420 forb_t *forb = port->forb;
421 CORBA_string peer_orb_id = NULL;
423 /* printf("Hello received at port %p\n", port); */
425 forb_server_id_deserialize(codec, &server_id);
426 if (port->desc.proto->deserialize_addr) {
427 port->desc.proto->deserialize_addr(codec, &addr);
430 char str[60], addrstr[60];
431 if (port->desc.proto->addr2str) {
432 port->desc.proto->addr2str(addrstr, sizeof(addrstr), addr);
435 ul_logdeb("rcvd hello from %s (addr %s)\n",
436 forb_server_id_to_string(str, &server_id, sizeof(str)),
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 */
447 forb_free(peer_orb_id);
450 forb_new_peer_discovered(port, peer, server_id, addr, peer_orb_id);
456 process_message(forb_port_t *port, const forb_iop_message_header *mh,
457 FORB_CDR_Codec *codec)
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. */
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);
470 process_reply(port, codec);
473 process_hello(port, codec);
476 ul_logmsg("rcvd unknown message type\n");
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;
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);
490 codec->rptr += mh->message_size - processed;
495 * Thread run for every port to receive FORB messages from that port.
497 * @param arg Pointer to ::forb_port_t typecasted to void *.
499 * @return Always NULL
501 void *forb_iop_receiver_thread(void *arg)
503 forb_port_t *port = arg;
504 const forb_proto_t *proto = port->desc.proto;
505 FORB_CDR_Codec *c = &port->codec;
508 forb_iop_message_header mh;
509 bool header_received = false;
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);
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,
521 c->wptr_max - c->wptr);
523 ul_logmsg("recv returned error %d\n", rcvd);
527 c->wptr_last = c->wptr;
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");
538 header_received = forb_iop_process_message_header(&mh, c);
539 len = FORB_CDR_data_size(c);
541 break; /* Wait for more data to arrive*/
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;
552 break; /* Wait for more data to arrive*/
561 discovery_cleanup(void *codec)
563 FORB_CDR_codec_release_buffer((FORB_CDR_Codec*)codec);
564 /* TODO: Broadcast some kind of bye bye message */
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).
573 * @param arg Pointer to ::forb_port_t typecasted to void *.
575 * @return Always NULL
577 void *forb_iop_discovery_thread(void *arg)
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);
586 FORB_CDR_codec_init_static(&codec, port->forb->orb);
587 FORB_CDR_buffer_init(&codec, 1024, 0);
589 pthread_cleanup_push(discovery_cleanup, &codec);
591 /* Next hello interval is now */
592 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &hello_time);
594 while (!port->finish) {
595 /* Wait for next hello interval or until somebody
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));
605 if (port->finish) break;
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));
615 pthread_cleanup_pop(1);
620 * Sends REQUEST message to another FORB.
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.
626 * If no exception is reported, then the caller must wait for response
627 * by calling forb_wait_for_reply().
629 * @param req A request prepared by forb_iop_prepare_request()
630 * @param env Environment for returning exceptions
633 forb_request_send(forb_request_t *req, CORBA_Environment *env)
639 fosa_abs_time_t timeout;
640 forb_t *forb = forb_object_to_forb(req->obj);
643 env->major = FORB_EX_INTERNAL;
647 req->env = env; /* Remember, where to return exceptions */
649 ret = forb_iop_prepend_message_header(&req->cdr_request, forb_iop_REQUEST);
651 /* This should never happen */
652 env->major = FORB_EX_INTERNAL;
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);
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;
667 /* Register the request with forb so we can match incomming
668 * reply to this request. */
669 ret = forb_request_insert(forb, req);
671 ul_logerr("Insert request error %d\n", ret);
672 env->major = FORB_EX_INTERNAL;
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)));
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() */