bool CmdExtended::onClient(DebuggerClient *client) {
  if (client->arg(1, "help") || client->arg(1, "?")) {
    if (client->argCount() == 1) {
      return help(client);
    }
    ExtendedCommandMap matches = match(client, 2);
    if (matches.empty()) {
      return help(client);
    }
    return helpCommands(client, matches);
  }

  ExtendedCommandMap matches = match(client, 1);
  if (matches.empty()) {
    return help(client);
  }
  if (matches.size() > 1) {
    client->error("Need more letters to tell which one of these:");
    for (ExtendedCommandMap::const_iterator iter = matches.begin();
         iter != matches.end(); ++iter) {
      client->error("\t%s", iter->first.c_str());
    }
    return true;
  }

  if (!invokeClient(client, matches.begin()->second)) {
    if (client->arg(2, "help") || client->arg(2, "?")) {
      return helpCommands(client, matches);
    }
  }
  return true;
}
Exemple #2
0
int main(int argc, char** argv) {

    if (argc != 3) {
        usage();
    }

    //general variables
    PracticaCaso::TcpClient *client = new PracticaCaso::TcpClient();
    string dnsName, ipAddressAndPort, msg, ip, aux;
    int port, length;
    vector<string> commandVec;
    bool exit = false, badCommand = true;



    //////////////NameServer Part/////////////////////
    client->connect("127.0.0.1", atoi(argv[1])); //connect with the name server

    // Lookup in NameServer(send the domain that we want)
    dnsName = argv[2];
    client->send(dnsName);

    //receive the response
    ipAddressAndPort = client->receive();
    client->close();

    if (ipAddressAndPort.find("ERROR") == 0) {
        cout << "The DNS name " << dnsName << " could not be resolved." << endl;
    }
    else
    {
        //connect to the proposal server
        ip = ipAddressAndPort.substr(0, ipAddressAndPort.find(":"));
        port = atoi((ipAddressAndPort.substr(ipAddressAndPort.find(":")+1)).c_str());
        client->connect(ip,port);
        //////////////////Server Part/////////////////////
        while(!exit)
        {
            //reset bad command boolean
            badCommand = true;

            while(badCommand) //if the command is bad then loop until is a good constructed command
            {
                //insert command
                cout << "Insert your command: ";
                getline(cin, msg);

                //copy the string to have the original msg intact, the strtoken brokens
                strcpy((char *)aux.c_str(),(char *)msg.c_str());
                aux =  string(aux.c_str(), aux.size());
                //split command
                commandVec = extractCommandAndArgs(aux);

                if ((commandVec.size() >= 2) || (commandVec[0].find("quit") == 0))
                    badCommand = false;
                else if(commandVec[0].find("help") == 0) //prompt help
                {
                    helpCommands();
                }
            }

            //if is the command then ecript
            if (commandVec[0].find("pass") == 0)
            {
                //encrypt password
                msg = encryptMsg(commandVec[1]);
                msg = "pass " + msg;
            } else if(commandVec[0].find("quit") == 0) //we want to quit, we send to the server and wait response to close connection
                exit = true;

            //send and wait response
            client->send(msg);
            msg = client->receive();

            cout << msg << endl;

            if(msg.find("[QUIT]") == 0)
                exit = true;
            else if((msg.find("[USER OK]") == 0)) //automate password asking
            {
                msg = "[PASS ERROR]"; //reset the msg to enter the while loop

                while( (msg.find("[PASS ERROR]") == 0) && (msg.find("[QUIT]") != 0) )
                {
                    //insert command
                    cout << "Insert your password: "******"pass " + msg;
                    client->send(msg);
                    msg = client->receive();
                    cout << msg << endl;
                }
            }
        }
        //finish
        client->close();
    }

    delete client;

}
int Component::handleCommand(const char* input)
{
   MC2String token = input ? input : "";
   // remove everything after first space " "
   MC2String::size_type pos = token.find_first_of( " " );
   if ( pos != MC2String::npos ) {
      token = token.substr( 0, pos );
   }

   if ( token ==  "help" ) {

      cout << m_componentName << " help" << endl 
           << endl << "  help         - This help"
           << endl << "  quit         - quit immediately [exit(0)]" 
           << endl << "  shutdown     - Shutdown this component gracefully "
                                       "(also Ctrl-D)" 
           << endl << "  abort        - Abort this component (immediate "
                                       "shutdown with core dump)"
           << endl << "  config       - Configuration details" 
           << endl << "  status       - Overall status of this component" 
           << endl << "  heapstatus   - Displays information about the heap"
           << endl;
      // Let subclasses print their help commands
      helpCommands();

   } else if ( token == "quit" ) { 
      mc2log << warn << "Component exiting without clean shutdown!" << endl;
      exit(0);
   } else if ( token == "shutdown" ) { 
      mc2log << info << "Initiating component shutdown" << endl;
      return -1;
   } else if ( token == "config" ) {

      cout << m_componentName << " configuration" 
           << endl   << "  Property file path : " 
                     << Properties::getPropertyFileName() << endl;

   } else if ( token == "status" ) {

      uint32 m_uptime = TimeUtility::getRealTime() - m_startTime;
      cout << m_componentName << " status" 
           << endl << "  pid          : " << getpid()
           << endl << "  uptime       : ";
      if (m_uptime / 86400 > 0) {
           cout << m_uptime / 86400 << " day(s), ";
           m_uptime = m_uptime % 86400;
      }
      if (m_uptime / 3600 > 0) {
           cout << m_uptime / 3600 << " hour(s), ";
           m_uptime = m_uptime % 3600;
      }
      if (m_uptime / 60 > 0) {
           cout << m_uptime / 60 << " minute(s), ";
           m_uptime = m_uptime % 60;
      }
      cout << m_uptime << " second(s)"
           << endl << "  CPU usage    : " << "N/A"
           << endl;
   } else if ( token == "heapstatus" ) {
      SysUtility::printHeapStatus( cout );
   } else if ( token == "abort" ) {
      mc2log << fatal << "Component aborted by user" << endl;
      PANIC("Aborted by user!", "");
   } else  {
      cout << "Unknown command. Use the command 'help' to get help" << endl;
   }

   return 1;
}