int SuccUBusScript::chooseResponse(const TTroomScript *roomScript, const TTsentence *sentence, uint tag) { uint dialogueId = tag; switch (tag) { case MKTAG('S', 'L', 'O', 'W'): case MKTAG('T', 'H', 'R', 'T'): dialogueId = 70021; case MKTAG('S', 'U', 'C', '1'): dialogueId = getDialogueId(230009); break; case MKTAG('S', 'U', 'C', '2'): dialogueId = 70117; break; case MKTAG('S', 'W', 'E', 'R'): dialogueId = getRandomNumber(100) > 40 ? 70103 : getDialogueId(230030); break; default: break; } if (dialogueId) { addResponse(dialogueId); applyResponse(); return 2; } else { return 1; } }
int SuccUBusScript::updateState(uint oldId, uint newId, int index) { if (newId == 230199) { return _isRoom101 ? 230148 : newId; } else if (newId >= 230208 && newId <= 230235) { addResponse(70158 - getRandomBit()); return newId; } else if (newId >= 230061 && newId <= 230063) { if (getValue(2)) return 230125; } static const uint UPDATE_STATES[][2] = { { 230078, 1 }, { 230106, 2 }, { 230112, 3 }, { 230115, 4 }, { 230127, 5 }, { 230140, 6 }, { 230156, 7 }, { 230157, 8 }, { 230159, 9 }, { 230160, 10 }, { 230161, 11 }, { 230072, 12 } }; for (int idx = 0; idx < 12; ++idx) { if (UPDATE_STATES[idx][0] == newId) { setState(UPDATE_STATES[idx][1]); break; } } return newId; }
ScriptChangedResult SuccUBusScript::scriptChanged(const TTroomScript *roomScript, uint id) { if (id == 148) CTrueTalkManager::setFlags(3, 1); else if (id == 150) CTrueTalkManager::setFlags(2, 1); if (id >= 230000 && id <= 230245) { addResponse(getDialogueId(id)); applyResponse(); } else if (id >= 70000 && id <= 70243) { addResponse(id); applyResponse(); } return SCR_2; }
void HttpServerTask::handleDone() { // get response from job request HttpResponse* response = httpJobRequest->getResponse(); if (response == 0) { response = new HttpResponse(HttpResponse::NO_RESPONSE); } // trace #ifdef ENABLE_TRACE_OPTION { string requestString = ""; if (httpRequest != 0) { requestString = httpRequest->getHeaderString(); const string bs = httpRequest->getBodyString(); if (!bs.empty()) { if (requestString.find("?") == string::npos) { requestString += "?"; } requestString += bs; } } server->traceRequest(requestString + response->getBody().c_str()); } #endif // add response to output buffer addResponse(response); // we can now free the response delete response; // and the jobe request delete httpJobRequest; httpJobRequest = 0; // and the http request if (httpRequest != 0) { httpRequest->releaseBuffers(); delete httpRequest; httpRequest = 0; } httpRequestPending = false; // remove body from read buffer and reset read position readBuffer.erase_front(bodyPosition + bodyLength); readPosition = 0; bodyPosition = 0; bodyLength = 0; }
int SuccUBusScript::setResponse(int dialogueId, int state) { addResponse(dialogueId); applyResponse(); if (state != -1) setState(state); return 2; }
int SuccUBusScript::doSentenceEntry(int val1, const int *srcIdP, const TTroomScript *roomScript, const TTsentence *sentence) { if (val1 == 1 && roomScript && roomScript->_scriptId == 101) { addResponse(getDialogueId(230239)); applyResponse(); return 2; } return 0; }
int ParrotScript::chooseResponse(const TTroomScript *roomScript, const TTsentence *sentence, uint tag) { if (tag == MKTAG('B', 'Y', 'Z', 'A')) { addResponse(getDialogueId(280246)); applyResponse(); return 2; } else { return 1; } }
int ParrotScript::process(const TTroomScript *roomScript, const TTsentence *sentence) { if (processEntries(roomScript, sentence) != 2) { int tagId = g_vm->_trueTalkManager->_quotes.find(sentence->_normalizedLine); if (!tagId || chooseResponse(roomScript, sentence, tagId) != 2) { addResponse(getDialogueId(sentence->checkCategory() ? 280248 : 280235)); applyResponse(); } } return 2; }
ScriptChangedResult ParrotScript::scriptChanged(const TTroomScript *roomScript, uint id) { if (id >= 280000 && id <= 280276) { if (id == 280258) { if (CTrueTalkManager::_currentNPC) { CGameObject *chicken; if (CTrueTalkManager::_currentNPC->find("Chicken", &chicken, FIND_PET)) id = 280147 - getRandomBit(); } id = getDialogueId(id); } else { if ((id == 280146 || id == 280147) && CTrueTalkManager::_currentNPC) { CGameObject *chicken; if (!CTrueTalkManager::_currentNPC->find("Chicken", &chicken, FIND_PET)) id = 280142; } addResponse(getDialogueId(id)); if (id == 280192) addResponse(getDialogueId(280222)); applyResponse(); } } if (id >= 80000 && id <= 80244) { if ((id == 80155 || id == 80156) && CTrueTalkManager::_currentNPC) { CGameObject *chicken; if (!CTrueTalkManager::_currentNPC->find("Chicken", &chicken, FIND_PET)) id = 80151; } addResponse(id); if (id == 80201) addResponse(getDialogueId(280222)); applyResponse(); } return (id == 3) ? SCR_2 : SCR_1; }
void Worker::pushToInBuf(int fd, std::string str) { assert(!str.empty()); assert(clients.find(fd) != clients.end()); clients[fd]->inBuf.append(str); int pos = clients[fd]->inBuf.find('\n'); /* Compose response */ while (pos != std::string::npos) { std::string query = clients[fd]->inBuf.substr(0, pos); clients[fd]->inBuf = clients[fd]->inBuf.substr(pos + 1); addResponse(fd, composeResponse(query)); pos = clients[fd]->inBuf.find('\n'); } }
void FileServer::loopWriteFile() { _writeRunning = true; while(!_writeEndThread) { _recvBufListMutex.lock(); size_t recvSize = _recvBufList.size(); _recvBufListMutex.unlock(); if(0 == recvSize) { usleep(500); continue; } _recvBufListMutex.lock(); RecvBufStruct recvDataBuf = _recvBufList.front(); _recvBufList.pop_front(); _recvBufListMutex.unlock(); std::string filename = recvDataBuf.fileProto.file_name(); std::string fullfilename = _writePath; fullfilename += filename; _fileNameMutex.lock(); _strFileName = filename; _fileNameMutex.unlock(); //cocos2d::log("WriteFile:: fullfilename = %s",filename.c_str()); createDir(fullfilename.substr(0, fullfilename.find_last_of("/")).c_str()); FILE *fp= nullptr; if (1 == recvDataBuf.fileProto.package_seq()) { _writeErrorFile =""; fp = fopen(fullfilename.c_str(), "wb"); } else { if (_writeErrorFile == filename) { continue; } fp=fopen(fullfilename.c_str(), "ab"); } if (nullptr == fp) { addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_FOPEN_ERROR, errno); continue; } if (fp) { if (recvDataBuf.contentBuf.size() > 0 && 0 == fwrite(recvDataBuf.contentBuf.c_str(), sizeof(char), recvDataBuf.contentBuf.size(), fp)) { addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_FWRITE_ERROR, errno); fclose(fp); continue; } fclose(fp); } if (1 == recvDataBuf.fileProto.package_seq()) { //record new file modify addResFileInfo(filename.c_str(), recvDataBuf.fileProto.modified_time()); addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_SUCCESS, 0); } } _writeRunning = false; }
void FileServer::loopReceiveFile() { struct sockaddr client; socklen_t client_len; /* new client */ client_len = sizeof(client); int fd = accept(_listenfd, (struct sockaddr *)&client, &client_len ); char *protoBuf = new char[MAXPROTOLENGTH]; while(!_receiveEndThread) { // recv start flag char startflag[13] = {0}; recvBuf(fd, startflag, sizeof(startflag) - 1); if (strcmp(startflag, PROTO_START) != 0) { continue; } // recv proto num union { char char_type[3]; unsigned short uint16_type; }protonum; recvBuf(fd, protonum.char_type, sizeof(protonum.char_type) - 1); //recv protobuf length union { char char_type[3]; unsigned short uint16_type; }protolength; recvBuf(fd, protolength.char_type, sizeof(protolength.char_type) - 1); //recv variable length memset(protoBuf, 0, MAXPROTOLENGTH); recvBuf(fd, protoBuf, protolength.uint16_type); RecvBufStruct recvDataBuf; recvDataBuf.fd = fd; recvDataBuf.fileProto.ParseFromString(protoBuf); if (1 == recvDataBuf.fileProto.package_seq()) { _recvErrorFile = ""; } else { // recv error if (_recvErrorFile == recvDataBuf.fileProto.file_name()) { continue; } } unsigned long contentSize = recvDataBuf.fileProto.content_size(); if (contentSize == 0) { recvDataBuf.contentBuf=""; _recvBufListMutex.lock(); _recvBufList.push_back(recvDataBuf); _recvBufListMutex.unlock(); }else if(contentSize > 0) { //recv body data Bytef *contentbuf = new Bytef[contentSize+1]; memset(contentbuf, 0, contentSize+1); unsigned long recvTotalLen = contentSize; while (recvTotalLen != 0){ unsigned long recvLen = MAXPROTOLENGTH; if(recvTotalLen < MAXPROTOLENGTH) recvLen = recvTotalLen; memset(protoBuf, 0, MAXPROTOLENGTH); unsigned long result = recv(fd, protoBuf, recvLen,0); if (result <= 0) { usleep(1); continue; } memcpy(contentbuf + contentSize - recvTotalLen, protoBuf, result); recvTotalLen -= result; } if (recvDataBuf.fileProto.compress_type() == runtime::FileSendProtos_CompressType::FileSendProtos_CompressType_ZIP){ unsigned long uncompressSize = recvDataBuf.fileProto.uncompress_size(); Bytef *buff = new Bytef[uncompressSize * sizeof(Bytef)]; memset(buff, 0, uncompressSize * sizeof(Bytef)); int err = ::uncompress(buff, &uncompressSize,contentbuf, contentSize * sizeof(Bytef)); if (err != Z_OK){ CC_SAFE_DELETE_ARRAY(buff); CC_SAFE_DELETE_ARRAY(contentbuf); addResponse(recvDataBuf.fd, recvDataBuf.fileProto.file_name(), runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_UNCOMPRESS_ERROR, err); continue; } CC_SAFE_DELETE_ARRAY(contentbuf); contentbuf = buff; contentSize = uncompressSize; } recvDataBuf.contentBuf.assign((const char*)contentbuf, contentSize); CC_SAFE_DELETE_ARRAY(contentbuf); _recvBufListMutex.lock(); _recvBufList.push_back(recvDataBuf); _recvBufListMutex.unlock(); } } _receiveRunning = false; CC_SAFE_DELETE_ARRAY(protoBuf); }
int SuccUBusScript::process(const TTroomScript *roomScript, const TTsentence *sentence) { if (!CTrueTalkManager::getStateValue(1)) return 2; if (roomScript && roomScript->_scriptId == 101) _isRoom101 = true; int currState = getState(); if (currState) { int currMode = sentence->_field2C; bool modeFlag1 = currMode == 11 || currMode == 13; bool modeFlag2 = currMode == 12; setState(0); switch (currState) { case 1: if (currMode == 3 || currMode == 10) return setResponse(70050, 0); break; case 2: if (modeFlag1 || modeFlag2) return setResponse(70070 + (getRandomBit() ? 254 : 0), 0); break; case 3: if (currMode == 3 || currMode == 10) return setResponse(70074, 0); break; case 4: if (currMode == 4) return setResponse(70077, 0); if (currMode == 3) return setResponse(getDialogueId(230117), 0); break; case 5: if (currMode == 3 || currMode == 10) return setResponse(70089, 0); break; case 6: if (modeFlag1) return setResponse(70103, 0); if (modeFlag2) return setResponse(70102, 0); break; case 7: if (modeFlag1) return setResponse(getDialogueId(230157), 0); break; case 8: if (modeFlag1) return setResponse(getDialogueId(230159), 0); break; case 9: if (modeFlag1) return setResponse(getDialogueId(230160), 0); break; case 10: if (modeFlag1) return setResponse(getDialogueId(230161), 0); break; case 11: if (modeFlag1) return setResponse(getDialogueId(230142), 0); break; case 12: return setResponse(70030, 0); default: break; } } if (processEntries(&_entries, _entryCount, roomScript, sentence) != 2) { uint tagId = g_vm->_trueTalkManager->_quotes.find(sentence->_normalizedLine.c_str()); if (tagId && chooseResponse(roomScript, sentence, tagId) != 2) { addResponse(getDialogueId(230030)); applyResponse(); } } return 2; }
void ControlDevice::controlDeviceThreadWorker() { if (remote) return; clearMessageQueue(); unsigned int WDRESETTIMER = 0; bool USBRESET = false; bool RESCANAFTEREXCEPTION = false; this->addMsgToDataExchangeLog(Valter::format_string("%s worker started...", this->getControlDeviceId().c_str())); string request; string response; bool isWDReset = false; wdTimerNotResetCnt = 0; string prevReading = ""; int filter = 0; if (Valter::getInstance()->getGlobalSetting("wdrIntent").compare("true") == 0) { this->getControlDevicePort()->write("WDINTENTIONALRESETON"); intentionalWDTimerResetOnAT91SAM7s = true; setResetWDTimer(true); } while (this->getStatus() == ControlDevice::StatusActive) { try { if (WDRESETTIMER == wdResetTime - 500) { isWDReset = false; if (resetWDTimer) { this->getControlDevicePort()->write("WDRESET"); this->addMsgToDataExchangeLog(Valter::format_string("%s ← WDRESET", this->getControlDeviceId().c_str())); } } //process outgoing if (requestsAwainting() > 0) { request = pullRequest(); if (request.length() == 0) { this_thread::sleep_for(std::chrono::milliseconds(1)); continue; } if (request.compare("WDINTENTIONALRESETON") == 0) { intentionalWDTimerResetOnAT91SAM7s = true; } if (request.compare("WDINTENTIONALRESETOFF") == 0) { intentionalWDTimerResetOnAT91SAM7s = false; } if (wdTimerNotResetCnt == 0) { this->getControlDevicePort()->write(request.c_str()); if (Valter::getInstance()->getLogControlDeviceMessages()) { this->addMsgToDataExchangeLog(Valter::format_string("%s ← %s", this->getControlDeviceId().c_str(), request.c_str())); } } } //process incoming if (this->getControlDevicePort()->available() > 0) { response = this->getControlDevicePort()->readline(); sanitizeConrtolDeviceResponse(response); if ((filter > 500) || (response.find_first_of(":") == string::npos)) { addResponse(response); filter = 0; } else { int value_str_pos = response.find_first_of(":"); if (response.substr(0, value_str_pos).compare(prevReading) != 0 || filter > 100) { prevReading = response.substr(0, value_str_pos); addResponse(response); filter = 0; } else { //qDebug("%s filtered", response.c_str()); } } if (Valter::getInstance()->getLogControlDeviceMessages()) { this->addMsgToDataExchangeLog(Valter::format_string("%s → %s", this->getControlDeviceId().c_str(), response.c_str())); } if (response.compare("WDRST") == 0) { isWDReset = true; WDRESETTIMER = 0; wdTimerNotResetCnt = 0; } } this_thread::sleep_for(std::chrono::milliseconds(1)); filter++; WDRESETTIMER++; if (WDRESETTIMER > wdResetTime) { if (!isWDReset) { if (resetWDTimer) { this->getControlDevicePort()->write("WDRESET"); this->addMsgToDataExchangeLog(Valter::format_string("%s ← WDRESET", this->getControlDeviceId().c_str())); } wdTimerNotResetCnt++; this->addMsgToDataExchangeLog(Valter::format_string("WD was not Reset on %s Control Device [attempt #%d]", this->getControlDeviceId().c_str(), wdTimerNotResetCnt)); if (wdTimerNotResetCnt > 3) { if (intentionalWDTimerResetOnAT91SAM7s) { USBRESET = true; break; } else { this->addMsgToDataExchangeLog(Valter::format_string("(EMULATED Reset %s system device of [%s] Control Device)", this->getSysDevicePath().c_str(), this->getControlDeviceId().c_str())); } } } WDRESETTIMER = 0; } } catch (const exception &ex) { this->addMsgToDataExchangeLog(Valter::format_string("Exception [%s] fired in the %s worker", ex.what(), this->getControlDeviceId().c_str())); RESCANAFTEREXCEPTION = true; break; } } wdTimerNotResetCnt = 0; this->addMsgToDataExchangeLog(Valter::format_string("%s worker stopped!", this->getControlDeviceId().c_str())); if (USBRESET) { setStatus(ControlDevice::StatusReady); intentionalWDTimerResetOnAT91SAM7s = false; rescanningAfterPossibleReset = true; this_thread::sleep_for(std::chrono::seconds(5)); this->setRescanNum(0); resetUSBSysDevice(); } if (RESCANAFTEREXCEPTION) { setStatus(ControlDevice::StatusReady); intentionalWDTimerResetOnAT91SAM7s = false; rescanningAfterPossibleReset = true; this_thread::sleep_for(std::chrono::seconds(5)); this->setRescanNum(0); reScanThisControlDevice(); } }