| 1 |
|
| /* |
| 2 |
|
| * Persistence wrapper interface - provide callbacks to a Perl object |
| 3 |
|
| * |
| 4 |
|
| * Brian Candler <B.Candler@pobox.com> |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| /* |
| 8 |
|
| * FIXME: Is newSVuv() guaranteed to handle all values of 'unsigned long'? |
| 9 |
|
| * If not, what should we use? Probably not an issue, since mspscada.c says: |
| 10 |
|
| * "Conversion from u_long to u_short is ok as a key greater |
| 11 |
|
| * than can be accomodated by u_short is never used." |
| 12 |
|
| */ |
| 13 |
|
| |
| 14 |
|
| /* |
| 15 |
|
| * FIXME: we don't arrange in any special way for the 'persist' object |
| 16 |
|
| * we are passed not to be garbage-collected. With our implementation |
| 17 |
|
| * of WebSphere::MQTT::Client this isn't a problem, since $self->{'persist'} |
| 18 |
|
| * holds a reference to this object as well, but this could be tidied up. |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| #include "EXTERN.h" |
| 22 |
|
| #include "perl.h" |
| 23 |
|
| #include "XSUB.h" |
| 24 |
|
| |
| 25 |
|
| #include "MQIsdp.h" |
| 26 |
|
| |
| 27 |
4 |
| static int pstOpen(void *pUserData, char *pClientId, char *pBroker, int port) |
| 28 |
|
| { |
| 29 |
4 |
| dSP ; |
| 30 |
|
| int count, rc ; |
| 31 |
|
| |
| 32 |
4 |
| ENTER ; |
| 33 |
4 |
| SAVETMPS ; |
| 34 |
|
| |
| 35 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 36 |
4 |
▶ | XPUSHs((SV*)pUserData); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs((SV*)pUserData); |
0 |
4 |
4 |
|
| 37 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpv(pClientId, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(pClientId, 0))); |
0 |
4 |
4 |
|
| 38 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpv(pBroker, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(pBroker, 0))); |
0 |
4 |
4 |
|
| 39 |
4 |
▶ | XPUSHs(sv_2mortal(newSViv(port))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(port))); |
0 |
4 |
4 |
|
| 40 |
4 |
| PUTBACK ; |
| 41 |
|
| |
| 42 |
4 |
| count = call_method("open", G_SCALAR); |
| 43 |
|
| |
| 44 |
4 |
| SPAGAIN ; |
| 45 |
|
| |
| 46 |
4 |
▶ | if (count != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) |
0 |
4 |
4 |
|
| 47 |
0 |
| croak("open must return one value"); |
| 48 |
|
| |
| 49 |
4 |
| rc = POPi; |
| 50 |
|
| |
| 51 |
4 |
| PUTBACK ; |
| 52 |
4 |
▶ | FREETMPS ; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS ; |
4 |
0 |
4 |
|
| 53 |
4 |
| LEAVE ; |
| 54 |
4 |
| return rc; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
4 |
| static int Close(void *pUserData, const char *method) |
| 58 |
|
| { |
| 59 |
4 |
| dSP ; |
| 60 |
|
| int count, rc ; |
| 61 |
|
| |
| 62 |
4 |
| ENTER ; |
| 63 |
4 |
| SAVETMPS ; |
| 64 |
|
| |
| 65 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 66 |
4 |
▶ | XPUSHs((SV*)pUserData); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs((SV*)pUserData); |
0 |
4 |
4 |
|
| 67 |
4 |
| PUTBACK ; |
| 68 |
|
| |
| 69 |
4 |
| count = call_method(method, G_SCALAR); |
| 70 |
|
| |
| 71 |
4 |
| SPAGAIN ; |
| 72 |
|
| |
| 73 |
4 |
▶ | if (count != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) |
0 |
4 |
4 |
|
| 74 |
0 |
| croak("%s must return one value", method); |
| 75 |
|
| |
| 76 |
4 |
| rc = POPi; |
| 77 |
|
| |
| 78 |
4 |
| PUTBACK ; |
| 79 |
4 |
▶ | FREETMPS ; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS ; |
4 |
0 |
4 |
|
| 80 |
4 |
| LEAVE ; |
| 81 |
4 |
| return rc; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
2 |
| static int pstClose(void *pUserData) |
| 85 |
|
| { |
| 86 |
2 |
| return Close(pUserData, "close"); |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
2 |
| static int pstReset(void *pUserData) |
| 90 |
|
| { |
| 91 |
2 |
| return Close(pUserData, "reset"); |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| /* NOTE: there seems to be nothing in the spec which says the restored |
| 95 |
|
| * data must be kept in the same order as it was saved. The method is |
| 96 |
|
| * expected to return [key,value,key,value...] but this could just be |
| 97 |
|
| * a flattened form of a hash |
| 98 |
|
| */ |
| 99 |
|
| |
| 100 |
6 |
| static int GetAllMessages(void *pUserData, int *numMsgs, |
| 101 |
|
| MQISDP_PMSG **pMsgs, const char *method) |
| 102 |
|
| { |
| 103 |
6 |
| dSP ; |
| 104 |
|
| int count ; |
| 105 |
|
| static MQISDP_PMSG *msgs = NULL; /* should store in pUserData hash? */ |
| 106 |
|
| |
| 107 |
6 |
| *numMsgs = 0; |
| 108 |
6 |
| *pMsgs = NULL; |
| 109 |
|
| |
| 110 |
6 |
| ENTER ; |
| 111 |
6 |
| SAVETMPS ; |
| 112 |
|
| |
| 113 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 114 |
6 |
▶ | XPUSHs((SV*)pUserData); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs((SV*)pUserData); |
0 |
6 |
6 |
|
| 115 |
6 |
| PUTBACK ; |
| 116 |
|
| |
| 117 |
6 |
| count = call_method(method, G_ARRAY); |
| 118 |
|
| |
| 119 |
6 |
| SPAGAIN ; |
| 120 |
|
| |
| 121 |
6 |
▶ | if (count % 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count % 2) |
0 |
6 |
6 |
|
| 122 |
0 |
| croak("%s must return even number of values", method); |
| 123 |
|
| |
| 124 |
6 |
| count /= 2; |
| 125 |
6 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
4 |
2 |
6 |
|
| 126 |
4 |
▶ | if (msgs) free(msgs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (msgs) free(msgs); |
3 |
1 |
4 |
|
| 127 |
4 |
| msgs = malloc(count * sizeof(MQISDP_PMSG)); |
| 128 |
4 |
▶ | if (!msgs) croak("out of memory in %s", method); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!msgs) croak("out of memory in %s", method); |
0 |
4 |
4 |
|
| 129 |
|
| |
| 130 |
4 |
| *numMsgs = count; |
| 131 |
4 |
| *pMsgs = msgs; |
| 132 |
|
| |
| 133 |
16 |
▶ | while(count--) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(count--) { |
12 |
4 |
16 |
|
| 134 |
12 |
| SV *val = POPs; |
| 135 |
12 |
| SV *key = POPs; |
| 136 |
|
| char *val1, *val2; |
| 137 |
|
| STRLEN len; |
| 138 |
|
| |
| 139 |
12 |
| val1 = SvPV(val, len); |
| 140 |
12 |
| val2 = malloc(len); |
| 141 |
12 |
▶ | if (!val2) croak("out of memory in %s", method); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!val2) croak("out of memory in %s", method); |
0 |
12 |
12 |
|
| 142 |
|
| |
| 143 |
12 |
| msgs[count].key = SvUV(key); |
| 144 |
12 |
▶ | if (msgs[count].key <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msgs[count].key <= 0) |
0 |
12 |
12 |
|
| 145 |
0 |
| warn("Invalid key in %s!", method); |
| 146 |
12 |
| memcpy(val2, val1, len); |
| 147 |
12 |
| msgs[count].length = len; |
| 148 |
12 |
| msgs[count].pWmqttMsg = val2; |
| 149 |
|
| } |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
|
| #if 0 |
| 153 |
|
| fprintf(stderr, "%s: *numMsgs = %d\n", method, *numMsgs); |
| 154 |
|
| for (count=0; count < *numMsgs; count++) { |
| 155 |
|
| int i; |
| 156 |
|
| fprintf(stderr, "Key: %ld\nValue:", msgs[count].key); |
| 157 |
|
| for (i=0; i<msgs[count].length; i++) |
| 158 |
|
| fprintf(stderr, " %02X", msgs[count].pWmqttMsg[i]); |
| 159 |
|
| fprintf(stderr, "\n"); |
| 160 |
|
| } |
| 161 |
|
| #endif |
| 162 |
|
| |
| 163 |
6 |
| PUTBACK ; |
| 164 |
6 |
▶ | FREETMPS ; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS ; |
6 |
0 |
6 |
|
| 165 |
6 |
| LEAVE ; |
| 166 |
6 |
| return 0; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
3 |
| static int pstGetAllReceivedMessages(void *pUserData, int *numMsgs, |
| 170 |
|
| MQISDP_PMSG **pMsgs) |
| 171 |
|
| { |
| 172 |
3 |
| return GetAllMessages(pUserData, numMsgs, pMsgs, "getAllReceivedMessages"); |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
3 |
| static int pstGetAllSentMessages(void *pUserData, int *numMsgs, |
| 176 |
|
| MQISDP_PMSG **pMsgs) |
| 177 |
|
| { |
| 178 |
3 |
| return GetAllMessages(pUserData, numMsgs, pMsgs, "getAllSentMessages"); |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
1 |
| static int AddMessage(void *pUserData, unsigned long key, |
| 182 |
|
| int msgLength, char *pWmqttMsg, const char *method) |
| 183 |
|
| { |
| 184 |
1 |
| dSP ; |
| 185 |
|
| int count, rc ; |
| 186 |
|
| |
| 187 |
1 |
| ENTER ; |
| 188 |
1 |
| SAVETMPS ; |
| 189 |
|
| |
| 190 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 191 |
1 |
▶ | XPUSHs((SV*)pUserData); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs((SV*)pUserData); |
0 |
1 |
1 |
|
| 192 |
1 |
▶ | XPUSHs(sv_2mortal(newSVuv(key))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVuv(key))); |
0 |
1 |
1 |
|
| 193 |
1 |
▶ | XPUSHs(sv_2mortal(newSVpvn(pWmqttMsg, msgLength))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(pWmqttMsg, msgLength))); |
0 |
1 |
1 |
|
| 194 |
1 |
| PUTBACK ; |
| 195 |
|
| |
| 196 |
1 |
| count = call_method(method, G_SCALAR); |
| 197 |
|
| |
| 198 |
1 |
| SPAGAIN ; |
| 199 |
|
| |
| 200 |
1 |
▶ | if (count != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) |
0 |
1 |
1 |
|
| 201 |
0 |
| croak("%s must return one value", method); |
| 202 |
|
| |
| 203 |
1 |
| rc = POPi; |
| 204 |
|
| |
| 205 |
1 |
| PUTBACK ; |
| 206 |
1 |
▶ | FREETMPS ; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS ; |
1 |
0 |
1 |
|
| 207 |
1 |
| LEAVE ; |
| 208 |
1 |
| return rc; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
1 |
| static int pstAddSentMessage(void *pUserData, unsigned long key, |
| 212 |
|
| int msgLength, char *pWmqttMsg) |
| 213 |
|
| { |
| 214 |
1 |
| return AddMessage(pUserData, key, msgLength, pWmqttMsg, "addSentMessage"); |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
0 |
| static int pstUpdSentMessage(void *pUserData, unsigned long key, |
| 218 |
|
| int msgLength, char *pWmqttMsg) |
| 219 |
|
| { |
| 220 |
0 |
| return AddMessage(pUserData, key, msgLength, pWmqttMsg, "updSentMessage"); |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
0 |
| static int DelMessage(void *pUserData, unsigned long key, const char *method) |
| 224 |
|
| { |
| 225 |
0 |
| dSP ; |
| 226 |
|
| int count, rc ; |
| 227 |
|
| |
| 228 |
0 |
| ENTER ; |
| 229 |
0 |
| SAVETMPS ; |
| 230 |
|
| |
| 231 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 232 |
0 |
▶ | XPUSHs((SV*)pUserData); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs((SV*)pUserData); |
0 |
0 |
0 |
|
| 233 |
0 |
▶ | XPUSHs(sv_2mortal(newSVuv(key))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVuv(key))); |
0 |
0 |
0 |
|
| 234 |
0 |
| PUTBACK ; |
| 235 |
|
| |
| 236 |
0 |
| count = call_method(method, G_SCALAR); |
| 237 |
|
| |
| 238 |
0 |
| SPAGAIN ; |
| 239 |
|
| |
| 240 |
0 |
▶ | if (count != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) |
0 |
0 |
0 |
|
| 241 |
0 |
| croak("%s must return one value", method); |
| 242 |
|
| |
| 243 |
0 |
| rc = POPi; |
| 244 |
|
| |
| 245 |
0 |
| PUTBACK ; |
| 246 |
0 |
▶ | FREETMPS ; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS ; |
0 |
0 |
0 |
|
| 247 |
0 |
| LEAVE ; |
| 248 |
0 |
| return rc; |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
0 |
| static int pstDelSentMessage(void *pUserData, unsigned long key) |
| 252 |
|
| { |
| 253 |
0 |
| return DelMessage(pUserData, key, "delSentMessage"); |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
0 |
| static int pstAddReceivedMessage(void *pUserData, unsigned long key, |
| 257 |
|
| int msgLength, char *pWmqttMsg) |
| 258 |
|
| { |
| 259 |
0 |
| return AddMessage(pUserData, key, msgLength, pWmqttMsg, "addReceivedMessage"); |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
0 |
| static int pstUpdReceivedMessage(void *pUserData, unsigned long key) |
| 263 |
|
| { |
| 264 |
0 |
| return DelMessage(pUserData, key, "updReceivedMessage"); |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
0 |
| static int pstDelReceivedMessage(void *pUserData, unsigned long key) |
| 268 |
|
| { |
| 269 |
0 |
| return DelMessage(pUserData, key, "delReceivedMessage"); |
| 270 |
|
| } |
| 271 |
|
| |
| 272 |
5 |
| MQISDP_PERSIST *new_persistence_wrapper(SV *object) |
| 273 |
|
| { |
| 274 |
5 |
| MQISDP_PERSIST *p = malloc(sizeof (MQISDP_PERSIST)); |
| 275 |
5 |
▶ | if (!p) croak("new_persistence_wrapper out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) croak("new_persistence_wrapper out of memory"); |
0 |
5 |
5 |
|
| 276 |
5 |
| p->pUserData = (void*)object; |
| 277 |
5 |
| p->open = pstOpen; |
| 278 |
5 |
| p->close = pstClose; |
| 279 |
5 |
| p->reset = pstReset; |
| 280 |
5 |
| p->getAllReceivedMessages = pstGetAllReceivedMessages; |
| 281 |
5 |
| p->getAllSentMessages = pstGetAllSentMessages; |
| 282 |
5 |
| p->addSentMessage = pstAddSentMessage; |
| 283 |
5 |
| p->updSentMessage = pstUpdSentMessage; |
| 284 |
5 |
| p->delSentMessage = pstDelSentMessage; |
| 285 |
5 |
| p->addReceivedMessage = pstAddReceivedMessage; |
| 286 |
5 |
| p->updReceivedMessage = pstUpdReceivedMessage; |
| 287 |
5 |
| p->delReceivedMessage = pstDelReceivedMessage; |
| 288 |
5 |
| return p; |
| 289 |
|
| } |
| 290 |
|
| |