~ubuntu-branches/ubuntu/utopic/python-omniorb/utopic

« back to all changes in this revision

Viewing changes to modules/pyORBFunc.cc

  • Committer: Bazaar Package Importer
  • Author(s): Floris Bruynooghe
  • Date: 2008-03-26 22:17:38 UTC
  • Revision ID: james.westby@ubuntu.com-20080326221738-r20t9hmikbvcg2vh
Tags: upstream-3.2
ImportĀ upstreamĀ versionĀ 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: C++; -*-
 
2
//                            Package   : omniORBpy
 
3
// pyORBFunc.cc               Created on: 2000/02/04
 
4
//                            Author    : Duncan Grisby (dpg1)
 
5
//
 
6
//    Copyright (C) 1999 AT&T Laboratories Cambridge
 
7
//
 
8
//    This file is part of the omniORBpy library
 
9
//
 
10
//    The omniORBpy library is free software; you can redistribute it
 
11
//    and/or modify it under the terms of the GNU Lesser General
 
12
//    Public License as published by the Free Software Foundation;
 
13
//    either version 2.1 of the License, or (at your option) any later
 
14
//    version.
 
15
//
 
16
//    This library is distributed in the hope that it will be useful,
 
17
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
//    GNU Lesser General Public License for more details.
 
20
//
 
21
//    You should have received a copy of the GNU Lesser General Public
 
22
//    License along with this library; if not, write to the Free
 
23
//    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
24
//    MA 02111-1307, USA
 
25
//
 
26
//
 
27
// Description:
 
28
//    ORB functions
 
29
 
 
30
// $Id: pyORBFunc.cc,v 1.1.4.3 2006/07/05 10:46:43 dgrisby Exp $
 
31
 
 
32
// $Log: pyORBFunc.cc,v $
 
33
// Revision 1.1.4.3  2006/07/05 10:46:43  dgrisby
 
34
// list_initial_services did not catch exceptions.
 
35
//
 
36
// Revision 1.1.4.2  2005/06/24 17:36:01  dgrisby
 
37
// Support for receiving valuetypes inside Anys; relax requirement for
 
38
// old style classes in a lot of places.
 
39
//
 
40
// Revision 1.1.4.1  2003/03/23 21:51:57  dgrisby
 
41
// New omnipy3_develop branch.
 
42
//
 
43
// Revision 1.1.2.9  2001/12/04 12:17:43  dpg1
 
44
// Cope with null ORB.
 
45
//
 
46
// Revision 1.1.2.8  2001/09/24 10:48:27  dpg1
 
47
// Meaningful minor codes.
 
48
//
 
49
// Revision 1.1.2.7  2001/09/20 14:51:25  dpg1
 
50
// Allow ORB reinitialisation after destroy(). Clean up use of omni namespace.
 
51
//
 
52
// Revision 1.1.2.6  2001/08/01 10:12:36  dpg1
 
53
// Main thread policy.
 
54
//
 
55
// Revision 1.1.2.5  2001/05/10 15:16:02  dpg1
 
56
// Big update to support new omniORB 4 internals.
 
57
//
 
58
// Revision 1.1.2.4  2001/03/13 10:38:07  dpg1
 
59
// Fixes from omnipy1_develop
 
60
//
 
61
// Revision 1.1.2.3  2000/12/04 18:57:23  dpg1
 
62
// Fix deadlock when trying to lock omniORB internal lock while holding
 
63
// the Python interpreter lock.
 
64
//
 
65
// Revision 1.1.2.2  2000/11/22 14:42:56  dpg1
 
66
// Fix segfault in string_to_object and resolve_initial_references with
 
67
// nil objref.
 
68
//
 
69
// Revision 1.1.2.1  2000/10/13 13:55:25  dpg1
 
70
// Initial support for omniORB 4.
 
71
//
 
72
 
 
73
#include <omnipy.h>
 
74
#include <pyThreadCache.h>
 
75
#include <corbaOrb.h>
 
76
#include <math.h>
 
77
 
 
78
extern "C" {
 
79
 
 
80
  static PyObject*
 
81
  pyORB_string_to_object(PyObject* self, PyObject* args)
 
82
  {
 
83
    PyObject* pyorb;
 
84
    char* s;
 
85
 
 
86
    if (!PyArg_ParseTuple(args, (char*)"Os", &pyorb, &s))
 
87
      return NULL;
 
88
 
 
89
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
90
 
 
91
    OMNIORB_ASSERT(orb);
 
92
 
 
93
    if (!s || strlen(s) == 0) {
 
94
      CORBA::INV_OBJREF ex;
 
95
      return omniPy::handleSystemException(ex);
 
96
    }
 
97
    CORBA::Object_ptr objref;
 
98
 
 
99
    try {
 
100
      objref = omniPy::stringToObject(s);
 
101
    }
 
102
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
103
    return omniPy::createPyCorbaObjRef(0, objref);
 
104
  }
 
105
 
 
106
  static PyObject*
 
107
  pyORB_object_to_string(PyObject* self, PyObject* args)
 
108
  {
 
109
    PyObject* pyorb;
 
110
    PyObject* pyobjref;
 
111
 
 
112
    if (!PyArg_ParseTuple(args, (char*)"OO", &pyorb, &pyobjref))
 
113
      return NULL;
 
114
 
 
115
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
116
 
 
117
    OMNIORB_ASSERT(orb);
 
118
 
 
119
    CORBA::Object_ptr objref;
 
120
 
 
121
    if (pyobjref == Py_None) {
 
122
      objref = CORBA::Object::_nil();
 
123
    }
 
124
    else {
 
125
      objref = (CORBA::Object_ptr)omniPy::getTwin(pyobjref, OBJREF_TWIN);
 
126
    }
 
127
    RAISE_PY_BAD_PARAM_IF(!objref, BAD_PARAM_WrongPythonType);
 
128
 
 
129
    CORBA::String_var str;
 
130
    try {
 
131
      omniPy::InterpreterUnlocker _u;
 
132
      str = orb->object_to_string(objref);
 
133
    }
 
134
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
135
    return PyString_FromString((char*)str);
 
136
  }
 
137
 
 
138
  static PyObject*
 
139
  pyORB_list_initial_services(PyObject* self, PyObject* args)
 
140
  {
 
141
    PyObject* pyorb;
 
142
 
 
143
    if (!PyArg_ParseTuple(args, (char*)"O", &pyorb))
 
144
      return NULL;
 
145
 
 
146
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
147
    OMNIORB_ASSERT(orb);
 
148
 
 
149
    CORBA::ORB::ObjectIdList_var ids;
 
150
    try {
 
151
      omniPy::InterpreterUnlocker _u;
 
152
      ids = orb->list_initial_services();
 
153
    }
 
154
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
155
 
 
156
    PyObject* pyids = PyList_New(ids->length());
 
157
 
 
158
    for (CORBA::ULong i=0; i<ids->length(); i++) {
 
159
      PyList_SetItem(pyids, i, PyString_FromString(ids[i]));
 
160
    }
 
161
    return pyids;
 
162
  }
 
163
 
 
164
  static PyObject*
 
165
  pyORB_resolve_initial_references(PyObject* self, PyObject* args)
 
166
  {
 
167
    PyObject* pyorb;
 
168
    char*     id;
 
169
 
 
170
    if (!PyArg_ParseTuple(args, (char*)"Os", &pyorb, &id))
 
171
      return NULL;
 
172
 
 
173
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
174
    OMNIORB_ASSERT(orb);
 
175
 
 
176
    CORBA::Object_ptr objref;
 
177
 
 
178
    try {
 
179
      omniPy::InterpreterUnlocker _u;
 
180
      objref = orb->resolve_initial_references(id);
 
181
 
 
182
      if (!(CORBA::is_nil(objref) || objref->_NP_is_pseudo())) {
 
183
        omniObjRef* cxxref = objref->_PR_getobj();
 
184
        omniObjRef* pyref  = omniPy::createObjRef(CORBA::Object::_PD_repoId,
 
185
                                                  cxxref->_getIOR(), 0, 0);
 
186
        CORBA::release(objref);
 
187
        objref =
 
188
          (CORBA::Object_ptr)pyref->_ptrToObjRef(CORBA::Object::_PD_repoId);
 
189
      }
 
190
    }
 
191
    catch (CORBA::ORB::InvalidName& ex) {
 
192
      PyObject* excc = PyObject_GetAttrString(pyorb, (char*)"InvalidName");
 
193
      OMNIORB_ASSERT(excc);
 
194
      PyObject* exci = PyEval_CallObject(excc, omniPy::pyEmptyTuple);
 
195
      PyErr_SetObject(excc, exci);
 
196
      return 0;
 
197
    }
 
198
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
199
 
 
200
    return omniPy::createPyCorbaObjRef(0, objref);
 
201
  }
 
202
 
 
203
  static PyObject*
 
204
  pyORB_work_pending(PyObject* self, PyObject* args)
 
205
  {
 
206
    PyObject* pyorb;
 
207
 
 
208
    if (!PyArg_ParseTuple(args, (char*)"O", &pyorb)) return NULL;
 
209
 
 
210
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
211
    OMNIORB_ASSERT(orb);
 
212
 
 
213
    CORBA::Boolean pending;
 
214
 
 
215
    try {
 
216
      omniPy::InterpreterUnlocker _u;
 
217
      pending = orb->work_pending();
 
218
    }
 
219
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
220
 
 
221
    return PyInt_FromLong(pending);
 
222
  }
 
223
 
 
224
  static PyObject*
 
225
  pyORB_perform_work(PyObject* self, PyObject* args)
 
226
  {
 
227
    PyObject* pyorb;
 
228
 
 
229
    if (!PyArg_ParseTuple(args, (char*)"O", &pyorb)) return NULL;
 
230
 
 
231
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
232
    OMNIORB_ASSERT(orb);
 
233
 
 
234
    try {
 
235
      omniPy::InterpreterUnlocker _u;
 
236
      orb->perform_work();
 
237
    }
 
238
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
239
 
 
240
    Py_INCREF(Py_None);
 
241
    return Py_None;
 
242
  }
 
243
 
 
244
  static PyObject*
 
245
  pyORB_run_timeout(PyObject* self, PyObject* args)
 
246
  {
 
247
    PyObject* pyorb;
 
248
    double    timeout;
 
249
 
 
250
    if (!PyArg_ParseTuple(args, (char*)"Od", &pyorb, &timeout)) return NULL;
 
251
 
 
252
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
253
    OMNIORB_ASSERT(orb);
 
254
 
 
255
    CORBA::Boolean shutdown;
 
256
    
 
257
    try {
 
258
      omniPy::InterpreterUnlocker _u;
 
259
      unsigned long s, ns;
 
260
      s  = (unsigned long)floor(timeout);
 
261
      ns = (unsigned long)((timeout - (double)s) * 1000000000.0);
 
262
      omni_thread::get_time(&s, &ns, s, ns);
 
263
      shutdown = ((omniOrbORB*)orb)->run_timeout(s, ns);
 
264
    }
 
265
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
266
 
 
267
    return PyInt_FromLong(shutdown);
 
268
  }
 
269
 
 
270
  static PyObject*
 
271
  pyORB_shutdown(PyObject* self, PyObject* args)
 
272
  {
 
273
    PyObject* pyorb;
 
274
    int       wait;
 
275
 
 
276
    if (!PyArg_ParseTuple(args, (char*)"Oi", &pyorb, &wait)) return NULL;
 
277
 
 
278
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
279
    OMNIORB_ASSERT(orb);
 
280
 
 
281
    try {
 
282
      omniPy::InterpreterUnlocker _u;
 
283
      orb->shutdown(wait);
 
284
    }
 
285
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
286
 
 
287
    Py_INCREF(Py_None);
 
288
    return Py_None;
 
289
  }
 
290
 
 
291
  static PyObject*
 
292
  pyORB_destroy(PyObject* self, PyObject* args)
 
293
  {
 
294
    PyObject* pyorb;
 
295
 
 
296
    if (!PyArg_ParseTuple(args, (char*)"O", &pyorb)) return NULL;
 
297
 
 
298
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
299
    OMNIORB_ASSERT(orb);
 
300
 
 
301
    try {
 
302
      omniPy::InterpreterUnlocker _u;
 
303
      orb->destroy();
 
304
    }
 
305
    OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
306
 
 
307
    Py_INCREF(Py_None);
 
308
    return Py_None;
 
309
  }
 
310
 
 
311
  static PyObject*
 
312
  pyORB_releaseRef(PyObject* self, PyObject* args)
 
313
  {
 
314
    PyObject* pyorb;
 
315
 
 
316
    if (!PyArg_ParseTuple(args, (char*)"O", &pyorb)) return NULL;
 
317
 
 
318
    CORBA::ORB_ptr orb = (CORBA::ORB_ptr)omniPy::getTwin(pyorb, ORB_TWIN);
 
319
 
 
320
    if (orb) {
 
321
      try {
 
322
        omniPy::InterpreterUnlocker _u;
 
323
        CORBA::release(orb);
 
324
      }
 
325
      OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
 
326
    }
 
327
    Py_INCREF(Py_None);
 
328
    return Py_None;
 
329
  }
 
330
 
 
331
 
 
332
  ////////////////////////////////////////////////////////////////////////////
 
333
  // Python method table                                                    //
 
334
  ////////////////////////////////////////////////////////////////////////////
 
335
 
 
336
  static PyMethodDef pyORB_methods[] = {
 
337
    {(char*)"string_to_object", pyORB_string_to_object,          METH_VARARGS},
 
338
    {(char*)"object_to_string", pyORB_object_to_string,          METH_VARARGS},
 
339
    {(char*)"list_initial_services",
 
340
                                pyORB_list_initial_services,     METH_VARARGS},
 
341
    {(char*)"resolve_initial_references",
 
342
                                pyORB_resolve_initial_references,METH_VARARGS},
 
343
    {(char*)"work_pending",     pyORB_work_pending,              METH_VARARGS},
 
344
    {(char*)"perform_work",     pyORB_perform_work,              METH_VARARGS},
 
345
    {(char*)"run_timeout",      pyORB_run_timeout,               METH_VARARGS},
 
346
    {(char*)"shutdown",         pyORB_shutdown,                  METH_VARARGS},
 
347
    {(char*)"destroy",          pyORB_destroy,                   METH_VARARGS},
 
348
    {(char*)"releaseRef",       pyORB_releaseRef,                METH_VARARGS},
 
349
    {NULL,NULL}
 
350
  };
 
351
}
 
352
 
 
353
 
 
354
void
 
355
omniPy::initORBFunc(PyObject* d)
 
356
{
 
357
  PyObject* m = Py_InitModule((char*)"_omnipy.orb_func", pyORB_methods);
 
358
  PyDict_SetItemString(d, (char*)"orb_func", m);
 
359
}