]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/forb/src/object.c
forb: object serialization working even for inter-executor invocation
[frescor/frsh-forb.git] / src / forb / src / object.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   object.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Sun Oct 12 17:03:06 2008
51  * 
52  * @brief  Functions for manipulation with FORB's object references.
53  * 
54  * 
55  */
56
57 #include <forb/forb-internal.h>
58 #include <forb/object.h>
59 #include <stdio.h>
60 #include <forb/cdr_codec.h>
61 #include <forb/cdr.h>
62 #include "forb_utils.h"
63 #include <inttypes.h>
64 #include "object_type.h"
65 #include <forb/types.h>
66 #include <forb/server_id.h>
67
68 GAVL_CUST_NODE_INT_IMP(forb_objects_nolock /* cust_prefix */,
69                        forb_t /* cust_root_t */,
70                        struct _forb_object /* cust_item_t */,
71                        forb_object_key /* cust_key_t */,
72                        objects /* cust_root_node */,
73                        node /* cust_item_node */,
74                        objkey /* cust_item_key */,
75                        forb_objkey_cmp/* cust_cmp_fnc */);
76
77 /** 
78  * Creates a new object reference.
79  * 
80  * @param orb FORB object this object reference is handled by. This
81  * can be NULL only when local forb_orb reference is created in
82  * forb_init().
83  * 
84  * @param server_id Server ID of the remote FORB, where this object is
85  * implemented or NULL if the object is implemented locally by @a orb.
86  * 
87  * @param key Key of the remote object. If server_id is NULL this
88  * value is ignored and objkey is assigned internally.
89  * 
90  * @return FORB object reference of NULL in case of error.
91  */
92 forb_object
93 forb_object_new(forb_orb orb,
94                forb_server_id *server_id,
95                forb_object_key key)
96 {
97         forb_object obj = forb_malloc(sizeof(*obj));
98         if (obj) {
99                 memset(obj, 0, sizeof(*obj));
100                 forb_ref_init(&obj->ref);
101                 obj->orb = orb ? orb : obj;
102                 if (server_id) {
103                         /* remote object */
104                         obj->server = *server_id;
105                         obj->objkey = key;
106                 } else {
107                         /* local object */
108                         if (orb) {
109                                 /* Normal local object */
110                                 forb_t *forb = forb_object_to_forb(obj);
111                                 obj->server = forb->server_id;
112                                 fosa_mutex_lock(&forb->objkey_mutex);
113                                 obj->objkey = ++forb->objkey;
114                                 /* We do not use object references for tree pointers. */
115                                 forb_objects_nolock_insert(forb, obj);
116                                 fosa_mutex_unlock(&forb->objkey_mutex);
117                         } else {
118                                 /* forb_orb object. The forb_init()
119                                  *  function must assign server field
120                                  *  and put the object into the
121                                  *  forb->objects  */
122                                 obj->objkey = 0;
123                         }
124                 }
125         }
126         return obj;
127 }
128
129 /** 
130  * This function does the real release of memory when reference count
131  * is zero.
132  *
133  * @note We add this function the _real suffix because the
134  * forb_object_release() is meant for applications use and we want the
135  * name similar to CORBA_Object_release().
136  */
137 static void
138 forb_object_release_real(forb_ref_t *ref)
139 {
140         forb_object obj = container_of(ref, struct _forb_object, ref);
141         
142         if (obj->orb) {
143                 forb_t *forb = forb_object_to_forb(obj);
144                 if (forb_server_id_cmp(&obj->server, &forb->server_id) == 0) {
145                         /* Local object - unregister it from FORB. */
146                         forb_objects_delete(forb, obj);
147                 }
148         }
149         forb_free(obj);
150 }
151
152 static inline void forb_object_get(forb_object obj)
153 {
154         forb_ref_get(&obj->ref);
155 }
156
157 static inline void forb_object_put(forb_object obj)
158 {
159         forb_ref_put(&obj->ref, forb_object_release_real);
160         /* TODO: Exit executor. Question: Shall the executor have its
161          * own refcount? Is so, how do we know to exit/kill it?
162          * Possible answer: The executor might be notified from here
163          * and then exit if it owns the last reference. */
164 }
165
166 /** 
167  * Releases all memory associated with a object reference and
168  * deregister the object from FORB.
169  * 
170  * @param obj Object reference to release
171  */
172 void forb_object_release(forb_object obj)
173 {
174         forb_object_put(obj);
175 }
176
177 /** 
178  * Returns the object's key.
179  * 
180  * @param obj 
181  * 
182  * @return The key.
183  */
184 forb_object_key forb_object_to_key(forb_object obj)
185 {
186         return obj->objkey;
187 }
188
189 /** 
190  * Returns the objects reference of a given key.
191  * 
192  * @param forb 
193  * @param key 
194  * 
195  * @return The object reference or NULL in case the object with a
196  * given key doesn't exist.
197  */
198 forb_object forb_key_to_object(forb_t *forb, forb_object_key key)
199 {
200         forb_object obj;
201         obj = forb_objects_find(forb, key);
202         return obj;
203 }
204
205 /** 
206  * Converts object reference to string.
207  * 
208  * @param obj Object reference to convert.
209  * 
210  * @return String, which must be freed by forb_free() or NULL in case
211  * of error.
212  */
213 char *
214 forb_object_to_string(const forb_object obj)
215 {
216         char *str;
217         size_t s;
218         forb_object_key key;
219         char server_id[65];
220         
221         s = 2*sizeof(forb_server_id)+1+2*sizeof(size_t)+1;
222         str = malloc(s);
223         if (!str) {
224                 return NULL;
225         }
226
227         forb_server_id_to_string(server_id, &obj->server, s);
228         key = forb_object_to_key(obj);
229         snprintf(str, s, "%s-%"PRIu64, server_id, key);
230
231         return str;
232 }
233
234 /** 
235  * Creates object reference from string.
236  *
237  * The string should be generated by forb_object_to_string() in the
238  * same or another FORB. When the returned reference is not needed, it
239  * should be freed by forb_object_release().
240  * 
241  * @param orb FORB which should handle the object.
242  * @param string The string returned by forb_object_to_string().
243  * 
244  * @return FORB object reference or NULL in case of error.
245  */
246 forb_object
247 forb_string_to_object(const forb_orb orb, const char *string)
248 {
249         forb_object obj;
250         forb_object_key key;
251         forb_server_id server_id;
252         int ret;
253
254         if (forb_server_id_from_string(&server_id, string) == NULL)
255                 return NULL;
256
257         if (string[2*sizeof(server_id)] != '-')
258                 return NULL;
259
260         ret = sscanf(&string[2*sizeof(server_id)+1], "%"SCNu64, &key);
261         if (ret == 0 || ret == EOF)
262                 return NULL;
263         
264         // check if the object you want to create is local or not
265         if (!forb_server_id_cmp(&orb->server, &server_id))              
266                 obj = forb_objects_find(forb_data(orb), key);
267         else
268                 obj = forb_object_new(orb, &server_id, key);
269         
270         return obj;
271 }
272
273 /** 
274  * Duplicates the object reference.
275  * 
276  * @param obj Object reference to duplicate.
277  * 
278  * @return The new copy of @a obj or NULL in case of error. The
279  * duplicated object should be released later by forb_object_release().
280  */
281 forb_object
282 forb_object_duplicate(const forb_object obj)
283 {
284         if (!obj) {
285                 return NULL;
286         }
287         forb_object_get(obj);
288         return obj;
289 }
290
291 /** 
292  * Serialize the object reference.
293  * 
294  * @param codec 
295  * @param obj 
296  * 
297  * @return CORBA_TRUE on success, CORBA_FALSE on error.
298  */
299 CORBA_boolean
300 forb_object_serialize(FORB_CDR_Codec *codec, const forb_object *obj)
301 {
302         if (!forb_server_id_serialize(codec, &(*obj)->server))
303                 return CORBA_FALSE;
304         if (!forb_object_key_serialize(codec, &(*obj)->objkey))
305                 return CORBA_FALSE;
306         return CORBA_TRUE;
307 }
308
309 /** 
310  * Creates the object reference by deserialization from ::FORB_CDR_Codec.
311  * 
312  * @param codec 
313  * @param obj 
314  * 
315  * @return CORBA_TRUE on success, CORBA_FALSE on error.
316  */
317 CORBA_boolean
318 forb_object_deserialize(FORB_CDR_Codec *codec, forb_object *obj)
319 {
320         forb_server_id server_id;
321         forb_object_key objkey;
322         if (!forb_server_id_deserialize(codec, &server_id))
323                 return CORBA_FALSE;
324         if (!forb_object_key_deserialize(codec, &objkey))
325                 return CORBA_FALSE;
326         if (!codec->orb)
327                 return CORBA_FALSE;
328         
329         // check if the object you want to create is local or not
330         if (!forb_server_id_cmp(&codec->orb->server, &server_id))
331                 *obj = forb_objects_find(forb_data(codec->orb), objkey);
332         else
333                 *obj = forb_object_new(codec->orb, &server_id, objkey);
334         return CORBA_TRUE;
335 }