int CVkProto::SetStatus(int iNewStatus) { if (m_iDesiredStatus == iNewStatus || iNewStatus == ID_STATUS_IDLE) return 0; int oldStatus = m_iStatus; m_iDesiredStatus = iNewStatus; if (iNewStatus == ID_STATUS_OFFLINE) { if ( IsOnline()) { SetServerStatus(ID_STATUS_OFFLINE); ShutdownSession(); } m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); } else if (m_hWorkerThread == NULL && !(m_iStatus >= ID_STATUS_CONNECTING && m_iStatus < ID_STATUS_CONNECTING + MAX_CONNECT_RETRIES)) { m_iStatus = ID_STATUS_CONNECTING; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); m_hWorkerThread = ForkThreadEx(&CVkProto::WorkerThread, 0, NULL); } else if ( IsOnline()) SetServerStatus(iNewStatus); else ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); return 0; }
void CVkProto::ConnectionFailed(int iReason) { delSetting("AccessToken"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, iReason); debugLogA("CVkProto::ConnectionFailed ShutdownSession"); ShutdownSession(); }
void CVkProto::ExecuteRequest(AsyncHttpRequest *pReq) { CMStringA str; do { pReq->bNeedsRestart = false; pReq->szUrl = pReq->m_szUrl.GetBuffer(); if (!pReq->m_szParam.IsEmpty()) { if (pReq->requestType == REQUEST_GET) { str.Format("%s?%s", pReq->m_szUrl, pReq->m_szParam); pReq->szUrl = str.GetBuffer(); } else { pReq->pData = mir_strdup(pReq->m_szParam); pReq->dataLength = pReq->m_szParam.GetLength(); } } if (pReq->m_bApiReq) { pReq->flags |= NLHRF_PERSISTENT; pReq->nlc = m_hAPIConnection; } debugLogA("CVkProto::ExecuteRequest \n====\n%s\n====\n", pReq->szUrl); NETLIBHTTPREQUEST *reply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)pReq); if (reply != NULL) { if (pReq->m_pFunc != NULL) (this->*(pReq->m_pFunc))(reply, pReq); // may be set pReq->bNeedsRestart if (pReq->m_bApiReq) m_hAPIConnection = reply->nlc; CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)reply); } else if (pReq->bIsMainConn) { if (IsStatusConnecting(m_iStatus)) ConnectionFailed(LOGINERR_NONETWORK); else if (pReq->m_iRetry && !m_bTerminated) { pReq->bNeedsRestart = true; Sleep(1000); //Pause for fix err pReq->m_iRetry--; debugLogA("CVkProto::ExecuteRequest restarting (retry = %d)", MAX_RETRIES - pReq->m_iRetry); } else { debugLogA("CVkProto::ExecuteRequest ShutdownSession"); ShutdownSession(); } } debugLogA("CVkProto::ExecuteRequest pReq->bNeedsRestart = %d", (int)pReq->bNeedsRestart); if (!reply && pReq->m_bApiReq) m_hAPIConnection = NULL; } while (pReq->bNeedsRestart && !m_bTerminated); delete pReq; }
int CVkProto::SetStatus(int iNewStatus) { debugLogA("CVkProto::SetStatus iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d m_hWorkerThread = %d", iNewStatus, m_iStatus, m_iDesiredStatus, m_hWorkerThread == NULL ? 0 : 1); if (m_iDesiredStatus == iNewStatus || iNewStatus == ID_STATUS_IDLE) return 0; int oldStatus = m_iStatus; m_iDesiredStatus = iNewStatus; if (iNewStatus == ID_STATUS_OFFLINE) { if (IsOnline()) { SetServerStatus(ID_STATUS_OFFLINE); debugLogA("CVkProto::SetStatus ShutdownSession"); ShutdownSession(); } m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); debugLogA("CVkProto::SetStatus (1) iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d oldStatus = %d", iNewStatus, m_iStatus, m_iDesiredStatus, oldStatus); } else if (m_hWorkerThread == NULL && !IsStatusConnecting(m_iStatus)) { m_iStatus = ID_STATUS_CONNECTING; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); debugLogA("CVkProto::SetStatus (2) iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d oldStatus = %d", iNewStatus, m_iStatus, m_iDesiredStatus, oldStatus); m_hWorkerThread = ForkThreadEx(&CVkProto::WorkerThread, 0, NULL); } else if (IsOnline()) { debugLogA("CVkProto::SetStatus (3) iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d oldStatus = %d", iNewStatus, m_iStatus, m_iDesiredStatus, oldStatus); SetServerStatus(iNewStatus); } else { debugLogA("CVkProto::SetStatus (4) iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d oldStatus = %d", iNewStatus, m_iStatus, m_iDesiredStatus, oldStatus); ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); if (!IsStatusConnecting(m_iStatus)) m_iDesiredStatus = m_iStatus; debugLogA("CVkProto::SetStatus (5) iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d oldStatus = %d", iNewStatus, m_iStatus, m_iDesiredStatus, oldStatus); } debugLogA("CVkProto::SetStatus (ret) iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d oldStatus = %d", iNewStatus, m_iStatus, m_iDesiredStatus, oldStatus); return 0; }
void CVkProto::OnReceivePollingInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceivePollingInfo %d", reply->resultCode); if (reply->resultCode != 200) return; JSONNode jnRoot; JSONNode jnResponse = CheckJsonResponse(pReq, reply, jnRoot); if (!jnResponse) return; char ts[32]; itoa(jnResponse["ts"].as_int(), ts, 10); m_pollingTs = mir_strdup(ts); m_pollingKey = mir_t2a(jnResponse["key"].as_mstring()); m_pollingServer = mir_t2a(jnResponse["server"].as_mstring()); if (!m_hPollingThread) { debugLogA("CVkProto::OnReceivePollingInfo m_hPollingThread is NULL"); debugLogA("CVkProto::OnReceivePollingInfo m_pollingTs = \'%s' m_pollingKey = \'%s\' m_pollingServer = \'%s\'", m_pollingTs ? m_pollingTs : "<NULL>", m_pollingKey ? m_pollingKey : "<NULL>", m_pollingServer ? m_pollingServer : "<NULL>"); if (m_pollingTs != NULL && m_pollingKey != NULL && m_pollingServer != NULL) { debugLogA("CVkProto::OnReceivePollingInfo PollingThread starting..."); m_hPollingThread = ForkThreadEx(&CVkProto::PollingThread, NULL, NULL); } else { debugLogA("CVkProto::OnReceivePollingInfo PollingThread not start"); m_pollingConn = NULL; ShutdownSession(); return; } } else debugLogA("CVkProto::OnReceivePollingInfo m_hPollingThread is not NULL"); }
int CVkProto::PollServer() { debugLogA("CVkProto::PollServer"); if (!IsOnline()) { debugLogA("CVkProto::PollServer is dead (not online)"); m_pollingConn = NULL; ShutdownSession(); return 0; } debugLogA("CVkProto::PollServer (online)"); int iPollConnRetry = MAX_RETRIES; NETLIBHTTPREQUEST *reply; CMStringA szReqUrl(FORMAT, "https://%s?act=a_check&key=%s&ts=%s&wait=25&access_token=%s&mode=%d", m_pollingServer, m_pollingKey, m_pollingTs, m_szAccessToken, 106); // see mode parametr description on https://vk.com/dev/using_longpoll (Russian version) NETLIBHTTPREQUEST req = {}; req.cbSize = sizeof(req); req.requestType = REQUEST_GET; req.szUrl = mir_strdup(szReqUrl); req.flags = VK_NODUMPHEADERS | NLHRF_PERSISTENT | NLHRF_HTTP11 | NLHRF_SSL; req.timeout = 30000; req.nlc = m_pollingConn; while ((reply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)&req)) == NULL) { debugLogA("CVkProto::PollServer is dead"); m_pollingConn = NULL; if (iPollConnRetry && !m_bTerminated) { iPollConnRetry--; debugLogA("CVkProto::PollServer restarting %d", MAX_RETRIES - iPollConnRetry); Sleep(1000); } else { debugLogA("CVkProto::PollServer => ShutdownSession"); mir_free(req.szUrl); ShutdownSession(); return 0; } } mir_free(req.szUrl); int retVal = 0; if (reply->resultCode == 200) { JSONNode jnRoot = JSONNode::parse(reply->pData); const JSONNode &jnFailed = jnRoot["failed"]; if (jnFailed && jnFailed.as_int() > 1) { RetrievePollingInfo(); retVal = -1; debugLogA("Polling key expired, restarting polling thread"); } else if (CheckJsonResult(NULL, jnRoot)) { char ts[32]; itoa(jnRoot["ts"].as_int(), ts, 10); m_pollingTs = mir_strdup(ts); const JSONNode &jnUpdates = jnRoot["updates"]; if (jnUpdates) PollUpdates(jnUpdates); retVal = 1; } } else if ((reply->resultCode >= 400 && reply->resultCode <= 417) || (reply->resultCode >= 500 && reply->resultCode <= 509)) { debugLogA("CVkProto::PollServer is dead. Error code - %d", reply->resultCode); m_pollingConn = NULL; CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)reply); ShutdownSession(); return 0; } m_pollingConn = reply->nlc; CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)reply); debugLogA("CVkProto::PollServer return %d", retVal); return retVal; }
int main(int argc, char **argv) { LCHAR phrase[MAX_LINE_LENGTH]; SR_Vocabulary *vocab = 0; LCHAR vocabfile[MAX_LINE_LENGTH]; LCHAR outfilename[MAX_LINE_LENGTH]; LCHAR testfilename[MAX_LINE_LENGTH]; LCHAR parfilename[MAX_LINE_LENGTH]; LCHAR wordfile[MAX_LINE_LENGTH]; LCHAR locale[MAX_LINE_LENGTH]; LCHAR ptemp[MAX_LINE_LENGTH]; LCHAR* p; ESR_ReturnCode rc; int i; PFile* fin = 0; FILE* fout = stdout; size_t len; ESR_BOOL bSession = ESR_FALSE; LCHAR *env_sdk_path; LCHAR *env_lang; CHKLOG(rc, PMemInit()); /* CHKLOG(rc, PFileSystemCreate()); CHKLOG(rc, PANSIFileSystemCreate()); CHKLOG(rc, PANSIFileSystemAddPath(L("/dev/ansi"), L("/")));*/ /* Set ANSI file-system as default file-system */ /* CHKLOG(rc, PANSIFileSystemSetDefault(ESR_TRUE));*/ /* Set virtual current working directory to native current working directory */ /* len = P_PATH_MAX; CHKLOG(rc, PANSIFileSystemGetcwd(cwd, &len)); CHKLOG(rc, PFileSystemChdir(cwd));*/ fout = stdout; *vocabfile = 0; *wordfile = 0; *locale = 0; *outfilename = 0; *testfilename = 0; *parfilename = 0; /* get some phrases from the user */ LPRINTF("\nDictation Test Program for esr (Nuance Communications, 2007)\n"); if(argc != 1 && argc != 3 && argc != 5 && argc != 7 && argc != 9) { usage(); rc = 1; goto CLEANUP; } for(i=1; i<argc; i++) { if(!LSTRCMP(argv[i], L("-words"))) LSTRCPY(wordfile, argv[++i]); else if(!LSTRCMP(argv[i], L("-vocab"))) LSTRCPY(vocabfile, argv[++i]); else if(!LSTRCMP(argv[i], L("-locale"))) LSTRCPY(locale, argv[++i]); else if(!LSTRCMP(argv[i], L("-out"))) LSTRCPY(outfilename, argv[++i]); else if(!LSTRCMP(argv[i], L("-itest"))) LSTRCPY(testfilename, argv[++i]); else if(!LSTRCMP(argv[i], L("-parfile")) || !LSTRCMP(argv[i], L("-par")) ) LSTRCPY(parfilename, argv[++i]); else { usage(); rc = 1; goto CLEANUP; } } if ( *parfilename == L('\0') ) { LPRINTF ( "Warning: No parfile defined in the command line.\n" ); LPRINTF ( "Looking for the default parfile, $ESRSDK/config/$ESRLANG/baseline.par...\n" ); env_sdk_path = LGETENV(L("ESRSDK")); if ( env_sdk_path != NULL ) { LSPRINTF ( parfilename, L("%s/config/"), env_sdk_path ); env_lang = LGETENV(L("ESRLANG")); if ( env_lang != NULL ) { LSTRCAT ( parfilename, env_lang ); LSTRCAT ( parfilename, L("/baseline.par") ); } else { LPRINTF("Error: An environment variable ESRLANG should be defined.\n"); goto CLEANUP; } } else { LPRINTF("Error: An environment variable ESRSDK should be defined.\n"); goto CLEANUP; } } rc = InitSession( parfilename ); if ( rc != ESR_SUCCESS ) { LPRINTF("Error: %s\n", ESR_rc2str(rc)); goto CLEANUP; } bSession = ESR_TRUE; if (*vocabfile == 0) { len = sizeof(vocabfile); rc = ESR_SessionGetLCHAR ( L("cmdline.vocabulary"), vocabfile, &len ); env_sdk_path = LGETENV(L("ESRSDK")); if ( env_sdk_path != NULL ) { LSPRINTF ( parfilename, L("%s/config/"), env_sdk_path ); env_lang = LGETENV(L("ESRLANG")); if ( env_lang != NULL ) { LSTRCAT ( parfilename, env_lang ); LSTRCAT ( parfilename, L("/baseline.par") ); } else { LPRINTF("Error: An environment variable ESRLANG should be defined.\n"); goto CLEANUP; } } else { LPRINTF("Error: An environment variable ESRSDK should be defined.\n"); goto CLEANUP; } strcpy(ptemp, env_sdk_path); strcat(ptemp,"/config/"); strcat(ptemp,env_lang); strcat(ptemp,"/"); strcat(ptemp,vocabfile); strcpy(vocabfile,ptemp); if ( rc == ESR_SUCCESS ) { len = sizeof(vocabfile); rc = ESR_SessionPrefixWithBaseDirectory(vocabfile, &len); } else { *vocabfile = 0; } } if (*vocabfile) rc = SR_VocabularyLoad(vocabfile, &vocab); else if (*locale) { ESR_Locale localeTag; rc = ESR_str2locale(locale, &localeTag); if (rc != ESR_SUCCESS) { LPRINTF("Error: %s\n",ESR_rc2str(rc)); goto CLEANUP; } rc = SR_VocabularyCreate(localeTag, &vocab); } else rc = SR_VocabularyCreate(ESR_LOCALE_EN_US, &vocab); if (rc != ESR_SUCCESS) { LPRINTF("Error: %s\n",ESR_rc2str(rc)); goto CLEANUP; } if (*outfilename) /* output file */ { if ((fout = fopen(outfilename,"w")) == NULL) { LPRINTF("Could not open file: %s\n",outfilename); rc = 1; goto CLEANUP; } } if (*wordfile) /* file mode */ { if ((fin = pfopen(wordfile,"r")) == NULL) { LPRINTF("Could not open file: %s\n", wordfile); goto CLEANUP; } while (pfgets(phrase, MAX_LINE_LENGTH, fin)!=NULL) { lstrtrim(phrase); doGetProns(vocab, phrase, MAX_PRONS_LENGTH, fout); } } else if (*testfilename) /* test file mode */ { if ((fin = pfopen(testfilename,"r")) == NULL) { LPRINTF("Could not open file: %s\n", testfilename); rc = 1; goto CLEANUP; } doInputTestPhonemes(vocab, fin, fout); } else /* interactive mode */ { LPRINTF("'qqq' to quit\n"); while (ESR_TRUE) { LPRINTF("> "); if(! pfgets(phrase, MAX_LINE_LENGTH, PSTDIN )) break; // remove trailing whitespace for(p=&phrase[0]; *p!=0 && *p!='\n' && *p!='\r'; p++) {} *p=0; lstrtrim(phrase); if(!LSTRCMP("qqq",phrase)) break; else doGetProns(vocab, phrase, MAX_PRONS_LENGTH, fout); } } CLEANUP: if(vocab) vocab->destroy(vocab); if(bSession) ShutdownSession(); if(fin) pfclose(fin); if(fout && fout != stdout) fclose(fout); /* PANSIFileSystemDestroy(); PFileSystemDestroy();*/ PMemShutdown(); return rc; }