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>
65 /** Version of the protocol */
69 #define VER(major, minor) ((major)<<8 || (minor))
71 extern UL_LOG_CUST(ulogd_forb_iop);
75 forb_iop_prepend_message_header(FORB_CDR_Codec *codec, forb_iop_message_type mt)
78 forb_iop_message_header mh;
79 mh.proto_version.major = VER_MAJOR;
80 mh.proto_version.minor = VER_MINOR;
82 mh.flags = (codec->data_endian == LittleEndian) ? forb_iop_LITTLE_ENDIAN : 0;
83 mh.message_size = FORB_CDR_data_size(codec);
84 ret = FORB_CDR_buffer_prepend(codec, forb_iop_MESSAGE_HEADER_SIZE);
86 ret = forb_iop_message_header_serialize(codec, &mh);
92 forb_iop_prepare_request(forb_request_t *req,
95 CORBA_Environment *env)
98 forb_iop_request_header rh;
100 rh.request_id = req->request_id;
102 rh.objkey = forb_object_to_key(req->obj);
103 rh.method_index = method_ind;
104 rh.source = forb_object_to_forb(req->obj)->server_id;
105 ret = forb_iop_request_header_serialize(&req->cdr_request, &rh);
107 /* Request body is 8 byte aligned */
108 ret = FORB_CDR_put_align(&req->cdr_request, 8);
114 forb_iop_prepare_hello(FORB_CDR_Codec *codec,
115 const forb_server_id *server_id,
116 const void *src_addr,
117 CORBA_boolean (*serialize_addr)(FORB_CDR_Codec *codec, const void *addr))
119 if (!forb_server_id_serialize(codec, server_id)) return CORBA_FALSE;
120 if (serialize_addr) {
121 if (!serialize_addr(codec, src_addr)) return CORBA_FALSE;
123 if (!forb_iop_prepend_message_header(codec, forb_iop_HELLO)) return CORBA_FALSE;
128 forb_iop_process_message_header(forb_iop_message_header *mh, FORB_CDR_Codec *codec)
130 /* FIXME: If we have multiple protocol versions, use different
131 * type (independent from version) for return value instead of
133 forb_iop_version_deserialize(codec, &mh->proto_version);
134 switch (VER(mh->proto_version.major, mh->proto_version.minor)) {
135 case VER(VER_MAJOR, VER_MINOR):
136 forb_iop_message_type_deserialize(codec, &mh->message_type);
137 forb_iop_message_flags_deserialize(codec, &mh->flags);
138 /* Check whwther the type is meaningfull */
139 switch (mh->message_type) {
140 case forb_iop_REQUEST:
147 codec->data_endian = (mh->flags && forb_iop_LITTLE_ENDIAN) ?
148 LittleEndian : BigEndian;
149 CORBA_unsigned_long_deserialize(codec, &mh->message_size);
157 static inline CORBA_boolean
158 forb_exception_serialize(FORB_CDR_Codec *codec, struct forb_env *env)
160 return CORBA_long_serialize(codec, &env->major);
163 static inline CORBA_boolean
164 forb_exception_deserialize(FORB_CDR_Codec *codec, struct forb_env *env)
166 /* TODO: Declare exceptions in IDL and don't typecast here. */
167 return CORBA_long_deserialize(codec, (CORBA_long*)&env->major);
171 forb_iop_send_reply(forb_t *forb,
172 forb_server_id *dest,
173 FORB_CDR_Codec *codec,
174 CORBA_long request_id,
175 struct forb_env *env)
177 forb_iop_reply_header reply_header;
180 fosa_abs_time_t timeout;
182 reply_header.request_id = request_id;
183 reply_header.flags = 0;
184 if (forb_exception_occurred(env)) {
185 reply_header.flags |= forb_iop_FLAG_EXCEPTION;
186 FORB_CDR_buffer_reset(codec, forb_iop_MESSAGE_HEADER_SIZE +
187 forb_iop_REPLY_HEADER_SIZE);
188 forb_exception_serialize(codec, env);
190 /* forb_iop_REPLY_HEADER_SIZE equals to 8 even if the real
191 * header is shorter. We want reply data to be 8 byte
193 ret = FORB_CDR_buffer_prepend(codec, forb_iop_REPLY_HEADER_SIZE);
197 ret = forb_iop_reply_header_serialize(codec, &reply_header);
201 forb_iop_prepend_message_header(codec, forb_iop_REPLY);
203 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
204 timeout = fosa_abs_time_incr(timeout,
205 fosa_msec_to_rel_time(1000));
207 peer = forb_get_next_hop(forb, dest, &timeout);
209 ul_logerr("Reply destination not found\n");
212 forb_proto_send(peer, codec);
220 process_request(forb_port_t *port, FORB_CDR_Codec *codec, uint32_t message_size)
222 forb_iop_request_header request_header;
226 forb_t *forb = port->forb;
228 FORB_CDR_Codec reply_codec;
229 forb_exec_req_t *exec_req;
230 uint32_t req_size, data_size, header_size;
233 data_size = FORB_CDR_data_size(codec);
234 ret = forb_iop_request_header_deserialize(codec, &request_header);
236 ul_logerr("Malformed request recevied\n");
237 env.major = FORB_EX_COMM_FAILURE;
240 ret = FORB_CDR_get_align(codec, 8);
242 ul_logerr("Malformed request recevied\n");
243 env.major = FORB_EX_COMM_FAILURE;
247 header_size = data_size - FORB_CDR_data_size(codec);
248 req_size = message_size - header_size;
250 ul_logdeb("rcvd request: src=%s, id=%u, iface=%s, method=%hd\n",
251 forb_server_id_to_string(str, &request_header.source, sizeof(str)),
252 request_header.request_id,
253 request_header.iface,
254 request_header.method_index);
256 obj = forb_key_to_object(forb, request_header.objkey);
258 ul_logerr("Nonexistent object key\n");
259 env.major = FORB_EX_OBJECT_NOT_EXIST;
263 n = strlen(request_header.iface);
264 ret = strncmp(request_header.iface, obj->interface->name, n);
265 forb_free(request_header.iface);
266 request_header.iface = NULL;
268 env.major = FORB_EX_INV_OBJREF;
269 ul_logerr("Object reference has incorrect type\n");
273 if (request_header.method_index >= obj->interface->num_methods) {
274 env.major = FORB_EX_INV_IDENT;
275 ul_logerr("To high method number\n");
279 if (!obj->executor) {
280 env.major = FORB_EX_NO_IMPLEMENT;
281 ul_logerr("No executor for object\n");
286 /* Enqueue execution request */
287 exec_req = forb_malloc(sizeof(*exec_req));
289 exec_req->request_id = request_header.request_id;
290 exec_req->source = request_header.source;
292 exec_req->method_index = request_header.method_index;
293 /* Copy the request to exec_req */
294 FORB_CDR_codec_init_static(&exec_req->codec, codec->orb);
295 ret = FORB_CDR_buffer_init(&exec_req->codec,
299 env.major = FORB_EX_NO_MEMORY;
300 ul_logerr("No memory for executor request bufer of size %d (header_size=%d)\n",
301 req_size, header_size);
304 exec_req->codec.data_endian = codec->data_endian;
305 FORB_CDR_buffer_gets(codec, exec_req->codec.buffer, req_size);
306 /* TODO: Use better data structure for incomming
307 buffer to achieve zero-copy behaviour. */
308 forb_exec_req_ins_tail(obj->executor, exec_req);
310 env.major = FORB_EX_NO_MEMORY;
311 ul_logerr("No memory for executor request\n");
319 FORB_CDR_codec_init_static(&reply_codec, codec->orb);
320 ret = FORB_CDR_buffer_init(&reply_codec, 4096,
321 forb_iop_MESSAGE_HEADER_SIZE +
322 forb_iop_REPLY_HEADER_SIZE);
324 ul_logerr("No memory for exception reply buffer\n");
328 forb_iop_send_reply(port->forb, &request_header.source,
329 &reply_codec, request_header.request_id, &env);
330 FORB_CDR_codec_release_buffer(&reply_codec);
334 process_reply(forb_port_t *port, FORB_CDR_Codec *codec)
336 forb_iop_reply_header rh;
337 forb_t *forb = port->forb;
340 forb_iop_reply_header_deserialize(codec, &rh);
341 /* Reply data are 8 byte aligned */
342 FORB_CDR_get_align(codec, 8);
343 ul_logdeb("rcvd reply: id=%u\n", rh.request_id);
344 req = forb_request_find(forb, &rh.request_id);
346 ul_logerr("Received reply to unknown request_id %u\n", rh.request_id);
349 forb_request_delete(forb, req); /* Deregister request from forb */
351 if (rh.flags & forb_iop_FLAG_EXCEPTION) {
352 forb_exception_deserialize(codec, req->env);
354 req->cdr_reply = codec;
357 /* Tell the stub where to signal that reply processing is
359 req->reply_processed = &port->reply_processed;
361 /* Resume the stub waiting in forb_wait_for_reply() */
362 forb_syncobj_signal(&req->reply_ready);
364 /* Wait for stub to process the results from the codec's buffer */
365 forb_syncobj_wait(&port->reply_processed);
368 void new_peer_discovered(forb_port_t *port, forb_peer_t *peer,
369 forb_server_id server_id, void *addr)
371 forb_t *forb = port->forb;
372 bool notify_waiters = false;
373 if (peer /* && peer->state == FORB_PEER_WANTED */) {
374 notify_waiters = true;
376 peer = forb_peer_new();
381 fosa_mutex_lock(&forb->peer_mutex);
382 peer->server_id = server_id;
385 peer->state = FORB_PEER_DISCOVERED;
386 if (notify_waiters) {
387 fosa_cond_broadcast(&peer->cond);
389 forb_peer_nolock_insert(forb, forb_peer_get(peer));
391 forb_port_peer_ins_tail(port, forb_peer_get(peer));
392 fosa_mutex_unlock(&forb->peer_mutex);
395 ul_logdeb("new peer discovered %s\n", forb_server_id_to_string(str, &peer->server_id, sizeof(str)));
399 /* Broadcast our hello packet now */
400 forb_syncobj_signal(&port->hello);
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;
422 /* printf("Hello received at port %p\n", port); */
424 forb_server_id_deserialize(codec, &server_id);
427 ul_logdeb("rcvd hello from %s\n", forb_server_id_to_string(str, &server_id, sizeof(str)));
429 if (port->desc.proto->deserialize_addr) {
430 port->desc.proto->deserialize_addr(codec, &addr);
432 if (forb_server_id_cmp(&server_id, &forb->server_id) != 0) {
433 peer = forb_peer_find(forb, &server_id);
434 if (peer && peer->state == FORB_PEER_DISCOVERED) {
435 /* TODO: Update last hello receive time */
440 new_peer_discovered(port, peer, server_id, addr);
446 process_message(forb_port_t *port, const forb_iop_message_header *mh,
447 FORB_CDR_Codec *codec)
449 CORBA_long data_size = FORB_CDR_data_size(codec);
450 /* TODO: Check destination address, whether the message is for
451 * us or should be routed. */
453 /* TODO: If there is some processing error, skip the rest of
454 * the message according mh.message_size. */
455 switch (mh->message_type) {
456 case forb_iop_REQUEST:
457 process_request(port, codec, mh->message_size);
460 process_reply(port, codec);
463 process_hello(port, codec);
466 ul_logmsg("rcvd unknown message type\n");
469 if (FORB_CDR_data_size(codec) != data_size - mh->message_size) {
470 size_t processed = data_size - FORB_CDR_data_size(codec);
471 ul_logmsg("Message of type %d handled incorrectly (size=%d, processed=%d); fixing\n",
472 mh->message_type, mh->message_size, processed);
474 codec->rptr += mh->message_size - processed;
479 * Thread run for every port to receive FORB messages from that port.
481 * @param arg Pointer to ::forb_port_t typecasted to void *.
483 * @return Always NULL
485 void *forb_iop_receiver_thread(void *arg)
487 forb_port_t *port = arg;
488 const forb_proto_t *proto = port->desc.proto;
489 FORB_CDR_Codec *c = &port->codec;
491 forb_iop_message_header mh;
492 bool header_received = false;
494 while (!port->finish) {
495 if (c->rptr == c->wptr) {
496 /* The buffer is empty now - start writing from begining*/
497 FORB_CDR_buffer_reset(c, 0);
499 /* TODO: If there is not enough space for reception,
500 * we should shift the already received data to the
501 * beginning of the buffer. */
502 rcvd = proto->recv(port,
504 c->wptr_max - c->wptr);
506 ul_logmsg("recv returned error %d\n", rcvd);
510 c->wptr_last = c->wptr;
512 /* While there are some data in the buffer, process them. */
513 while (FORB_CDR_data_size(c) > 0) {
514 len = FORB_CDR_data_size(c);
515 /* Wait for and then process message header */
516 if (!header_received) {
517 if (len >= forb_iop_MESSAGE_HEADER_SIZE) {
518 if (c->rptr % 8 != 0) {
519 ul_logerr("Header doesn't start at 8 byte bounday\n");
521 header_received = forb_iop_process_message_header(&mh, c);
522 len = FORB_CDR_data_size(c);
524 break; /* Wait for more data to arrive*/
528 /* Wait for and then process the message body */
529 if (header_received) {
530 if (len >= mh.message_size) {
531 process_message(port, &mh, c);
532 /* Wait for the next message */
533 header_received = false;
535 break; /* Wait for more data to arrive*/
544 discovery_cleanup(void *codec)
546 FORB_CDR_codec_release_buffer((FORB_CDR_Codec*)codec);
547 /* TODO: Broadcast some kind of bye bye message */
552 * Thread run for every port to broadcast HELLO messages. These
553 * messages are used for a FORB to discover all peers (and in future
554 * also to detect their disconnection).
556 * @param arg Pointer to ::forb_port_t typecasted to void *.
558 * @return Always NULL
560 void *forb_iop_discovery_thread(void *arg)
562 forb_port_t *port = arg;
563 const forb_proto_t *proto = port->desc.proto;
564 FORB_CDR_Codec codec;
565 fosa_abs_time_t hello_time;
566 fosa_rel_time_t hello_interval = fosa_msec_to_rel_time(1000*proto->hello_interval);
569 FORB_CDR_codec_init_static(&codec, port->forb->orb);
570 FORB_CDR_buffer_init(&codec, 1024, 0);
572 pthread_cleanup_push(discovery_cleanup, &codec);
574 /* Next hello interval is now */
575 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &hello_time);
577 while (!port->finish) {
578 /* Wait for next hello interval or until somebody
580 ret = forb_syncobj_timedwait(&port->hello, &hello_time);
581 /* sem_timedwait would be more appropriate */
582 if (ret == FOSA_ETIMEDOUT) {
583 hello_time = fosa_abs_time_incr(hello_time, hello_interval);
584 } else if (ret != 0) {
585 ul_logerr("hello syncobj error: %s\n", strerror(ret));
588 if (port->finish) break;
590 FORB_CDR_buffer_reset(&codec, forb_iop_MESSAGE_HEADER_SIZE);
591 forb_iop_prepare_hello(&codec, &port->forb->server_id, port->desc.addr,
592 proto->serialize_addr);
593 /* printf("Broadcasting hello from port %p\n", port); */
594 proto->broadcast(port, &codec.buffer[codec.rptr],
595 FORB_CDR_data_size(&codec));
598 pthread_cleanup_pop(1);