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