bool LongPollSession::GetLongPollServer() { MakeUrl(); rapidjson::Document document; std::string response = GetResponseString(this->urlForLongPoll); std::cout << response << std::endl; if(response == "") { return false; } const char* cstr = response.c_str(); document.Parse<0>(cstr); const rapidjson::Value& tmp = document["response"]; if(tmp.HasMember("key")) { this->Response.key = tmp["key"].GetString(); } if(tmp.HasMember("server")) { this->Response.server = tmp["server"].GetString(); } if(tmp.HasMember("ts")) { this->Response.ts = tmp["ts"].GetInt(); } return tmp.HasMember("failed") ? false : true; }
int main() { int sfd; // serial port file descriptor int i = 0; // loop counter char RspBytes[MAX_RSP_CHARS]; // Response string int numBytes; // Open the serial port device associated with the HRMI if ((sfd = OpenSerialPort("/dev/cu.usbserial-A9003PDh")) == -1) { return(-1); } // Send a series of Get Heart Rate commands, each time requesting more history buffer // entries while (i++ < 33) { if (SendGetHeartRate(sfd, i) == 0) { printf("Error: SendGetHeartRate failed!\n"); break; } if (GetResponseString(sfd, RspBytes) == -1) { printf("Error: GetResponseString failed!\n"); break; } else { printf("Request %2d => %s\n", i, RspBytes); } sleep(1); } CloseSerialPort(sfd); }
void LongPollSession::Start() { // std::cout << "Start session" << std::endl; action = true; int counter = 0; while(counter < 4) { if(GetLongPollServer()) //Trying 4 times to connect to longpoll server { break; } else counter++; } if(counter == 4) { this->error = "Can't connect to longPoll server"; this->wasError = true; } else { rapidjson::Document document; std::string urlToConnect = "https://" + this->Response.server + "?act=a_check" "&key=" + this->Response.key + "&ts=" + std::to_string(this->Response.ts) + "&wait=25" "&mode=0"; std::list<Messages> tmpList; while (action) { std::string response = GetResponseString(urlToConnect); if(response == "") { this->error = "Problems in connection to longpoll"; wasError = true; } else { const char *tmp = response.c_str(); document.Parse<0>(tmp); if (document.HasMember("ts")) { this->Response.ts = document["ts"].GetInt(); } urlToConnect = "https://" + this->Response.server + "?act=a_check" "&key=" + this->Response.key + "&ts=" + std::to_string(this->Response.ts) + "&wait=25" "&mode=0"; tmpList = Messages::GetMessageList(response); for (auto c : tmpList) { // lock_queue.lock(); queueOfMessages.push(c); } } } this->wasError = false; // std::cout << "Thread was killed" << std::endl; } }
void CSiemensSR::Response(RB1 *rb) { WORD response; SR_RBH *rb_H; static int pp = 0; if (rb == NULL) return; rb_H = (SR_RBH *) rb; /* check success/failure */ response = rb_H->rb_response; if ((response % 2) == 0 || response == REQ_WAITING) { /* print out opcode and response */ MSGERROR3 ("Error in \"%s\"\nrb_response = %s (%d)", GetOpcodeString(rb_H->rb_opcode), GetResponseString(response), response); CString strTemp; strTemp.Format (IDS_ERRSIEMENSSR, GetOpcodeString(rb_H->rb_opcode), GetResponseString(response), response); CAlarma * ptrAlarma = new CAlarma; ptrAlarma->AvisarAlarma (6,2,0,0,0,0,0,0,0,strTemp); switch (response) { case REM_ABORT: case ILLEGAL_REQ: // Michael 08.07.2008 case UNKNOWN_REFERENCE: // Michael 08.07.2008 fin MSGFATAL ("Cerrando comunicacion con el PLC por Error"); m_fConnected = FALSE; // Michael 27.11.2001 } // Michael 29.01.2002 switch (rb_H->rb_opcode) { case SEND_CONN_REQ: case OPEN_REQ: // Si ha dado error en la conexión, no va más MSGFATAL ("Fatal Siemens Error"); g_Container.m_fSiemensFatalError = true; case RECEIVE_DATA: MSGFATAL ("Response () : Anulando receive por error en recepción"); m_fReceivePend = false; // para que se inicie otro cuando se envíe algo } return; } switch (rb_H->rb_opcode) { case OPEN_REQ: /* save reference to the opened connection */ if (rb_H->rb_response == OK_RESP) { m_OpenRef = rb->rb.open.open_reference; Process(DO_CONNECT); } return; case SEND_CONN_REQ: m_fConnected = TRUE; m_fConnecting = FALSE; Process(DO_SEND_EOM); // PC es activo - iniciar send al CP return; case SEND_EOM_DATA: MSGTRACE ("CSiemensSR::Response (): rb_opcode = SEND_EOM_DATA"); // Michael 29.01.2002 Solo si no hay recieve pendiente if (!m_fReceivePend) Process(DO_RECEIVE); return; case RECEIVE_DATA: MSGTRACE ("CSiemensSR::Response (): rb_opcode = RECEIVE_DATA"); VerMensajeRecibido (); Process(DO_RECEIVE); // Preparar siguiente lectura MSGAVISO ("CSiemensSR::Response (): Siguiente Recepcion pedido"); return; case CLOSE_REQ: m_OpenRef = 0; m_fConnected = FALSE; // Michael 22.11.2001 SendMessage(m_hWnd, WM_DESTROY, 0, 0); return; } }
bool VK_API::Authorize() { rapidjson::Document document; std::string url = "https://oauth.vk.com/token" "?grant_type=password" "&client_id=3697615" "&client_secret=AlVXZFMUqyrnABp8ncuU" "&username="******"&password="******"&v=5.40" "&lang=ru";; if(needCaptcha) { url += "&captcha_sid=" + this->captcha.captcha_sid + "&captcha_key=" + this->captcha.captcha_key; } std::string tmpStr = GetResponseString(url); if(tmpStr == "") { this->error = "connection"; this->error_description = "no_connection"; this->has_error = true; return false; } const char* tmp = tmpStr.c_str(); document.Parse<0>(tmp); if(document.HasMember("access_token")) { this->_AccessToken = document["access_token"].GetString(); return true; } if (document.HasMember("error")) { if(document.HasMember("captcha_sid") && document.HasMember("captcha_img")) { this->error = document["error"].GetString(); this->captcha.captcha_sid = document["captcha_sid"].GetString(); this->captcha.captcha_img = document["captcha_img"].GetString(); this->needCaptcha = true; this->has_error = true; return false; } this->_AccessToken = ""; this->error = document["error"].GetString(); this->has_error = true; return false; } if (document.HasMember("error_description")) { this->_AccessToken = ""; this->error_description = document["error_description"].GetString(); this->has_error = true; return false; } else { this->_AccessToken = ""; this->error = "ERROR"; this->error_description = "unknown_error"; this->has_error = true; return false; } }