/* * 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/RequestManager.h" #include "Client/RequestProcessor.h" #include #include #include #include #include #include static Mad::Client::RequestProcessor *processor; static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address[:port]" << std::endl; } static void handleCommand(char *cmd) { if(!cmd) { processor->requestDisconnect(); rl_callback_handler_remove(); return; } if(!*cmd) return; if(std::strcmp(cmd, "quit") == 0) { processor->requestDisconnect(); rl_callback_handler_remove(); } else { std::cerr << "Unknown command \"" << cmd << "\"." << std::endl; } add_history(cmd); } int main(int argc, char *argv[]) { if(argc != 2) { usage(argv[0]); std::exit(1); } Mad::Net::Connection::init(); Mad::Common::RequestManager requestManager(false); Mad::Net::ClientConnection *connection = new Mad::Net::ClientConnection; try { connection->connect(Mad::Net::IPAddress(argv[1])); std::cout << "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::cout << " connected." << std::endl << std::endl; requestManager.registerConnection(connection); processor = new Mad::Client::RequestProcessor(&requestManager, connection); struct pollfd fds[2]; fds[0].fd = STDIN_FILENO; fds[0].events = POLLIN; rl_callback_handler_install("mad: ", handleCommand); while(connection->isConnected()) { fds[1] = connection->getPollfd(); if(poll(fds, 2, 10000) > 0) { if(fds[0].revents) rl_callback_read_char(); connection->sendReceive(fds[1].revents); } } delete processor; requestManager.unregisterConnection(connection); } catch(Mad::Net::Exception &e) { std::cerr << "Connection error: " << e.what() << std::endl; } delete connection; Mad::Net::Connection::deinit(); return 0; }