/** * Execute the queued commands **/ void CmdManager::exec(GameState* st) { Cmd* c; while (work.trypop(c)) { c->exec(st); } }
bool CmdMessenger::send(const Cmd &command, bool ack, int ack_id, int simple_timeout) { std::stringstream ss; //stream to hold the command to be sent. if(!command.getNumArgs()) ss << command.getId() << cmd_separator_; // if there are no arguments to be sent, the stream receives only the command id and the command separator. else { //otherwise, if there are some arguments, we will have to iterate over the std::vector args_ ss << command.getId() << field_separator_; for(unsigned long i=0; i<command.args_.size() - 1; i++) { //Note that we go from i = 0, to i = args_.size() - 1, because after the last argument a cmd_separator must be added. ss << command.args_[i] << field_separator_; //adds an argument and the field separator to the stream. } ss << command.args_.back() << cmd_separator_; //finishes the command. Now it is ready to be sent. } // else std::cout << "Comando: " << ss.str() << std::endl; serial_port_.write(ss.str()); //sends the command! if(ack) { // if ack is required return CmdMessenger::waitCmd(ack_id, simple_timeout); //Verifies if the command arrived or not and returns true if received (false otherwise). } return true; //if ack is not required, the function returns true anyway. }
void VectorCommand::VectorCompletionHandler( IOReturn result, void* quads[], UInt32 numQuads ) { //printf( "VectorCommand::VectorCompletionHandler - status = 0x%08lx\n", result ); CFIndex count = CFArrayGetCount( mCommandArray ); for( CFIndex index = 0; (index < count); index++ ) { Cmd * cmd = (Cmd*)mResultBuffer[index].refCon; IOReturn status = mResultBuffer[index].result; // pack 'em up like the kernel would void * args[3]; args[0] = (void*)mResultBuffer[index].bytesTransferred; args[1] = (void*)mResultBuffer[index].ackCode; args[2] = (void*)mResultBuffer[index].responseCode; // call the completion routine cmd->CommandCompletionHandler( cmd, status, args, 3 ); if( mInflightCount > 0 ) { mInflightCount--; } } (*mCallback)( mRefCon, mStatus ); }
// --------------------------------------------------------------------- string smopen() { string c=cmd.getid(); string p=cmd.getparms(); if (c=="edit") term->vieweditor(); if (c=="edit2") { if (note==0) return smerror("no edit window open"); note->on_winotherAct_triggered(); } if (c=="edit" || c=="edit2") return ""; if (c!="tab") { return smerror("unrecognized sm command: open " + c); } term->vieweditor(); if (p.empty()) note->newtemp(); else { QString f=s2q(smgetscript(p)); if (!cfexist(f)) return smerror("file not found: " + q2s(f)); note->fileopen(f); } rc=-1; return i2s(note->editIndex()); }
Cmd* Parser::parse_term() { Cmd* cmd = tokenizer.read(); switch (cmd->token_type) { case TOKEN_IDENTIFIER: return cmd; case TOKEN_LITERAL_INTEGER: case TOKEN_LITERAL_REAL: case TOKEN_LITERAL_STRING: return cmd; case TOKEN_OPEN_PAREN: { Cmd* result = parse_expression(); must_consume( TOKEN_CLOSE_PAREN, "')'" ); return result; } default: break; } cmd->error( "Unexpected '" ); cmd->print( vm->error_message ); vm->error_message.print( "'." ); BOSS_THROW( vm, COMPILE_ERROR ); }
int _tmain(int argc, _TCHAR* argv[]) { //检测内存泄漏 #if defined(WIN32) && defined(_DEBUG) int tmpDbgFlag; tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF; tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag(tmpDbgFlag); //设置内存泄漏跟踪标志 #endif //WIN32 & _DEBUG // CClockManager* clockMgr = INSTANCE(CClockManager); pNet = new TcpClient(); pNet->AddListener(new NetListener()); HANDLE ht = fge::CThread::StartThread( &MainLoop,0 ); CCmdInterface ui; Cmd cmd; cmd.Reg(ui); ui.SetName(">"); ui.Run(argc,argv); ::WaitForSingleObject(ht,10000); pNet->Close(); SAFE_DELETE(pNet); return 0; }
void PaletteEditor::colourChanged() { QColor qc = m_RGBWidget->colour(); Colour c( qc.red(), qc.green(), qc.blue(), qc.alpha() ); Colour existing( m_Proj.GetColour( m_Selected ) ); if (c==existing) { return; } // if the last cmd was a modification of the same colour, we'll just amend it! // this lets us keep the project up-to-date as user twiddles the colour, // but also avoids clogging up the undo stack with insane numbers of operations. Cmd* cmd = m_Ed.TopCmd(); if (cmd) { Cmd_PaletteModify* mod = cmd->ToPaletteModify(); if (mod) { if (mod->Merge(m_Selected, c)) { return; } } } // if we get this far we need a fresh cmd. cmd = new Cmd_PaletteModify(m_Proj, m_Selected, 1, &c); m_Ed.AddCmd(cmd); }
/** * Stop the daemon **/ CmdManager::~CmdManager() { Cmd* c; while (work.trypop(c)) { c->cancel(); } MHD_stop_daemon(daemon); }
bool Tree::Action() { Cmd* cmd = Cmd::Instance(); Display display; display.Tree(cmd->GetFs()->GetRoot(), 0, "| "); return true; }
void CmdManager::registerCommand(const Cmd& cmd) { QMap<QString, Cmd>::iterator it = m_CommandMap.find(cmd.name()); if (it == m_CommandMap.end()) { m_Commands.push_back(cmd); m_CommandMap.insert(cmd.name(), cmd); } }
void *threadFn(void *arg){ int fd; struct pollfd fds; char buf[BUFSIZE]; char fifo[BUFSIZE]; struct stat st; void **arr = (void**)arg; char *target = (char*)arr[0]; int polr = 0; bzero(fifo, BUFSIZE); strcpy(fifo, target); Cmd *cmd = (Cmd*)arr[1]; fd = open(fifo, O_RDONLY); if (!fd) { fprintf(stderr, "Can not open %s\n", fifo); return NULL; } fds.fd = fd; fds.events = POLLIN; polr = poll(&fds, 1, 1000); if (polr < 0) { fprintf(stderr, "Poll error for %s\n", fifo); return NULL; } if (!polr) { fprintf(stderr, "Can not read from %s by timeout\n", fifo); if (!stat(fifo, &st)) unlink(fifo); return NULL; } FILE *fp = fdopen(fd, "r"); if (!fp) { fprintf(stderr, "Can not read %s\n", fifo); return NULL; } fgets(buf, BUFSIZE, fp); fclose(fp); if (buf[BUFSIZE - 1]) { fprintf(stderr, "Recieved string is too long, exit\n"); return NULL; } cmd->exec(buf, BUFSIZE); fp = fopen(fifo, "w"); if (!fp) { fprintf(stderr, "Can not write to %s\n", fifo); return NULL; } fwrite(buf, 1, strlen(buf), fp); fclose(fp); sleep(1); if (!stat(fifo, &st)) unlink(fifo); return NULL; }
bool RemoveDir::Action() { if (!GetParams()->empty()) { Cmd* cmd = Cmd::Instance(); cmd->GetFs()->RemoveDir(cmd->GetWorkPath(&GetParams()->front())); } return true; }
bool DelTree::Action() { if (!GetParams()->empty()) { Cmd* cmd = Cmd::Instance(); cmd->GetFs()->DelTree(cmd->GetWorkPath(&GetParams()->front())); } return true; }
/** * Handle incoming HTTP requests **/ int CmdManager::AccessHandlerCallback( struct MHD_Connection* connection, const char* url, const char* method, const char* upload_data, size_t* upload_data_size, void** ptr ) { static int dummy; struct MHD_Response* response; int ret; if (0 != strcmp(method, "GET")) { return MHD_NO; /* unexpected method */ } // The first time only the headers are valid, so store data but don't respond if (&dummy != *ptr) { *ptr = &dummy; return MHD_YES; } if (0 != *upload_data_size) { return MHD_NO; /* upload data in a GET!? */ } // clear context pointer *ptr = NULL; Cmd* c = this->dispatch(connection, url, method); if (c == NULL) { return MHD_NO; } work.push(c); std::string resp = c->waitDone(); delete c; response = MHD_create_response_from_buffer( resp.length(), (void*) resp.c_str(), MHD_RESPMEM_MUST_COPY ); // Allows other origins to access data via AJAX MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); ret = MHD_queue_response( connection, MHD_HTTP_OK, response ); MHD_destroy_response(response); return ret; }
Cmd* Parser::parse_statement() { Cmd* result = parse_expression(); while (tokenizer.has_another() && !tokenizer.next_is_end_token()) { result->add_arg( parse_expression() ); } result = new(result) CmdEvaluateAndDiscard( result ); return result; }
bool MakeFile::Action() { if (!GetParams()->empty()) { Cmd* cmd = Cmd::Instance(); cmd->GetFs()->MakeFile(cmd->GetWorkPath(&GetParams()->front()), &GetParams()->front()); } return true; }
void PaletteEntryEditor::updateCurrentSpritePalette(const char* operationName) { if (UIContext::instance()->activeDocument() && UIContext::instance()->activeDocument()->sprite()) { try { ContextWriter writer(UIContext::instance()); Document* document(writer.document()); Sprite* sprite(writer.sprite()); Palette* newPalette = get_current_palette(); // System current pal frame_t frame = writer.frame(); Palette* currentSpritePalette = sprite->palette(frame); // Sprite current pal int from, to; // Check differences between current sprite palette and current system palette from = to = -1; currentSpritePalette->countDiff(newPalette, &from, &to); if (from >= 0 && to >= from) { DocumentUndo* undo = document->undoHistory(); Cmd* cmd = new cmd::SetPalette(sprite, frame, newPalette); // Add undo information to save the range of pal entries that will be modified. if (m_implantChange && undo->lastExecutedCmd() && undo->lastExecutedCmd()->label() == operationName) { // Implant the cmd in the last CmdSequence if it's // related about color palette modifications ASSERT(dynamic_cast<CmdSequence*>(undo->lastExecutedCmd())); static_cast<CmdSequence*>(undo->lastExecutedCmd())->add(cmd); cmd->execute(UIContext::instance()); } else { Transaction transaction(writer.context(), operationName, ModifyDocument); transaction.execute(cmd); transaction.commit(); } } } catch (base::Exception& e) { Console::showException(e); } } PaletteView* palette_editor = ColorBar::instance()->getPaletteView(); palette_editor->invalidate(); if (!m_redrawTimer.isRunning()) m_redrawTimer.start(); m_redrawAll = false; m_implantChange = true; }
// --------------------------------------------------------------------- string smset() { string p=cmd.getid(); if (p.empty()) return smerror("sm set parameters not given"); string c=cmd.getid(); if (c.empty()) return smerror("sm set " + p + " parameters not given"); string q=cmd.getparms(); if (p=="inputlog") return smsetinputlog(c,q); if (p=="term") { if (c=="text") return smsettext(p,q); if (c=="xywh") return smsetxywh(p,q); return smerror("command applies only to an edit window: " + c); } Bedit *e; if (p=="edit") { if (note==0) return smerror("No active edit window"); e=(Bedit *)note->editPage(); } else if (p=="edit2") { if (note2==0) return smerror("No active edit2 window"); e=(Bedit *)note2->editPage(); } else return smerror("unrecognized sm command: set " + p); if (e==0 && (c=="scroll" || c=="select" || c=="text")) return smerror("no edit window for sm command: set " + c); if (c=="scroll") return smsetscroll(e,q); if (c=="select") return smsetselect(e,q); if (c=="text") return smsettext(p,q); if (c=="xywh") return smsetxywh(p,q); return smerror("unrecognized sm command: set " + p + " " + q); }
bool MakeDir::Action() { if (!GetParams()->empty()) { Cmd* cmd = Cmd::Instance(); Fs::Path path = GetParams()->at(0); path.SetFile(path.GetPath()->back()); path.GetPath()->pop_back(); cmd->GetFs()->MakeDir(cmd->GetWorkPath(&path), &path); } return true; };
void Phase::StartCmd(CmdPtr pCmd, CommitSession& session) { Colour c = pCmd->GetColour(); GetCmdStack(c).StartCmd(std::move(pCmd)); Cmd* pStartedCmd = GetCurrentCmd(c); if (pStartedCmd->IsAutoProcess()) { const Player& player = Players::Get(pStartedCmd->GetTeam(session.GetGame()).GetPlayerID()); ProcessCmdMessage(Input::CmdMessage(), session, player); } else pStartedCmd->UpdateClient(session.GetController(), GetGame()); }
// --------------------------------------------------------------------- string smfocus() { string p=cmd.getparms(); if (p.empty()) return smerror("sm focus needs additional parameters"); if (p=="term") term->smact(); else if (p=="edit") { if (note==0) return smerror("No active edit window"); if (note->editIndex()==-1) return ""; note->activateWindow(); note->raise(); note->repaint(); } else if (p=="edit2") { if (note2==0 || note2->editIndex()==-1) return smerror("No active edit2 window"); setnote(note2); note->activateWindow(); note->raise(); note->repaint(); } else return smerror("unrecognized sm command: focus " + p); return ""; }
bool TLVExecutor::execute(Cmd& cmd) { if(cmd.size() < 2) { log("Error cmd with no params:%s", printCmd(cmd)); return false; } const char* key = cmd[0].c_str(); if(0 == strcasecmp("settag", key)) { CheckParam(2); return TLVInterface::setTag(atoi(getp(1)),getp(2)); } else if(0 == strcasecmp("gettag", key)) { CheckParam(2); return TLVInterface::getTag(atoi(getp(1)),getp(2)); } else if(0 == strcasecmp("addtag", key)) { CheckParam(2); return TLVInterface::addTag(atoi(getp(1)),getp(2)); } return false; }
// --------------------------------------------------------------------- string smreplace() { string c=cmd.getid(); string p=cmd.getparms(); if (note==0 || note->editIndex()<0) return smerror ("No active edit window"); if (c!="edit") return smerror("unrecognized sm command: replace " + c); if (p.empty()) return smerror("replace needs 2 parameters: edit filename"); QString f=s2q(smgetscript(p)); if (!cfexist(f)) return smerror("file not found: " + q2s(f)); note->filereplace(f); return""; }
// --------------------------------------------------------------------- string sm(string c) { rc=0; if (c=="act") return smact(); if (c=="active") return smactive(); if (c=="close") return smclose(); if (c=="focus") return smfocus(); if (c=="font") return smfont(); if (c=="get") return smget(); if (c=="html") return smhtml(); if (c=="new") return smopen(); if (c=="open") return smopen(); if (c=="profont") return smprofont(); if (c=="replace") return smreplace(); if (c=="save") return smsave(); if (c=="set") return smset(); else if (c=="prompt") return smprompt(); cmd.getparms(); return smerror("unrecognized sm command: " + c); }
void CmdManager::unregisterCommand(const Cmd& cmd) { QMap<QString, Cmd>::iterator it = m_CommandMap.find(cmd.name()); if (it != m_CommandMap.end()) { std::vector<Cmd>::iterator cmdIt = m_Commands.begin(); while (cmdIt != m_Commands.end()) { if (cmd.name() == (*cmdIt).name()) { m_Commands.erase(cmdIt); break; } ++cmdIt; } m_CommandMap.erase(it); } }
int main( int argc, char **argv ) { char *cmdStr; int argIdx; Cmd *cmd; if ( argc == 1 ) { Usage( ); exit( 0 ); } if (( gFd = open( "/dev/robostix", O_RDWR )) < 0 ) { perror( "Unable to open /dev/robostix" ); exit( 1 ); } argIdx = 1; cmdStr = argv[ argIdx++ ]; for ( cmd = gCmd; cmd->cmdStr != NULL; cmd++ ) { if ( strcasecmp( cmdStr, cmd->cmdStr ) == 0 ) { break; } } if ( cmd->cmdStr == NULL ) { fprintf( stderr, "Unrecognized command: '%s'\n", cmdStr ); exit( 1 ); } if ( cmd->parseArgs( argc - argIdx, &argv[ argIdx ] )) { cmd->execFunc( cmd->cmd ); } close( gFd ); exit( 0 ); return 0; } // main
bool MakeDynLink::Action() { if (GetParams()->size() < 2) { return false; } else { Cmd* cmd = Cmd::Instance(); Fs::Path path1 = GetParams()->at(0); Fs::Path path2 = GetParams()->at(1); Fs::Object* obj = cmd->GetWorkPath(&path1); if (obj) { if (!path1.GetFile().empty()) { for (size_t i = 0; i < obj->GetChilds().size(); i++) { if (cmd->GetFs()->IsFile(obj->GetChilds()[i]) && (obj->GetChilds()[i]->GetName() == path1.GetFile())) { cmd->GetFs()->MakeDynLink(obj->GetChilds()[i], cmd->GetWorkPath(&path2)); break; } } } else { cmd->GetFs()->MakeDynLink(obj, cmd->GetWorkPath(&path2)); } } } return true; }
bool ChangeDir::Action() { Cmd* cmd = Cmd::Instance(); if (!GetParams()->empty()) { Fs::Path path = GetParams()->front(); Fs::Object* obj = cmd->GetCurrentPath(); obj = cmd->GetWorkPath(&path); if (obj) cmd->SetCurrentPath(obj); } return true; }
void Phase::ProcessCmdMessage(const Input::CmdMessage& msg, CommitSession& session, const Player& player) { LiveGame& game = GetGame(); VERIFY_MODEL_MSG(player.GetName(), &game == player.GetCurrentLiveGame()); const Colour colour = game.GetTeam(player).GetColour(); Cmd* pCmd = GetCurrentCmd(colour); VERIFY_MODEL_MSG("No current command", !!pCmd); Cmd::ProcessResult result = pCmd->Process(msg, session); // Might be null. CmdStack& cmdStack = GetCmdStack(colour); cmdStack.AddCmd(std::move(result.next), std::move(result.queue)); if (cmdStack.GetCurrentCmd() == nullptr) OnCmdFinished(*pCmd, session); // This phase might have finished, get potentially new phase. game.GetPhase().UpdateClient(session.GetController(), &player); }
// --------------------------------------------------------------------- string smsave() { string p=cmd.getparms(); if (note==0) return smerror("No active edit window"); if (p.empty()) return smerror("sm save parameter not given"); if (p=="edit") return smsaveactive(); if (p=="tabs") return smsaveall(); return smerror("sm save parameter should be 'edit' or 'tabs': " + p); }