Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, 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 | struct sess_state { |
| 42 | PyObject * pystate; |
| 43 | }; |
| 44 | |
| 45 | /* call it (might be called from a different thread than the interpreter, when session times out) */ |
| 46 | static void call_the_python_cleanup_callback(struct sess_state * state, os0_t sid, void * cb) { |
| 47 | PyObject *result; |
| 48 | if (!cb) { |
| 49 | fd_log_debug("Internal error: missing callback object!"); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | /* Call the function */ |
| 54 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 55 | result = PyObject_CallFunction((PyObject *)cb, "(Os)", state, sid); |
| 56 | Py_XDECREF(result); |
| 57 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 58 | return; |
| 59 | } |
| 60 | %} |
| 61 | |
| 62 | struct session_handler { |
| 63 | }; |
| 64 | |
| 65 | %nodefaultctor session_handler; |
| 66 | %extend session_handler { |
| 67 | session_handler(PyObject * PyCb) { |
| 68 | struct session_handler * hdl = NULL; |
| 69 | int ret; |
| 70 | |
| 71 | Py_XINCREF(PyCb); |
| 72 | |
| 73 | ret = fd_sess_handler_create ( &hdl, call_the_python_cleanup_callback, NULL, PyCb ); |
| 74 | if (ret != 0) { |
| 75 | DI_ERROR(ret, NULL, NULL); |
| 76 | return NULL; |
| 77 | } |
| 78 | return hdl; |
| 79 | } |
| 80 | ~session_handler() { |
| 81 | struct session_handler * hdl = self; |
| 82 | PyObject * cb = NULL; |
| 83 | |
| 84 | int ret = fd_sess_handler_destroy(&hdl, (void *)&cb); |
| 85 | if (ret != 0) { |
| 86 | DI_ERROR(ret, NULL, NULL); |
| 87 | } |
| 88 | /* Now free the callback */ |
| 89 | Py_XDECREF(cb); |
| 90 | return; |
| 91 | } |
| 92 | void dump() { |
| 93 | char * buf = NULL; |
| 94 | size_t len; |
| 95 | printf("%s", fd_sess_dump_hdl(&buf, &len, NULL, $self)); |
| 96 | free(buf); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | struct session { |
| 102 | }; |
| 103 | |
| 104 | %extend session { |
| 105 | /* The first two versions create a new session string. The third one allow to use an existing string. */ |
| 106 | session() { |
| 107 | int ret; |
| 108 | struct session * s = NULL; |
| 109 | ret = fd_sess_new(&s, fd_g_config->cnf_diamid, fd_g_config->cnf_diamid_len, (os0_t)"dbg_interactive", CONSTSTRLEN("dbg_interactive")); |
| 110 | if (ret != 0) { |
| 111 | DI_ERROR(ret, NULL, NULL); |
| 112 | return NULL; |
| 113 | } |
| 114 | return s; |
| 115 | } |
| 116 | session(char * diamid, char * STRING, size_t LENGTH) { |
| 117 | int ret; |
| 118 | struct session * s = NULL; |
| 119 | ret = fd_sess_new(&s, diamid, 0, (os0_t)STRING, LENGTH); |
| 120 | if (ret != 0) { |
| 121 | DI_ERROR(ret, NULL, NULL); |
| 122 | return NULL; |
| 123 | } |
| 124 | return s; |
| 125 | } |
| 126 | session(char * STRING, size_t LENGTH) { |
| 127 | int ret, n; |
| 128 | struct session * s = NULL; |
| 129 | ret = fd_sess_fromsid((os0_t)STRING, LENGTH, &s, &n); |
| 130 | if (ret != 0) { |
| 131 | DI_ERROR(ret, NULL, NULL); |
| 132 | return NULL; |
| 133 | } |
| 134 | /* When defining n as OUTPUT parameter, we get something strange... Use fd_sess_fromsid if you need it */ |
| 135 | #if 0 |
| 136 | if (n) { |
| 137 | fd_log_debug("A new session has been created"); |
| 138 | } else { |
| 139 | fd_log_debug("A session with same id already existed"); |
| 140 | } |
| 141 | #endif /* 0 */ |
| 142 | |
| 143 | return s; |
| 144 | } |
| 145 | ~session() { |
| 146 | struct session * s = self; |
| 147 | int ret = fd_sess_reclaim(&s); |
| 148 | if (ret != 0) { |
| 149 | DI_ERROR(ret, NULL, NULL); |
| 150 | } |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | %cstring_output_allocate_size(char ** outsid, size_t * sidlen, /* do not free */); |
| 155 | void getsid(char ** outsid, size_t * sidlen) { |
| 156 | int ret; |
| 157 | ret = fd_sess_getsid( $self, (void *)outsid, sidlen); |
| 158 | if (ret != 0) { |
| 159 | DI_ERROR(ret, NULL, NULL); |
| 160 | return; |
| 161 | } |
| 162 | return; |
| 163 | } |
| 164 | void settimeout(long seconds) { |
| 165 | struct timespec timeout; |
| 166 | int ret; |
| 167 | clock_gettime(CLOCK_REALTIME, &timeout); |
| 168 | timeout.tv_sec += seconds; |
| 169 | ret = fd_sess_settimeout( $self, &timeout ); |
| 170 | if (ret != 0) { |
| 171 | DI_ERROR(ret, NULL, NULL); |
| 172 | } |
| 173 | } |
| 174 | void dump() { |
| 175 | char * buf = NULL; |
| 176 | size_t len = 0; |
| 177 | printf("%s", fd_sess_dump(&buf, &len, NULL, $self, 1) ); |
| 178 | free(buf); |
| 179 | } |
| 180 | void store(struct session_handler * handler, PyObject * DISOWN) { |
| 181 | int ret; |
| 182 | struct sess_state * st = NULL; |
| 183 | st = malloc(sizeof(struct sess_state)); |
| 184 | st->pystate = DISOWN; |
| 185 | Py_XINCREF(DISOWN); |
| 186 | ret = fd_sess_state_store(handler, $self, (void *) &st); |
| 187 | if (ret != 0) { |
| 188 | DI_ERROR(ret, NULL, NULL); |
| 189 | } |
| 190 | } |
| 191 | %newobject retrieve; |
| 192 | PyObject * retrieve(struct session_handler * handler) { |
| 193 | int ret; |
| 194 | struct sess_state * st = NULL; |
| 195 | PyObject * state = NULL; |
| 196 | ret = fd_sess_state_retrieve(handler, $self, (void *) &st); |
| 197 | if (ret != 0) { |
| 198 | DI_ERROR(ret, NULL, NULL); |
| 199 | return NULL; |
| 200 | } |
| 201 | if (st == NULL) { |
| 202 | Py_INCREF(Py_None); |
| 203 | return Py_None; |
| 204 | } |
| 205 | state = st->pystate; |
| 206 | free(st); |
| 207 | return state; |
| 208 | } |
| 209 | } |
| 210 | |