/* * 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/Connection.h" #include "Net/FdManager.h" #include "Net/IPAddress.h" #include "Common/ClientConnection.h" #include "Common/ConfigManager.h" #include "Common/LogManager.h" #include "Common/Logger.h" #include "Common/RequestManager.h" #include "Common/ThreadManager.h" #include "Client/CommandParser.h" #include "Client/InformationManager.h" #include #include #include #include #include #include using namespace Mad; static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address[:port]" << std::endl; } static void handleCommand(char *cmd) { if(!cmd) Client::CommandParser::get()->requestDisconnect(); else if(!*cmd) return; else { Client::CommandParser::get()->parse(cmd); add_history(cmd); } if(Client::CommandManager::get()->requestsActive()) { rl_callback_handler_remove(); Net::FdManager::get()->setFdEvents(STDIN_FILENO, 0); } } static void charHandler(short events) { if(events & POLLIN) rl_callback_read_char(); } static void activateReadline() { if(Client::CommandManager::get()->willDisconnect()) return; rl_callback_handler_install("mad> ", handleCommand); Net::FdManager::get()->setFdEvents(STDIN_FILENO, POLLIN); } int main(int argc, char *argv[]) { if(argc != 2) { usage(argv[0]); std::exit(1); } Net::Connection::init(); Common::ThreadManager::get()->init(); Client::InformationManager::get()->init(); Common::ConfigManager::get()->finish(); Common::ClientConnection *connection = new Common::ClientConnection; try { connection->connect(Net::IPAddress(argv[1])); std::cerr << "Connecting to " << argv[1] << "..." << std::flush; while(connection->isConnecting()) Net::FdManager::get()->run(); std::cerr << " connected." << std::endl; Common::RequestManager::get()->registerConnection(connection); std::cerr << "Receiving host list..." << std::flush; Client::InformationManager::get()->updateDaemonList(connection); while(Client::InformationManager::get()->isUpdating()) Net::FdManager::get()->run(); std::cerr << " done." << std::endl << std::endl; Client::CommandParser::get()->setConnection(connection); Client::CommandManager::get()->signalFinished().connect(sigc::ptr_fun(activateReadline)); Net::FdManager::get()->registerFd(STDIN_FILENO, sigc::ptr_fun(charHandler)); activateReadline(); while(connection->isConnected()) Net::FdManager::get()->run(); Net::FdManager::get()->unregisterFd(STDIN_FILENO); Common::RequestManager::get()->unregisterConnection(connection); } catch(Mad::Common::Exception &e) { Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; Common::Initializable::deinit(); Net::Connection::deinit(); return 0; }