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 | /****** SESSIONS *********/ |
---|
39 | |
---|
40 | %{ |
---|
41 | /* call it (might be called from a different thread than the interpreter, when session times out) */ |
---|
42 | static void call_the_python_cleanup_callback(session_state * state, char * sid, void * cb) { |
---|
43 | PyObject *result; |
---|
44 | if (!cb) { |
---|
45 | fd_log_debug("Internal error: missing callback object!\n"); |
---|
46 | return; |
---|
47 | } |
---|
48 | |
---|
49 | /* Call the function */ |
---|
50 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
---|
51 | result = PyEval_CallFunction((PyObject *)cb, "(Os)", state, sid); |
---|
52 | Py_XDECREF(result); |
---|
53 | SWIG_PYTHON_THREAD_END_BLOCK; |
---|
54 | return; |
---|
55 | } |
---|
56 | %} |
---|
57 | |
---|
58 | struct session_handler { |
---|
59 | }; |
---|
60 | |
---|
61 | %nodefaultctor session_handler; |
---|
62 | %extend session_handler { |
---|
63 | session_handler(PyObject * PyCb) { |
---|
64 | struct session_handler * hdl = NULL; |
---|
65 | int ret; |
---|
66 | |
---|
67 | Py_XINCREF(PyCb); |
---|
68 | |
---|
69 | ret = fd_sess_handler_create_internal ( &hdl, call_the_python_cleanup_callback, PyCb ); |
---|
70 | if (ret != 0) { |
---|
71 | DI_ERROR(ret, NULL, NULL); |
---|
72 | return NULL; |
---|
73 | } |
---|
74 | return hdl; |
---|
75 | } |
---|
76 | ~session_handler() { |
---|
77 | struct session_handler * hdl = self; |
---|
78 | PyObject * cb = NULL; |
---|
79 | |
---|
80 | int ret = fd_sess_handler_destroy(&hdl, (void *)&cb); |
---|
81 | if (ret != 0) { |
---|
82 | DI_ERROR(ret, NULL, NULL); |
---|
83 | } |
---|
84 | /* Now free the callback */ |
---|
85 | Py_XDECREF(cb); |
---|
86 | return; |
---|
87 | } |
---|
88 | void dump() { |
---|
89 | fd_sess_dump_hdl(0, $self); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | struct session { |
---|
95 | }; |
---|
96 | |
---|
97 | %extend session { |
---|
98 | /* The first two versions create a new session string. The third one allow to use an existing string. */ |
---|
99 | session() { |
---|
100 | int ret; |
---|
101 | struct session * s = NULL; |
---|
102 | ret = fd_sess_new(&s, fd_g_config->cnf_diamid, "dbg_interactive", sizeof("dbg_interactive")); |
---|
103 | if (ret != 0) { |
---|
104 | DI_ERROR(ret, NULL, NULL); |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | return s; |
---|
108 | } |
---|
109 | session(char * diamid, char * STRING, size_t LENGTH) { |
---|
110 | int ret; |
---|
111 | struct session * s = NULL; |
---|
112 | ret = fd_sess_new(&s, diamid, STRING, LENGTH); |
---|
113 | if (ret != 0) { |
---|
114 | DI_ERROR(ret, NULL, NULL); |
---|
115 | return NULL; |
---|
116 | } |
---|
117 | return s; |
---|
118 | } |
---|
119 | session(char * STRING, size_t LENGTH) { |
---|
120 | int ret, n; |
---|
121 | struct session * s = NULL; |
---|
122 | ret = fd_sess_fromsid(STRING, LENGTH, &s, &n); |
---|
123 | if (ret != 0) { |
---|
124 | DI_ERROR(ret, NULL, NULL); |
---|
125 | return NULL; |
---|
126 | } |
---|
127 | /* When defining n as OUTPUT parameter, we get something strange... Use fd_sess_fromsid if you need it */ |
---|
128 | #if 0 |
---|
129 | if (n) { |
---|
130 | fd_log_debug("A new session has been created\n"); |
---|
131 | } else { |
---|
132 | fd_log_debug("A session with same id already existed\n"); |
---|
133 | } |
---|
134 | #endif /* 0 */ |
---|
135 | |
---|
136 | return s; |
---|
137 | } |
---|
138 | ~session() { |
---|
139 | struct session * s = self; |
---|
140 | int ret = fd_sess_reclaim(&s); |
---|
141 | if (ret != 0) { |
---|
142 | DI_ERROR(ret, NULL, NULL); |
---|
143 | } |
---|
144 | return; |
---|
145 | } |
---|
146 | char * getsid() { |
---|
147 | int ret; |
---|
148 | char * sid = NULL; |
---|
149 | ret = fd_sess_getsid( $self, &sid); |
---|
150 | if (ret != 0) { |
---|
151 | DI_ERROR(ret, NULL, NULL); |
---|
152 | return NULL; |
---|
153 | } |
---|
154 | return sid; |
---|
155 | } |
---|
156 | void settimeout(long seconds) { |
---|
157 | struct timespec timeout; |
---|
158 | int ret; |
---|
159 | clock_gettime(CLOCK_REALTIME, &timeout); |
---|
160 | timeout.tv_sec += seconds; |
---|
161 | ret = fd_sess_settimeout( $self, &timeout ); |
---|
162 | if (ret != 0) { |
---|
163 | DI_ERROR(ret, NULL, NULL); |
---|
164 | } |
---|
165 | } |
---|
166 | void dump() { |
---|
167 | fd_sess_dump(0, $self); |
---|
168 | } |
---|
169 | void store(struct session_handler * handler, PyObject * DISOWN) { |
---|
170 | int ret; |
---|
171 | void * store = DISOWN; |
---|
172 | Py_XINCREF(DISOWN); |
---|
173 | ret = fd_sess_state_store_internal(handler, $self, (void *) &store); |
---|
174 | if (ret != 0) { |
---|
175 | DI_ERROR(ret, NULL, NULL); |
---|
176 | } |
---|
177 | } |
---|
178 | %newobject retrieve; |
---|
179 | PyObject * retrieve(struct session_handler * handler) { |
---|
180 | int ret; |
---|
181 | PyObject * state = NULL; |
---|
182 | ret = fd_sess_state_retrieve_internal(handler, $self, (void *) &state); |
---|
183 | if (ret != 0) { |
---|
184 | DI_ERROR(ret, NULL, NULL); |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | if (state == NULL) { |
---|
188 | Py_INCREF(Py_None); |
---|
189 | return Py_None; |
---|
190 | } |
---|
191 | return state; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|