![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
operation/PollThread.cpp@95:5dfdc8568e9f, 2015-04-27 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 27 10:50:21 2015 +0000
- Revision:
- 95:5dfdc8568e9f
- Child:
- 98:e369fc75c000
LCDDisplay now is singleton
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 "lex.h" |
xinlei | 95:5dfdc8568e9f | 5 | #include "logging.h" |
xinlei | 95:5dfdc8568e9f | 6 | |
xinlei | 95:5dfdc8568e9f | 7 | bool PollThread::handshake() |
xinlei | 95:5dfdc8568e9f | 8 | { |
xinlei | 95:5dfdc8568e9f | 9 | int l = snprintf(buf2, SMARRESTBODY_SIZE, "%s", "80\r\n"); |
xinlei | 95:5dfdc8568e9f | 10 | l = snprintf(buf, SMARTREST_SIZE, getSmartRestFmt(), uri, l, buf2); |
xinlei | 95:5dfdc8568e9f | 11 | l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); |
xinlei | 95:5dfdc8568e9f | 12 | if (l < 0) |
xinlei | 95:5dfdc8568e9f | 13 | return false; |
xinlei | 95:5dfdc8568e9f | 14 | size_t i = 0; |
xinlei | 95:5dfdc8568e9f | 15 | for (const char* p = skipHTTPHeader(buf); isalnum(*p); ++p, ++i) { |
xinlei | 95:5dfdc8568e9f | 16 | bayeuxId[i] = *p; |
xinlei | 95:5dfdc8568e9f | 17 | } |
xinlei | 95:5dfdc8568e9f | 18 | bayeuxId[i] = '\0'; |
xinlei | 95:5dfdc8568e9f | 19 | return bayeuxId[0]; |
xinlei | 95:5dfdc8568e9f | 20 | } |
xinlei | 95:5dfdc8568e9f | 21 | |
xinlei | 95:5dfdc8568e9f | 22 | bool PollThread::subscribe() |
xinlei | 95:5dfdc8568e9f | 23 | { |
xinlei | 95:5dfdc8568e9f | 24 | int l = snprintf(buf2, SMARRESTBODY_SIZE, "81,%s,/%s\r\n", bayeuxId, chn); |
xinlei | 95:5dfdc8568e9f | 25 | l = snprintf(buf, SMARTREST_SIZE, getSmartRestFmt(), uri, l, buf2); |
xinlei | 95:5dfdc8568e9f | 26 | l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); |
xinlei | 95:5dfdc8568e9f | 27 | return l>=0; |
xinlei | 95:5dfdc8568e9f | 28 | } |
xinlei | 95:5dfdc8568e9f | 29 | |
xinlei | 95:5dfdc8568e9f | 30 | bool PollThread::connect() |
xinlei | 95:5dfdc8568e9f | 31 | { |
xinlei | 95:5dfdc8568e9f | 32 | int l = snprintf(buf2, SMARRESTBODY_SIZE, "83,%s\r\n", bayeuxId); |
xinlei | 95:5dfdc8568e9f | 33 | l = snprintf(buf, SMARTREST_SIZE, getSmartRestFmt(), uri, l, buf2); |
xinlei | 95:5dfdc8568e9f | 34 | l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); |
xinlei | 95:5dfdc8568e9f | 35 | return l>=0; |
xinlei | 95:5dfdc8568e9f | 36 | } |
xinlei | 95:5dfdc8568e9f | 37 | |
xinlei | 95:5dfdc8568e9f | 38 | void PollThread::threadFunc() |
xinlei | 95:5dfdc8568e9f | 39 | { |
xinlei | 95:5dfdc8568e9f | 40 | uint8_t state = 1; |
xinlei | 95:5dfdc8568e9f | 41 | aInfo("Poll thread: %p\n", Thread::gettid()); |
xinlei | 95:5dfdc8568e9f | 42 | while (true) { |
xinlei | 95:5dfdc8568e9f | 43 | switch (state) { |
xinlei | 95:5dfdc8568e9f | 44 | case 1: if (!handshake()) { |
xinlei | 95:5dfdc8568e9f | 45 | aCritical("Poll: handshake fail!\n"); |
xinlei | 95:5dfdc8568e9f | 46 | break; |
xinlei | 95:5dfdc8568e9f | 47 | } |
xinlei | 95:5dfdc8568e9f | 48 | case 2: if(!subscribe()) { |
xinlei | 95:5dfdc8568e9f | 49 | aCritical("Poll: subscribe fail!\n"); |
xinlei | 95:5dfdc8568e9f | 50 | state = 1; |
xinlei | 95:5dfdc8568e9f | 51 | break; |
xinlei | 95:5dfdc8568e9f | 52 | } |
xinlei | 95:5dfdc8568e9f | 53 | case 3: if(!connect()) { |
xinlei | 95:5dfdc8568e9f | 54 | aCritical("Poll: connect fail!\n"); |
xinlei | 95:5dfdc8568e9f | 55 | state = 1; |
xinlei | 95:5dfdc8568e9f | 56 | break; |
xinlei | 95:5dfdc8568e9f | 57 | } |
xinlei | 95:5dfdc8568e9f | 58 | default: parser.handleControlMessage(buf); |
xinlei | 95:5dfdc8568e9f | 59 | if (parser.getBayeuxAdvice() == BA_HANDSHAKE) |
xinlei | 95:5dfdc8568e9f | 60 | state = 1; |
xinlei | 95:5dfdc8568e9f | 61 | else |
xinlei | 95:5dfdc8568e9f | 62 | state = 3; |
xinlei | 95:5dfdc8568e9f | 63 | } |
xinlei | 95:5dfdc8568e9f | 64 | } |
xinlei | 95:5dfdc8568e9f | 65 | } |