/* * madc.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "Net/ThreadManager.h" #include "Common/ClientConnection.h" #include "Common/ConfigManager.h" #include "Common/Requests/IdentifyRequest.h" #include "Common/Logger.h" #include "Common/RequestManager.h" #include "Client/CommandParser.h" #include "Client/InformationManager.h" #include #include #include using namespace Mad; static bool commandRunning = false; static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address" << std::endl; } static void commandFinished() { commandRunning = false; } int main(int argc, char *argv[]) { if(argc != 2) { usage(argv[0]); std::exit(1); } Net::ThreadManager::get()->init(); Client::InformationManager::get()->init(); Common::ConfigManager::get()->finish(); Common::ClientConnection *connection = new Common::ClientConnection; try { connection->connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(argv[1]), 6666)); std::cerr << "Connecting to " << argv[1] << "..." << std::flush; while(connection->isConnecting()) usleep(100000); Common::RequestManager::get()->registerConnection(connection); commandRunning = true; Common::RequestManager::get()->sendRequest(connection, boost::bind(&commandFinished)); while(commandRunning) { usleep(100000); } std::cerr << " connected." << std::endl; std::cerr << "Receiving host list..." << std::flush; Client::InformationManager::get()->updateDaemonList(connection); while(Client::InformationManager::get()->isUpdating()) usleep(100000); std::cerr << " done." << std::endl << std::endl; Client::CommandParser::get()->setConnection(connection); Client::CommandManager::get()->signalFinished().connect(&commandFinished); while(connection->isConnected()) { char *cmd = readline("mad> "); if(!cmd) { commandRunning = true; Client::CommandParser::get()->requestDisconnect(); } else if(*cmd) { commandRunning = true; Client::CommandParser::get()->parse(cmd); add_history(cmd); } while(Client::CommandManager::get()->requestsActive()) usleep(100000); } Common::RequestManager::get()->unregisterConnection(connection); } catch(Net::Exception &e) { Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; Common::Initializable::deinit(); return 0; }