lib/WebSphere/MQTT/Client.xs

filter: Statementstmt 45.9% 149 / 324 Branchbran 40.8% 58 / 142 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 44.4% 207 / 466 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mspclnt.c ↑ Summary src/msputils.c »
1 /*
2  
3 WebSphere MQ Telemetry Transport
4 Perl Interface to IA93
5  
6 Nicholas Humfrey
7 University of Southampton
8 njh@ecs.soton.ac.uk
9
10 */
11  
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15  
16 /* WMQTT include */
17 #include "MQIsdp.h"
18 #include "ppersist.h"
19  
20  
21  
22 /* Get the connection handle from hashref */
23 MQISDPCH
24 51 get_handle_from_hv( HV* hash ) {
25 51 SV** svp = NULL;
26 IV pointer;
27
28 51 svp = hv_fetch( hash, "handle", 6, 0 );
29 51 ▶ if (svp == NULL || !SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp == NULL || !SvOK(*svp)) { 51 0 51
if (svp == NULL || !SvOK(*svp)) { 1 50 51
30 1 warn("Connection handle is missing from hash");
31 1 return NULL;
32 }
33
34 50 ▶ if (!sv_derived_from(*svp, "MQISDPCH")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(*svp, "MQISDPCH")) { 0 50 50
35 //warn("Connection handle isn't isn't of type MQISDPCH");
36 0 return NULL;
37 }
38  
39 // Re-reference and extract the pointer
40 50 pointer = SvIV((SV*)SvRV(*svp));
41 50 return INT2PTR(MQISDPCH,pointer);
42 }
43  
44  
45 /* Get debug settings from hashref */
46 int
47 43 get_debug_from_hv( HV* hash ) {
48 43 SV** svp = NULL;
49
50 43 svp = hv_fetch( hash, "debug", 5, 0 );
51 43 ▶ if (svp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp == NULL) { 0 43 43
52 0 warn("Debug setting is missing from hash");
53 0 return 0;
54 }
55
56 43 return SvTRUE( *svp );
57 }
58  
59  
60 /* Get task info from hashref */
61 MQISDPTI*
62 28 get_task_info_from_hv( HV* hash, char* name ) {
63 28 SV** svp = NULL;
64 IV pointer;
65
66 28 svp = hv_fetch( hash, name, strlen(name), 0 );
67 28 ▶ if (svp == NULL || !SvOK(*svp)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (svp == NULL || !SvOK(*svp)) return NULL; 26 2 28
if (svp == NULL || !SvOK(*svp)) return NULL; 22 4 26
68 4 ▶ if (!sv_derived_from(*svp, "MQISDPTIPtr")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(*svp, "MQISDPTIPtr")) return NULL; 0 4 4
69  
70 // Re-reference and extract the pointer
71 4 pointer = SvIV((SV*)SvRV(*svp));
72 4 return INT2PTR(MQISDPTI*,pointer);
73 }
74  
75  
76 /* Undefine the value of a key */
77 void
78 32 hv_key_undef( HV* hash, char* key ) {
79 32 SV** svp = NULL;
80  
81 32 svp = hv_fetch( hash, key, strlen(key), 0 );
82 32 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 30 2 32
83 30 sv_setsv(*svp, &PL_sv_undef);
84 } /* else {
85 warn("hv_key_undef: Didn't find key in hash");
86 } */
87 32 }
88  
89  
90 /* Make an array from pubOptions */
91 AV*
92 0 options_to_av( long pubOptions ) {
93 0 SV* sv = NULL;
94 0 AV* av = newAV();
95
96 0 ▶ if (pubOptions & MQISDP_WILL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_WILL) { 0 0 0
97 0 sv = newSVpv( "WILL", 0 );
98 0 av_push( av, sv );
99 }
100
101 0 ▶ if (pubOptions & MQISDP_RETAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_RETAIN) { 0 0 0
102 0 sv = newSVpv( "RETAIN", 0 );
103 0 av_push( av, sv );
104 }
105
106 0 ▶ if (pubOptions & MQISDP_QOS_0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_QOS_0) { 0 0 0
107 0 sv = newSVpv( "QOS_0", 0 );
108 0 av_push( av, sv );
109 }
110
111 0 ▶ if (pubOptions & MQISDP_QOS_1) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_QOS_1) { 0 0 0
112 0 sv = newSVpv( "QOS_1", 0 );
113 0 av_push( av, sv );
114 }
115
116 0 ▶ if (pubOptions & MQISDP_QOS_2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_QOS_2) { 0 0 0
117 0 sv = newSVpv( "QOS_2", 0 );
118 0 av_push( av, sv );
119 }
120
121 0 ▶ if (pubOptions & MQISDP_CLEAN_START) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_CLEAN_START) { 0 0 0
122 0 sv = newSVpv( "CLEAN_START", 0 );
123 0 av_push( av, sv );
124 }
125
126 0 ▶ if (pubOptions & MQISDP_WILL_RETAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_WILL_RETAIN) { 0 0 0
127 0 sv = newSVpv( "WILL_RETAIN", 0 );
128 0 av_push( av, sv );
129 }
130
131 0 ▶ if (pubOptions & MQISDP_DUPLICATE) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubOptions & MQISDP_DUPLICATE) { 0 0 0
132 0 sv = newSVpv( "DUPLICATE", 0 );
133 0 av_push( av, sv );
134 }
135  
136 0 return av;
137 }
138  
139  
140  
141 #define STATUS_CASE_RET( x ) \
142 case x: \
143 return #x; \
144 break;
145  
146  
147 const char*
148 65 get_status_string( int statusCode ) {
149  
150 65 switch(statusCode) {
151 18 case MQISDP_OK: return "OK";
152 0 case MQISDP_PROTOCOL_VERSION_ERROR: return "PROTOCOL_VERSION_ERROR";
153 1 case MQISDP_HOSTNAME_NOT_FOUND: return "HOSTNAME_NOT_FOUND";
154 1 case MQISDP_Q_FULL: return "Q_FULL";
155 0 case MQISDP_FAILED: return "FAILED";
156 0 case MQISDP_PUBS_AVAILABLE: return "PUBS_AVAILABLE";
157 0 case MQISDP_NO_PUBS_AVAILABLE: return "NO_PUBS_AVAILABLE";
158 1 case MQISDP_PERSISTENCE_FAILED: return "PERSISTENCE_FAILED";
159 1 case MQISDP_CONN_HANDLE_ERROR: return "CONN_HANDLE_ERROR";
160 0 case MQISDP_NO_WILL_TOPIC: return "NO_WILL_TOPIC";
161 0 case MQISDP_INVALID_STRUC_LENGTH: return "INVALID_STRUC_LENGTH";
162 0 case MQISDP_DATA_LENGTH_ERROR: return "DATA_LENGTH_ERROR";
163 0 case MQISDP_DATA_TOO_BIG: return "DATA_TOO_BIG";
164 0 case MQISDP_ALREADY_CONNECTED: return "ALREADY_CONNECTED";
165 4 case MQISDP_CONNECTION_BROKEN: return "CONNECTION_BROKEN";
166 0 case MQISDP_DATA_TRUNCATED: return "DATA_TRUNCATED";
167 0 case MQISDP_CLIENT_ID_ERROR: return "CLIENT_ID_ERROR";
168 0 case MQISDP_BROKER_UNAVAILABLE: return "BROKER_UNAVAILABLE";
169 0 case MQISDP_SOCKET_CLOSED: return "SOCKET_CLOSED";
170 0 case MQISDP_OUT_OF_MEMORY: return "OUT_OF_MEMORY";
171
172 0 case MQISDP_DELIVERED: return "DELIVERED";
173 0 case MQISDP_RETRYING: return "RETRYING";
174 0 case MQISDP_IN_PROGRESS: return "IN_PROGRESS";
175 0 case MQISDP_MSG_HANDLE_ERROR: return "MSG_HANDLE_ERROR";
176
177 39 case MQISDP_CONNECTING: return "CONNECTING";
178 0 case MQISDP_CONNECTED: return "CONNECTED";
179 0 case MQISDP_DISCONNECTED: return "DISCONNECTED";
180 }
181
182 0 return "UNKNOWN";
183 }
184  
185  
186 MODULE = WebSphere::MQTT::Client PACKAGE = WebSphere::MQTT::Client
187  
188  
189 ##
190 ## Prints library version to STDOUT
191 ##
192 void
193 xs_version()
194 CODE:
195 0 MQIsdp_version();
196  
197  
198 ##
199 ## Alocate memory for TaskInfo
200 ##
201 int
202 xs_start_tasks( self )
203 HV* self
204 PREINIT:
205 7 MQISDPTI* pSendTaskInfo = NULL;
206 7 MQISDPTI* pRcvTaskInfo = NULL;
207 7 MQISDPTI* pApiTaskInfo = NULL;
208 7 char *clientid = NULL;
209 7 SV** svp = NULL;
210 7 ▶ SV* sv = NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV* sv = NULL; 7 0 7
211  
212 CODE:
213 /* Get the client ID */
214 7 svp = hv_fetch( self, "clientid", 8, 0 );
215 7 ▶ if (svp != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp != NULL) { 7 0 7
216 7 clientid = SvPV_nolen( *svp );
217 7 ▶ if (strlen(clientid) < 1 || strlen(clientid) > 23) {
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(clientid) < 1 || strlen(clientid) > 23) { 7 0 7
if (strlen(clientid) < 1 || strlen(clientid) > 23) { 0 7 7
218 0 croak("clientid is not valid");
219 }
220 } else {
221 0 croak("clientid is not defined");
222 }
223
224
225 /* Allocate the WMQTT thread parameter structures */
226 7 pSendTaskInfo = (MQISDPTI*)malloc( sizeof(MQISDPTI) );
227 7 pRcvTaskInfo = (MQISDPTI*)malloc( sizeof(MQISDPTI) );
228 7 pApiTaskInfo = (MQISDPTI*)malloc( sizeof(MQISDPTI) );
229
230 /* Zero the memory */
231 7 bzero( pSendTaskInfo, sizeof(MQISDPTI) );
232 7 bzero( pRcvTaskInfo, sizeof(MQISDPTI) );
233 7 bzero( pApiTaskInfo, sizeof(MQISDPTI) );
234
235 /* Turn thread tracing off */
236 7 pSendTaskInfo->logLevel = LOGNONE;
237 7 pRcvTaskInfo->logLevel = LOGNONE;
238 7 pApiTaskInfo->logLevel = LOGNONE;
239  
240  
241 /* Start the threads (if enabled) */
242 7 ▶ if ( MQIsdp_StartTasks( pApiTaskInfo, pSendTaskInfo,
Branch: 1 branch
BranchTrueFalseTotal
if ( MQIsdp_StartTasks( pApiTaskInfo, pSendTaskInfo, 0 7 7
243 pRcvTaskInfo, clientid ) != 0 ) {
244 0 croak("Failed to start MQIsdp protocol threads");
245 XSRETURN_UNDEF;
246 }
247
248 /* Store thread parameter pointers */
249 7 sv=sv_setref_pv(newSV(0), "MQISDPTIPtr", (void *)pSendTaskInfo);
250 7 ▶ if (hv_store(self, "send_task_info", 14, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(self, "send_task_info", 14, sv, 0) == NULL) { 0 7 7
251 0 croak("send_task_info not stored");
252 }
253
254 7 sv=sv_setref_pv(newSV(0), "MQISDPTIPtr", (void *)pRcvTaskInfo);
255 7 ▶ if (hv_store(self, "recv_task_info", 14, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(self, "recv_task_info", 14, sv, 0) == NULL) { 0 7 7
256 0 croak("recv_task_info not stored");
257 }
258
259 7 sv=sv_setref_pv(newSV(0), "MQISDPTIPtr", (void *)pApiTaskInfo);
260 7 ▶ if (hv_store(self, "api_task_info", 13, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(self, "api_task_info", 13, sv, 0) == NULL) { 0 7 7
261 0 croak("api_task_info not stored");
262 }
263  
264
265 /* Successful */
266 7 RETVAL = 1;
267
268 OUTPUT:
269 self
270 RETVAL
271  
272
273
274 ##
275 ## Get connection status
276 ##
277 const char*
278 xs_status( self )
279 HV* self
280  
281 PREINIT:
282 43 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
283 43 int statusCode=0;
284 43 int debug=0;
285 43 char infoString[MQISDP_INFO_STRING_LENGTH] = "";
286 43 ▶ const char *statusString = NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *statusString = NULL; 43 0 43
287
288 CODE:
289 /* get the connection handle */
290 43 handle = get_handle_from_hv( self );
291 43 debug = get_debug_from_hv( self );
292
293 /* Not connected ? */
294 43 ▶ if (handle == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (handle == NULL) { 0 43 43
295 0 statusCode = MQISDP_DISCONNECTED;
296 } else {
297 /* get the connection status */
298 43 statusCode = MQIsdp_status( handle, MQISDP_RC_STRING_LENGTH, NULL, infoString );
299 }
300
301 /* Turn status code into a string */
302 43 statusString = get_status_string( statusCode );
303 43 ▶ if (debug) {
Branch: 1 branch
BranchTrueFalseTotal
if (debug) { 0 43 43
304 0 fprintf(stderr, "xs_status: %s [%d] - %s\n", statusString, statusCode, infoString);
305 }
306
307 43 RETVAL = statusString;
308
309 OUTPUT:
310 RETVAL
311
312
313
314
315 ##
316 ## Connect to broker
317 ##
318 const char*
319 xs_connect( self, pApiTaskInfo )
320 HV* self
321 MQISDPTI *pApiTaskInfo
322  
323 PREINIT:
324 7 CONN_PARMS *pCp = NULL;
325 7 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
326 7 long connMsgLength = 0;
327 7 SV** svp = NULL;
328 7 SV* sv = NULL;
329 7 ▶ int rc = MQISDP_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
int rc = MQISDP_FAILED; 7 0 7
330
331 CODE:
332 /* length of Connect Messgae */
333 7 connMsgLength = sizeof(CONN_PARMS);
334
335 /* Create Connect data structure */
336 7 pCp = (CONN_PARMS*)malloc( connMsgLength );
337 7 pCp->strucLength = connMsgLength;
338  
339  
340  
341 /* Fill out parameters from hashref */
342 7 svp = hv_fetch( self, "clientid", 8, 0 );
343 14 ▶ if (svp && SvPOK(*svp)) strcpy( pCp->clientId, SvPV_nolen(*svp) );
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) strcpy( pCp->clientId, SvPV_nolen(*svp) ); 7 0 7
if (svp && SvPOK(*svp)) strcpy( pCp->clientId, SvPV_nolen(*svp) ); 7 0 7
344 0 else croak("'clientid' setting isn't available");
345
346 7 svp = hv_fetch( self, "retry_count", 11, 0 );
347 7 ▶ if (svp && SvIOK(*svp)) pCp->retryCount = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) pCp->retryCount = SvIV(*svp); 7 0 7
if (svp && SvIOK(*svp)) pCp->retryCount = SvIV(*svp); 7 0 7
348 0 else croak("'retry_count' setting isn't available");
349  
350 7 svp = hv_fetch( self, "retry_interval", 14, 0 );
351 7 ▶ if (svp && SvIOK(*svp)) pCp->retryInterval = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) pCp->retryInterval = SvIV(*svp); 7 0 7
if (svp && SvIOK(*svp)) pCp->retryInterval = SvIV(*svp); 7 0 7
352 0 else croak("'retry_interval' setting isn't available");
353  
354 7 svp = hv_fetch( self, "keep_alive", 10, 0 );
355 7 ▶ if (svp && SvIOK(*svp)) pCp->keepAliveTime = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) pCp->keepAliveTime = SvIV(*svp); 7 0 7
if (svp && SvIOK(*svp)) pCp->keepAliveTime = SvIV(*svp); 7 0 7
356 0 else croak("'keep_alive' setting isn't available");
357  
358 7 svp = hv_fetch( self, "host", 4, 0 );
359 14 ▶ if (svp && SvPOK(*svp)) pCp->brokerHostname = SvPV_nolen(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) pCp->brokerHostname = SvPV_nolen(*svp); 7 0 7
if (svp && SvPOK(*svp)) pCp->brokerHostname = SvPV_nolen(*svp); 7 0 7
360 0 else croak("'host' setting isn't available");
361  
362 7 svp = hv_fetch( self, "port", 4, 0 );
363 7 ▶ if (svp && SvIOK(*svp)) pCp->brokerPort = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) pCp->brokerPort = SvIV(*svp); 7 0 7
if (svp && SvIOK(*svp)) pCp->brokerPort = SvIV(*svp); 7 0 7
364 0 else croak("'port' setting isn't available");
365  
366 7 svp = hv_fetch( self, "persist", 7, 0 );
367 7 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 7 0 7
if (svp && SvOK(*svp)) { 5 2 7
368 10 ▶ if (sv_isobject(*svp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(*svp)) { 5 0 5
369 5 pCp->pPersistFuncs = new_persistence_wrapper(*svp);
370 5 sv=sv_setref_pv(newSV(0), "MQISDPTIPtr", (void *)pCp->pPersistFuncs);
371 5 ▶ if (hv_store(self, "persist_info", 12, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(self, "persist_info", 12, sv, 0) == NULL) { 0 5 5
372 0 croak("persist_info not stored");
373 }
374 }
375 0 else croak("'persist' setting must be an object");
376 }
377 2 else pCp->pPersistFuncs = NULL;
378  
379 /* Set options flags */
380 7 pCp->options = MQISDP_NONE;
381 7 svp = hv_fetch( self, "clean_start", 11, 0 );
382 7 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 7 0 7
383 7 ▶ if (SvIV(*svp)) pCp->options |= MQISDP_CLEAN_START;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(*svp)) pCp->options |= MQISDP_CLEAN_START; 3 4 7
384 } else {
385 0 croak("'clean_start' setting isn't available");
386 }
387  
388 /* Perform the connect */
389 7 rc = MQIsdp_connect( &handle, pCp, pApiTaskInfo );
390 7 free( pCp );
391  
392  
393 /* Store connection handle pointer */
394 7 sv=sv_setref_pv(newSV(0), "MQISDPCH", (void *)handle);
395 7 ▶ if (hv_store(self, "handle", 6, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(self, "handle", 6, sv, 0) == NULL) { 0 7 7
396 0 croak("connection handle not stored");
397 }
398
399 /* Return result code as a string */
400 7 RETVAL = get_status_string( rc );
401
402 OUTPUT:
403 RETVAL
404
405  
406  
407 ##
408 ## Disconnect from broker
409 ##
410 const char*
411 xs_disconnect( self )
412 HV* self
413  
414 PREINIT:
415 4 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
416 4 ▶ int rc = MQISDP_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
int rc = MQISDP_FAILED; 4 0 4
417  
418 CODE:
419 /* get the connection handle */
420 4 handle = get_handle_from_hv( self );
421  
422 4 ▶ if (handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (handle) { 4 0 4
423 /* perform the disconnect */
424 4 rc = MQIsdp_disconnect( &handle );
425
426 /* Undef 'handle' if its value now NULL */
427 4 ▶ if (handle==MQISDP_INV_CONN_HANDLE) hv_key_undef( self, "handle" );
Branch: 1 branch
BranchTrueFalseTotal
if (handle==MQISDP_INV_CONN_HANDLE) hv_key_undef( self, "handle" ); 4 0 4
428 }
429
430 4 RETVAL = get_status_string( rc );
431
432 OUTPUT:
433 RETVAL
434  
435  
436 ##
437 ## Free memory and Terminate threads
438 ##
439 const char*
440 xs_terminate( self )
441 HV* self
442  
443 PREINIT:
444  
445 CODE:
446 7 MQISDPTI *pApiTaskInfo = get_task_info_from_hv( self, "api_task_info" );
447 7 MQISDPTI *pSendTaskInfo = get_task_info_from_hv( self, "send_task_info" );
448 7 MQISDPTI *pRcvTaskInfo = get_task_info_from_hv( self, "recv_task_info" );
449 7 MQISDPTI *pPersistInfo = get_task_info_from_hv( self, "persist_info" );
450
451 /* Free the memory */
452 7 ▶ if (pApiTaskInfo) free( pApiTaskInfo );
Branch: 1 branch
BranchTrueFalseTotal
if (pApiTaskInfo) free( pApiTaskInfo ); 1 6 7
453 7 ▶ if (pSendTaskInfo) free( pSendTaskInfo );
Branch: 1 branch
BranchTrueFalseTotal
if (pSendTaskInfo) free( pSendTaskInfo ); 1 6 7
454 7 ▶ if (pRcvTaskInfo) free( pRcvTaskInfo );
Branch: 1 branch
BranchTrueFalseTotal
if (pRcvTaskInfo) free( pRcvTaskInfo ); 2 5 7
455 7 ▶ if (pPersistInfo) free( pPersistInfo );
Branch: 1 branch
BranchTrueFalseTotal
if (pPersistInfo) free( pPersistInfo ); 0 7 7
456  
457 /* Undef them in the hash */
458 7 hv_key_undef( self, "api_task_info");
459 7 hv_key_undef( self, "send_task_info");
460 7 hv_key_undef( self, "recv_task_info");
461 7 hv_key_undef( self, "persist_info");
462  
463 /* Terminate threads and return result as a string */
464 7 RETVAL = get_status_string( MQIsdp_terminate() );
465
466 OUTPUT:
467 RETVAL
468  
469  
470  
471  
472  
473 ##
474 ## Subscribe to a topic
475 ##
476 const char*
477 xs_subscribe( self, topic, qos )
478 HV* self
479 char* topic
480 int qos
481  
482 PREINIT:
483 0 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
484 0 MQISDPMH hMsg = 0;
485 0 SUB_PARMS *pSp = NULL;
486 0 int bufSize = 0;
487 0 ▶ int rc = 0;
Branch: 1 branch
BranchTrueFalseTotal
int rc = 0; 0 0 0
488
489 CODE:
490 /* get the connection handle */
491 0 handle = get_handle_from_hv( self );
492
493 /* Allocate memory for stucture */
494 0 bufSize = sizeof(SUB_PARMS) + (2 * sizeof(long)) + strlen(topic);
495 0 pSp = (SUB_PARMS*)malloc( bufSize );
496  
497 0 ▶ if (pSp) {
Branch: 1 branch
BranchTrueFalseTotal
if (pSp) { 0 0 0
498 0 char *pTmpPtr = NULL;
499 0 long options = 0;
500 0 long tLength = 0;
501
502 0 pSp->strucLength = bufSize;
503
504 /* Set the topic length field */
505 0 pTmpPtr = (char*)pSp + sizeof(long);
506 0 tLength = strlen(topic);
507 0 memcpy( pTmpPtr, &tLength, sizeof(long) );
508  
509 /* Set the topic field */
510 0 pTmpPtr += sizeof(long);
511 0 memcpy( pTmpPtr, topic, strlen(topic) );
512  
513 /* Set the options field */
514 0 pTmpPtr += strlen(topic);
515 0 switch ( qos ) {
516 0 case 0: options |= MQISDP_QOS_0; break;
517 0 case 1: options |= MQISDP_QOS_1; break;
518 0 case 2: options |= MQISDP_QOS_2; break;
519 }
520 0 memcpy( pTmpPtr, &options, sizeof(long) );
521  
522 /* Subscribe */
523 0 rc = MQIsdp_subscribe( handle, &hMsg, pSp );
524 0 free( pSp );
525  
526 } else {
527 0 rc = MQISDP_OUT_OF_MEMORY;
528 }
529  
530 0 RETVAL = get_status_string(rc);
531
532 OUTPUT:
533 RETVAL
534
535
536
537
538 ##
539 ## Unsubscribe from a topic
540 ##
541 const char*
542 xs_unsubscribe( self, topic )
543 HV* self
544 char* topic
545  
546 PREINIT:
547 0 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
548 0 MQISDPMH hMsg = 0;
549 0 UNSUB_PARMS *pUp = NULL;
550 0 int bufSize = 0;
551 0 ▶ int rc = 0;
Branch: 1 branch
BranchTrueFalseTotal
int rc = 0; 0 0 0
552
553 CODE:
554 /* get the connection handle */
555 0 handle = get_handle_from_hv( self );
556
557 /* Allocate memory for stucture */
558 0 bufSize = sizeof(UNSUB_PARMS) + sizeof(long) + strlen(topic);
559 0 pUp = (UNSUB_PARMS*)malloc( bufSize );
560  
561 0 ▶ if (pUp) {
Branch: 1 branch
BranchTrueFalseTotal
if (pUp) { 0 0 0
562 0 char *pTmpPtr = NULL;
563 0 long tLength = 0;
564
565 0 pUp->strucLength = bufSize;
566
567 /* Set the topic length field */
568 0 pTmpPtr = (char*)pUp + sizeof(long);
569 0 tLength = strlen(topic);
570 0 memcpy( pTmpPtr, &tLength, sizeof(long) );
571  
572 /* Set the topic field */
573 0 pTmpPtr += sizeof(long);
574 0 memcpy( pTmpPtr, topic, strlen(topic) );
575  
576 /* Unsubscribe */
577 0 rc = MQIsdp_unsubscribe( handle, &hMsg, pUp );
578 0 free( pUp );
579  
580 } else {
581 0 rc = MQISDP_OUT_OF_MEMORY;
582 }
583  
584 0 RETVAL = get_status_string(rc);
585
586 OUTPUT:
587 RETVAL
588  
589  
590  
591 ##
592 ## Receive a publication
593 ##
594 HV*
595 xs_receivePub( self )
596 HV* self
597  
598 PREINIT:
599 0 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
600 0 HV *hash = newHV();
601 0 SV *sv = NULL;
602
603 0 int rc = 0;
604 0 int stillWaiting = 1;
605 0 long timeToWait = 10000; // 10 Seconds
606
607 0 long dataLength = 0;
608 0 long topicLength = 0;
609 0 long pubOptions = 0;
610 0 long bufferSz = 1024;
611 0 char *pBuffer = NULL;
612  
613  
614 CODE:
615 /* get the connection handle */
616 0 handle = get_handle_from_hv( self );
617
618
619 /* Allocate some memory to store messgae */
620 0 pBuffer = (char*)malloc( bufferSz );
621
622
623 /* Wait for a message */
624 0 ▶ while( stillWaiting )
Branch: 1 branch
BranchTrueFalseTotal
while( stillWaiting ) 0 0 0
625 {
626
627 0 rc = MQIsdp_receivePub( handle, timeToWait, &pubOptions, &topicLength, &dataLength, bufferSz-1, pBuffer );
628  
629 /* Not sure why this is required */
630 0 dataLength -= topicLength;
631  
632 0 switch( rc ) {
633 0 case MQISDP_DATA_TRUNCATED:
634 0 bufferSz = dataLength+topicLength+1;
635 0 ▶ if (pBuffer == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pBuffer == NULL) { 0 0 0
636 0 pBuffer = (char*)malloc( bufferSz );
637 } else {
638 0 pBuffer = (char*)realloc( pBuffer, bufferSz );
639 }
640 0 break;
641
642 0 case MQISDP_NO_PUBS_AVAILABLE:
643 /* Do nothing */
644 0 break;
645
646 0 case MQISDP_PUBS_AVAILABLE:
647 case MQISDP_OK:
648
649 /* Store the options */
650 0 sv = newRV_noinc((SV*)options_to_av( pubOptions ));
651 0 ▶ if (hv_store(hash, "options", 7, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hash, "options", 7, sv, 0) == NULL) { 0 0 0
652 0 croak("xs_receivePub: options not stored");
653 }
654  
655 /* Store the topic length */
656 0 sv=newSViv(topicLength);
657 0 ▶ if (hv_store(hash, "topic_length", 12, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hash, "topic_length", 12, sv, 0) == NULL) { 0 0 0
658 0 croak("xs_receivePub: topic_length not stored");
659 }
660  
661 /* Store the topic */
662 0 sv=newSVpv(pBuffer, topicLength);
663 0 ▶ if (hv_store(hash, "topic", 5, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hash, "topic", 5, sv, 0) == NULL) { 0 0 0
664 0 croak("xs_receivePub: topic not stored");
665 }
666
667 /* Store the data length */
668 0 sv=newSViv(dataLength);
669 0 ▶ if (hv_store(hash, "data_length", 8, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hash, "data_length", 8, sv, 0) == NULL) { 0 0 0
670 0 croak("xs_receivePub: data_length not stored");
671 }
672
673 /* Store the data */
674 0 sv=newSVpv(pBuffer+topicLength, dataLength);
675 0 ▶ if (hv_store(hash, "data", 4, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hash, "data", 4, sv, 0) == NULL) { 0 0 0
676 0 croak("xs_receivePub: data not stored");
677 }
678  
679 /* FALLTHROUGH */
680  
681 default:
682
683 /* Store the status */
684 0 sv=newSVpv(get_status_string(rc), 0);
685 0 ▶ if (hv_store(hash, "status", 6, sv, 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hash, "status", 6, sv, 0) == NULL) { 0 0 0
686 0 croak("xs_receivePub: status not stored");
687 }
688
689 0 stillWaiting = 0;
690 0 break;
691 }
692
693 }
694
695  
696 /* Free the message buffer */
697 0 ▶ if (pBuffer) free( pBuffer );
Branch: 1 branch
BranchTrueFalseTotal
if (pBuffer) free( pBuffer ); 0 0 0
698  
699 0 RETVAL = hash;
700
701 OUTPUT:
702 RETVAL
703  
704  
705  
706 ##
707 ## Publish a message
708 ## Note that you cannot send or queue more than MSP_DEFAULT_MAX_OUTQ_SZ
709 ## bytes (defined as 32768 in mspsh.h), or you will get Q_FULL response
710 ##
711 void
712 xs_publish( self, data, topic, qos, retain )
713 HV* self
714 char* data
715 char* topic
716 int qos
717 int retain
718
719 PREINIT:
720 4 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
721 4 MQISDPMH hMsg = MQISDP_INV_MSG_HANDLE;
722 PUB_PARMS Pp;
723 4 int rc = 0;
724
725 PPCODE:
726
727 /* get the connection handle */
728 4 handle = get_handle_from_hv( self );
729  
730 4 Pp.strucLength = sizeof(PUB_PARMS);
731
732 /* Set the options field */
733 4 Pp.options = MQISDP_NONE;
734 4 switch ( qos ) {
735 1 case 0: Pp.options |= MQISDP_QOS_0; break;
736 3 case 1: Pp.options |= MQISDP_QOS_1; break;
737 0 case 2: Pp.options |= MQISDP_QOS_2; break;
738 }
739 4 ▶ if ( retain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( retain ) { 0 4 4
740 0 Pp.options |= MQISDP_RETAIN;
741 }
742  
743 /* Set the topic length field */
744 4 Pp.topicLength = strlen(topic);
745  
746 /* Set the topic field */
747 4 Pp.topic = topic;
748  
749 /* Set the data length field */
750 4 Pp.dataLength = strlen(data);
751  
752 /* Set the data field */
753 4 Pp.data = data;
754  
755 /* Publish */
756 4 rc = MQIsdp_publish( handle, &hMsg, &Pp );
757  
758 /* Return two values as an array */
759 4 ▶ XPUSHs( sv_2mortal( newSVpv( get_status_string(rc), 0 ) ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( get_status_string(rc), 0 ) ) ); 0 4 4
760 4 ▶ XPUSHs( sv_2mortal( newSVnv( hMsg ) ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVnv( hMsg ) ) ); 0 4 4
761  
762 ##
763 ## Check status of a message published at QOS 1/2
764 ## Returns one of:
765 ## DELIVERED, IN_PROGRESS, RETRYING, MSG_HANDLE_ERROR, CONN_HANDLE_ERROR
766 ##
767 const char*
768 xs_getMsgStatus( self, hMsg )
769 HV* self
770 long hMsg
771
772 PREINIT:
773 0 MQISDPCH handle = MQISDP_INV_CONN_HANDLE;
774 0 ▶ int rc = MQISDP_INV_MSG_HANDLE;
Branch: 1 branch
BranchTrueFalseTotal
int rc = MQISDP_INV_MSG_HANDLE; 0 0 0
775
776 CODE:
777
778 /* get the connection handle */
779 0 handle = get_handle_from_hv( self );
780
781 0 rc = MQIsdp_getMsgStatus( handle, hMsg );
782
783 0 RETVAL = get_status_string(rc);
784
785 OUTPUT:
786 RETVAL
787  
« src/mspclnt.c ↑ Summary src/msputils.c »