![](/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
- Committer:
- xinlei
- Date:
- 2015-05-07
- Revision:
- 98:e369fc75c000
- Parent:
- 95:5dfdc8568e9f
- Child:
- 100:dbcd3bc51758
File content as of revision 98:e369fc75c000:
#include <stdio.h> #include <ctype.h> #include "PollThread.h" #include "lex.h" #include "logging.h" bool PollThread::handshake() { int l = snprintf(buf2, SMARRESTBODY_SIZE, "%s", "80\r\n"); l = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, uri, l, buf2); sock.set_blocking(false); l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); if (l < 0) return false; size_t i = 0; for (const char* p = skipHTTPHeader(buf); isalnum(*p); ++p, ++i) { bayeuxId[i] = *p; } bayeuxId[i] = '\0'; return bayeuxId[0]; } bool PollThread::subscribe() { int l = snprintf(buf2, SMARRESTBODY_SIZE, "81,%s,/%s\r\n", bayeuxId, chn); l = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, uri, l, buf2); sock.set_blocking(false); l = sock.sendOnly(buf, l, SMARTREST_SIZE); return l>=0; } bool PollThread::connect() { int l = snprintf(buf2, SMARRESTBODY_SIZE, "83,%s\r\n", bayeuxId); l = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, uri, l, buf2); sock.set_blocking(true); l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); return l>=0; } void PollThread::threadFunc() { unsigned short state = 1; aInfo("Poll thread: %p\n", Thread::gettid()); while (true) { switch (state) { case 1: if (!handshake()) { aCritical("Poll: handshake fail!\n"); break; } case 2: if(!subscribe()) { aCritical("Poll: subscribe fail!\n"); state = 1; break; } case 3: if(!connect()) { aCritical("Poll: connect fail!\n"); state = 1; break; } default: parser.parse(buf); if (parser.getBayeuxAdvice() == BA_HANDSHAKE) state = 1; else state = 3; } } }