blob: ff1728d080bf5a7ebe4bcb2728f022c6ca39c56f [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
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/****** FIFO QUEUES *********/
39
40struct fifo {
41};
42
43%extend fifo {
44 fifo(int max = 0) {
45 struct fifo * q = NULL;
46 int ret = fd_fifo_new(&q, max);
47 if (ret != 0) {
48 DI_ERROR(ret, NULL, NULL);
49 return NULL;
50 }
51 return q;
52 }
53 ~fifo() {
54 struct fifo *q = self;
55 fd_fifo_del(&q);
56 }
57
58 /* Move all elements to another queue */
59 void move(struct fifo * to) {
60 int ret = fd_fifo_move($self, to, NULL);
61 if (ret != 0) {
62 DI_ERROR(ret, NULL, NULL);
63 }
64 }
65
66 /* Get the length of the queue (nb elements) */
67 int length() {
68 return fd_fifo_length ( $self ) ;
69 }
70
71 /* Is the threashold function useful here? TODO... */
72
73 /* Post an item */
74 void post(PyObject * item, char * type = NULL) {
75 int ret;
76 if (type) {
77 void * real_obj = NULL;
78 swig_type_info * desttype = NULL;
79 desttype = SWIG_TypeQuery(type);
80 if (!desttype) {
81 DI_ERROR(EINVAL, NULL, "Unable to resolve this type. Please check the form: 'struct blahbla *'");
82 return;
83 }
84 /* Now, get the "real" value under the shadow umbrella */
85 ret = SWIG_ConvertPtr(item, &real_obj, desttype, SWIG_POINTER_DISOWN );
86 if (!SWIG_IsOK(ret)) {
87 DI_ERROR(EINVAL, SWIG_ErrorType(ret), "Unable to convert the item to given type");
88 return;
89 }
90 ret = fd_fifo_post($self, &real_obj);
91 } else {
92 PyObject * i = item;
93 Py_XINCREF(i);
94 ret = fd_fifo_post($self, &i);
95 }
96 if (ret != 0) {
97 DI_ERROR(ret, NULL, NULL);
98 }
99 }
100
101 /* Get (blocking) */
102 PyObject * get(char * type = NULL) {
103 int ret;
104 PyObject * i = NULL;
105 void * obj = NULL;
106 swig_type_info * desttype = NULL;
107 if (type) {
108 desttype = SWIG_TypeQuery(type);
109 if (!desttype) {
110 DI_ERROR(EINVAL, NULL, "Unable to resolve this type. Please check the form: 'struct blahbla *'");
111 return NULL;
112 }
113 }
114
115 ret = fd_fifo_get($self, &obj);
116 if (ret != 0) {
117 DI_ERROR(ret, NULL, NULL);
118 }
119
120 if (type) {
121 return SWIG_NewPointerObj(obj, desttype, 0 );
122 } else {
123 i = obj;
124 return i;
125 }
126 }
127
128 /* TryGet (non-blocking, returns None on empty queue) */
129 PyObject * tryget(char * type = NULL) {
130 int ret;
131 PyObject * i = NULL;
132 void * obj = NULL;
133 swig_type_info * desttype = NULL;
134 if (type) {
135 desttype = SWIG_TypeQuery(type);
136 if (!desttype) {
137 DI_ERROR(EINVAL, NULL, "Unable to resolve this type. Please check the form: 'struct blahbla *'");
138 return NULL;
139 }
140 }
141
142 ret = fd_fifo_tryget($self, &obj);
143 if (ret == EWOULDBLOCK) {
144 Py_INCREF(Py_None);
145 return Py_None;
146 }
147 if (ret != 0) {
148 DI_ERROR(ret, NULL, NULL);
149 }
150
151 if (type) {
152 return SWIG_NewPointerObj(obj, desttype, 0 );
153 } else {
154 i = obj;
155 return i;
156 }
157 }
158
159 /* TimedGet (blocking for a while) */
160 PyObject * timedget(long seconds, char * type = NULL) {
161 int ret;
162 PyObject * i = NULL;
163 struct timespec ts;
164 void * obj = NULL;
165 swig_type_info * desttype = NULL;
166 if (type) {
167 desttype = SWIG_TypeQuery(type);
168 if (!desttype) {
169 DI_ERROR(EINVAL, NULL, "Unable to resolve this type. Please check the form: 'struct blahbla *'");
170 return NULL;
171 }
172 }
173
174 clock_gettime(CLOCK_REALTIME, &ts);
175 ts.tv_sec += seconds;
176
177 ret = fd_fifo_timedget($self, &obj, &ts);
178 if (ret == ETIMEDOUT) {
179 Py_INCREF(Py_None);
180 return Py_None;
181 }
182 if (ret != 0) {
183 DI_ERROR(ret, NULL, NULL);
184 }
185
186 if (type) {
187 return SWIG_NewPointerObj(obj, desttype, 0 );
188 } else {
189 i = obj;
190 return i;
191 }
192 }
193
194}
195
196
197
198