123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961 |
- #include "../include/cmdman.h"
- #include "../include/global.h"
- #include <iostream>
- #define DEBUGPRINT(x) debugprintfunc(x)
- //~ #define DEBUGPRINT(x) std::cerr << x
- //~ #define DEBUGPRINT(x)
- CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
- /* setup json stuff */
- Json::CharReaderBuilder rbuilder;
- wbuilder.settings_["indentation"] = "";
- reader = rbuilder.newCharReader();
- currentState = connectionpossible;
- /* initialize execute command map */
- execmap["help"] = &CmdMan::cmdHelp;
- execmap["status"] = &CmdMan::cmdStatus;
- execmap["disconnect"] = &CmdMan::cmdDisconnect;
- execmap["put"] = &CmdMan::cmdPut;
- execmap["get"] = &CmdMan::cmdGet;
- execmap["list"] = &CmdMan::cmdList;
- execmap["version"] = &CmdMan::cmdVersion;
- execmap["login"] = &CmdMan::cmdLogin;
- execmap["signup"] = &CmdMan::cmdSignup;
- execmap["putdata"] = &CmdMan::cmdPutdata;
- execmap["getdata"] = &CmdMan::cmdGetdata;
- execmap["listdata"] = &CmdMan::cmdListdata;
- execmap["head"] = &CmdMan::cmdHead;
- execmap["deletefile"] = &CmdMan::cmdDeletefile;
- execmap["deleteme"] = &CmdMan::cmdDeleteme;
- execmap["keyfile"] = &CmdMan::cmdKeyfile;
- execmap["closekey"] = &CmdMan::cmdClosekey;
- execmap["connect"] = &CmdMan::cmdConnect;
- execmap["exit"] = &CmdMan::cmdExit;
- /* initialize description map */
- helpmap["help"] = descHelp;
- helpmap["status"] = descStatus;
- helpmap["disconnect"] = descDisconnect;
- helpmap["put"] = descPut;
- helpmap["get"] = descGet;
- helpmap["list"] = descList;
- helpmap["head"] = descHead;
- helpmap["login"] = descLogin;
- helpmap["signup"] = descSignup;
- helpmap["deletefile"] = descDeletefile;
- helpmap["deleteme"] = descDeleteme;
- helpmap["keyfile"] = descKeyfile;
- helpmap["closekey"] = descClosekey;
- helpmap["connect"] = descConnect;
- helpmap["exit"] = descExit;
- /* initialize handle command map */
- handlemap["status"] = &CmdMan::handleStatus;
- handlemap["close"] = &CmdMan::handleClose;
- handlemap["put"] = &CmdMan::handlePut;
- handlemap["get"] = &CmdMan::handleGet;
- handlemap["putdata"] = &CmdMan::handlePutdata;
- handlemap["getdata"] = &CmdMan::handleGetdata;
- handlemap["list"] = &CmdMan::handleList;
- handlemap["version"] = &CmdMan::handleVersion;
- handlemap["login"] = &CmdMan::handleLogin;
- handlemap["signup"] = &CmdMan::handleSignup;
- handlemap["listdata"] = &CmdMan::handleListdata;
- handlemap["head"] = &CmdMan::handleHead;
- handlemap["deletefile"] = &CmdMan::handleDeletefile;
- handlemap["deleteme"] = &CmdMan::handleDeleteme;
- debugprintfunc = dpf;
- }
- CmdMan::~CmdMan() { delete reader; }
- void CmdMan::stateSetConnectionOk() { currentState = versionpossible; }
- CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
- CmdRet retval;
- Json::Value root, arr;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- map<string, string>::iterator it;
- root["command"] = "help";
- for (it = helpmap.begin(); it != helpmap.end(); it++) {
- arr.append(it->first + " - " + it->second);
- }
- root["names"] = arr;
- retval.type = print;
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "status";
- retval.type = send;
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- retval.type = send;
- if (currentState == loginpossible || currentState == disconnecttoexitearly) {
- // not logged in, send appropriate login message instead of normal close
- root["login"] = false;
- root["user"] = "";
- root["pass"] = "";
- root["cancel"] = true;
- retval.type |= close;
- if (currentState == disconnecttoexitearly) {
- retval.nextcommand = "exit";
- }
- currentState = connectionpossible;
- } else {
- root["command"] = "close";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "put";
- if (args.size() < 1) {
- retval.type = error;
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else {
- if (fileman.isPutting()) {
- retval.type = error;
- root["file"] = args[0];
- root["accept"] = false;
- root["error"] = "already putting file \"" + fileman.getPutName() + "\"";
- } else {
- bool opened = fileman.openPut(args[0]);
- if (opened) {
- root["file"] = fileman.getPutName();
- root["size"] = fileman.getPutSize();
- root["chunks"] = fileman.getPutChunks();
- retval.type = send;
- } else {
- retval.type = error;
- root["file"] = fileman.pathToFilename(args[0]);
- root["accept"] = false;
- root["error"] = "couldnt open local file \"" + args[0] + "\"";
- }
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "putdata";
- if (!fileman.isPutting()) {
- root["error"] = "Client cannot handle input (received command \"putdata\").";
- retval.type = error;
- } else {
- root["file"] = fileman.getPutName();
- root["cancel"] = false;
- root["data"] = fileman.readBase64();
- root["remaining"] = fileman.getPutRemainingChunks(); // number already decremented by readBase64
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "get";
- if (args.size() < 1) {
- retval.type = error;
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else {
- if (fileman.isGetting()) {
- retval.type = error;
- root["file"] = args[0];
- root["accept"] = false;
- root["error"] = "already getting file \"" + fileman.getGetName() + "\"";
- } else {
- bool opened = fileman.openGet(args[0]);
- root["file"] = fileman.getGetName();
- if (opened) {
- root["file"] = fileman.getGetName();
- retval.type = send;
- } else {
- root["file"] = fileman.pathToFilename(args[0]);
- root["accept"] = false;
- root["error"] = "local file \"" + args[0] + "\" already exists";
- retval.type = error;
- }
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdGetdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "getdata";
- if (!fileman.isGetting()) {
- root["error"] = "Client cannot handle input (received command \"getdata\").";
- retval.type = error;
- } else {
- root["file"] = fileman.getGetName();
- root["chunk"] = fileman.getGetRemainingChunks();
- root["cancel"] = false;
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- bool opened = fileman.openList();
- root["command"] = "list";
- if (opened) {
- retval.type = send;
- } else {
- retval.type = error;
- root["accept"] = false;
- root["names"] = "";
- root["error"] = "cannot list, already listing";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (!fileman.isListing()) {
- root["command"] = "list";
- root["error"] = "Client cannot handle input (received command \"listdata\").";
- retval.type = error;
- } else {
- root["command"] = "listdata";
- root["chunk"] = fileman.getListRemainingChunks();
- root["cancel"] = false;
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdHead(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "head";
- if (args.size() < 1) {
- retval.type = error;
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else {
- root["file"] = args[0];
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdDeletefile(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "deletefile";
- if (args.size() < 1) {
- retval.type = error;
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else {
- root["file"] = args[0];
- retval.type = send;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdConnect(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "connect";
- if (args.size() < 1) {
- retval.type = error;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else if (args.size() < 2) {
- retval.type = connect;
- root["address"] = args[0];
- root["port"] = 1234;
- } else {
- retval.type = connect;
- root["address"] = args[0];
- root["port"] = (unsigned int)stoul(args[1]);
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd + "\" with arguments [ ");
- for (string s : args)
- DEBUGPRINT(s + " ");
- DEBUGPRINT("]");
- map<string, CmdRet (CmdMan::*)(vector<string>)>::iterator it = execmap.find(cmd);
- CmdRet retval;
- Json::Value root;
- root["command"] = cmd;
- if (it == execmap.end()) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd + "\".\ntype help to list available commands.";
- retval.msg = root;
- return retval;
- } else if (!cmd.compare("help") || !cmd.compare("exit")) {
- // allow help and exit in all cases
- } else if (currentState == loginpossible || currentState == dologin || currentState == dosignup) {
- DEBUGPRINT("execute does login");
- DEBUGPRINT(string("comparison is ") +
- std::to_string(cmd.compare("login") && cmd.compare("signup") && cmd.compare("disconnect") && cmd.compare("help")));
- if (cmd.compare("login") && cmd.compare("signup") && cmd.compare("disconnect") && cmd.compare("help")) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = string("Not logged in. Available commands are limited to ") + "login" + ", " + "signup" + " and " + "disconnect" + "\n" +
- "Use help for usage of these commands.";
- retval.msg = root;
- return retval;
- }
- } else if (currentState == versionpossible || currentState == doversion) {
- DEBUGPRINT("execute does version");
- DEBUGPRINT(string("comparison is ") + std::to_string(cmd.compare("version")));
- if (cmd.compare("version")) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = string("Version not checked yet. No commands avalable.");
- retval.msg = root;
- return retval;
- }
- } else if (currentState == connectionpossible) {
- DEBUGPRINT("execute does connect");
- DEBUGPRINT(string("comparison is ") + std::to_string(cmd.compare("connect")));
- if (cmd.compare("version") && cmd.compare("connect")) {
- retval.type = error;
- root["command"] = "error";
- root["error"] = string("Not connected. Connect using \"connect ip [port]\".");
- retval.msg = root;
- return retval;
- }
- }
- return (this->*(execmap[cmd]))(args);
- }
- CmdMan::CmdRet CmdMan::cmdDeleteme(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- root["command"] = "deleteme";
- if (args.size() < 1) {
- retval.type = error;
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else {
- retval.type = send;
- root["pass"] = args[0];
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdKeyfile(vector<string> args) {
- CmdRet retval;
- Json::Value root;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- root["command"] = "keyfile";
- if (args.size() < 1) {
- retval.type = error;
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 1 argument required";
- } else {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " haveargs");
- if (!fileman.openKey(args[0])) {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " openkey fail");
- root["accept"] = false;
- root["file"] = args[0];
- root["error"] = string("couldnt open keyfile, openssl reports: ") + fileman.getOpensslError();
- retval.type = error;
- } else {
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " openkey good");
- root["accept"] = true;
- root["file"] = args[0];
- retval.type = print;
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdClosekey(vector<string> args) {
- CmdRet retval;
- Json::Value root;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- root["command"] = "closekey";
- if (!fileman.closeKey()) {
- root["accept"] = false;
- root["error"] = "couldnt close keyfile. ensure no put or get is running";
- retval.type = error;
- } else {
- root["accept"] = true;
- retval.type = print;
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdExit(vector<string> args) {
- CmdRet retval;
- Json::Value root;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- root["command"] = "exit";
- if (currentState != connectionpossible) {
- // we are connected, disconnect first
- retval.nextcommand = "disconnect";
- if (currentState == loginpossible)
- currentState = disconnecttoexitearly;
- else
- currentState = disconnecttoexit;
- retval.type = none;
- } else {
- retval.type = exit;
- }
- retval.msg = root;
- return retval;
- }
- /* login and signup commands */
- CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 2) {
- retval.type = error;
- root["command"] = "login";
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 2 argument required";
- } else {
- if (currentState == loginpossible) {
- currentState = dologin;
- root["user"] = args[0];
- root["pass"] = args[1];
- root["login"] = true;
- root["cancel"] = false;
- retval.type = send;
- } else {
- root["command"] = "login";
- root["error"] = "Login not possible, because you already requested a login "
- "or you are logged in";
- root["accept"] = false;
- retval.type = error;
- }
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (args.size() < 2) {
- retval.type = error;
- root["command"] = "signup";
- root["accept"] = false;
- root["error"] = "not enough arguments, at least 2 argument required";
- } else {
- if (currentState == loginpossible) {
- currentState = dosignup;
- root["user"] = args[0];
- root["pass"] = args[1];
- root["login"] = false;
- root["cancel"] = false;
- retval.type = send;
- } else {
- root["command"] = "signup";
- root["error"] = "Signup not possible, because you already requested a "
- "login or you are logged in";
- root["accept"] = false;
- retval.type = error;
- }
- }
- retval.msg = root;
- return retval;
- }
- /* internal commands */
- CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- Json::Value root;
- if (currentState == versionpossible) {
- root["major"] = protocolMajorVersion;
- root["minor"] = protocolMinorVersion;
- retval.type = send;
- currentState = doversion;
- } else {
- retval.type = error;
- root["command"] = "error";
- root["error"] = "Executing version command not possible. Type help to list available commands.";
- }
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handle(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (currentState == doversion)
- root["command"] = "version";
- else if (currentState == dosignup)
- root["command"] = "signup";
- else if (currentState == dologin)
- root["command"] = "login";
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" + Json::writeString(wbuilder, root) + "\n");
- string retmsg;
- map<string, CmdRet (CmdMan::*)(Json::Value)>::iterator it = handlemap.find(root["command"].asString());
- if (it == handlemap.end()) {
- retval.type = error;
- output["command"] = "error";
- output["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nEnsure code is implemented.";
- retval.msg = output;
- return retval;
- }
- return (this->*(handlemap[root["command"].asString()]))(root);
- }
- CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- retval.type = print;
- retval.msg = root;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "disconnect";
- output["accept"] = true;
- retval.type = close | print;
- retval.msg = output;
- if (currentState == disconnecttoexit) {
- retval.nextcommand = "exit";
- }
- currentState = connectionpossible;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- output["command"] = "put";
- output["file"] = fileman.getPutName();
- output["accept"] = false;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelPut();
- } else if (!fileman.isPutting()) {
- retval.type = error;
- output["error"] = "Server responds to put message which was never sent.";
- } else if (root["file"].asString() != fileman.getPutName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
- fileman.cancelPut();
- } else {
- output["accept"] = true;
- output["error"] = "";
- retval.type = print | send;
- retval.nextcommand = "putdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "putdata";
- output["file"] = fileman.getPutName();
- output["speed"] = 0.0f; // TODO
- output["cancel"] = true;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelPut();
- } else if (!fileman.isPutting()) {
- retval.type = error;
- output["error"] = "Server responds to put message which was never sent.";
- } else if (root["received"].asInt() != fileman.getPutRemainingChunks()) {
- // the number of remaining chunks received from the daemon does not equal
- // the number stored at the client side
- retval.type = error;
- output["error"] = std::string("Server reports number of "
- "remaining chunks as ") +
- std::to_string(root["received"].asInt()) + " but actual number is " + std::to_string(fileman.getPutRemainingChunks());
- fileman.cancelPut();
- } else if (root["file"].asString() != fileman.getPutName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
- fileman.cancelPut();
- } else {
- output["cancel"] = false;
- output["error"] = "";
- // sent successfully
- if (!root["received"].asInt()) {
- // everything sent
- retval.type = print;
- // TODO
- //~ retval.msg = "succesfully uploaded file " + fileman.getPutName();
- fileman.closePut();
- } else {
- retval.type = print | send;
- retval.nextcommand = "putdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- output["command"] = "get";
- output["file"] = fileman.getGetName();
- output["accept"] = false;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelGet();
- } else if (!fileman.isGetting()) {
- retval.type = error;
- output["error"] = "Server responds to get message which was never sent.";
- } else if (root["file"].asString() != fileman.getGetName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
- fileman.cancelGet();
- } else {
- fileman.setGetChunks(root["chunks"].asInt());
- output["accept"] = true;
- output["error"] = "";
- retval.type = print | send;
- retval.nextcommand = "getdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
- CmdRet retval;
- Json::Value output;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "getdata";
- output["file"] = fileman.getGetName();
- output["speed"] = 0.0f; // TODO
- output["cancel"] = true;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelGet();
- } else if (!fileman.isGetting()) {
- retval.type = error;
- output["error"] = "Server responds to get message which was never sent.";
- } else if (root["remaining"].asInt() != fileman.getGetRemainingChunks()) {
- retval.type = error;
- output["error"] = std::string("Server reports number of remaining chunks as ") + std::to_string(root["remaining"].asInt()) + " but actual number is " +
- std::to_string(fileman.getGetRemainingChunks());
- fileman.cancelGet();
- } else if (root["file"].asString() != fileman.getGetName()) {
- retval.type = error;
- output["error"] = "Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
- fileman.cancelGet();
- } else {
- output["cancel"] = false;
- output["error"] = "";
- fileman.writeBase64(root["data"].asString());
- // loaded successfully
- if (!root["remaining"].asInt()) {
- // everything received
- retval.type = print;
- //~ retval.msg = "succesfully downloaded file " + fileman.getGetName();
- fileman.closeGet();
- } else {
- retval.type = print | send;
- retval.nextcommand = "getdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "list";
- output["names"] = "";
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["accept"] = false;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelList();
- } else if (!fileman.isListing()) {
- retval.type = error;
- output["accept"] = false;
- output["error"] = "Server responds to list message which was never sent.";
- } else {
- fileman.setListChunks(root["chunks"].asInt());
- retval.type = send;
- output["accept"] = true;
- retval.nextcommand = "listdata";
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleListdata(Json::Value root) {
- CmdRet retval;
- Json::Value output, arr;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- vector<string> toadd;
- output["command"] = "list";
- output["names"] = "";
- output["accept"] = false;
- if (root["cancel"].asBool()) {
- retval.type = error;
- output["error"] = "Server reports: " + root["error"].asString();
- fileman.cancelList();
- } else if (!fileman.isListing()) {
- retval.type = error;
- output["error"] = "Server responds to list message which was never sent.";
- } else if (root["remaining"].asInt() != fileman.getListRemainingChunks()) {
- // the passed number of recieved chunks should equal the number of sent chunks
- retval.type = error;
- output["error"] = std::string("Server reports number of "
- "remaining chunks as ") +
- std::to_string(root["remaining"].asInt()) + " but actual number is " + std::to_string(fileman.getListRemainingChunks());
- fileman.cancelList();
- } else {
- output["accept"] = true;
- for (Json::Value i : root["names"])
- toadd.push_back(i.asString());
- fileman.putListData(toadd);
- // loaded successfully
- if (root["remaining"] <= 0) {
- // everything sent
- retval.type = print;
- for (string s : fileman.getListData())
- arr.append(s);
- output["names"] = arr;
- fileman.closeList();
- } else {
- retval.type = send;
- retval.nextcommand = "listdata";
- }
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "version";
- output["serverversion"] = std::to_string(root["major"].asInt()) + "." + std::to_string(root["minor"].asInt());
- output["clientversion"] = std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion);
- if (!root["accept"].asBool()) {
- retval.type = error | close;
- output["accept"] = false;
- currentState = connectionpossible;
- } else {
- retval.type = print;
- output["accept"] = true;
- currentState = loginpossible;
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "login";
- if (!root["accept"].asBool()) {
- retval.type = error | close;
- output["error"] = root["error"].asString();
- output["accept"] = false;
- currentState = connectionpossible;
- } else {
- retval.type = print;
- output["error"] = "";
- output["accept"] = true;
- currentState = normal;
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleSignup(Json::Value root) {
- CmdRet retval;
- Json::Value output; // LOCALOUTPUT
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- output["command"] = "signup";
- if (!root["accept"].asBool()) {
- retval.type = error;
- output["error"] = root["error"].asString();
- output["accept"] = false;
- currentState = loginpossible;
- } else {
- retval.type = print;
- output["error"] = "";
- output["accept"] = true;
- currentState = normal;
- }
- retval.msg = output;
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleHead(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- Json::Value output;
- output["command"] = "head";
- output["file"] = root["file"];
- output["error"] = "Server reports: " + root["error"].asString();
- output["accept"] = false;
- retval.type = error;
- retval.msg = output;
- } else {
- retval.type = print;
- retval.msg = root;
- }
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleDeletefile(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- Json::Value output;
- output["command"] = "deletefile";
- output["file"] = root["file"];
- output["error"] = "Server reports: " + root["error"].asString();
- output["accept"] = false;
- retval.type = error;
- retval.msg = output;
- } else {
- retval.type = print;
- retval.msg = root;
- }
- return retval;
- }
- CmdMan::CmdRet CmdMan::handleDeleteme(Json::Value root) {
- CmdRet retval;
- DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
- if (!root["accept"].asBool()) {
- retval.type = error;
- } else {
- retval.type = close | print;
- currentState = connectionpossible;
- }
- retval.msg = root;
- return retval;
- }
|