/* * 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/ClientConnection.h" #include "Net/IPAddress.h" #include "Common/Logger.h" #include "Common/Backends/ConsoleLogger.h" #include "Common/RequestManager.h" #include "Common/Util.h" #include "Client/CommandParser.h" #include "Client/InformationManager.h" #include #include #include #include #include #include using namespace Mad; static Client::CommandParser *parser; static struct pollfd fds[2]; static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address[:port]" << std::endl; } static void handleCommand(char *cmd) { if(!cmd) parser->requestDisconnect(); else if(!*cmd) return; else { parser->parse(cmd); add_history(cmd); } if(parser->requestsActive()) { rl_callback_handler_remove(); fds[0].events = 0; } } static void activateReadline() { if(parser->willDisconnect()) return; rl_callback_handler_install("mad> ", handleCommand); fds[0].events = POLLIN; } int main(int argc, char *argv[]) { if(argc != 2) { usage(argv[0]); std::exit(1); } Common::Backends::ConsoleLogger logger; Common::Logger::registerLogger(&logger); Net::Connection::init(); Common::RequestManager::init(false); Net::ClientConnection *connection = new Net::ClientConnection; try { connection->connect(Net::IPAddress(argv[1])); std::cerr << "Connecting to " << argv[1] << "..." << std::flush; while(connection->isConnecting()) { struct pollfd fd = connection->getPollfd(); if(poll(&fd, 1, 10000) > 0) connection->sendReceive(fd.revents); } std::cerr << " connected." << std::endl; Common::RequestManager::getRequestManager()->registerConnection(connection); std::cerr << "Receiving host list..." << std::flush; Client::InformationManager::init(connection); while(!Client::InformationManager::getInformationManager()->isInitialised()) { struct pollfd fd = connection->getPollfd(); if(poll(&fd, 1, 10000) > 0) connection->sendReceive(fd.revents); } std::cerr << " done." << std::endl << std::endl; parser = new Mad::Client::CommandParser(connection); parser->signalFinished().connect(sigc::ptr_fun(activateReadline)); fds[0].fd = STDIN_FILENO; activateReadline(); while(connection->isConnected()) { fds[1] = connection->getPollfd(); if(poll(fds, 2, 10000) > 0) { if(fds[0].revents & POLLIN) rl_callback_read_char(); connection->sendReceive(fds[1].revents); } } delete parser; Common::RequestManager::getRequestManager()->unregisterConnection(connection); } catch(Mad::Common::Exception &e) { Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; Net::Connection::deinit(); return 0; }