From f50844eb7f9772f865ce9f272eadb15943319885 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 9 Sep 2008 01:27:18 +0200 Subject: Readline-Interface f?r den Client --- src/madc.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 20 deletions(-) (limited to 'src/madc.cpp') diff --git a/src/madc.cpp b/src/madc.cpp index 3732079..06d65af 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -19,21 +19,62 @@ #include "Net/ClientConnection.h" #include "Net/IPAddress.h" -#include "Net/Packet.h" #include "Common/RequestManager.h" -#include "Common/Request/GSSAPIAuthRequest.h" -#include "Common/Request/DisconnectRequest.h" -#include "Common/Request/IdentifyRequest.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); + } -int main() { Mad::Net::Connection::init(); Mad::Common::RequestManager requestManager(false); Mad::Net::ClientConnection *connection = new Mad::Net::ClientConnection; try { - connection->connect(Mad::Net::IPAddress("127.0.0.1", 6666)); + connection->connect(Mad::Net::IPAddress(argv[1])); + + std::cout << "Connecting to " << argv[1] << "..." << std::flush; while(connection->isConnecting()) { struct pollfd fd = connection->getPollfd(); @@ -42,28 +83,32 @@ int main() { connection->sendReceive(fd.revents); } + std::cout << " connected." << std::endl << std::endl; + requestManager.registerConnection(connection); - Mad::Common::Request::IdentifyRequest::send(connection, requestManager, "localhost"); - Mad::Common::Request::GSSAPIAuthRequest::send(connection, requestManager, "host"); + processor = new Mad::Client::RequestProcessor(&requestManager, connection); - while(true) { - struct pollfd fd = connection->getPollfd(); + struct pollfd fds[2]; + fds[0].fd = STDIN_FILENO; + fds[0].events = POLLIN; - if(poll(&fd, 1, 1500) > 0) - connection->sendReceive(fd.revents); - else - break; - } - - Mad::Common::Request::DisconnectRequest::send(connection, requestManager); + rl_callback_handler_install("mad: ", handleCommand); while(connection->isConnected()) { - struct pollfd fd = connection->getPollfd(); + fds[1] = connection->getPollfd(); - if(poll(&fd, 1, 10000) > 0) - connection->sendReceive(fd.revents); + 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; -- cgit v1.2.3