123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #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) {
- /* setup json stuff */
- Json::CharReaderBuilder rbuilder;
- wbuilder.settings_["indentation"] = "";
- reader = rbuilder.newCharReader();
- /* initialize print command map */
- 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["putdata"] = &UserIoMan::printPutdata;
- printmap["getdata"] = &UserIoMan::printGetdata;
- }
- UserIoMan::~UserIoMan() { delete reader; }
- void UserIoMan::printMessage(std::string msg, OutMsgType type) {
- Json::Value root;
- msgmutex.lock();
- switch (type) {
- case normal: {
- // 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,
- error);
- } 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 enter user and password" << std::endl;
- }
- std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
- 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.",
- error);
- return;
- }
- (this->*(printmap[root["command"].asString()]))(root);
- }
- 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()) {
- std::cout << "Upload request for file " << root["file"].asString()
- << " 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()) {
- std::cout << "Download request for file " << root["file"].asString()
- << " 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::printPutdata(Json::Value root) {}
- void UserIoMan::printGetdata(Json::Value root) {}
- void UserIoMan::printListdata(Json::Value root) {}
|