]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/iop.c
forb_exception_occured renamed to forb_exception_occurred
[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
65 /** Version of the protocol */
66 #define VER_MAJOR 0
67 #define VER_MINOR 0
68
69 #define VER(major, minor) ((major)<<8 || (minor))
70
71 extern UL_LOG_CUST(ulogd_forb_iop);
72
73
74 CORBA_boolean
75 forb_iop_prepend_message_header(CDR_Codec *codec, forb_iop_message_type mt)
76 {
77         CORBA_boolean ret;
78         forb_iop_message_header mh;
79         mh.proto_version.major = VER_MAJOR;
80         mh.proto_version.minor = VER_MINOR;
81         mh.message_type = mt;
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);
85         if (ret) {
86                 ret = forb_iop_message_header_serialize(codec, &mh);
87         }
88         return ret;
89 }
90
91 CORBA_boolean
92 forb_iop_prepare_request(forb_request_t *req,
93                          char *iface,
94                          unsigned method_ind,
95                          CORBA_Environment *env)
96 {
97         CORBA_boolean ret;
98         forb_iop_request_header rh;
99
100         rh.request_id = req->request_id;
101         rh.iface = iface;
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);
106         if (ret) {
107                 /* Request body is 8 byte aligned */
108                 ret = CDR_put_align(&req->cdr_request, 8);
109         }
110         return ret;
111 }
112
113 CORBA_boolean
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))
118 {
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;
122         }
123         if (!forb_iop_prepend_message_header(codec, forb_iop_HELLO)) return CORBA_FALSE;
124         return CORBA_TRUE;
125 }
126
127 bool
128 forb_iop_process_message_header(forb_iop_message_header *mh, CDR_Codec *codec)
129 {
130         /* FIXME: If we have multiple protocol versions, use different
131          * type (independent from version) for return value instead of
132          * mh */
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:
141                                 case forb_iop_REPLY:
142                                 case forb_iop_HELLO:
143                                         break;
144                                 default:
145                                         return false;
146                         }
147                         codec->data_endian = (mh->flags && forb_iop_LITTLE_ENDIAN) ?
148                                 LittleEndian : BigEndian;                       
149                         CORBA_unsigned_long_deserialize(codec, &mh->message_size);
150                         return true;
151                         break;                  
152                 default:
153                         return false;
154         }
155 }
156
157 static inline CORBA_boolean
158 forb_exception_serialize(CDR_Codec *codec, struct forb_env *env)
159 {
160         return CORBA_long_serialize(codec, &env->major);
161 }
162
163 static inline CORBA_boolean
164 forb_exception_deserialize(CDR_Codec *codec, struct forb_env *env)
165 {
166         /* TODO: Declare exceptions in IDL and don't typecast here. */
167         return CORBA_long_deserialize(codec, (CORBA_long*)&env->major);
168 }
169
170 void
171 forb_iop_send_reply(forb_t *forb,
172            forb_server_id *dest,
173            CDR_Codec *codec,
174            CORBA_long request_id,
175            struct forb_env *env)
176 {
177         forb_iop_reply_header reply_header;
178         CORBA_boolean ret;
179         forb_peer_t *peer;
180         fosa_abs_time_t timeout;
181         
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);
189         }
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
192          * aligned */
193         ret = CDR_buffer_prepend(codec, forb_iop_REPLY_HEADER_SIZE);
194         if (!ret) {
195                 goto err;
196         }
197         ret = forb_iop_reply_header_serialize(codec, &reply_header);
198         if (!ret) {
199                 goto err;
200         }
201         forb_iop_prepend_message_header(codec, forb_iop_REPLY);
202
203         fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
204         timeout = fosa_abs_time_incr(timeout,
205                                      fosa_msec_to_rel_time(1000));
206
207         peer = forb_get_next_hop(forb, dest, &timeout);
208         if (!peer) {
209                 ul_logerr("Reply destination not found\n");
210                 goto err;
211         }
212         forb_proto_send(peer, codec);
213         forb_peer_put(peer);
214 err:
215         ;
216 }
217
218
219 static void
220 process_request(forb_port_t *port, CDR_Codec *codec, uint32_t message_size)
221 {
222         forb_iop_request_header request_header;
223         CORBA_boolean ret;
224         forb_object obj;
225         size_t n;
226         forb_t *forb = port->forb;
227         struct forb_env env;
228         CDR_Codec reply_codec;
229         forb_exec_req_t *exec_req;
230         uint32_t req_size, data_size, header_size;
231         char str[32];
232
233         data_size = CDR_data_size(codec);
234         ret = forb_iop_request_header_deserialize(codec, &request_header);
235         if (!ret) {
236                 ul_logerr("Malformed request recevied\n");
237                 env.major = FORB_EX_COMM_FAILURE;
238                 goto out;
239         }
240         ret = CDR_get_align(codec, 8);
241         if (!ret) {
242                 ul_logerr("Malformed request recevied\n");
243                 env.major = FORB_EX_COMM_FAILURE;
244                 goto out;
245         }
246
247         header_size = data_size - CDR_data_size(codec);
248         req_size = message_size - header_size;
249
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);
255         
256         obj = forb_key_to_object(forb, request_header.objkey);
257         if (!obj) {
258                 ul_logerr("Nonexistent object key\n");
259                 env.major = FORB_EX_OBJECT_NOT_EXIST;
260                 goto send_execption;
261         }
262
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");
267                 goto send_execption;
268         }
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");
272                 goto send_execption;
273         }
274
275         if (!obj->executor) {
276                 env.major = FORB_EX_NO_IMPLEMENT;
277                 ul_logerr("No executor for object\n");
278                 goto send_execption;
279
280         }
281
282         /* Enqueue execution request */
283         exec_req = forb_malloc(sizeof(*exec_req));
284         if (exec_req) {
285                 exec_req->request_id = request_header.request_id;
286                 exec_req->source = request_header.source;
287                 exec_req->obj = obj;
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,
292                                       req_size,
293                                       0);
294                 if (!ret) {
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);
298                         goto send_execption;
299                 }
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);
305         } else {
306                 env.major = FORB_EX_NO_MEMORY;
307                 ul_logerr("No memory for executor request\n");
308                 goto send_execption;
309         }
310         
311 out:
312         return;
313         
314 send_execption:
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);
319         if (!ret) {
320                 ul_logerr("No memory for exception reply buffer\n");
321                 return;
322         }
323
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);
327 }
328
329 static void
330 process_reply(forb_port_t *port, CDR_Codec *codec)
331 {
332         forb_iop_reply_header rh;
333         forb_t *forb = port->forb;
334         forb_request_t *req;
335
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);
341         if (!req) {
342                 ul_logerr("Received reply to unknown request_id %u\n", rh.request_id);
343                 return;
344         }
345         forb_request_delete(forb, req); /* Deregister request from forb */
346
347         if (rh.flags & forb_iop_FLAG_EXCEPTION) {
348                 forb_exception_deserialize(codec, req->env);
349         } else {
350                 req->cdr_reply = codec;
351         }
352
353         /* Tell the stub where to signal that reply processing is
354          * finished */
355         req->reply_processed = &port->reply_processed;
356
357         /* Resume the stub waiting in forb_wait_for_reply() */
358         forb_syncobj_signal(&req->reply_ready);
359
360         /* Wait for stub to process the results from the codec's buffer */
361         forb_syncobj_wait(&port->reply_processed);
362 }
363
364 /** 
365  * Process incomming HELLO messages.
366  *
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.
371  * 
372  * @param port Port, where hello was received
373  * @param codec Buffer with the hello message
374  */
375 static void
376 process_hello(forb_port_t *port, CDR_Codec *codec)
377 {
378         forb_server_id server_id;
379         void *addr = NULL;
380         forb_peer_t *peer;
381         forb_t *forb = port->forb;
382
383 /*      printf("Hello received at port %p\n", port); */
384
385         forb_server_id_deserialize(codec, &server_id);
386         {
387                 char str[60];
388                 ul_logdeb("rcvd hello from %s\n", forb_server_id_to_string(str, &server_id, sizeof(str)));
389         }
390         if (port->proto->deserialize_addr) {
391                 port->proto->deserialize_addr(codec, &addr);
392         }
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 */
397                         forb_peer_put(peer);
398                 } else {
399                         /* New peer discovered */
400                         bool notify_waiters = false;
401                         if (peer /* && peer->state == FORB_PEER_WANTED */) {
402                                 notify_waiters = true;
403                         } else {
404                                 peer = forb_peer_new();
405                         }
406                         if (peer) {
407                                 fosa_mutex_lock(&forb->peer_mutex);
408                                 peer->server_id = server_id;
409                                 peer->port = port;
410                                 peer->addr = addr;
411                                 peer->state = FORB_PEER_DISCOVERED;
412                                 if (notify_waiters) {
413                                         fosa_cond_broadcast(&peer->cond);
414                                 } else {
415                                         forb_peer_get(peer);
416                                         forb_peer_nolock_insert(forb, peer);
417                                 }
418                                 forb_peer_get(peer);
419                                 forb_port_peer_ins_tail(port, peer);
420                                 fosa_mutex_unlock(&forb->peer_mutex);
421                                 forb_peer_put(peer);
422                         }
423                         /* Broadcast our hello packet now */
424                         forb_syncobj_signal(&port->hello);
425                 }
426         }
427 }
428
429 static void
430 process_message(forb_port_t *port, const forb_iop_message_header *mh,
431                 CDR_Codec *codec)
432 {
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. */
436
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);
442                         break;
443                 case forb_iop_REPLY:
444                         process_reply(port, codec);
445                         break;
446                 case forb_iop_HELLO:
447                         process_hello(port, codec);
448                         break;
449                 default:
450                         ul_logmsg("rcvd unknown message type\n");
451                         break;
452         }
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);
457                           ;
458                 codec->rptr += mh->message_size - processed;
459         }
460 }
461
462 /** 
463  * Thread run for every port to receive FORB messages from that port. 
464  * 
465  * @param arg Pointer to ::forb_port_t typecasted to void *.
466  * 
467  * @return Always NULL
468  */
469 void *forb_iop_receiver_thread(void *arg)
470 {
471         forb_port_t *port = arg;
472         const forb_proto_t *proto = port->proto;
473         CDR_Codec *c = &port->codec;
474         size_t rcvd, len;
475         forb_iop_message_header mh;
476         bool header_received = false;
477
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);
482                 }
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,
487                                    &c->buffer[c->wptr],
488                                    c->wptr_max - c->wptr);
489                 c->wptr += rcvd;
490                 c->wptr_last = c->wptr;
491
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");
500                                         }
501                                         header_received = forb_iop_process_message_header(&mh, c);
502                                         len = CDR_data_size(c);
503                                 } else {
504                                         break; /* Wait for more data to arrive*/
505                                 }
506                         }
507                         
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;
514                                 } else {
515                                         break; /* Wait for more data to arrive*/
516                                 }
517                         }
518                 }
519         }
520         return NULL;
521 }
522
523 static void
524 discovery_cleanup(void *codec)
525 {
526         CDR_codec_release_buffer((CDR_Codec*)codec);
527         /* TODO: Broadcast some kind of bye bye message */
528 }
529
530
531 /** 
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).
535  * 
536  * @param arg Pointer to ::forb_port_t typecasted to void *.
537  * 
538  * @return Always NULL
539  */
540 void *forb_iop_discovery_thread(void *arg)
541 {
542         forb_port_t *port = arg;
543         const forb_proto_t *proto = port->proto;
544         CDR_Codec codec;
545         fosa_abs_time_t hello_time;
546         fosa_rel_time_t hello_interval = fosa_msec_to_rel_time(1000*proto->hello_interval);
547         int ret;
548
549         CDR_codec_init_static(&codec, port->forb->orb);
550         CDR_buffer_init(&codec, 1024, 0);
551
552         pthread_cleanup_push(discovery_cleanup, &codec);
553         
554         /* Next hello interval is now */
555         fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &hello_time);
556         
557         while (!port->finish) {
558                 /* Wait for next hello interval or until somebody
559                  * signals us. */
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));
566                 }
567
568                 if (port->finish) break;
569
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));
576         }
577
578         pthread_cleanup_pop(1);
579         return NULL;
580 }
581
582