Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
operation/PollThread.cpp@129:dc9e37d4bc05, 2015-06-29 (annotated)
- Committer:
- xinlei
- Date:
- Mon Jun 29 11:14:27 2015 +0000
- Revision:
- 129:dc9e37d4bc05
- Parent:
- 126:871e6eb0fa76
- Child:
- 134:c4009ecaf5c0
PollThread: support for heartbeat message from server.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 95:5dfdc8568e9f | 1 | #include <stdio.h> |
xinlei | 95:5dfdc8568e9f | 2 | #include <ctype.h> |
xinlei | 95:5dfdc8568e9f | 3 | #include "PollThread.h" |
xinlei | 95:5dfdc8568e9f | 4 | #include "logging.h" |
xinlei | 95:5dfdc8568e9f | 5 | |
xinlei | 95:5dfdc8568e9f | 6 | bool PollThread::handshake() |
xinlei | 95:5dfdc8568e9f | 7 | { |
xinlei | 105:c61f0d62b625 | 8 | int l = snprintf(buf2, sizeof(buf2), "%s", "80\r\n"); |
xinlei | 105:c61f0d62b625 | 9 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 10 | sock.setBlocking(3000); |
xinlei | 105:c61f0d62b625 | 11 | l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 124:979ab0d075de | 12 | if (l <= 0) return false; |
xinlei | 124:979ab0d075de | 13 | const char* p = skipHTTPHeader(buf); |
xinlei | 124:979ab0d075de | 14 | if (p == NULL) return false; |
xinlei | 95:5dfdc8568e9f | 15 | size_t i = 0; |
xinlei | 124:979ab0d075de | 16 | for (; isalnum(*p); ++p, ++i) { |
xinlei | 95:5dfdc8568e9f | 17 | bayeuxId[i] = *p; |
xinlei | 95:5dfdc8568e9f | 18 | } |
xinlei | 116:5de54f09f754 | 19 | bayeuxId[i] = 0; |
xinlei | 95:5dfdc8568e9f | 20 | return bayeuxId[0]; |
xinlei | 95:5dfdc8568e9f | 21 | } |
xinlei | 95:5dfdc8568e9f | 22 | |
xinlei | 95:5dfdc8568e9f | 23 | bool PollThread::subscribe() |
xinlei | 95:5dfdc8568e9f | 24 | { |
xinlei | 105:c61f0d62b625 | 25 | int l = snprintf(buf2, sizeof(buf2), "81,%s,%s\r\n", bayeuxId, chn); |
xinlei | 105:c61f0d62b625 | 26 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 27 | sock.setBlocking(3000); |
xinlei | 122:3e4a1ed4bad5 | 28 | l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 122:3e4a1ed4bad5 | 29 | if (l <= 0) return false; |
xinlei | 122:3e4a1ed4bad5 | 30 | const char *p = skipHTTPHeader(buf); |
xinlei | 122:3e4a1ed4bad5 | 31 | if (p == NULL) return false; |
xinlei | 122:3e4a1ed4bad5 | 32 | for (; *p && !isgraph(*p); ++p); |
xinlei | 122:3e4a1ed4bad5 | 33 | return *p == 0; |
xinlei | 95:5dfdc8568e9f | 34 | } |
xinlei | 95:5dfdc8568e9f | 35 | |
xinlei | 95:5dfdc8568e9f | 36 | bool PollThread::connect() |
xinlei | 95:5dfdc8568e9f | 37 | { |
xinlei | 105:c61f0d62b625 | 38 | int l = snprintf(buf2, sizeof(buf2), "83,%s\r\n", bayeuxId); |
xinlei | 105:c61f0d62b625 | 39 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 126:871e6eb0fa76 | 40 | sock.setBlocking(610000); // Timeout after 10m:10s |
xinlei | 129:dc9e37d4bc05 | 41 | // sock.setBlocking(-1); |
xinlei | 129:dc9e37d4bc05 | 42 | // l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 129:dc9e37d4bc05 | 43 | int c = sock.connect(); |
xinlei | 129:dc9e37d4bc05 | 44 | if (c < 0) { |
xinlei | 129:dc9e37d4bc05 | 45 | sock.close(); |
xinlei | 129:dc9e37d4bc05 | 46 | return false; |
xinlei | 129:dc9e37d4bc05 | 47 | } |
xinlei | 129:dc9e37d4bc05 | 48 | c = sock.send(buf, l); |
xinlei | 129:dc9e37d4bc05 | 49 | if (c < 0) { |
xinlei | 129:dc9e37d4bc05 | 50 | sock.close(); |
xinlei | 129:dc9e37d4bc05 | 51 | return false; |
xinlei | 129:dc9e37d4bc05 | 52 | } |
xinlei | 129:dc9e37d4bc05 | 53 | // Avoid heartbeat message overflows the buffer |
xinlei | 129:dc9e37d4bc05 | 54 | l = 0; |
xinlei | 129:dc9e37d4bc05 | 55 | for (unsigned short i = 0; i < 200; ++i) { |
xinlei | 129:dc9e37d4bc05 | 56 | int l2 = sock.receive(buf+l, sizeof(buf)-l); |
xinlei | 129:dc9e37d4bc05 | 57 | printf("l2: %d\n", l2); |
xinlei | 129:dc9e37d4bc05 | 58 | if (l2 > 0) { |
xinlei | 129:dc9e37d4bc05 | 59 | printf("poll: %.*s\n", l2, buf+l); |
xinlei | 129:dc9e37d4bc05 | 60 | l += l2; |
xinlei | 129:dc9e37d4bc05 | 61 | } else if (!sock.is_connected()) { |
xinlei | 129:dc9e37d4bc05 | 62 | break; |
xinlei | 129:dc9e37d4bc05 | 63 | } else { |
xinlei | 129:dc9e37d4bc05 | 64 | l = l2; |
xinlei | 129:dc9e37d4bc05 | 65 | break; |
xinlei | 129:dc9e37d4bc05 | 66 | } |
xinlei | 129:dc9e37d4bc05 | 67 | } |
xinlei | 129:dc9e37d4bc05 | 68 | if (l >= 0) |
xinlei | 129:dc9e37d4bc05 | 69 | buf[l] = 0; |
xinlei | 129:dc9e37d4bc05 | 70 | sock.close(); |
xinlei | 122:3e4a1ed4bad5 | 71 | return l>0; |
xinlei | 95:5dfdc8568e9f | 72 | } |
xinlei | 95:5dfdc8568e9f | 73 | |
xinlei | 95:5dfdc8568e9f | 74 | void PollThread::threadFunc() |
xinlei | 95:5dfdc8568e9f | 75 | { |
xinlei | 98:e369fc75c000 | 76 | unsigned short state = 1; |
xinlei | 95:5dfdc8568e9f | 77 | while (true) { |
xinlei | 95:5dfdc8568e9f | 78 | switch (state) { |
xinlei | 95:5dfdc8568e9f | 79 | case 1: if (!handshake()) { |
xinlei | 95:5dfdc8568e9f | 80 | aCritical("Poll: handshake fail!\n"); |
xinlei | 95:5dfdc8568e9f | 81 | break; |
xinlei | 95:5dfdc8568e9f | 82 | } |
xinlei | 95:5dfdc8568e9f | 83 | case 2: if(!subscribe()) { |
xinlei | 95:5dfdc8568e9f | 84 | aCritical("Poll: subscribe fail!\n"); |
xinlei | 95:5dfdc8568e9f | 85 | state = 1; |
xinlei | 95:5dfdc8568e9f | 86 | break; |
xinlei | 95:5dfdc8568e9f | 87 | } |
xinlei | 95:5dfdc8568e9f | 88 | case 3: if(!connect()) { |
xinlei | 95:5dfdc8568e9f | 89 | aCritical("Poll: connect fail!\n"); |
xinlei | 95:5dfdc8568e9f | 90 | state = 1; |
xinlei | 95:5dfdc8568e9f | 91 | break; |
xinlei | 95:5dfdc8568e9f | 92 | } |
xinlei | 98:e369fc75c000 | 93 | default: parser.parse(buf); |
xinlei | 95:5dfdc8568e9f | 94 | if (parser.getBayeuxAdvice() == BA_HANDSHAKE) |
xinlei | 95:5dfdc8568e9f | 95 | state = 1; |
xinlei | 95:5dfdc8568e9f | 96 | else |
xinlei | 95:5dfdc8568e9f | 97 | state = 3; |
xinlei | 95:5dfdc8568e9f | 98 | } |
xinlei | 95:5dfdc8568e9f | 99 | } |
xinlei | 95:5dfdc8568e9f | 100 | } |