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