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(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 = CDR_data_size(codec);
84 ret = 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 = CDR_put_align(&req->cdr_request, 8);
114 forb_iop_prepare_hello(CDR_Codec *codec,
115 const forb_server_id *server_id,
116 const void *src_addr,
117 CORBA_boolean (*serialize_addr)(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, 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(CDR_Codec *codec, struct forb_env *env)
160 return CORBA_long_serialize(codec, &env->major);
163 static inline CORBA_boolean
164 forb_exception_deserialize(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,
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 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 = 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, CDR_Codec *codec, uint32_t message_size)
222 forb_iop_request_header request_header;
226 forb_t *forb = port->forb;
228 CDR_Codec reply_codec;
229 forb_exec_req_t *exec_req;
230 uint32_t req_size, data_size, header_size;
233 data_size = 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 = CDR_get_align(codec, 8);
242 ul_logerr("Malformed request recevied\n");
243 env.major = FORB_EX_COMM_FAILURE;
247 header_size = data_size - 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 if (strncmp(request_header.iface, obj->interface->name, n) != 0) {
265 env.major = FORB_EX_INV_OBJREF;
266 ul_logerr("Object reference has incorrect type\n");
269 if (request_header.method_index >= obj->interface->num_methods) {
270 env.major = FORB_EX_INV_IDENT;
271 ul_logerr("To high method number\n");
275 if (!obj->executor) {
276 env.major = FORB_EX_NO_IMPLEMENT;
277 ul_logerr("No executor for object\n");
282 /* Enqueue execution request */
283 exec_req = forb_malloc(sizeof(*exec_req));
285 exec_req->request_id = request_header.request_id;
286 exec_req->source = request_header.source;
288 exec_req->method_index = request_header.method_index;
289 /* Copy the request to exec_req */
290 CDR_codec_init_static(&exec_req->codec, codec->orb);
291 ret = CDR_buffer_init(&exec_req->codec,
295 env.major = FORB_EX_NO_MEMORY;
296 ul_logerr("No memory for executor request bufer of size %d (header_size=%d)\n",
297 req_size, header_size);
300 exec_req->codec.data_endian = codec->data_endian;
301 CDR_buffer_gets(codec, exec_req->codec.buffer, req_size);
302 /* TODO: Use better data structure for incomming
303 buffer to achieve zero-copy behaviour. */
304 forb_exec_req_ins_tail(obj->executor, exec_req);
306 env.major = FORB_EX_NO_MEMORY;
307 ul_logerr("No memory for executor request\n");
315 CDR_codec_init_static(&reply_codec, codec->orb);
316 ret = CDR_buffer_init(&reply_codec, 4096,
317 forb_iop_MESSAGE_HEADER_SIZE +
318 forb_iop_REPLY_HEADER_SIZE);
320 ul_logerr("No memory for exception reply buffer\n");
324 forb_iop_send_reply(port->forb, &request_header.source,
325 &reply_codec, request_header.request_id, &env);
326 CDR_codec_release_buffer(&reply_codec);
330 process_reply(forb_port_t *port, CDR_Codec *codec)
332 forb_iop_reply_header rh;
333 forb_t *forb = port->forb;
336 forb_iop_reply_header_deserialize(codec, &rh);
337 /* Reply data are 8 byte aligned */
338 CDR_get_align(codec, 8);
339 ul_logdeb("rcvd reply: id=%u\n", rh.request_id);
340 req = forb_request_find(forb, &rh.request_id);
342 ul_logerr("Received reply to unknown request_id %u\n", rh.request_id);
345 forb_request_delete(forb, req); /* Deregister request from forb */
347 if (rh.flags & forb_iop_FLAG_EXCEPTION) {
348 forb_exception_deserialize(codec, req->env);
350 req->cdr_reply = codec;
353 /* Tell the stub where to signal that reply processing is
355 req->reply_processed = &port->reply_processed;
357 /* Resume the stub waiting in forb_wait_for_reply() */
358 forb_syncobj_signal(&req->reply_ready);
360 /* Wait for stub to process the results from the codec's buffer */
361 forb_syncobj_wait(&port->reply_processed);
365 * Process incomming HELLO messages.
367 * For every incomming HELLO message the peer table is searched
368 * whether it already contains a record for that peer or not. If not,
369 * the new peer is added to the table and another hello message is
370 * sent so that the new peer discovers us quickly.
372 * @param port Port, where hello was received
373 * @param codec Buffer with the hello message
376 process_hello(forb_port_t *port, CDR_Codec *codec)
378 forb_server_id server_id;
381 forb_t *forb = port->forb;
383 /* printf("Hello received at port %p\n", port); */
385 forb_server_id_deserialize(codec, &server_id);
388 ul_logdeb("rcvd hello from %s\n", forb_server_id_to_string(str, &server_id, sizeof(str)));
390 if (port->proto->deserialize_addr) {
391 port->proto->deserialize_addr(codec, &addr);
393 if (forb_server_id_cmp(&server_id, &forb->server_id) != 0) {
394 peer = forb_peer_find(forb, &server_id);
395 if (peer && peer->state == FORB_PEER_DISCOVERED) {
396 /* TODO: Update last hello receive time */
399 /* New peer discovered */
400 bool notify_waiters = false;
401 if (peer /* && peer->state == FORB_PEER_WANTED */) {
402 notify_waiters = true;
404 peer = forb_peer_new();
407 fosa_mutex_lock(&forb->peer_mutex);
408 peer->server_id = server_id;
411 peer->state = FORB_PEER_DISCOVERED;
412 if (notify_waiters) {
413 fosa_cond_broadcast(&peer->cond);
416 forb_peer_nolock_insert(forb, peer);
419 forb_port_peer_ins_tail(port, peer);
420 fosa_mutex_unlock(&forb->peer_mutex);
423 /* Broadcast our hello packet now */
424 forb_syncobj_signal(&port->hello);
430 process_message(forb_port_t *port, const forb_iop_message_header *mh,
433 CORBA_long data_size = CDR_data_size(codec);
434 /* TODO: Check destination address, whether the message is for
435 * us or should be routed. */
437 /* TODO: If there is some processing error, skip the rest of
438 * the message according mh.message_size. */
439 switch (mh->message_type) {
440 case forb_iop_REQUEST:
441 process_request(port, codec, mh->message_size);
444 process_reply(port, codec);
447 process_hello(port, codec);
450 ul_logmsg("rcvd unknown message type\n");
453 if (CDR_data_size(codec) != data_size - mh->message_size) {
454 size_t processed = data_size - CDR_data_size(codec);
455 ul_logmsg("Message of type %d handled incorrectly (size=%d, processed=%d); fixing\n",
456 mh->message_type, mh->message_size, processed);
458 codec->rptr += mh->message_size - processed;
463 * Thread run for every port to receive FORB messages from that port.
465 * @param arg Pointer to ::forb_port_t typecasted to void *.
467 * @return Always NULL
469 void *forb_iop_receiver_thread(void *arg)
471 forb_port_t *port = arg;
472 const forb_proto_t *proto = port->proto;
473 CDR_Codec *c = &port->codec;
475 forb_iop_message_header mh;
476 bool header_received = false;
478 while (!port->finish) {
479 if (c->rptr == c->wptr) {
480 /* The buffer is empty now - start writing from begining*/
481 CDR_buffer_reset(c, 0);
483 /* TODO: If there is not enough space for reception,
484 * we should shift the already received data to the
485 * beginning of the buffer. */
486 rcvd = proto->recv(port,
488 c->wptr_max - c->wptr);
490 c->wptr_last = c->wptr;
492 /* While there are some data in the buffer, process them. */
493 while (CDR_data_size(c) > 0) {
494 len = CDR_data_size(c);
495 /* Wait for and then process message header */
496 if (!header_received) {
497 if (len >= forb_iop_MESSAGE_HEADER_SIZE) {
498 if (c->rptr % 8 != 0) {
499 ul_logerr("Header doesn't start at 8 byte bounday\n");
501 header_received = forb_iop_process_message_header(&mh, c);
502 len = CDR_data_size(c);
504 break; /* Wait for more data to arrive*/
508 /* Wait for and then process the message body */
509 if (header_received) {
510 if (len >= mh.message_size) {
511 process_message(port, &mh, c);
512 /* Wait for the next message */
513 header_received = false;
515 break; /* Wait for more data to arrive*/
524 discovery_cleanup(void *codec)
526 CDR_codec_release_buffer((CDR_Codec*)codec);
527 /* TODO: Broadcast some kind of bye bye message */
532 * Thread run for every port to broadcast HELLO messages. These
533 * messages are used for a FORB to discover all peers (and in future
534 * also to detect their disconnection).
536 * @param arg Pointer to ::forb_port_t typecasted to void *.
538 * @return Always NULL
540 void *forb_iop_discovery_thread(void *arg)
542 forb_port_t *port = arg;
543 const forb_proto_t *proto = port->proto;
545 fosa_abs_time_t hello_time;
546 fosa_rel_time_t hello_interval = fosa_msec_to_rel_time(1000*proto->hello_interval);
549 CDR_codec_init_static(&codec, port->forb->orb);
550 CDR_buffer_init(&codec, 1024, 0);
552 pthread_cleanup_push(discovery_cleanup, &codec);
554 /* Next hello interval is now */
555 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &hello_time);
557 while (!port->finish) {
558 /* Wait for next hello interval or until somebody
560 ret = forb_syncobj_timedwait(&port->hello, &hello_time);
561 /* sem_timedwait would be more appropriate */
562 if (ret == FOSA_ETIMEDOUT) {
563 hello_time = fosa_abs_time_incr(hello_time, hello_interval);
564 } else if (ret != 0) {
565 ul_logerr("hello syncobj error: %s\n", strerror(ret));
568 if (port->finish) break;
570 CDR_buffer_reset(&codec, forb_iop_MESSAGE_HEADER_SIZE);
571 forb_iop_prepare_hello(&codec, &port->forb->server_id, port->addr,
572 proto->serialize_addr);
573 /* printf("Broadcasting hello from port %p\n", port); */
574 proto->broadcast(port, &codec.buffer[codec.rptr],
575 CDR_data_size(&codec));
578 pthread_cleanup_pop(1);