diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-09 01:27:18 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-09 01:27:18 +0200 |
commit | f50844eb7f9772f865ce9f272eadb15943319885 (patch) | |
tree | c1abc4e8f194ff196e92a97e3337dbfcc08dd39b /src/madc.cpp | |
parent | 87aad429d08b2a352f0f4345e45a03a0a7da2407 (diff) | |
download | mad-f50844eb7f9772f865ce9f272eadb15943319885.tar mad-f50844eb7f9772f865ce9f272eadb15943319885.zip |
Readline-Interface f?r den Client
Diffstat (limited to 'src/madc.cpp')
-rw-r--r-- | src/madc.cpp | 85 |
1 files changed, 65 insertions, 20 deletions
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 <iostream> +#include <cstring> +#include <cstdlib> +#include <string> + +#include <readline/readline.h> +#include <readline/history.h> + + +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; |