1 // -*- Mode: C++; -*-
2 //                            Package   : omniORBpy
3 // pyORBFunc.cc               Created on: 2000/02/04
4 //                            Author    : Duncan Grisby (dpg1)
5 //
6 //    Copyright (C) 2003-2014 Apasphere Ltd
7 //    Copyright (C) 1999 AT&T Laboratories Cambridge
8 //
9 //    This file is part of the omniORBpy library
10 //
11 //    The omniORBpy library is free software; you can redistribute it
12 //    and/or modify it under the terms of the GNU Lesser General
13 //    Public License as published by the Free Software Foundation;
14 //    either version 2.1 of the License, or (at your option) any later
15 //    version.
16 //
17 //    This library is distributed in the hope that it will be useful,
18 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //    GNU Lesser General Public License for more details.
21 //
22 //    You should have received a copy of the GNU Lesser General Public
23 //    License along with this library. If not, see http://www.gnu.org/licenses/
24 //
25 //
26 // Description:
27 //    ORB functions
28 
29 #include <omnipy.h>
30 #include <corbaOrb.h>
31 #include <math.h>
32 
33 extern "C" {
34 
35   static void
pyORB_dealloc(PyORBObject * self)36   pyORB_dealloc(PyORBObject* self)
37   {
38     {
39       omniPy::InterpreterUnlocker _u;
40       CORBA::release(self->orb);
41       CORBA::release(self->base.obj);
42     }
43     Py_TYPE(self)->tp_free((PyObject*)self);
44   }
45 
46   static PyObject*
pyORB_string_to_object(PyORBObject * self,PyObject * args)47   pyORB_string_to_object(PyORBObject* self, PyObject* args)
48   {
49     char* s;
50 
51     if (!PyArg_ParseTuple(args, (char*)"s", &s))
52       return NULL;
53 
54     if (!s || strlen(s) == 0) {
55       CORBA::INV_OBJREF ex;
56       return omniPy::handleSystemException(ex);
57     }
58     CORBA::Object_ptr objref;
59 
60     try {
61       objref = omniPy::stringToObject(s);
62     }
63     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
64 
65     return omniPy::createPyCorbaObjRef(0, objref);
66   }
67 
68   static PyObject*
pyORB_object_to_string(PyORBObject * self,PyObject * args)69   pyORB_object_to_string(PyORBObject* self, PyObject* args)
70   {
71     PyObject* pyobjref;
72 
73     if (!PyArg_ParseTuple(args, (char*)"O", &pyobjref))
74       return NULL;
75 
76     CORBA::Object_ptr objref;
77 
78     if (pyobjref == Py_None)
79       objref = CORBA::Object::_nil();
80     else
81       objref = omniPy::getObjRef(pyobjref);
82 
83     RAISE_PY_BAD_PARAM_IF(!objref, BAD_PARAM_WrongPythonType);
84 
85     CORBA::String_var str;
86     try {
87       omniPy::InterpreterUnlocker _u;
88       str = self->orb->object_to_string(objref);
89     }
90     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
91     return String_FromString((char*)str);
92   }
93 
94   static PyObject*
pyORB_register_initial_reference(PyORBObject * self,PyObject * args)95   pyORB_register_initial_reference(PyORBObject* self, PyObject* args)
96   {
97     char*     identifier;
98     PyObject* pyobjref;
99 
100     if (!PyArg_ParseTuple(args, (char*)"sO", &identifier, &pyobjref))
101       return NULL;
102 
103     CORBA::Object_ptr objref;
104 
105     if (pyobjref == Py_None)
106       objref = CORBA::Object::_nil();
107     else
108       objref = omniPy::getObjRef(pyobjref);
109 
110     RAISE_PY_BAD_PARAM_IF(!objref, BAD_PARAM_WrongPythonType);
111 
112     try {
113       omniPy::InterpreterUnlocker _u;
114       self->orb->register_initial_reference(identifier, objref);
115     }
116     catch (CORBA::ORB::InvalidName& ex) {
117       return omniPy::raiseScopedException(omniPy::pyCORBAmodule,
118                                           "ORB", "InvalidName");
119     }
120     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
121 
122     Py_INCREF(Py_None);
123     return Py_None;
124   }
125 
126   static PyObject*
pyORB_list_initial_services(PyORBObject * self,PyObject * args)127   pyORB_list_initial_services(PyORBObject* self, PyObject* args)
128   {
129     CORBA::ORB::ObjectIdList_var ids;
130     try {
131       omniPy::InterpreterUnlocker _u;
132       ids = self->orb->list_initial_services();
133     }
134     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
135 
136     PyObject* pyids = PyList_New(ids->length());
137 
138     for (CORBA::ULong i=0; i < ids->length(); i++) {
139       PyList_SetItem(pyids, i, String_FromString(ids[i]));
140     }
141     return pyids;
142   }
143 
144   static PyObject*
pyORB_resolve_initial_references(PyORBObject * self,PyObject * args)145   pyORB_resolve_initial_references(PyORBObject* self, PyObject* args)
146   {
147     char* id;
148 
149     if (!PyArg_ParseTuple(args, (char*)"s", &id))
150       return NULL;
151 
152     CORBA::Object_ptr objref;
153 
154     try {
155       omniPy::InterpreterUnlocker _u;
156       objref = self->orb->resolve_initial_references(id);
157 
158       if (!(CORBA::is_nil(objref) || objref->_NP_is_pseudo())) {
159 	omniObjRef* cxxref = objref->_PR_getobj();
160 	omniObjRef* pyref  = omniPy::createObjRef(CORBA::Object::_PD_repoId,
161 						  cxxref->_getIOR(), 0, 0);
162 	CORBA::release(objref);
163 	objref =
164 	  (CORBA::Object_ptr)pyref->_ptrToObjRef(CORBA::Object::_PD_repoId);
165       }
166     }
167     catch (CORBA::ORB::InvalidName& ex) {
168       return omniPy::raiseScopedException(omniPy::pyCORBAmodule,
169                                           "ORB", "InvalidName");
170     }
171     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
172 
173     return omniPy::createPyCorbaObjRef(0, objref);
174   }
175 
176   static PyObject*
pyORB_work_pending(PyORBObject * self,PyObject * args)177   pyORB_work_pending(PyORBObject* self, PyObject* args)
178   {
179     CORBA::Boolean pending;
180 
181     try {
182       omniPy::InterpreterUnlocker _u;
183       pending = self->orb->work_pending();
184     }
185     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
186 
187     return PyBool_FromLong(pending);
188   }
189 
190   static PyObject*
pyORB_perform_work(PyORBObject * self,PyObject * args)191   pyORB_perform_work(PyORBObject* self, PyObject* args)
192   {
193     try {
194       omniPy::InterpreterUnlocker _u;
195       self->orb->perform_work();
196     }
197     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
198 
199     Py_INCREF(Py_None);
200     return Py_None;
201   }
202 
203   static PyObject*
pyORB_run_timeout(PyORBObject * self,PyObject * args)204   pyORB_run_timeout(PyORBObject* self, PyObject* args)
205   {
206     double timeout;
207 
208     if (!PyArg_ParseTuple(args, (char*)"d", &timeout)) return NULL;
209 
210     CORBA::Boolean shutdown;
211 
212     try {
213       omniPy::InterpreterUnlocker _u;
214       unsigned long s, ns;
215       s  = (unsigned long)floor(timeout);
216       ns = (unsigned long)((timeout - (double)s) * 1000000000.0);
217       omni_thread::get_time(&s, &ns, s, ns);
218       shutdown = ((omniOrbORB*)self->orb)->run_timeout(s, ns);
219     }
220     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
221 
222     return PyBool_FromLong(shutdown);
223   }
224 
225   static PyObject*
pyORB_shutdown(PyORBObject * self,PyObject * args)226   pyORB_shutdown(PyORBObject* self, PyObject* args)
227   {
228     int wait;
229 
230     if (!PyArg_ParseTuple(args, (char*)"i", &wait)) return NULL;
231 
232     try {
233       omniPy::InterpreterUnlocker _u;
234       self->orb->shutdown(wait);
235     }
236     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
237 
238     Py_INCREF(Py_None);
239     return Py_None;
240   }
241 
242   static PyObject*
pyORB_destroy(PyORBObject * self,PyObject * args)243   pyORB_destroy(PyORBObject* self, PyObject* args)
244   {
245     try {
246       omniPy::InterpreterUnlocker _u;
247       self->orb->destroy();
248     }
249     OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
250 
251     Py_INCREF(Py_None);
252     return Py_None;
253   }
254 
255   ////////////////////////////////////////////////////////////////////////////
256   // Python method table                                                    //
257   ////////////////////////////////////////////////////////////////////////////
258 
259   static PyMethodDef pyORB_methods[] = {
260     {(char*)"string_to_object",
261      (PyCFunction)pyORB_string_to_object,
262      METH_VARARGS},
263 
264     {(char*)"object_to_string",
265      (PyCFunction)pyORB_object_to_string,
266      METH_VARARGS},
267 
268     {(char*)"register_initial_reference",
269      (PyCFunction)pyORB_register_initial_reference,
270      METH_VARARGS},
271 
272     {(char*)"list_initial_services",
273      (PyCFunction)pyORB_list_initial_services,
274      METH_NOARGS},
275 
276     {(char*)"resolve_initial_references",
277      (PyCFunction)pyORB_resolve_initial_references,
278      METH_VARARGS},
279 
280     {(char*)"work_pending",
281      (PyCFunction)pyORB_work_pending,
282      METH_NOARGS},
283 
284     {(char*)"perform_work",
285      (PyCFunction)pyORB_perform_work,
286      METH_NOARGS},
287 
288     {(char*)"run_timeout",
289      (PyCFunction)pyORB_run_timeout,
290      METH_VARARGS},
291 
292     {(char*)"shutdown",
293      (PyCFunction)pyORB_shutdown,
294      METH_VARARGS},
295 
296     {(char*)"destroy",
297      (PyCFunction)pyORB_destroy,
298      METH_NOARGS},
299 
300     {NULL,NULL}
301   };
302 
303   static PyTypeObject PyORBType = {
304     PyVarObject_HEAD_INIT(0,0)
305     (char*)"_omnipy.PyORBObject",      /* tp_name */
306     sizeof(PyORBObject),               /* tp_basicsize */
307     0,                                 /* tp_itemsize */
308     (destructor)pyORB_dealloc,         /* tp_dealloc */
309     0,                                 /* tp_print */
310     0,                                 /* tp_getattr */
311     0,                                 /* tp_setattr */
312     0,                                 /* tp_compare */
313     0,                                 /* tp_repr */
314     0,                                 /* tp_as_number */
315     0,                                 /* tp_as_sequence */
316     0,                                 /* tp_as_mapping */
317     0,                                 /* tp_hash  */
318     0,                                 /* tp_call */
319     0,                                 /* tp_str */
320     0,                                 /* tp_getattro */
321     0,                                 /* tp_setattro */
322     0,                                 /* tp_as_buffer */
323     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
324     (char*)"Internal ORB object",      /* tp_doc */
325     0,                                 /* tp_traverse */
326     0,                                 /* tp_clear */
327     0,                                 /* tp_richcompare */
328     0,                                 /* tp_weaklistoffset */
329     0,                                 /* tp_iter */
330     0,                                 /* tp_iternext */
331     pyORB_methods,                     /* tp_methods */
332   };
333 }
334 
335 
336 PyObject*
createPyORBObject(CORBA::ORB_ptr orb)337 omniPy::createPyORBObject(CORBA::ORB_ptr orb)
338 {
339   PyORBObject* self = PyObject_New(PyORBObject, &PyORBType);
340   self->orb = orb;
341   self->base.obj = CORBA::Object::_duplicate(orb);
342 
343   omniPy::PyRefHolder args(PyTuple_New(1));
344   PyTuple_SET_ITEM(args, 0, (PyObject*)self);
345 
346   return PyObject_CallObject(omniPy::pyCORBAORBClass, args);
347 }
348 
349 CORBA::Boolean
pyORBCheck(PyObject * pyobj)350 omniPy::pyORBCheck(PyObject* pyobj)
351 {
352   return pyobj->ob_type == &PyORBType;
353 }
354 
355 void
initORBFunc(PyObject * d)356 omniPy::initORBFunc(PyObject* d)
357 {
358   PyORBType.tp_base = omniPy::PyObjRefType;
359   int r = PyType_Ready(&PyORBType);
360   OMNIORB_ASSERT(r == 0);
361 }
362