AstActive* getActive(FileLine* fl, AstSenTree* sensesp) { // Return a sentree in this scope that matches given sense list. // Not the fastest, but scopes tend to have few clocks AstActive* activep = NULL; //sitemsp->dumpTree(cout," Lookingfor: "); for (vector<AstActive*>::iterator it = m_activeVec.begin(); it!=m_activeVec.end(); ++it) { activep = *it; if (activep) { // Not deleted // Compare the list AstSenTree* asenp = activep->sensesp(); if (asenp->sameTree(sensesp)) { UINFO(8," Found ACTIVE "<<activep<<endl); goto found; } } activep = NULL; } found: // Not found, form a new one if (!activep) { AstSenTree* newsenp = sensesp->cloneTree(false); activep = new AstActive(fl, "sequent", newsenp); activep->sensesStorep(activep->sensesp()); UINFO(8," New ACTIVE "<<activep<<endl); // Form the sensitivity list addActive(activep); m_activeVec.push_back(activep); // Note actives may have also been added above in the Active visitor } return activep; }
void test_spec4() { int n, m; scanf("%d %d", &n, &m); Medicine *medi = (Medicine *) malloc(sizeof(Medicine) * n); int cmd, mid, weight; char name[128]; for (int i = 0; i < n; i++) init(&medi[i]); for (int i = 0; i < m; i++) { scanf("%d %d", &cmd, &mid); if (cmd == 1) { // addActive scanf("%s %d", name, &weight); int af = addActive(&medi[mid], name, weight); printf("af %d\n", af); } else if (cmd == 2) { // addInactive scanf("%s %d", name, &weight); int bf = addInactive(&medi[mid], name, weight); printf("bf %d\n", bf); } else if (cmd == 3) { // print print(&medi[mid]); } else if (cmd == 4) { // totalWeight printf("weight = %d\n", totalWeight(&medi[mid])); } else if (cmd == 5) { // maxIngredient char *ret = maxIngredient(&medi[mid]); printf("main ingredient = %s\n", ret == NULL ? "NOT FOUND" : ret); } } }
/* void init(Medicine *medicine); int addActive(Medicine *medicine, char *name, int weight); int addInactive(Medicine *medicine, char *name, int weight); void print(Medicine *medicine); int totalWeight(Medicine *medicine); char *maxIngredient(Medicine *medicine); */ void test_spec0() { int n, m; scanf("%d %d", &n, &m); Medicine *medi = (Medicine *) malloc(sizeof(Medicine) * n); int cmd, mid, weight; char name[128]; for (int i = 0; i < n; i++) init(&medi[i]); for (int i = 0; i < m; i++) { scanf("%d %d", &cmd, &mid); if (cmd == 1) { // addActive scanf("%s %d", name, &weight); int af = addActive(&medi[mid], name, weight); printf("af %d\n", af); } else if (cmd == 2) { // addInactive assert(false); } else if (cmd == 3) { // print assert(false); } else if (cmd == 4) { // totalWeight printf("weight = %d\n", totalWeight(&medi[mid])); } else if (cmd == 5) { // maxIngredient assert(false); } } }
AstActive* getCActive(FileLine* fl) { if (!m_cActivep) { m_cActivep = new AstActive(fl, "combo", new AstSenTree(fl, new AstSenItem(fl,AstSenItem::Combo()))); m_cActivep->sensesStorep(m_cActivep->sensesp()); addActive(m_cActivep); } return m_cActivep; }
AstActive* getIActive(FileLine* fl) { if (!m_iActivep) { m_iActivep = new AstActive(fl, "initial", new AstSenTree(fl, new AstSenItem(fl,AstSenItem::Initial()))); m_iActivep->sensesStorep(m_iActivep->sensesp()); addActive(m_iActivep); } return m_iActivep; }
void SignalSubscriber::call(const GenericFunctionParameters& args, MetaCallType callType) { // this is held alive by caller if (handler) { bool async = true; if (threadingModel != MetaCallType_Auto) async = (threadingModel == MetaCallType_Queued); else if (callType != MetaCallType_Auto) async = (callType == MetaCallType_Queued); qiLogDebug() << "subscriber call async=" << async <<" ct " << callType <<" tm " << threadingModel; if (async) { GenericFunctionParameters* copy = new GenericFunctionParameters(args.copy()); // We will check enabled when we will be scheduled in the target // thread, and we hold this SignalSubscriber alive, so no need to // explicitly track the asynccall // courtesy-check of el, but it should be kept alive longuer than us qi::EventLoop* el = getEventLoop(); if (!el) // this is an assert basicaly, no sense trying to do something clever. throw std::runtime_error("Event loop was destroyed"); el->post(FunctorCall(copy, new SignalSubscriberPtr(shared_from_this()))); } else { // verify-enabled-then-register-active op must be locked { boost::mutex::scoped_lock sl(mutex); if (!enabled) return; addActive(false); } //do not throw bool mustDisconnect = false; try { handler(args); } catch(const qi::PointerLockException&) { qiLogDebug() << "PointerLockFailure excepton, will disconnect"; mustDisconnect = true; } catch(const std::exception& e) { qiLogWarning() << "Exception caught from signal subscriber: " << e.what(); } catch (...) { qiLogWarning() << "Unknown exception caught from signal subscriber"; } removeActive(true); if (mustDisconnect) source->disconnect(linkId); } } else if (target) { AnyObject lockedTarget = target->lock(); if (!lockedTarget) { source->disconnect(linkId); } else // no need to keep anything locked, whatever happens this is not used lockedTarget.metaPost(method, args); } }
/** * @brief Parses Telnet Options negotiations * @param c * @return */ unsigned char TelnetManager::telnetOptionParse(unsigned char c) { // TEL-OPT Parser switch(m_teloptStage) { // Find IAC case 0: // Check first Char in Fresh Sequence if(c != IAC) { // Add character to output buffer. return c; } else { m_teloptStage++; } break; // Find Command case 1: if(c == IAC) { // If binary, then we expect double IAC for actual characer if (m_is_active_bin) { std::cout << "\r\n Got double IAC w/ Binary!!\r\n" << std::endl; m_teloptStage = 0; return IAC; } else { // Restart on next character // Double IAC = IAC character, not valid so skip it for now. // Telnet states binary mode, double IAC mean pass through // Char 255. But this doesn't equal any text in ExtASCII // So we going to stuff it. std::cout << "\r\n Got double IAC!!\r\n" << std::endl; break; } } if(c != IAC) { switch(c) { // Catch Pass-through commands. case GA: // 249 /* you may reverse the line */ case EL: // 248 /* erase the current line */ case EC: // 247 /* erase the current character */ case AYT: // 246 /* are you there */ case AO: // 245 /* abort output--but let prog finish */ case IP: // 244 /* interrupt process--permanently */ case BREAK: // 243 /* break */ case DM: // 242 /* data mark--for connect. cleaning */ case NOP: // 241 /* nop */ case SE: // 240 /* end sub negotiation */ case EOR: // 239 /* end of record (transparent mode) */ case ABORT: // 238 /* Abort process */ case SUSP: // 237 /* Suspend process */ case xEOF: // 236 /* End of file: EOF is already used... */ // Pass Through commands that don't need Response. std::cout << "[IAC] [" << (int)c << "] PASS-THROUGH" << std::endl; m_teloptStage = 0; break; default: // Move to Command Parsing. m_teloptCommand = c; m_teloptStage++; break; } } break; // Find Option case 2: m_teloptStage = 0; // Catch if were getting Invalid Commands. if(TELCMD_OK(m_teloptCommand)) std::cout << "[IAC] [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; else { // Hopefully won't get here! std::cout << "[IAC] [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; m_teloptStage = 0; break; } switch(m_teloptCommand) { // No responses needed, just stuff these. case DONT: std::cout << "[IAC] RECEIVED DONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; switch(c) { case IAC : m_teloptStage = 1; break; default: // Only Response Once, if we've already received this, then ignore it a second time! if (!checkActive(m_teloptCommand)) { std::cout << "[IAC] DONT -> WONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); m_teloptStage = 0; // Only Response once to Don't! addActive(m_teloptCommand); } break; } break; case DO: // Replies WILL / WON'T std::cout << "[IAC] RECEIVED DO [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; switch(c) { case TELOPT_ECHO: if (!m_isECHOCompleted) { std::cout << "[IAC] DO TELOPT_ECHO [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionDeny(m_teloptCommand),c); m_is_active_echo = false; m_isECHOCompleted = false; } break; case TELOPT_BINARY: if (!m_isBINCompleted) { std::cout << "[IAC] DO TELOPT_BINARY [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); m_isBINCompleted = true; m_is_active_bin = true; } break; case TELOPT_SGA: if (!m_isSGACompleted) { std::cout << "[IAC] DO TELOPT_SGA [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); m_isSGACompleted = true; m_is_active_sga = true; } break; case TELOPT_TTYPE: std::cout << "[IAC] DO TELOPT_TTYPE [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); m_is_active_ttype = true; break; case TELOPT_NAWS: std::cout << "[IAC] DO TELOPT_NAWS [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); telnetOptionNawsReply(); m_is_active_naws = true; break; /* case IAC : printf("\r\n [DO - INCORRECT IAC Received, resetting.] \r\n"); stage = 1; return 255; */ default: // Only Response Once, if we've already received this, then ignore it a second time! if (!checkActive(m_teloptCommand)) { std::cout << "[IAC] DO -> WONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionDeny(m_teloptCommand),c); addActive(m_teloptCommand); } break; } m_teloptStage = 0; break; // WILL means the Server Will DO IT! // We reply Fine, do it! case WILL: // Replies DO And DONT std::cout << "[IAC] RECEIVED WILL [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; // Don't response to WILL Requests. switch(c) { case TELOPT_ECHO: if(!m_isECHOCompleted) { std::cout << "[IAC] WILL TELOPT_ECHO [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionDeny(m_teloptCommand),c); m_is_active_echo = false; m_isECHOCompleted = true; } break; case TELOPT_BINARY : if(!m_isBINCompleted) { std::cout << "[IAC] WILL TELOPT_BINARY [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); m_is_active_bin = true; m_isBINCompleted = true; } break; case TELOPT_SGA : if(!m_isSGACompleted) { std::cout << "[IAC] WILL TELOPT_SGA [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); m_is_active_sga = true; m_isSGACompleted = true; } break; /* case IAC : stage = 1; return 255; */ default : // Only Response Once, if we've already received this, then ignore it a second time! if (!checkActive(m_teloptCommand)) { std::cout << "[IAC] WILL -> DONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; telnetSendIAC(telnetOptionDeny(m_teloptCommand),c); addActive(m_teloptCommand); } break; } m_teloptStage = 0; break; case WONT: // Don't respond to WONT std::cout << "[IAC] RECEIVED WONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; //telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c); //printf("\r\n [WONT - responded DONT %i] \r\n",c); m_teloptStage = 0; break; // Start of Sub Negotiations and Stages 3 - 4 case SB: // 250 std::cout << "[IAC] TELNET_STATE_SB [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; if(c == TELOPT_TTYPE) { m_currentOption = c; m_teloptStage = 3; } else { std::cout << "[IAC] TELNET_STATE_SB INVALID [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; // Invalid, reset back. m_teloptStage = 0; } break; default: // Options or Commands Not Parsed, RESET. std::cout << "[IAC] INVALID [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl; m_teloptStage = 0; break; } break; case 3: std::cout << "--> STAGE 3 [" << (int)c << "]" << std::endl; //Options will be 1 After SB //IAC SB TTYPE SEND IAC SE switch(m_currentOption) { case TELOPT_TTYPE: if(c == TELQUAL_SEND) // SEND { std::cout << "[IAC] TELQUAL_SEND [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl; m_teloptStage = 4; } else m_teloptStage = 0; break; default: //printf("\r\n [Stage 3 - unregistered stuff it] - %i, %i \r\n",opt, c); if(c == SE) { std::cout << "[IAC] SB END [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl; m_teloptStage = 0; } else { // reset m_teloptStage = 0; } break; } break; // Only Gets here on TTYPE Sub-Negotiation. case 4: std::cout << "--> STAGE 4 [" << (int)c << "]" << std::endl; switch(c) { case IAC: std::cout << "[IAC] TTYPE TELNET_STATE_SB IAC [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl; break; case SE: std::cout << "[IAC] TTYPE TELNET_STATE_SB SE [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl; // Send TTYPE After End of Complete Sequence is Registered. telnetOptionTerminalTypeReply(); m_teloptStage = 0; break; } break; } //std::cout << "[IAC] null return [" << (int)c << "] " << std::endl; return '\0'; }