1 | /********************************************************************************************************* |
---|
2 | * Software License Agreement (BSD License) * |
---|
3 | * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
---|
4 | * * |
---|
5 | * Copyright (c) 2010, WIDE Project and NICT * |
---|
6 | * All rights reserved. * |
---|
7 | * * |
---|
8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
---|
9 | * permitted provided that the following conditions are met: * |
---|
10 | * * |
---|
11 | * * Redistributions of source code must retain the above * |
---|
12 | * copyright notice, this list of conditions and the * |
---|
13 | * following disclaimer. * |
---|
14 | * * |
---|
15 | * * Redistributions in binary form must reproduce the above * |
---|
16 | * copyright notice, this list of conditions and the * |
---|
17 | * following disclaimer in the documentation and/or other * |
---|
18 | * materials provided with the distribution. * |
---|
19 | * * |
---|
20 | * * Neither the name of the WIDE Project or NICT nor the * |
---|
21 | * names of its contributors may be used to endorse or * |
---|
22 | * promote products derived from this software without * |
---|
23 | * specific prior written permission of WIDE Project and * |
---|
24 | * NICT. * |
---|
25 | * * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
---|
27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
---|
28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
---|
29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
---|
30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
---|
32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
---|
33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
---|
34 | *********************************************************************************************************/ |
---|
35 | |
---|
36 | /* Do not include this directly, use dbg_interactive.i instead */ |
---|
37 | |
---|
38 | /****** ROUTING *********/ |
---|
39 | |
---|
40 | struct rt_data { |
---|
41 | }; |
---|
42 | |
---|
43 | %extend rt_data { |
---|
44 | rt_data() { |
---|
45 | struct rt_data * r = NULL; |
---|
46 | int ret = fd_rtd_init(&r); |
---|
47 | if (ret != 0) { |
---|
48 | DI_ERROR(ret, NULL, NULL); |
---|
49 | return NULL; |
---|
50 | } |
---|
51 | return r; |
---|
52 | } |
---|
53 | ~rt_data() { |
---|
54 | struct rt_data *r = self; |
---|
55 | fd_rtd_free(&r); |
---|
56 | } |
---|
57 | void add(char * peerid, char * realm) { |
---|
58 | int ret = fd_rtd_candidate_add($self, peerid, realm); |
---|
59 | if (ret != 0) { |
---|
60 | DI_ERROR(ret, NULL, NULL); |
---|
61 | } |
---|
62 | } |
---|
63 | void remove(char * STRING, size_t LENGTH) { |
---|
64 | fd_rtd_candidate_del($self, STRING, LENGTH); |
---|
65 | } |
---|
66 | void error(char * dest, char * STRING, size_t LENGTH, uint32_t rcode) { |
---|
67 | int ret = fd_rtd_error_add($self, dest, (uint8_t *)STRING, LENGTH, rcode); |
---|
68 | if (ret != 0) { |
---|
69 | DI_ERROR(ret, NULL, NULL); |
---|
70 | } |
---|
71 | } |
---|
72 | struct fd_list * extract(int score = 0) { |
---|
73 | struct fd_list * li = NULL; |
---|
74 | fd_rtd_candidate_extract($self, &li, score); |
---|
75 | return li; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | %extend rtd_candidate { |
---|
82 | void dump() { |
---|
83 | fd_log_debug("candidate %p\n", $self); |
---|
84 | fd_log_debug(" id : %s\n", $self->diamid); |
---|
85 | fd_log_debug(" rlm: %s\n", $self->realm); |
---|
86 | fd_log_debug(" sc : %d\n", $self->score); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | %{ |
---|
92 | /* call it (will be called from a different thread than the interpreter, when message arrives) */ |
---|
93 | static int call_the_python_rt_fwd_callback(void * pycb, struct msg **msg) { |
---|
94 | PyObject *PyMsg; |
---|
95 | PyObject *cb, *result = NULL; |
---|
96 | int ret = 0; |
---|
97 | |
---|
98 | if (!pycb) { |
---|
99 | fd_log_debug("Internal error: missing the callback!\n"); |
---|
100 | return ENOTSUP; |
---|
101 | } |
---|
102 | cb = pycb; |
---|
103 | |
---|
104 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
---|
105 | /* Convert the arguments */ |
---|
106 | PyMsg = SWIG_NewPointerObj((void *)*msg, SWIGTYPE_p_msg, 0 ); |
---|
107 | |
---|
108 | /* Call the function */ |
---|
109 | result = PyEval_CallFunction(cb, "(O)", PyMsg); |
---|
110 | |
---|
111 | /* The result is supposedly composed of: [ ret, *msg ] */ |
---|
112 | if ((result == NULL) || (!PyList_Check(result)) || (PyList_Size(result) != 2)) { |
---|
113 | fd_log_debug("Error: The Python callback did not return [ ret, msg ].\n"); |
---|
114 | ret = EINVAL; |
---|
115 | goto out; |
---|
116 | } |
---|
117 | |
---|
118 | /* Convert the return values */ |
---|
119 | if (!SWIG_IsOK(SWIG_AsVal_int(PyList_GetItem(result, 0), &ret))) { |
---|
120 | fd_log_debug("Error: Cannot convert the first return value to integer.\n"); |
---|
121 | ret = EINVAL; |
---|
122 | goto out; |
---|
123 | } |
---|
124 | if (ret) { |
---|
125 | TRACE_DEBUG(INFO, "The Python callback returned the error code %d (%s)\n", ret, strerror(ret)); |
---|
126 | goto out; |
---|
127 | } |
---|
128 | |
---|
129 | if (!SWIG_IsOK(SWIG_ConvertPtr(PyList_GetItem(result, 1), (void *)msg, SWIGTYPE_p_msg, SWIG_POINTER_DISOWN))) { |
---|
130 | fd_log_debug("Error: Cannot convert the second return value to message.\n"); |
---|
131 | ret = EINVAL; |
---|
132 | goto out; |
---|
133 | } |
---|
134 | |
---|
135 | out: |
---|
136 | Py_XDECREF(result); |
---|
137 | |
---|
138 | SWIG_PYTHON_THREAD_END_BLOCK; |
---|
139 | return ret; |
---|
140 | } |
---|
141 | %} |
---|
142 | |
---|
143 | |
---|
144 | struct fd_rt_fwd_hdl { |
---|
145 | }; |
---|
146 | |
---|
147 | %extend fd_rt_fwd_hdl{ |
---|
148 | fd_rt_fwd_hdl(PyObject * PyCb, enum fd_rt_fwd_dir dir) { |
---|
149 | struct fd_rt_fwd_hdl * r = NULL; |
---|
150 | int ret; |
---|
151 | |
---|
152 | Py_XINCREF(PyCb); |
---|
153 | |
---|
154 | ret = fd_rt_fwd_register( call_the_python_rt_fwd_callback, PyCb, dir, &r ); |
---|
155 | if (ret != 0) { |
---|
156 | DI_ERROR(ret, NULL, NULL); |
---|
157 | return NULL; |
---|
158 | } |
---|
159 | return r; |
---|
160 | } |
---|
161 | |
---|
162 | ~fd_rt_fwd_hdl() { |
---|
163 | PyObject * func; |
---|
164 | int ret = fd_rt_fwd_unregister ( $self, (void *) &func ); |
---|
165 | if (ret != 0) { |
---|
166 | DI_ERROR(ret, NULL, NULL); |
---|
167 | return; |
---|
168 | } |
---|
169 | Py_XDECREF(func); |
---|
170 | return; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | %{ |
---|
176 | /* call it (will be called from a different thread than the interpreter, when message arrives) */ |
---|
177 | static int call_the_python_rt_out_callback(void * pycb, struct msg *msg, struct fd_list * candidates) { |
---|
178 | PyObject *PyMsg, *PyCands; |
---|
179 | PyObject *cb, *result = NULL; |
---|
180 | int ret = 0; |
---|
181 | |
---|
182 | if (!pycb) { |
---|
183 | fd_log_debug("Internal error: missing the callback!\n"); |
---|
184 | return ENOTSUP; |
---|
185 | } |
---|
186 | cb = pycb; |
---|
187 | |
---|
188 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
---|
189 | /* Convert the arguments */ |
---|
190 | PyMsg = SWIG_NewPointerObj((void *)msg, SWIGTYPE_p_msg, 0 ); |
---|
191 | PyCands = SWIG_NewPointerObj((void *)candidates, SWIGTYPE_p_fd_list, 0 ); |
---|
192 | |
---|
193 | /* Call the function */ |
---|
194 | result = PyEval_CallFunction(cb, "(OO)", PyMsg, PyCands); |
---|
195 | |
---|
196 | /* The result is supposedly composed of: [ ret, *msg ] */ |
---|
197 | if (result == NULL){ |
---|
198 | fd_log_debug("Error: The Python callback raised an exception.\n"); |
---|
199 | ret = EINVAL; |
---|
200 | goto out; |
---|
201 | } |
---|
202 | |
---|
203 | /* Convert the return values */ |
---|
204 | if (!SWIG_IsOK(SWIG_AsVal_int(result, &ret))) { |
---|
205 | fd_log_debug("Error: Cannot convert the return value to integer.\n"); |
---|
206 | ret = EINVAL; |
---|
207 | goto out; |
---|
208 | } |
---|
209 | out: |
---|
210 | Py_XDECREF(result); |
---|
211 | |
---|
212 | SWIG_PYTHON_THREAD_END_BLOCK; |
---|
213 | return ret; |
---|
214 | } |
---|
215 | %} |
---|
216 | |
---|
217 | |
---|
218 | struct fd_rt_out_hdl { |
---|
219 | }; |
---|
220 | |
---|
221 | %extend fd_rt_out_hdl{ |
---|
222 | fd_rt_out_hdl(PyObject * PyCb, int priority = 0) { |
---|
223 | struct fd_rt_out_hdl * r = NULL; |
---|
224 | int ret; |
---|
225 | |
---|
226 | Py_XINCREF(PyCb); |
---|
227 | |
---|
228 | ret = fd_rt_out_register( call_the_python_rt_out_callback, PyCb, priority, &r ); |
---|
229 | if (ret != 0) { |
---|
230 | DI_ERROR(ret, NULL, NULL); |
---|
231 | return NULL; |
---|
232 | } |
---|
233 | return r; |
---|
234 | } |
---|
235 | |
---|
236 | ~fd_rt_out_hdl() { |
---|
237 | PyObject * func; |
---|
238 | int ret = fd_rt_out_unregister ( $self, (void *) &func ); |
---|
239 | if (ret != 0) { |
---|
240 | DI_ERROR(ret, NULL, NULL); |
---|
241 | return; |
---|
242 | } |
---|
243 | Py_XDECREF(func); |
---|
244 | return; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|