///////////////////////////////////////////////////////////////////////// // ServerPrototype.cpp - Console App that processes incoming messages // // ver 1.0 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2018 // ///////////////////////////////////////////////////////////////////////// #include "ServerPrototype.h" #include "../FileSystem-Windows/FileSystemDemo/FileSystem.h" #include namespace MsgPassComm = MsgPassingCommunication; using namespace Repository; using namespace FileSystem; using Msg = MsgPassingCommunication::Message; Files Server::getFiles(const Repository::SearchPath& path) { return Directory::getFiles(path); } Dirs Server::getDirs(const Repository::SearchPath& path) { return Directory::getDirectories(path); } template void show(const T& t, const std::string& msg) { std::cout << "\n " << msg.c_str(); for (auto item : t) { std::cout << "\n " << item.c_str(); } } std::function echo = [](Msg msg) { Msg reply = msg; reply.to(msg.from()); reply.from(msg.to()); return reply; }; std::function getFiles = [](Msg msg) { Msg reply; reply.to(msg.from()); reply.from(msg.to()); reply.command("getFiles"); std::string path = msg.value("path"); if (path != "") { std::string searchPath = storageRoot; if (path != ".") searchPath = searchPath + "\\" + path; Files files = Server::getFiles(searchPath); size_t count = 0; for (auto item : files) { std::string countStr = Utilities::Converter::toString(++count); reply.attribute("file" + countStr, item); } } else { std::cout << "\n getFiles message did not define a path attribute"; } return reply; }; std::function getDirs = [](Msg msg) { Msg reply; reply.to(msg.from()); reply.from(msg.to()); reply.command("getDirs"); std::string path = msg.value("path"); if (path != "") { std::string searchPath = storageRoot; if (path != ".") searchPath = searchPath + "\\" + path; Files dirs = Server::getDirs(searchPath); size_t count = 0; for (auto item : dirs) { if (item != ".." && item != ".") { std::string countStr = Utilities::Converter::toString(++count); reply.attribute("dir" + countStr, item); } } } else { std::cout << "\n getDirs message did not define a path attribute"; } return reply; }; int main() { std::cout << "\n Testing Server Prototype"; std::cout << "\n =========================="; std::cout << "\n"; //StaticLogger<1>::attach(&std::cout); //StaticLogger<1>::start(); Server server(serverEndPoint, "ServerPrototype"); server.start(); std::cout << "\n testing getFiles and getDirs methods"; std::cout << "\n --------------------------------------"; Files files = server.getFiles(); show(files, "Files:"); Dirs dirs = server.getDirs(); show(dirs, "Dirs:"); std::cout << "\n"; std::cout << "\n testing message processing"; std::cout << "\n ----------------------------"; server.addMsgProc("echo", echo); server.addMsgProc("getFiles", getFiles); server.addMsgProc("getDirs", getDirs); server.addMsgProc("serverQuit", echo); server.processMessages(); Msg msg(serverEndPoint, serverEndPoint); // send to self msg.name("msgToSelf"); msg.command("echo"); msg.attribute("verbose", "show me"); server.postMessage(msg); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); msg.command("getFiles"); msg.remove("verbose"); msg.attributes()["path"] = storageRoot; server.postMessage(msg); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); msg.command("getDirs"); msg.attributes()["path"] = storageRoot; server.postMessage(msg); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::cout << "\n press enter to exit"; std::cin.get(); std::cout << "\n"; msg.command("serverQuit"); server.postMessage(msg); server.stop(); return 0; }