123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #include "../include/userioman.h"
- #include <boost/algorithm/string.hpp>
- #include <fstream>
- #include <iostream>
- #include <vector>
- #include <readline/history.h>
- #include <readline/readline.h>
- using boost::asio::buffer;
- UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring) {};
- UserIoMan::UserIoMan(char *ipcstring, bool usessl) : IoMan(ipcstring, usessl) {
- /* setup json stuff */
- Json::CharReaderBuilder rbuilder;
- wbuilder.settings_["indentation"] = "";
- reader = rbuilder.newCharReader();
- /* initialize print command map */
- printmap["error"] = &UserIoMan::printError;
- printmap["connect"] = &UserIoMan::printConnect;
- printmap["help"] = &UserIoMan::printHelp;
- printmap["status"] = &UserIoMan::printStatus;
- printmap["disconnect"] = &UserIoMan::printDisconnect;
- printmap["put"] = &UserIoMan::printPut;
- printmap["get"] = &UserIoMan::printGet;
- printmap["list"] = &UserIoMan::printList;
- printmap["version"] = &UserIoMan::printVersion;
- printmap["login"] = &UserIoMan::printLogin;
- printmap["signup"] = &UserIoMan::printSignup;
- printmap["putdata"] = &UserIoMan::printPutdata;
- printmap["getdata"] = &UserIoMan::printGetdata;
- printmap["head"] = &UserIoMan::printHead;
- printmap["deleteme"] = &UserIoMan::printDeleteme;
- printmap["keyfile"] = &UserIoMan::printKeyfile;
- printmap["closekey"] = &UserIoMan::printClosekey;
- }
- UserIoMan::~UserIoMan() { delete reader; }
- void UserIoMan::printMessage(std::string msg, OutMsgType type) {
- Json::Value root;
- msgmutex.lock();
- switch (type) {
- case normal:
- case error: {
- // this should never happen outside of development
- if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root, &jsonerror)) {
- printMessage(string(__PRETTY_FUNCTION__) + " couldnt parse json data: " + jsonerror, debug);
- } else {
- printJson(root);
- }
- break;
- }
- //~ case error: {
- //~ std::cout << msg << std::endl;
- //~ break;
- //~ }
- case debug: {
- std::cerr << msg << std::endl;
- break;
- }
- }
- rl_redisplay();
- msgmutex.unlock();
- }
- void UserIoMan::printWelcomeMessage() {
- std::cout << "please login by entering \"login <username> <password>\" \nor "
- "sign up and log in with \"signup <username> <password>\""
- << std::endl;
- }
- std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
- // currently not in use:
- //~ std::string UserIoMan::getUserPrompt() { return "User: "; }
- //~ std::string UserIoMan::getPassPrompt() { return "Pass: "; }
- void UserIoMan::printJson(Json::Value root) {
- map<string, void (UserIoMan::*)(Json::Value)>::iterator it = printmap.find(root["command"].asString());
- if (it == printmap.end()) {
- // this should never happen outside of development
- printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nensure code is implemented.", debug);
- return;
- }
- (this->*(printmap[root["command"].asString()]))(root);
- }
- void UserIoMan::printError(Json::Value root) { std::cout << "Error: " << root["error"].asString() << std::endl; }
- void UserIoMan::printConnect(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "Couldnt connect to " << root["address"].asString() << ":" << root["port"].asUInt() << std::endl
- << "Reason: " << root["error"].asString() << std::endl;
- }
- }
- void UserIoMan::printHelp(Json::Value root) {
- std::cout << "Available commands are: " << std::endl;
- for (Json::Value i : root["names"])
- std::cout << i.asString() << std::endl;
- }
- void UserIoMan::printStatus(Json::Value root) { std::cout << "Server reports status: " << root["response"].asString() << std::endl; }
- void UserIoMan::printDisconnect(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "Disconnect failed." << std::endl;
- } else {
- std::cout << "Disconnect successful." << std::endl;
- }
- }
- void UserIoMan::printPut(Json::Value root) {
- if (!root["accept"].asBool()) {
- if (root.isMember("file")) {
- std::cout << "Upload request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
- } else {
- std::cout << "Upload request failed: " << root["error"].asString() << std::endl;
- }
- } else
- std::cout << "Begin uploading file " << root["file"].asString() << std::endl;
- }
- void UserIoMan::printGet(Json::Value root) {
- if (!root["accept"].asBool()) {
- if (root.isMember("file")) {
- std::cout << "Download request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
- } else {
- std::cout << "Download request failed: " << root["error"].asString() << std::endl;
- }
- } else
- std::cout << "Begin downloading file " << root["file"].asString() << std::endl;
- }
- void UserIoMan::printList(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "Listing files failed: " << root["error"].asString() << std::endl;
- } else {
- std::cout << "Listing files stored on server: " << std::endl;
- for (Json::Value i : root["names"])
- std::cout << i.asString() << std::endl;
- std::cout << "End of list." << std::endl;
- }
- }
- void UserIoMan::printVersion(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "Version check failed. Server reports " << root["serverversion"].asString() << " but client is " << root["clientversion"].asString()
- << std::endl;
- } else
- std::cout << "Version check ok." << std::endl;
- }
- void UserIoMan::printLogin(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "Login failed: " << root["error"].asString() << std::endl;
- } else
- std::cout << "Login ok." << std::endl;
- }
- void UserIoMan::printSignup(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "Signup failed: " << root["error"].asString() << std::endl;
- } else
- std::cout << "Signup ok. You are now logged in." << std::endl;
- }
- void UserIoMan::printDeleteme(Json::Value root) {
- if (!root["accept"].asBool()) {
- std::cout << "User deletion failed: " << root["error"].asString() << std::endl;
- } else
- std::cout << "User deletion ok. You are now disconnected from the server." << std::endl;
- }
- void UserIoMan::printPutdata(Json::Value root) {}
- void UserIoMan::printGetdata(Json::Value root) {}
- void UserIoMan::printListdata(Json::Value root) {}
- void UserIoMan::printHead(Json::Value root) {
- if (!root["accept"].asBool())
- std::cout << "Request of the first four bytes failed, server reports: " << root["error"].asString() << std::endl;
- else
- std::cout << "First four bytes of file " << root["file"].asString() << " are: " << root["data"].asString() << std::endl;
- }
- void UserIoMan::printKeyfile(Json::Value root) {
- if (!root["accept"].asBool())
- std::cout << "Couldnt select keyfile " << root["file"].asString() << ": " << root["error"].asString() << std::endl;
- else
- std::cout << "Using keyfile " << root["file"].asString() << std::endl;
- }
- void UserIoMan::printClosekey(Json::Value root) {
- if (!root["accept"].asBool())
- std::cout << "Failed to close key: " << root["error"].asString() << std::endl;
- else
- std::cout << "Key closed." << std::endl;
- }
|