#include "commandline.h" wxString shutdownNest(CommandLine *cmdline,const wxString &args) { wxString msg = "\r\nShutting down...\r\n"; killsignal = true; return msg; } wxString help(CommandLine *cmdline, const wxString &args) { // echo help message of all functions registered wxString msg = "\r\nCommands currently supported:\r\n\r\n"; callbackVector::iterator pFun; for ( pFun = cmdline->functions.begin() ; pFun != cmdline->functions.end() ; pFun++ ) { msg += "\t" + pFun->sCommand; msg += "\t\t"; msg += pFun->sHelp; msg += "\r\n"; } msg += "\r\n"; return msg; } CommandLine::CommandLine(TCP_socket psocket) : wxThread(wxTHREAD_DETACHED) { //register functions then start running.... socket = psocket; running = false; RegisterCmd(help,"help","Displays this message"); RegisterCmd(shutdownNest,"bail","Emergency shutdown of the NEST server"); Create(); } CommandLine::~CommandLine() { // do destruction! } wxThread::ExitCode CommandLine::Entry() { running = true; wxLogMessage("Control connection accepted."); // socket->SetFlags(wxSOCKET_WAITALL); int result; wxString data = ""; while (!killsignal && socket) { //if (!socket->WaitForRead(-1,0)) continue; // read a char from the stream and see what command we should do if (TestDestroy()) { killsignal=true; break; } result=WriteChars(socket,(void *)_T("NEST > "),7); if (result<=0) break; // FIXME: Should wait for socket read here... if (ReadChars(socket,data)) { wxString args; cmdcallback fun = MatchFunction( data , args ); if ( fun != NULL ) { wxString databack = fun( this,args ); WriteChars( socket,(void *)databack.c_str(),databack.Length() ); } data.Empty(); } wxThread::Sleep(200); } //socket->Destroy(); Network::Close(socket); running = false; return 0; } bool CommandLine::RegisterCmd( wxString (*func)(CommandLine *cmdline, const wxString&), const wxString& sCommand, const wxString& sHelp) { if ( !running ) { cmdCallBackFunction fu; fu.function = func; fu.sCommand = sCommand; fu.sHelp = sHelp; fu.sCommand.MakeLower(); // add to the vector functions.push_back( fu ); } else { wxLogError("Cannot register callback function when commandline is running"); return false; } return true; } cmdcallback CommandLine::MatchFunction( const wxString& sCommand , wxString& sArgs ) { callbackVector::iterator pFun; for ( pFun = functions.begin() ; pFun != functions.end() ; pFun++ ) { unsigned int iLenCommand = pFun->sCommand.Length(); wxString sCopCommand = sCommand; sCopCommand.MakeLower(); wxString givenCmd = sCopCommand.Left(iLenCommand); if (givenCmd.Cmp(pFun->sCommand)==0 && (sCopCommand.Length()==iLenCommand || sCopCommand.GetChar(iLenCommand)==' ')) { if ( iLenCommand < sCommand.Length() ) { sArgs = sCommand.Mid( iLenCommand+1 ); } return pFun->function; } } return NULL; }