author | Gustavo J. A. M. Carneiro <gjc@inescporto.pt> |
Wed, 15 Oct 2008 15:55:09 +0100 | |
changeset 3753 | a84a48233eb3 |
parent 3722 | 38ace50047e1 |
child 3929 | 909b0a724ed3 |
permissions | -rw-r--r-- |
3408 | 1 |
#include "ns3module.h" |
2 |
||
3 |
||
4 |
namespace ns3{ |
|
5 |
||
6 |
void PythonCompleteConstruct (Ptr<Object> object, TypeId typeId, const AttributeList &attributes) |
|
7 |
{ |
|
8 |
object->SetTypeId (typeId); |
|
9 |
object->Object::Construct (attributes); |
|
10 |
} |
|
11 |
||
12 |
} |
|
13 |
||
14 |
||
15 |
||
16 |
class PythonEventImpl : public ns3::EventImpl |
|
17 |
{ |
|
18 |
private: |
|
19 |
PyObject *m_callback; |
|
20 |
PyObject *m_args; |
|
21 |
public: |
|
22 |
PythonEventImpl (PyObject *callback, PyObject *args) |
|
23 |
{ |
|
24 |
m_callback = callback; |
|
25 |
Py_INCREF(m_callback); |
|
26 |
m_args = args; |
|
27 |
Py_INCREF(m_args); |
|
28 |
} |
|
29 |
virtual ~PythonEventImpl () |
|
30 |
{ |
|
3722
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
31 |
PyGILState_STATE __py_gil_state; |
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
32 |
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0); |
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
33 |
|
3408 | 34 |
Py_DECREF(m_callback); |
35 |
Py_DECREF(m_args); |
|
3722
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
36 |
|
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
37 |
if (PyEval_ThreadsInitialized()) |
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
38 |
PyGILState_Release(__py_gil_state); |
3408 | 39 |
} |
40 |
virtual void Notify () |
|
41 |
{ |
|
3702
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
42 |
PyGILState_STATE __py_gil_state; |
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
43 |
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0); |
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
44 |
|
3408 | 45 |
PyObject *retval = PyObject_CallObject(m_callback, m_args); |
46 |
if (retval) { |
|
47 |
if (retval != Py_None) { |
|
48 |
PyErr_SetString(PyExc_TypeError, "event callback should return None"); |
|
49 |
PyErr_Print(); |
|
50 |
} |
|
51 |
Py_DECREF(retval); |
|
52 |
} else { |
|
53 |
PyErr_Print(); |
|
54 |
} |
|
3702
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
55 |
|
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
56 |
if (PyEval_ThreadsInitialized()) |
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
57 |
PyGILState_Release(__py_gil_state); |
3408 | 58 |
} |
59 |
}; |
|
60 |
||
61 |
||
62 |
PyObject * |
|
63 |
_wrap_Simulator_Schedule(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
|
64 |
PyObject **return_exception) |
|
65 |
{ |
|
66 |
PyObject *exc_type, *traceback; |
|
67 |
PyObject *py_time; |
|
68 |
PyObject *py_callback; |
|
69 |
PyObject *user_args; |
|
70 |
ns3::Ptr<PythonEventImpl> py_event_impl; |
|
71 |
PyNs3EventId *py_EventId; |
|
72 |
||
73 |
if (kwargs && PyObject_Length(kwargs) > 0) { |
|
74 |
PyErr_SetString(PyExc_TypeError, "keyword arguments not supported"); |
|
75 |
goto error; |
|
76 |
} |
|
77 |
||
78 |
if (PyTuple_GET_SIZE(args) < 2) { |
|
79 |
PyErr_SetString(PyExc_TypeError, "ns3.Simulator.Schedule needs at least 2 arguments"); |
|
80 |
goto error; |
|
81 |
} |
|
82 |
py_time = PyTuple_GET_ITEM(args, 0); |
|
83 |
py_callback = PyTuple_GET_ITEM(args, 1); |
|
84 |
||
85 |
if (!PyObject_IsInstance(py_time, (PyObject*) &PyNs3Time_Type)) { |
|
86 |
PyErr_SetString(PyExc_TypeError, "Parameter 1 should be a ns3.Time instance"); |
|
87 |
goto error; |
|
88 |
} |
|
89 |
if (!PyCallable_Check(py_callback)) { |
|
90 |
PyErr_SetString(PyExc_TypeError, "Parameter 2 should be callable"); |
|
91 |
goto error; |
|
92 |
} |
|
93 |
user_args = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args)); |
|
94 |
py_event_impl = ns3::Create<PythonEventImpl>(py_callback, user_args); |
|
95 |
Py_DECREF(user_args); |
|
96 |
||
97 |
py_EventId = PyObject_New(PyNs3EventId, &PyNs3EventId_Type); |
|
98 |
py_EventId->obj = new ns3::EventId( |
|
99 |
ns3::Simulator::Schedule(*((PyNs3Time *) py_time)->obj, py_event_impl)); |
|
100 |
return (PyObject *) py_EventId; |
|
101 |
||
102 |
error: |
|
103 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
|
104 |
Py_XDECREF(exc_type); |
|
105 |
Py_XDECREF(traceback); |
|
106 |
return NULL; |
|
107 |
} |
|
108 |
||
109 |
||
110 |
PyObject * |
|
111 |
_wrap_Simulator_ScheduleNow(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
|
112 |
PyObject **return_exception) |
|
113 |
{ |
|
114 |
PyObject *exc_type, *traceback; |
|
115 |
PyObject *py_callback; |
|
116 |
PyObject *user_args; |
|
117 |
ns3::Ptr<PythonEventImpl> py_event_impl; |
|
118 |
PyNs3EventId *py_EventId; |
|
119 |
||
120 |
if (kwargs && PyObject_Length(kwargs) > 0) { |
|
121 |
PyErr_SetString(PyExc_TypeError, "keyword arguments not supported"); |
|
122 |
goto error; |
|
123 |
} |
|
124 |
||
125 |
if (PyTuple_GET_SIZE(args) < 1) { |
|
126 |
PyErr_SetString(PyExc_TypeError, "ns3.Simulator.Schedule needs at least 1 argument"); |
|
127 |
goto error; |
|
128 |
} |
|
129 |
py_callback = PyTuple_GET_ITEM(args, 0); |
|
130 |
||
131 |
if (!PyCallable_Check(py_callback)) { |
|
132 |
PyErr_SetString(PyExc_TypeError, "Parameter 2 should be callable"); |
|
133 |
goto error; |
|
134 |
} |
|
135 |
user_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|
136 |
py_event_impl = ns3::Create<PythonEventImpl>(py_callback, user_args); |
|
137 |
Py_DECREF(user_args); |
|
138 |
||
139 |
py_EventId = PyObject_New(PyNs3EventId, &PyNs3EventId_Type); |
|
140 |
py_EventId->obj = new ns3::EventId(ns3::Simulator::ScheduleNow(py_event_impl)); |
|
141 |
return (PyObject *) py_EventId; |
|
142 |
||
143 |
error: |
|
144 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
|
145 |
Py_XDECREF(exc_type); |
|
146 |
Py_XDECREF(traceback); |
|
147 |
return NULL; |
|
148 |
} |
|
149 |
||
150 |
||
151 |
PyObject * |
|
152 |
_wrap_Simulator_ScheduleDestroy(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
|
153 |
PyObject **return_exception) |
|
154 |
{ |
|
155 |
PyObject *exc_type, *traceback; |
|
156 |
PyObject *py_callback; |
|
157 |
PyObject *user_args; |
|
158 |
ns3::Ptr<PythonEventImpl> py_event_impl; |
|
159 |
PyNs3EventId *py_EventId; |
|
160 |
||
161 |
if (kwargs && PyObject_Length(kwargs) > 0) { |
|
162 |
PyErr_SetString(PyExc_TypeError, "keyword arguments not supported"); |
|
163 |
goto error; |
|
164 |
} |
|
165 |
||
166 |
if (PyTuple_GET_SIZE(args) < 1) { |
|
167 |
PyErr_SetString(PyExc_TypeError, "ns3.Simulator.Schedule needs at least 1 argument"); |
|
168 |
goto error; |
|
169 |
} |
|
170 |
py_callback = PyTuple_GET_ITEM(args, 0); |
|
171 |
||
172 |
if (!PyCallable_Check(py_callback)) { |
|
173 |
PyErr_SetString(PyExc_TypeError, "Parameter 2 should be callable"); |
|
174 |
goto error; |
|
175 |
} |
|
176 |
user_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|
177 |
py_event_impl = ns3::Create<PythonEventImpl>(py_callback, user_args); |
|
178 |
Py_DECREF(user_args); |
|
179 |
||
180 |
py_EventId = PyObject_New(PyNs3EventId, &PyNs3EventId_Type); |
|
181 |
py_EventId->obj = new ns3::EventId(ns3::Simulator::ScheduleDestroy(py_event_impl)); |
|
182 |
return (PyObject *) py_EventId; |
|
183 |
||
184 |
error: |
|
185 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
|
186 |
Py_XDECREF(exc_type); |
|
187 |
Py_XDECREF(traceback); |
|
188 |
return NULL; |
|
189 |
} |
|
190 |
||
3753
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
191 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
192 |
PyObject * |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
193 |
_wrap_TypeId_LookupByNameFailSafe(PyNs3TypeId *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
194 |
PyObject **return_exception) |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
195 |
{ |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
196 |
bool ok; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
197 |
const char *name; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
198 |
Py_ssize_t name_len; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
199 |
ns3::TypeId tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
200 |
PyNs3TypeId *py_tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
201 |
const char *keywords[] = {"name", NULL}; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
202 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
203 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s#", (char **) keywords, &name, &name_len)) { |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
204 |
PyObject *exc_type, *traceback; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
205 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
206 |
Py_XDECREF(exc_type); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
207 |
Py_XDECREF(traceback); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
208 |
return NULL; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
209 |
} |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
210 |
ok = ns3::TypeId::LookupByNameFailSafe(std::string(name, name_len), &tid); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
211 |
if (!ok) |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
212 |
{ |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
213 |
PyErr_Format(PyExc_KeyError, "The ns3 type with name `%s' is not registered", name); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
214 |
return NULL; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
215 |
} |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
216 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
217 |
py_tid = PyObject_New(PyNs3TypeId, &PyNs3TypeId_Type); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
218 |
py_tid->obj = new ns3::TypeId (tid); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
219 |
PyNs3TypeId_wrapper_registry[(void *) py_tid->obj] = (PyObject *) py_tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
220 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
221 |
return (PyObject *) py_tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
222 |
} |