From 9ef9705490ece618b6cb3157ce075d8c6e67349e Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 06:25:00 +0300 Subject: [PATCH 01/36] added classes and methods prototypes --- .github/workflows/blank.yml | 48 ++++++++++++++++++++ .gitignore | 14 ++++++ CMakeLists.txt | 8 ++++ include/application.hpp | 64 ++++++++++++++++++++++++++ include/client.hpp | 21 +++++++++ include/request.hpp | 89 +++++++++++++++++++++++++++++++++++++ include/response.hpp | 73 ++++++++++++++++++++++++++++++ include/socket.hpp | 18 ++++++++ src/CMakeLists.txt | 4 ++ src/application.cpp | 24 ++++++++++ src/client.cpp | 15 +++++++ src/request.cpp | 34 ++++++++++++++ src/response.cpp | 38 ++++++++++++++++ src/socket.cpp | 14 ++++++ 14 files changed, 464 insertions(+) create mode 100644 .github/workflows/blank.yml create mode 100644 CMakeLists.txt create mode 100644 include/application.hpp create mode 100644 include/client.hpp create mode 100644 include/request.hpp create mode 100644 include/response.hpp create mode 100644 include/socket.hpp create mode 100644 src/CMakeLists.txt create mode 100644 src/application.cpp create mode 100644 src/client.cpp create mode 100644 src/request.cpp create mode 100644 src/response.cpp create mode 100644 src/socket.cpp diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml new file mode 100644 index 0000000..75441fd --- /dev/null +++ b/.github/workflows/blank.yml @@ -0,0 +1,48 @@ +name: CI + +on: + push: + branches: [ client ] + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: install cppcheck + run: sudo apt-get install -y cppcheck + + - name: install valgrind + run: sudo apt install valgrind + + - name: install gcovr and lcov + run: | + sudo apt install gcovr + sudo apt install lcov + + - name: run cppcheck + run: cd src && cppcheck *.cpp + + - name: build + run: | + mkdir build + cd build + cmake .. + make + + - name: test client lib + run: | + cd build + valgrind --leak-check=full ./test/client_tests + - name: coverage + run: | + cd build + make gcov + make lcov + - name: archive code coverage results + uses: actions/upload-artifact@v2 + with: + name: code-coverage-report_linear + path: build/test/lcoverage \ No newline at end of file diff --git a/.gitignore b/.gitignore index 259148f..3cd64ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,17 @@ +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +build/ +.vscode/ + # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5d729fa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.16.3) + +project(link_share) + +add_subdirectory(src) + +#enable_testing() +#add_subdirectory(test) \ No newline at end of file diff --git a/include/application.hpp b/include/application.hpp new file mode 100644 index 0000000..c329e35 --- /dev/null +++ b/include/application.hpp @@ -0,0 +1,64 @@ + +#include "client.hpp" +#include +#include + + +class UserInfo{ +public: + UserInfo(); + UserInfo(std::string nm, std::string psw); + ~UserInfo(); + +private: + std::string name; + std::string password; +}; + + +class Room{ +public: + Room(std::string roomName); + void addLink(std::string newLink); + void removeLink(std::string linkName); + void addParticipant(std::string newPart); + void removeParticipant(std::string partName); + std::string saveMask(std::string linkName); +private: + std::string roomName; + std::string roomHost; + std::vector participants; + std::vector links; +}; + + +class Link { +public: + Link(std::string name, std::string url, std::string tag); + ~Link(); + std::string addSnapshot(); + +private: + std::string linkname; + std::string url; + std::vector tags; + std::vector snapshotPaths; +}; + + + +class Application { +public: + Application(); + ~Application(); + /* void logIn(UserInfo inf); + void logOut(); + void createRoom(std::vector participants); + void addLink(std::string newLink); + void removeLink(std::string linkName); + */ +private: + Client client; + UserInfo info; + std::vector rooms; +}; \ No newline at end of file diff --git a/include/client.hpp b/include/client.hpp new file mode 100644 index 0000000..b96051b --- /dev/null +++ b/include/client.hpp @@ -0,0 +1,21 @@ + +#include "socket.hpp" + +#include +#include +#include + + +class Client { +public: + Client(); + ~Client(); + void connect(const std::string& host, int port); + void writeToServer(std::string req); + std::string readFromServer(); + void closeCon(); + +private: + Socket sock; +}; + diff --git a/include/request.hpp b/include/request.hpp new file mode 100644 index 0000000..2c7b42a --- /dev/null +++ b/include/request.hpp @@ -0,0 +1,89 @@ + +#include +#include +#include +#include + +class Request { +public: + virtual void buildRequestFromInput(std::istream s) = 0; + virtual std::string RequestToString() = 0; +}; + +class LogInReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: + std::string name; + std::string password; +}; + +class LogOutReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: +}; + +class CreateRoomReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: + std::string roomName; + std::string roomHost; + std::vector participants; +}; + +class RemoveRoomReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: + std::string name; +}; + +class AddLinkReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: + std::string linkName; + std::string url; + std::string tag; +}; + +class RemoveLinkReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: + std::string linkName; +}; + +class ArchiveLinkReq : public Request { +public: + void buildRequestFromInput(std::istream s) override; + std::string RequestToString() override; +private: + std::string linkName; +}; + + +using requestPtr = std::shared_ptr; + +class RequestHandler { +public: + RequestHandler(); + requestPtr HandleInput(std::string which); +private: + requestPtr _LogInReq; + requestPtr _LogOutReq; + requestPtr _CreateRoomReq; + requestPtr _RemoveRoomReq; + requestPtr _AddLinkReq; + requestPtr _RemoveLinkReq; + requestPtr _LinkReq; + requestPtr _ArchiveLinkReq; +}; \ No newline at end of file diff --git a/include/response.hpp b/include/response.hpp new file mode 100644 index 0000000..d7c59c5 --- /dev/null +++ b/include/response.hpp @@ -0,0 +1,73 @@ + +#include +#include +#include +#include +#include +#include + +class Response { +public: + virtual void buildResponse(std::string responseBody) = 0; + virtual void doLogic(const Request& req, Application& app) = 0; +}; + +class LogInResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +}; + +class LogOutResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +}; + +class CreateRoomResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +}; + +class RemoveRoomResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +}; + +class AddLinkResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +}; + +class RemoveLinkResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +}; + +class ArchiveLinkResp : public Response{ +public: + void buildResponse(std::string responseBody) override; + void doLogic(const Request& req, Application& app) override; +private: + std::string body; +}; + + +using responsePtr = std::shared_ptr; + +class ResponseHandler { +public: + ResponseHandler(); + responsePtr HandleInput(std::string resposeBody); +private: + responsePtr _LogInResp; + responsePtr _LogOutResp; + responsePtr _CreateRoomResp; + responsePtr _RemoveRoomResp; + responsePtr _AddLinkResp; + responsePtr _RemoveLinkResp; +}; \ No newline at end of file diff --git a/include/socket.hpp b/include/socket.hpp new file mode 100644 index 0000000..c60deba --- /dev/null +++ b/include/socket.hpp @@ -0,0 +1,18 @@ +#include // close() +#include +#include + +class Socket +{ +public: + Socket(); + Socket(int sd); + ~Socket(); + + void connect(const std::string& host, int port); + void send(const std::string& str); + std::string recv(); + void close(); +private: + int sd; +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..32de970 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library(client STATIC application.src client.cpp request.cpp response.cpp socket.src) +target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") + +add_definitions(-Wall -Werror -Wpedantic) \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp new file mode 100644 index 0000000..a0af291 --- /dev/null +++ b/src/application.cpp @@ -0,0 +1,24 @@ +#include "application.hpp" + + +UserInfo::UserInfo() {} +UserInfo::UserInfo(std::string nm, std::string psw) {} +UserInfo::~UserInfo() {} + + +Room::Room(std::string roomName) {} +void Room::addLink(std::string newLink) {} +void Room::removeLink(std::string linkName) {} +void Room::addParticipant(std::string newPart) {} +void Room::removeParticipant(std::string partName) {} +std::string Room::saveMask(std::string linkName) {} + + + +Link::Link(std::string name, std::string url, std::string tag) {} +Link::~Link() {} +std::string Link::addSnapshot() {} + + +Application::Application() {} +Application::~Application() {} \ No newline at end of file diff --git a/src/client.cpp b/src/client.cpp new file mode 100644 index 0000000..a56f921 --- /dev/null +++ b/src/client.cpp @@ -0,0 +1,15 @@ +#include "client.hpp" + +Client::Client() {} + +Client::~Client() {} + +void Client::connect(const std::string& host, int port) {} + +std::string Client::readFromServer() { return "we"; } + +void Client::writeToServer(std::string req) {} + +void Client::closeCon() {} + + diff --git a/src/request.cpp b/src/request.cpp new file mode 100644 index 0000000..c189506 --- /dev/null +++ b/src/request.cpp @@ -0,0 +1,34 @@ +#include "request.hpp" + + +void LogInReq::buildRequestFromInput(std::istream s) {} +std::string LogInReq::RequestToString() {return "a";} + + +void LogOutReq::buildRequestFromInput(std::istream s) {} +std::string LogOutReq::RequestToString() {return "a";} + + +void CreateRoomReq::buildRequestFromInput(std::istream s) {} +std::string CreateRoomReq::RequestToString() {return "a";} + + +void RemoveRoomReq::buildRequestFromInput(std::istream s) {} +std::string RemoveRoomReq::RequestToString() {return "a";} + + +void AddLinkReq::buildRequestFromInput(std::istream s) {} +std::string AddLinkReq::RequestToString() {return "a";} + + +void RemoveLinkReq::buildRequestFromInput(std::istream s) {} +std::string RemoveLinkReq::RequestToString() {return "a";} + + +void ArchiveLinkReq::buildRequestFromInput(std::istream s) {} +std::string ArchiveLinkReq::RequestToString() {return "a";} + + + +RequestHandler::RequestHandler() {} +requestPtr RequestHandler::HandleInput(std::string which) {return _LogInReq;} \ No newline at end of file diff --git a/src/response.cpp b/src/response.cpp new file mode 100644 index 0000000..77e49cc --- /dev/null +++ b/src/response.cpp @@ -0,0 +1,38 @@ + +#include "response.hpp" + + +void LogInResp::buildResponse(std::string responseBody) {} +void LogInResp::doLogic(const Request& req, Application& app) {} + + +void LogOutResp::buildResponse(std::string responseBody) {} +void LogOutResp::doLogic(const Request& req, Application& app) {} + + +void CreateRoomResp::buildResponse(std::string responseBody) {} +void CreateRoomResp::doLogic(const Request& req, Application& app) {} + + +void RemoveRoomResp::buildResponse(std::string responseBody) {} +void RemoveRoomResp::doLogic(const Request& req, Application& app) {} + + +void AddLinkResp::buildResponse(std::string responseBody) {} +void AddLinkResp::doLogic(const Request& req, Application& app) {} + + +void RemoveLinkResp::buildResponse(std::string responseBody) {} +void RemoveLinkResp::doLogic(const Request& req, Application& app) {} + + +void ArchiveLinkResp::buildResponse(std::string responseBody) {} +void ArchiveLinkResp::doLogic(const Request& req, Application& app) {} + + + + + +ResponseHandler::ResponseHandler() {} +responsePtr ResponseHandler::HandleInput(std::string resposeBody) {return _LogInResp;} + diff --git a/src/socket.cpp b/src/socket.cpp new file mode 100644 index 0000000..389f5f6 --- /dev/null +++ b/src/socket.cpp @@ -0,0 +1,14 @@ + +#include "socket.hpp" + + +Socket::Socket() {} + +Socket::Socket(int sd) {} + +Socket::~Socket() {} + +void Socket::connect(const std::string& host, int port) {} +void Socket::send(const std::string& str) {} +std::string Socket::recv() {} +void Socket::close() {} \ No newline at end of file From 87ddd0ed4cd6c2615f13f6309e69c7158beb41e9 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:22:05 +0300 Subject: [PATCH 02/36] added tests and reworked code --- .github/workflows/blank.yml | 22 ++++++++ client/CMakeLists.txt | 8 +++ client/include/application.hpp | 62 ++++++++++++++++++++++ client/include/client.hpp | 22 ++++++++ client/include/request.hpp | 97 ++++++++++++++++++++++++++++++++++ client/include/response.hpp | 80 ++++++++++++++++++++++++++++ client/include/socket.hpp | 21 ++++++++ client/src/CMakeLists.txt | 4 ++ client/src/application.cpp | 23 ++++++++ client/src/client.cpp | 15 ++++++ client/src/request.cpp | 35 ++++++++++++ client/src/response.cpp | 35 ++++++++++++ client/src/socket.cpp | 14 +++++ client/test/CMakeLists.txt | 60 +++++++++++++++++++++ client/test/application.cpp | 18 +++++++ client/test/main.cpp | 6 +++ client/test/request.cpp | 63 ++++++++++++++++++++++ client/test/response.cpp | 70 ++++++++++++++++++++++++ client/test/socket.cpp | 11 ++++ 19 files changed, 666 insertions(+) create mode 100644 client/CMakeLists.txt create mode 100644 client/include/application.hpp create mode 100644 client/include/client.hpp create mode 100644 client/include/request.hpp create mode 100644 client/include/response.hpp create mode 100644 client/include/socket.hpp create mode 100644 client/src/CMakeLists.txt create mode 100644 client/src/application.cpp create mode 100644 client/src/client.cpp create mode 100644 client/src/request.cpp create mode 100644 client/src/response.cpp create mode 100644 client/src/socket.cpp create mode 100644 client/test/CMakeLists.txt create mode 100644 client/test/application.cpp create mode 100644 client/test/main.cpp create mode 100644 client/test/request.cpp create mode 100644 client/test/response.cpp create mode 100644 client/test/socket.cpp diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 75441fd..bfe7635 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -27,15 +27,37 @@ jobs: - name: build run: | + cd client mkdir build cd build cmake .. make + - name: test socket + run: | + cd build + valgrind --leak-check=full ./test/socket_tests + + - name: test client lib + run: | + cd build + valgrind --leak-check=full ./test/response_tests + + - name: test client lib + run: | + cd build + valgrind --leak-check=full ./test/request_tests + - name: test client lib run: | cd build valgrind --leak-check=full ./test/client_tests + + - name: test client lib + run: | + cd build + valgrind --leak-check=full ./test/application_tests + - name: coverage run: | cd build diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt new file mode 100644 index 0000000..3808f68 --- /dev/null +++ b/client/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.16.3) + +project(link_share) + +add_subdirectory(src) + +enable_testing() +add_subdirectory(test) diff --git a/client/include/application.hpp b/client/include/application.hpp new file mode 100644 index 0000000..9e2bf7e --- /dev/null +++ b/client/include/application.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include "client.hpp" +#include +#include + + +class UserInfo{ +public: + UserInfo(); + UserInfo(std::string& nm, std::string& psw); + ~UserInfo(); + +private: + std::string name; + std::string password; +}; + + +class Link { +public: + Link(std::string& name, std::string& url, std::string& tag); + ~Link(); + std::string addSnapshot(); + +private: + std::string linkname; + std::string url; + std::vector tags; + std::vector snapshotPaths; +}; + + +class Room{ +public: + Room(std::string& roomName, std::string& roomId); + void addLink(std::string& newLink); + void removeLink(std::string& linkName); + void addParticipant(std::string& newPart); + void removeParticipant(std::string& partName); + std::string archiveLink(std::string& linkName); +private: + std::string roomName; + std::string roomId; + std::string roomHost; + std::vector participants; + std::vector links; +}; + + + + +class Application { +public: + Application(); + ~Application(); + +private: + Client client; + UserInfo info; + std::vector rooms; +}; \ No newline at end of file diff --git a/client/include/client.hpp b/client/include/client.hpp new file mode 100644 index 0000000..6eef1ea --- /dev/null +++ b/client/include/client.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "socket.hpp" + +#include +#include +#include + + +class Client { +public: + Client(); + ~Client(); + void connect(const std::string& host, int port); + void writeToServer(std::string& req); + std::string readFromServer(); + void closeCon(); + +private: + Socket sock; +}; + diff --git a/client/include/request.hpp b/client/include/request.hpp new file mode 100644 index 0000000..a5e95b2 --- /dev/null +++ b/client/include/request.hpp @@ -0,0 +1,97 @@ +#pragma once + +#include +#include +#include +#include + + +typedef enum ReqExitStatus { + REQ_SUCCESS, + REQ_FAILURE +}ReqExitStatus; + + +class Request { +public: + virtual ReqExitStatus buildRequestFromInput(std::istream& s) = 0; + virtual std::string RequestToString() = 0; +}; + +class LogInReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream& s) override; + std::string RequestToString() override; +private: + std::string name; + std::string password; +}; + +class LogOutReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream& s) override; + std::string RequestToString() override; +private: +}; + +class CreateRoomReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream& s) override; + std::string RequestToString() override; +private: + std::string roomName; + std::string roomHost; + std::vector participants; +}; + +class RemoveRoomReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream& s) override; + std::string RequestToString() override; +private: + std::string name; +}; + +class AddLinkReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream& s) override; + std::string RequestToString() override; +private: + std::string linkName; + std::string url; + std::string tag; +}; + +class RemoveLinkReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream&s) override; + std::string RequestToString() override; +private: + std::string linkName; +}; + +class ArchiveLinkReq : public Request { +public: + ReqExitStatus buildRequestFromInput(std::istream& s) override; + std::string RequestToString() override; +private: + std::string linkName; +}; + + +using requestPtr = std::shared_ptr; + +class RequestHandler { +public: + RequestHandler(); + requestPtr HandleInput(std::string& which); +private: + requestPtr _LogInReq; + requestPtr _LogOutReq; + requestPtr _CreateRoomReq; + requestPtr _RemoveRoomReq; + requestPtr _AddLinkReq; + requestPtr _RemoveLinkReq; + requestPtr _LinkReq; + requestPtr _ArchiveLinkReq; +}; \ No newline at end of file diff --git a/client/include/response.hpp b/client/include/response.hpp new file mode 100644 index 0000000..04dad42 --- /dev/null +++ b/client/include/response.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +typedef enum RespExitStatus { + RESP_SUCCESS, + RESP_FAILURE +}RespExitStatus; + + +class Response { +public: + virtual RespExitStatus buildResponse(std::string& responseBody) = 0; + virtual RespExitStatus doLogic(const Request& req, Application& app) = 0; +}; + +class LogInResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +}; + +class LogOutResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +}; + +class CreateRoomResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +}; + +class RemoveRoomResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +}; + +class AddLinkResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +}; + +class RemoveLinkResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +}; + +class ArchiveLinkResp : public Response{ +public: + RespExitStatus buildResponse(std::string& responseBody) override; + RespExitStatus doLogic(const Request& req, Application& app) override; +private: + std::string body; +}; + + +using responsePtr = std::shared_ptr; + +class ResponseHandler { +public: + ResponseHandler(); + responsePtr HandleInput(std::string& resposeBody); +private: + responsePtr _LogInResp; + responsePtr _LogOutResp; + responsePtr _CreateRoomResp; + responsePtr _RemoveRoomResp; + responsePtr _AddLinkResp; + responsePtr _RemoveLinkResp; +}; \ No newline at end of file diff --git a/client/include/socket.hpp b/client/include/socket.hpp new file mode 100644 index 0000000..34487d0 --- /dev/null +++ b/client/include/socket.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include // close() +#include +#include + + +class Socket +{ +public: + Socket(); + Socket(int _sd); + ~Socket(); + + void connect(const std::string& host, int port); + void send(const std::string& str); + std::string recv(); + void close(); +private: + int sd; +}; \ No newline at end of file diff --git a/client/src/CMakeLists.txt b/client/src/CMakeLists.txt new file mode 100644 index 0000000..2f0e5e5 --- /dev/null +++ b/client/src/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library(client STATIC application.cpp client.cpp request.cpp response.cpp socket.cpp) +target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") + +add_definitions(-Wall -Werror -Wpedantic) \ No newline at end of file diff --git a/client/src/application.cpp b/client/src/application.cpp new file mode 100644 index 0000000..52ee118 --- /dev/null +++ b/client/src/application.cpp @@ -0,0 +1,23 @@ +#include "application.hpp" + +UserInfo::UserInfo() : name(""), password("") {} +UserInfo::UserInfo(std::string& nm, std::string& psw) {} +UserInfo::~UserInfo() {} + + +Room::Room(std::string& roomName, std::string& roomId) {} +void Room::addLink(std::string& newLink) {} +void Room::removeLink(std::string& linkName) {} +void Room::addParticipant(std::string& newPart) {} +void Room::removeParticipant(std::string& partName) {} +std::string Room::archiveLink(std::string& linkName) {return "str";} + + + +Link::Link(std::string& name, std::string& url, std::string& tag) {} +Link::~Link() {} +std::string Link::addSnapshot() { return "str"; } + + +Application::Application() : client(), info(), rooms() {} +Application::~Application() {} \ No newline at end of file diff --git a/client/src/client.cpp b/client/src/client.cpp new file mode 100644 index 0000000..ea9ae72 --- /dev/null +++ b/client/src/client.cpp @@ -0,0 +1,15 @@ +#include "client.hpp" + +Client::Client() : sock() {} + +Client::~Client() {} + +void Client::connect(const std::string& host, int port) {} + +std::string Client::readFromServer() { return "we"; } + +void Client::writeToServer(std::string& req) {} + +void Client::closeCon() {} + + diff --git a/client/src/request.cpp b/client/src/request.cpp new file mode 100644 index 0000000..6446836 --- /dev/null +++ b/client/src/request.cpp @@ -0,0 +1,35 @@ + +#include "request.hpp" + + +ReqExitStatus LogInReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string LogInReq::RequestToString() {return "a";} + + +ReqExitStatus LogOutReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string LogOutReq::RequestToString() {return "a";} + + +ReqExitStatus CreateRoomReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string CreateRoomReq::RequestToString() {return "a";} + + +ReqExitStatus RemoveRoomReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string RemoveRoomReq::RequestToString() {return "a";} + + +ReqExitStatus AddLinkReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string AddLinkReq::RequestToString() {return "a";} + + +ReqExitStatus RemoveLinkReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string RemoveLinkReq::RequestToString() {return "a";} + + +ReqExitStatus ArchiveLinkReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} +std::string ArchiveLinkReq::RequestToString() {return "a";} + + + +RequestHandler::RequestHandler() {} +requestPtr RequestHandler::HandleInput(std::string& which) {return _LogInReq;} \ No newline at end of file diff --git a/client/src/response.cpp b/client/src/response.cpp new file mode 100644 index 0000000..e934b4f --- /dev/null +++ b/client/src/response.cpp @@ -0,0 +1,35 @@ + +#include "response.hpp" + + +RespExitStatus LogInResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus LogInResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +RespExitStatus LogOutResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus LogOutResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +RespExitStatus CreateRoomResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus CreateRoomResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +RespExitStatus RemoveRoomResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus RemoveRoomResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +RespExitStatus AddLinkResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus AddLinkResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +RespExitStatus RemoveLinkResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus RemoveLinkResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +RespExitStatus ArchiveLinkResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} +RespExitStatus ArchiveLinkResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} + + +ResponseHandler::ResponseHandler() {} +responsePtr ResponseHandler::HandleInput(std::string& resposeBody) {return _LogInResp;} + diff --git a/client/src/socket.cpp b/client/src/socket.cpp new file mode 100644 index 0000000..df1c82f --- /dev/null +++ b/client/src/socket.cpp @@ -0,0 +1,14 @@ + +#include "socket.hpp" + + +Socket::Socket() : sd(0) {} + +Socket::Socket(int _sd) : sd(_sd) {} + +Socket::~Socket() {} + +void Socket::connect(const std::string& host, int port) {} +void Socket::send(const std::string& str) {} +std::string Socket::recv() {return "str";} +void Socket::close() {} \ No newline at end of file diff --git a/client/test/CMakeLists.txt b/client/test/CMakeLists.txt new file mode 100644 index 0000000..6e2d7b2 --- /dev/null +++ b/client/test/CMakeLists.txt @@ -0,0 +1,60 @@ +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test) + + +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.8.0 +) + +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) +set(BUILD_GTEST ON CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(googletest) + +include_directories("${PROJECT_SOURCE_DIR}/include") + +file(GLOB sources "${PROJECT_SOURCE_DIR}/src/*.cpp") +file(GLOB tests "${PROJECT_SOURCE_DIR}/test/*.cpp") + +#add_definitions(-Wall -Werror -Wpedantic) + +foreach(file ${tests}) + set(name) + get_filename_component(name ${file} NAME_WE) + add_executable("${name}_tests" + ${sources} + ${file} + "${PROJECT_SOURCE_DIR}/test/main.cpp") + target_link_libraries("${name}_tests" gtest gtest_main) + add_test(NAME ${name} COMMAND "${name}_tests") +endforeach() + +add_definitions(-fprofile-arcs -ftest-coverage) + +add_custom_target(gcov + COMMAND ${CMAKE_MAKE_PROGRAM} test + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test +) +add_custom_command(TARGET gcov + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src/gcoverage +) + +add_custom_target(lcov + COMMAND mkdir -p lcoverage + ) +add_custom_command(TARGET lcov + COMMAND cd ${CMAKE_BINARY_DIR}/test + COMMAND lcov --capture --directory ${CMAKE_BINARY_DIR}/test/CMakeFiles/ --output-file lcoverage/main_coverage.info + COMMAND lcov --remove lcoverage/main_coverage.info '/usr/*' --output-file lcoverage/main_coverage.info + COMMAND lcov --remove lcoverage/main_coverage.info '${CMAKE_BINARY_DIR}/_deps/*' --output-file lcoverage/main_coverage.info + COMMAND genhtml lcoverage/main_coverage.info --output-directory lcoverage +) + +set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES gcoverage) + +target_link_libraries(gtest_main -fprofile-arcs) \ No newline at end of file diff --git a/client/test/application.cpp b/client/test/application.cpp new file mode 100644 index 0000000..d39a37b --- /dev/null +++ b/client/test/application.cpp @@ -0,0 +1,18 @@ +#include "gtest/gtest.h" + +#include "application.hpp" + + +TEST(RoomTest, archiveLinkTest) { + std::string name, id, link; + Room room(name, id); + + ASSERT_STREQ(room.archiveLink(link).c_str(), "str"); +} + +TEST(LinkTest, addSnapshot) { + std::string name, url, tag; + Link lk(name, url, tag); + + ASSERT_STREQ(lk.addSnapshot().c_str(), "str"); +} diff --git a/client/test/main.cpp b/client/test/main.cpp new file mode 100644 index 0000000..b42b721 --- /dev/null +++ b/client/test/main.cpp @@ -0,0 +1,6 @@ +#include "gtest/gtest.h" + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/client/test/request.cpp b/client/test/request.cpp new file mode 100644 index 0000000..2d31cb7 --- /dev/null +++ b/client/test/request.cpp @@ -0,0 +1,63 @@ +#include "gtest/gtest.h" + +#include "request.hpp" +#include +#include +#include + + +TEST(RequestTest, logInTest) { + std::stringstream is; + LogInReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, logOutTest) { + std::stringstream is; + LogOutReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, CreateRoomTest) { + std::stringstream is; + CreateRoomReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, RemoveRoomTest) { + std::stringstream is; + RemoveRoomReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, AddLinkTest) { + std::stringstream is; + AddLinkReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, RemoveLinkTest) { + std::stringstream is; + RemoveLinkReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, ArchiveLinkTest) { + std::stringstream is; + ArchiveLinkReq req; + + ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); +} + +TEST(RequestTest, RequestHandlerTest) { + std::string str; + RequestHandler handler; + + ASSERT_EQ(typeid(handler.HandleInput(str)), typeid(std::shared_ptr)); +} diff --git a/client/test/response.cpp b/client/test/response.cpp new file mode 100644 index 0000000..e058a2e --- /dev/null +++ b/client/test/response.cpp @@ -0,0 +1,70 @@ +#include "gtest/gtest.h" + +#include +#include "request.hpp" +#include "response.hpp" + + +TEST(ResponseTest, logInTest) { + Application app; + LogInReq req; + LogInResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, logOutTest) { + Application app; + LogOutReq req; + LogOutResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, CreateRoomTest) { + Application app; + CreateRoomReq req; + CreateRoomResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, RemoveRoomTest) { + Application app; + RemoveRoomReq req; + RemoveRoomResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, AddLinkTest) { + Application app; + AddLinkReq req; + AddLinkResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, RemoveLinkTest) { + Application app; + RemoveLinkReq req; + RemoveLinkResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, ArchiveLinkTest) { + Application app; + ArchiveLinkReq req; + ArchiveLinkResp resp; + + ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); +} + +TEST(ResponseTest, ResponseHandlerTest) { + std::string str; + ResponseHandler handler; + + ASSERT_EQ(typeid(handler.HandleInput(str)), typeid(std::shared_ptr)); +} + diff --git a/client/test/socket.cpp b/client/test/socket.cpp new file mode 100644 index 0000000..a5e1a52 --- /dev/null +++ b/client/test/socket.cpp @@ -0,0 +1,11 @@ + +#include "gtest/gtest.h" + +#include "socket.hpp" + + +TEST(SocketTest, RecvTest) { + Socket sock; + + ASSERT_STREQ(sock.recv().c_str(), "str"); +} \ No newline at end of file From 37715c5dd3684b7f6103165e1e2ff016d46e32cf Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:26:40 +0300 Subject: [PATCH 03/36] fixed blank.yml --- .github/workflows/blank.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index bfe7635..f4b63e6 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -38,22 +38,22 @@ jobs: cd build valgrind --leak-check=full ./test/socket_tests - - name: test client lib + - name: test response run: | cd build valgrind --leak-check=full ./test/response_tests - - name: test client lib + - name: test request run: | cd build valgrind --leak-check=full ./test/request_tests - - name: test client lib + - name: test client run: | cd build valgrind --leak-check=full ./test/client_tests - - name: test client lib + - name: test application run: | cd build valgrind --leak-check=full ./test/application_tests From 7fc3ae401006a45b33a28b318f99759e099a11ac Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:33:11 +0300 Subject: [PATCH 04/36] fixed blank.yml 2 --- .github/workflows/blank.yml | 2 +- include/application.hpp | 64 -------------------------- include/client.hpp | 21 --------- include/request.hpp | 89 ------------------------------------- include/response.hpp | 73 ------------------------------ include/socket.hpp | 18 -------- src/CMakeLists.txt | 4 -- src/application.cpp | 24 ---------- src/client.cpp | 15 ------- src/request.cpp | 34 -------------- src/response.cpp | 38 ---------------- src/socket.cpp | 14 ------ 12 files changed, 1 insertion(+), 395 deletions(-) delete mode 100644 include/application.hpp delete mode 100644 include/client.hpp delete mode 100644 include/request.hpp delete mode 100644 include/response.hpp delete mode 100644 include/socket.hpp delete mode 100644 src/CMakeLists.txt delete mode 100644 src/application.cpp delete mode 100644 src/client.cpp delete mode 100644 src/request.cpp delete mode 100644 src/response.cpp delete mode 100644 src/socket.cpp diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index f4b63e6..3e7a8cc 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -43,7 +43,7 @@ jobs: cd build valgrind --leak-check=full ./test/response_tests - - name: test request + - name: test request run: | cd build valgrind --leak-check=full ./test/request_tests diff --git a/include/application.hpp b/include/application.hpp deleted file mode 100644 index c329e35..0000000 --- a/include/application.hpp +++ /dev/null @@ -1,64 +0,0 @@ - -#include "client.hpp" -#include -#include - - -class UserInfo{ -public: - UserInfo(); - UserInfo(std::string nm, std::string psw); - ~UserInfo(); - -private: - std::string name; - std::string password; -}; - - -class Room{ -public: - Room(std::string roomName); - void addLink(std::string newLink); - void removeLink(std::string linkName); - void addParticipant(std::string newPart); - void removeParticipant(std::string partName); - std::string saveMask(std::string linkName); -private: - std::string roomName; - std::string roomHost; - std::vector participants; - std::vector links; -}; - - -class Link { -public: - Link(std::string name, std::string url, std::string tag); - ~Link(); - std::string addSnapshot(); - -private: - std::string linkname; - std::string url; - std::vector tags; - std::vector snapshotPaths; -}; - - - -class Application { -public: - Application(); - ~Application(); - /* void logIn(UserInfo inf); - void logOut(); - void createRoom(std::vector participants); - void addLink(std::string newLink); - void removeLink(std::string linkName); - */ -private: - Client client; - UserInfo info; - std::vector rooms; -}; \ No newline at end of file diff --git a/include/client.hpp b/include/client.hpp deleted file mode 100644 index b96051b..0000000 --- a/include/client.hpp +++ /dev/null @@ -1,21 +0,0 @@ - -#include "socket.hpp" - -#include -#include -#include - - -class Client { -public: - Client(); - ~Client(); - void connect(const std::string& host, int port); - void writeToServer(std::string req); - std::string readFromServer(); - void closeCon(); - -private: - Socket sock; -}; - diff --git a/include/request.hpp b/include/request.hpp deleted file mode 100644 index 2c7b42a..0000000 --- a/include/request.hpp +++ /dev/null @@ -1,89 +0,0 @@ - -#include -#include -#include -#include - -class Request { -public: - virtual void buildRequestFromInput(std::istream s) = 0; - virtual std::string RequestToString() = 0; -}; - -class LogInReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: - std::string name; - std::string password; -}; - -class LogOutReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: -}; - -class CreateRoomReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: - std::string roomName; - std::string roomHost; - std::vector participants; -}; - -class RemoveRoomReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: - std::string name; -}; - -class AddLinkReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: - std::string linkName; - std::string url; - std::string tag; -}; - -class RemoveLinkReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: - std::string linkName; -}; - -class ArchiveLinkReq : public Request { -public: - void buildRequestFromInput(std::istream s) override; - std::string RequestToString() override; -private: - std::string linkName; -}; - - -using requestPtr = std::shared_ptr; - -class RequestHandler { -public: - RequestHandler(); - requestPtr HandleInput(std::string which); -private: - requestPtr _LogInReq; - requestPtr _LogOutReq; - requestPtr _CreateRoomReq; - requestPtr _RemoveRoomReq; - requestPtr _AddLinkReq; - requestPtr _RemoveLinkReq; - requestPtr _LinkReq; - requestPtr _ArchiveLinkReq; -}; \ No newline at end of file diff --git a/include/response.hpp b/include/response.hpp deleted file mode 100644 index d7c59c5..0000000 --- a/include/response.hpp +++ /dev/null @@ -1,73 +0,0 @@ - -#include -#include -#include -#include -#include -#include - -class Response { -public: - virtual void buildResponse(std::string responseBody) = 0; - virtual void doLogic(const Request& req, Application& app) = 0; -}; - -class LogInResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -}; - -class LogOutResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -}; - -class CreateRoomResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -}; - -class RemoveRoomResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -}; - -class AddLinkResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -}; - -class RemoveLinkResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -}; - -class ArchiveLinkResp : public Response{ -public: - void buildResponse(std::string responseBody) override; - void doLogic(const Request& req, Application& app) override; -private: - std::string body; -}; - - -using responsePtr = std::shared_ptr; - -class ResponseHandler { -public: - ResponseHandler(); - responsePtr HandleInput(std::string resposeBody); -private: - responsePtr _LogInResp; - responsePtr _LogOutResp; - responsePtr _CreateRoomResp; - responsePtr _RemoveRoomResp; - responsePtr _AddLinkResp; - responsePtr _RemoveLinkResp; -}; \ No newline at end of file diff --git a/include/socket.hpp b/include/socket.hpp deleted file mode 100644 index c60deba..0000000 --- a/include/socket.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#include // close() -#include -#include - -class Socket -{ -public: - Socket(); - Socket(int sd); - ~Socket(); - - void connect(const std::string& host, int port); - void send(const std::string& str); - std::string recv(); - void close(); -private: - int sd; -}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index 32de970..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -add_library(client STATIC application.src client.cpp request.cpp response.cpp socket.src) -target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") - -add_definitions(-Wall -Werror -Wpedantic) \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp deleted file mode 100644 index a0af291..0000000 --- a/src/application.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "application.hpp" - - -UserInfo::UserInfo() {} -UserInfo::UserInfo(std::string nm, std::string psw) {} -UserInfo::~UserInfo() {} - - -Room::Room(std::string roomName) {} -void Room::addLink(std::string newLink) {} -void Room::removeLink(std::string linkName) {} -void Room::addParticipant(std::string newPart) {} -void Room::removeParticipant(std::string partName) {} -std::string Room::saveMask(std::string linkName) {} - - - -Link::Link(std::string name, std::string url, std::string tag) {} -Link::~Link() {} -std::string Link::addSnapshot() {} - - -Application::Application() {} -Application::~Application() {} \ No newline at end of file diff --git a/src/client.cpp b/src/client.cpp deleted file mode 100644 index a56f921..0000000 --- a/src/client.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "client.hpp" - -Client::Client() {} - -Client::~Client() {} - -void Client::connect(const std::string& host, int port) {} - -std::string Client::readFromServer() { return "we"; } - -void Client::writeToServer(std::string req) {} - -void Client::closeCon() {} - - diff --git a/src/request.cpp b/src/request.cpp deleted file mode 100644 index c189506..0000000 --- a/src/request.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "request.hpp" - - -void LogInReq::buildRequestFromInput(std::istream s) {} -std::string LogInReq::RequestToString() {return "a";} - - -void LogOutReq::buildRequestFromInput(std::istream s) {} -std::string LogOutReq::RequestToString() {return "a";} - - -void CreateRoomReq::buildRequestFromInput(std::istream s) {} -std::string CreateRoomReq::RequestToString() {return "a";} - - -void RemoveRoomReq::buildRequestFromInput(std::istream s) {} -std::string RemoveRoomReq::RequestToString() {return "a";} - - -void AddLinkReq::buildRequestFromInput(std::istream s) {} -std::string AddLinkReq::RequestToString() {return "a";} - - -void RemoveLinkReq::buildRequestFromInput(std::istream s) {} -std::string RemoveLinkReq::RequestToString() {return "a";} - - -void ArchiveLinkReq::buildRequestFromInput(std::istream s) {} -std::string ArchiveLinkReq::RequestToString() {return "a";} - - - -RequestHandler::RequestHandler() {} -requestPtr RequestHandler::HandleInput(std::string which) {return _LogInReq;} \ No newline at end of file diff --git a/src/response.cpp b/src/response.cpp deleted file mode 100644 index 77e49cc..0000000 --- a/src/response.cpp +++ /dev/null @@ -1,38 +0,0 @@ - -#include "response.hpp" - - -void LogInResp::buildResponse(std::string responseBody) {} -void LogInResp::doLogic(const Request& req, Application& app) {} - - -void LogOutResp::buildResponse(std::string responseBody) {} -void LogOutResp::doLogic(const Request& req, Application& app) {} - - -void CreateRoomResp::buildResponse(std::string responseBody) {} -void CreateRoomResp::doLogic(const Request& req, Application& app) {} - - -void RemoveRoomResp::buildResponse(std::string responseBody) {} -void RemoveRoomResp::doLogic(const Request& req, Application& app) {} - - -void AddLinkResp::buildResponse(std::string responseBody) {} -void AddLinkResp::doLogic(const Request& req, Application& app) {} - - -void RemoveLinkResp::buildResponse(std::string responseBody) {} -void RemoveLinkResp::doLogic(const Request& req, Application& app) {} - - -void ArchiveLinkResp::buildResponse(std::string responseBody) {} -void ArchiveLinkResp::doLogic(const Request& req, Application& app) {} - - - - - -ResponseHandler::ResponseHandler() {} -responsePtr ResponseHandler::HandleInput(std::string resposeBody) {return _LogInResp;} - diff --git a/src/socket.cpp b/src/socket.cpp deleted file mode 100644 index 389f5f6..0000000 --- a/src/socket.cpp +++ /dev/null @@ -1,14 +0,0 @@ - -#include "socket.hpp" - - -Socket::Socket() {} - -Socket::Socket(int sd) {} - -Socket::~Socket() {} - -void Socket::connect(const std::string& host, int port) {} -void Socket::send(const std::string& str) {} -std::string Socket::recv() {} -void Socket::close() {} \ No newline at end of file From db999d73c4e45172eb47fbca1339e689099504aa Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:37:25 +0300 Subject: [PATCH 05/36] fixed blank.yml 3 --- .github/workflows/blank.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 3e7a8cc..b6876c5 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -1,4 +1,4 @@ -name: CI +name: CI_client on: push: @@ -23,7 +23,7 @@ jobs: sudo apt install lcov - name: run cppcheck - run: cd src && cppcheck *.cpp + run: cd client/src && cppcheck *.cpp - name: build run: | @@ -35,36 +35,36 @@ jobs: - name: test socket run: | - cd build + cd client/build valgrind --leak-check=full ./test/socket_tests - name: test response run: | - cd build + cd client/build valgrind --leak-check=full ./test/response_tests - name: test request run: | - cd build + cd client/build valgrind --leak-check=full ./test/request_tests - name: test client run: | - cd build + cd client/build valgrind --leak-check=full ./test/client_tests - name: test application run: | - cd build + cd client/build valgrind --leak-check=full ./test/application_tests - name: coverage run: | - cd build + cd client/build make gcov make lcov - name: archive code coverage results uses: actions/upload-artifact@v2 with: name: code-coverage-report_linear - path: build/test/lcoverage \ No newline at end of file + path: client/build/test/lcoverage \ No newline at end of file From 82bde2568a4a13c0e305e9832972479cfeae1788 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:40:03 +0300 Subject: [PATCH 06/36] fixed blank.yml 4 --- .github/workflows/blank.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index b6876c5..2e4a93a 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -36,27 +36,27 @@ jobs: - name: test socket run: | cd client/build - valgrind --leak-check=full ./test/socket_tests + valgrind --leak-check=full ./client/test/socket_tests - name: test response run: | cd client/build - valgrind --leak-check=full ./test/response_tests + valgrind --leak-check=full ./client/test/response_tests - name: test request run: | cd client/build - valgrind --leak-check=full ./test/request_tests + valgrind --leak-check=full ./client/test/request_tests - name: test client run: | cd client/build - valgrind --leak-check=full ./test/client_tests + valgrind --leak-check=full ./client/test/client_tests - name: test application run: | cd client/build - valgrind --leak-check=full ./test/application_tests + valgrind --leak-check=full ./client/test/application_tests - name: coverage run: | From 6531e161cbcc4820a8c0f8e17615f252cba4b256 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:47:40 +0300 Subject: [PATCH 07/36] fixed blank.yml 5 --- .github/workflows/blank.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 2e4a93a..aa3967c 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -36,27 +36,27 @@ jobs: - name: test socket run: | cd client/build - valgrind --leak-check=full ./client/test/socket_tests + valgrind --leak-check=full ./client/build/test/socket_tests - name: test response run: | cd client/build - valgrind --leak-check=full ./client/test/response_tests + valgrind --leak-check=full ./client/build/test/response_tests - name: test request run: | cd client/build - valgrind --leak-check=full ./client/test/request_tests + valgrind --leak-check=full ./client/build/test/request_tests - name: test client run: | cd client/build - valgrind --leak-check=full ./client/test/client_tests + valgrind --leak-check=full ./client/build/test/client_tests - name: test application run: | cd client/build - valgrind --leak-check=full ./client/test/application_tests + valgrind --leak-check=full ./client/build/test/application_tests - name: coverage run: | From ebfa8db78905ea1ca4877d8d74eb811f7a907ae1 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:51:17 +0300 Subject: [PATCH 08/36] fixed blank.yml 6 --- .github/workflows/blank.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index aa3967c..b6876c5 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -36,27 +36,27 @@ jobs: - name: test socket run: | cd client/build - valgrind --leak-check=full ./client/build/test/socket_tests + valgrind --leak-check=full ./test/socket_tests - name: test response run: | cd client/build - valgrind --leak-check=full ./client/build/test/response_tests + valgrind --leak-check=full ./test/response_tests - name: test request run: | cd client/build - valgrind --leak-check=full ./client/build/test/request_tests + valgrind --leak-check=full ./test/request_tests - name: test client run: | cd client/build - valgrind --leak-check=full ./client/build/test/client_tests + valgrind --leak-check=full ./test/client_tests - name: test application run: | cd client/build - valgrind --leak-check=full ./client/build/test/application_tests + valgrind --leak-check=full ./test/application_tests - name: coverage run: | From 24227bb3894378022340ae64b3edd21caeea1339 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 16:54:18 +0300 Subject: [PATCH 09/36] fixed blank.yml 7 --- .github/workflows/blank.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index b6876c5..a10cd75 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -48,10 +48,10 @@ jobs: cd client/build valgrind --leak-check=full ./test/request_tests - - name: test client - run: | - cd client/build - valgrind --leak-check=full ./test/client_tests + #- name: test client + # run: | + # cd client/build + # valgrind --leak-check=full ./test/client_tests - name: test application run: | From 076bc9eac60be4a218c785a9fc4ffc131ed74a77 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 17:36:41 +0300 Subject: [PATCH 10/36] removed old Cmakelist --- CMakeLists.txt | 8 -------- client/test/client.cpp | 13 +++++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) delete mode 100644 CMakeLists.txt create mode 100644 client/test/client.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 5d729fa..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.16.3) - -project(link_share) - -add_subdirectory(src) - -#enable_testing() -#add_subdirectory(test) \ No newline at end of file diff --git a/client/test/client.cpp b/client/test/client.cpp new file mode 100644 index 0000000..8207379 --- /dev/null +++ b/client/test/client.cpp @@ -0,0 +1,13 @@ +#include "gtest/gtest.h" + +#include +#include "client.hpp" +#include "socket.hpp" + + + +TEST(ClientTest, ReadFromServerTest) { + Client client; + + ASSERT_EQ(client.readFromServer(), "str"); +} From b8fd891dcb774e0eb4ead4d94862f801d12fdfa4 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 17:38:07 +0300 Subject: [PATCH 11/36] fixed blank.yml 8 --- .github/workflows/blank.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index a10cd75..b6876c5 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -48,10 +48,10 @@ jobs: cd client/build valgrind --leak-check=full ./test/request_tests - #- name: test client - # run: | - # cd client/build - # valgrind --leak-check=full ./test/client_tests + - name: test client + run: | + cd client/build + valgrind --leak-check=full ./test/client_tests - name: test application run: | From 225299db145767dd2cfa766dba7eaa0dbc762089 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 17:40:57 +0300 Subject: [PATCH 12/36] some changes --- src/client.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/client.cpp diff --git a/src/client.cpp b/src/client.cpp new file mode 100644 index 0000000..dea24d0 --- /dev/null +++ b/src/client.cpp @@ -0,0 +1,13 @@ +#include "client.hpp" + +Client::Client() : sock() {} + +Client::~Client() {} + +void Client::connect(const std::string& host, int port) {} + +std::string Client::readFromServer() { return "str"; } + +void Client::writeToServer(std::string& req) {} + +void Client::closeCon() {} From 6de03f0fdad401857293001b7fe7a3f993accba8 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 12 Nov 2020 17:47:51 +0300 Subject: [PATCH 13/36] some changes2 --- client/src/client.cpp | 2 +- src/client.cpp | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 src/client.cpp diff --git a/client/src/client.cpp b/client/src/client.cpp index ea9ae72..9a95756 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -6,7 +6,7 @@ Client::~Client() {} void Client::connect(const std::string& host, int port) {} -std::string Client::readFromServer() { return "we"; } +std::string Client::readFromServer() { return "str"; } void Client::writeToServer(std::string& req) {} diff --git a/src/client.cpp b/src/client.cpp deleted file mode 100644 index dea24d0..0000000 --- a/src/client.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "client.hpp" - -Client::Client() : sock() {} - -Client::~Client() {} - -void Client::connect(const std::string& host, int port) {} - -std::string Client::readFromServer() { return "str"; } - -void Client::writeToServer(std::string& req) {} - -void Client::closeCon() {} From 985dd79ca46f57e8125cbeabded4b3fa63af8c89 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 10 Dec 2020 17:22:45 +0300 Subject: [PATCH 14/36] tried to implement code --- client/CMakeLists.txt | 2 +- client/include/application.hpp | 62 -------------- client/include/client.hpp | 16 ++-- client/include/link.hpp | 16 ++++ client/include/model.hpp | 21 +++++ client/include/presenter.hpp | 18 ++++ client/include/request.hpp | 97 --------------------- client/include/requestForm.hpp | 33 +++++++ client/include/requestHandler.hpp | 99 +++++++++++++++++++++ client/include/requestManager.hpp | 22 +++++ client/include/response.hpp | 80 ----------------- client/include/room.hpp | 29 +++++++ client/include/socket.hpp | 22 +++-- client/include/userinfo.hpp | 14 +++ client/include/view.hpp | 19 ++++ client/src/CMakeLists.txt | 8 +- client/src/application.cpp | 23 ----- client/src/client.cpp | 18 ++-- client/src/link.cpp | 37 ++++++++ client/src/main.cpp | 8 ++ client/src/model.cpp | 115 +++++++++++++++++++++++++ client/src/presenter.cpp | 17 ++++ client/src/request.cpp | 35 -------- client/src/requestForm.cpp | 0 client/src/requestHandler.cpp | 138 ++++++++++++++++++++++++++++++ client/src/requestManager.cpp | 45 ++++++++++ client/src/response.cpp | 35 -------- client/src/room.cpp | 79 +++++++++++++++++ client/src/socket.cpp | 99 +++++++++++++++++++-- client/src/userinfo.cpp | 6 ++ client/src/view.cpp | 114 ++++++++++++++++++++++++ client/test/application.cpp | 16 +--- client/test/client.cpp | 4 +- client/test/main.cpp | 2 +- client/test/request.cpp | 37 ++++---- client/test/response.cpp | 70 --------------- client/test/socket.cpp | 6 +- 37 files changed, 984 insertions(+), 478 deletions(-) delete mode 100644 client/include/application.hpp create mode 100644 client/include/link.hpp create mode 100644 client/include/model.hpp create mode 100644 client/include/presenter.hpp delete mode 100644 client/include/request.hpp create mode 100644 client/include/requestForm.hpp create mode 100644 client/include/requestHandler.hpp create mode 100644 client/include/requestManager.hpp delete mode 100644 client/include/response.hpp create mode 100644 client/include/room.hpp create mode 100644 client/include/userinfo.hpp create mode 100644 client/include/view.hpp delete mode 100644 client/src/application.cpp create mode 100644 client/src/link.cpp create mode 100644 client/src/main.cpp create mode 100644 client/src/model.cpp create mode 100644 client/src/presenter.cpp delete mode 100644 client/src/request.cpp create mode 100644 client/src/requestForm.cpp create mode 100644 client/src/requestHandler.cpp create mode 100644 client/src/requestManager.cpp delete mode 100644 client/src/response.cpp create mode 100644 client/src/room.cpp create mode 100644 client/src/userinfo.cpp create mode 100644 client/src/view.cpp delete mode 100644 client/test/response.cpp diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 3808f68..f78872f 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -5,4 +5,4 @@ project(link_share) add_subdirectory(src) enable_testing() -add_subdirectory(test) +#add_subdirectory(test) diff --git a/client/include/application.hpp b/client/include/application.hpp deleted file mode 100644 index 9e2bf7e..0000000 --- a/client/include/application.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#include "client.hpp" -#include -#include - - -class UserInfo{ -public: - UserInfo(); - UserInfo(std::string& nm, std::string& psw); - ~UserInfo(); - -private: - std::string name; - std::string password; -}; - - -class Link { -public: - Link(std::string& name, std::string& url, std::string& tag); - ~Link(); - std::string addSnapshot(); - -private: - std::string linkname; - std::string url; - std::vector tags; - std::vector snapshotPaths; -}; - - -class Room{ -public: - Room(std::string& roomName, std::string& roomId); - void addLink(std::string& newLink); - void removeLink(std::string& linkName); - void addParticipant(std::string& newPart); - void removeParticipant(std::string& partName); - std::string archiveLink(std::string& linkName); -private: - std::string roomName; - std::string roomId; - std::string roomHost; - std::vector participants; - std::vector links; -}; - - - - -class Application { -public: - Application(); - ~Application(); - -private: - Client client; - UserInfo info; - std::vector rooms; -}; \ No newline at end of file diff --git a/client/include/client.hpp b/client/include/client.hpp index 6eef1ea..5d02d50 100644 --- a/client/include/client.hpp +++ b/client/include/client.hpp @@ -1,22 +1,22 @@ #pragma once +#include +#include #include "socket.hpp" +#include "socket.hpp" -#include -#include -#include class Client { public: - Client(); - ~Client(); - void connect(const std::string& host, int port); + Client(const std::string& _host, int _port); + ~Client() {} + void Connect(); void writeToServer(std::string& req); std::string readFromServer(); - void closeCon(); - private: + const std::string& host; + int port; Socket sock; }; diff --git a/client/include/link.hpp b/client/include/link.hpp new file mode 100644 index 0000000..ab29643 --- /dev/null +++ b/client/include/link.hpp @@ -0,0 +1,16 @@ +#pragma once +#include + +class LinkImpl; + +class Link { +public: + Link(std::string& name, std::string& url); + ~Link(); + const std::string GetLinkName(); + void addSnapshot(std::string& path); + +private: + std::shared_ptr linkImpl; +}; + diff --git a/client/include/model.hpp b/client/include/model.hpp new file mode 100644 index 0000000..d2ed7b0 --- /dev/null +++ b/client/include/model.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include "client.hpp" +#include "requestForm.hpp" + + +class ModelImpl; + + +class Model { +public: + Model(); + ~Model(); + std::string GetMainRoomInfo(); + void PassAction(std::string& action); + +private: + std::shared_ptr modelImpl; +}; \ No newline at end of file diff --git a/client/include/presenter.hpp b/client/include/presenter.hpp new file mode 100644 index 0000000..93f5534 --- /dev/null +++ b/client/include/presenter.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "model.hpp" +#include "view.hpp" + +class Presenter { +public: + Presenter(); + Presenter(Presenter& pr) = delete; + Presenter& operator=(Presenter& pr) = delete; + ~Presenter(); + void run(); +private: + Model model; + ConsoleView view; +}; \ No newline at end of file diff --git a/client/include/request.hpp b/client/include/request.hpp deleted file mode 100644 index a5e95b2..0000000 --- a/client/include/request.hpp +++ /dev/null @@ -1,97 +0,0 @@ -#pragma once - -#include -#include -#include -#include - - -typedef enum ReqExitStatus { - REQ_SUCCESS, - REQ_FAILURE -}ReqExitStatus; - - -class Request { -public: - virtual ReqExitStatus buildRequestFromInput(std::istream& s) = 0; - virtual std::string RequestToString() = 0; -}; - -class LogInReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream& s) override; - std::string RequestToString() override; -private: - std::string name; - std::string password; -}; - -class LogOutReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream& s) override; - std::string RequestToString() override; -private: -}; - -class CreateRoomReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream& s) override; - std::string RequestToString() override; -private: - std::string roomName; - std::string roomHost; - std::vector participants; -}; - -class RemoveRoomReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream& s) override; - std::string RequestToString() override; -private: - std::string name; -}; - -class AddLinkReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream& s) override; - std::string RequestToString() override; -private: - std::string linkName; - std::string url; - std::string tag; -}; - -class RemoveLinkReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream&s) override; - std::string RequestToString() override; -private: - std::string linkName; -}; - -class ArchiveLinkReq : public Request { -public: - ReqExitStatus buildRequestFromInput(std::istream& s) override; - std::string RequestToString() override; -private: - std::string linkName; -}; - - -using requestPtr = std::shared_ptr; - -class RequestHandler { -public: - RequestHandler(); - requestPtr HandleInput(std::string& which); -private: - requestPtr _LogInReq; - requestPtr _LogOutReq; - requestPtr _CreateRoomReq; - requestPtr _RemoveRoomReq; - requestPtr _AddLinkReq; - requestPtr _RemoveLinkReq; - requestPtr _LinkReq; - requestPtr _ArchiveLinkReq; -}; \ No newline at end of file diff --git a/client/include/requestForm.hpp b/client/include/requestForm.hpp new file mode 100644 index 0000000..24687fd --- /dev/null +++ b/client/include/requestForm.hpp @@ -0,0 +1,33 @@ +#pragma once +#include + +struct RequestBody {std::string ff = "AAAAAAA";}; + +struct RequestForm { + struct RequestBody Body; + + int Type; +}; + + +struct AddLinkBody : public RequestBody { + std::string linkName; + std::string url; +}; + +struct RemoveLinkBody : public RequestBody { + std::string linkName; +}; + +struct ArchiveLinkBody : public RequestBody { + std::string linkName; +}; + +struct CreateRoomBody : public RequestBody { + std::string roomName; + std::string roomHost; +}; + +struct RemoveRoomBody : public RequestBody { + std::string roomName; +}; \ No newline at end of file diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp new file mode 100644 index 0000000..1319200 --- /dev/null +++ b/client/include/requestHandler.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include +#include +#include +#include + +#include "model.hpp" + +typedef enum ExitStatus { + SUCCESS, + FAILURE +} ExitStatus; + + +class RequestHandler { +public: + virtual ExitStatus FillRequest(std::string action, Model& model) = 0; + virtual ExitStatus HandleResponse(std::string& responseBody); + virtual ExitStatus DoLogic(Model& app) = 0; + std::string& GetRequestToSend(); +protected: + std::string requestToSend; +}; + +class AddUsersReqHandler : public RequestHandler { +public: + AddUsersReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::vector users; +}; + +class RemoveUsersReqHandler : public RequestHandler { +public: + RemoveUsersReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: +std::vector users; + +}; + +class AddLinkReqHandler : public RequestHandler { +public: + AddLinkReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string linkName; + std::string url; +}; + +class RemoveLinkReqHandler : public RequestHandler { +public: + RemoveLinkReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string linkName; +}; + +class ArchiveLinkReqHandler : public RequestHandler { +public: + ArchiveLinkReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string linkName; + std::string body; +}; + +class CreateRoomReqHandler : public RequestHandler { +public: + CreateRoomReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string roomName; + std::string roomHost; +}; + +class RemoveRoomReqHandler : public RequestHandler { +public: + RemoveRoomReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string roomName; + std::string roomHost; +}; diff --git a/client/include/requestManager.hpp b/client/include/requestManager.hpp new file mode 100644 index 0000000..33c28ef --- /dev/null +++ b/client/include/requestManager.hpp @@ -0,0 +1,22 @@ +#pragma once + + +#include +#include +#include + + +class RequestManagerImpl; +class Model; + +class RequestManager { +public: + RequestManager(); + ~RequestManager(); + RequestManager& operator=(RequestManager& manager) = delete; + RequestManager(RequestManager& manager) = delete; + /* void HandleInput(std::string& action, Model& model); */ +private: + void addRequest(std::string& action); + std::shared_ptr managerImpl; +}; \ No newline at end of file diff --git a/client/include/response.hpp b/client/include/response.hpp deleted file mode 100644 index 04dad42..0000000 --- a/client/include/response.hpp +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -typedef enum RespExitStatus { - RESP_SUCCESS, - RESP_FAILURE -}RespExitStatus; - - -class Response { -public: - virtual RespExitStatus buildResponse(std::string& responseBody) = 0; - virtual RespExitStatus doLogic(const Request& req, Application& app) = 0; -}; - -class LogInResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -}; - -class LogOutResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -}; - -class CreateRoomResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -}; - -class RemoveRoomResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -}; - -class AddLinkResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -}; - -class RemoveLinkResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -}; - -class ArchiveLinkResp : public Response{ -public: - RespExitStatus buildResponse(std::string& responseBody) override; - RespExitStatus doLogic(const Request& req, Application& app) override; -private: - std::string body; -}; - - -using responsePtr = std::shared_ptr; - -class ResponseHandler { -public: - ResponseHandler(); - responsePtr HandleInput(std::string& resposeBody); -private: - responsePtr _LogInResp; - responsePtr _LogOutResp; - responsePtr _CreateRoomResp; - responsePtr _RemoveRoomResp; - responsePtr _AddLinkResp; - responsePtr _RemoveLinkResp; -}; \ No newline at end of file diff --git a/client/include/room.hpp b/client/include/room.hpp new file mode 100644 index 0000000..2f584c3 --- /dev/null +++ b/client/include/room.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +class RoomImpl; + +class Room { +public: + Room(std::string roomName/* , std::string roomId */); + std::string GetRoomHost(); + std::string GetRoomName(); + void addLink(std::string& name, std::string& url); + void removeLink(std::string& linkName); + void addParticipant(std::string& newPart); + void removeParticipant(std::string& partName); + std::string archiveLink(std::string& linkName); +private: + std::shared_ptr roomImpl; +}; + +class PublicRoom : public Room { + +}; + +class PrivateRoom : public Room { + +}; + diff --git a/client/include/socket.hpp b/client/include/socket.hpp index 34487d0..b731332 100644 --- a/client/include/socket.hpp +++ b/client/include/socket.hpp @@ -4,18 +4,24 @@ #include #include - class Socket { public: - Socket(); - Socket(int _sd); - ~Socket(); + Socket() : sd (-1) {} + Socket(int _sd) : sd(_sd) {} + ~Socket() { + if (sd > 0) { + ::close(sd); + } + } - void connect(const std::string& host, int port); - void send(const std::string& str); - std::string recv(); - void close(); + int GetSd() { + return sd; + } + void Connect(const std::string& host, int port); + void Send(const std::string& str); + std::string Recv(); + void Close(); private: int sd; }; \ No newline at end of file diff --git a/client/include/userinfo.hpp b/client/include/userinfo.hpp new file mode 100644 index 0000000..59e9e80 --- /dev/null +++ b/client/include/userinfo.hpp @@ -0,0 +1,14 @@ +#pragma once +#include + + +class UserInfo{ +public: + UserInfo(); + UserInfo(std::string nm, std::string psw); + ~UserInfo(); + +private: + std::string name; + std::string password; +}; \ No newline at end of file diff --git a/client/include/view.hpp b/client/include/view.hpp new file mode 100644 index 0000000..beeb3f2 --- /dev/null +++ b/client/include/view.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include "requestForm.hpp" + +class IView { +public: + virtual ~IView() = 0; +}; + + +class ConsoleView : public IView { +public: + ConsoleView() = default; + ~ConsoleView() = default; + std::string GetRequest(); + void PrintCommands(); +}; \ No newline at end of file diff --git a/client/src/CMakeLists.txt b/client/src/CMakeLists.txt index 2f0e5e5..059d44f 100644 --- a/client/src/CMakeLists.txt +++ b/client/src/CMakeLists.txt @@ -1,4 +1,10 @@ -add_library(client STATIC application.cpp client.cpp request.cpp response.cpp socket.cpp) +add_executable(main main.cpp) +include_directories("${PROJECT_SOURCE_DIR}/include") + +add_library(client STATIC model.cpp client.cpp requestManager.cpp requestHandler.cpp presenter.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") + +target_link_libraries(main PUBLIC client) + add_definitions(-Wall -Werror -Wpedantic) \ No newline at end of file diff --git a/client/src/application.cpp b/client/src/application.cpp deleted file mode 100644 index 52ee118..0000000 --- a/client/src/application.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "application.hpp" - -UserInfo::UserInfo() : name(""), password("") {} -UserInfo::UserInfo(std::string& nm, std::string& psw) {} -UserInfo::~UserInfo() {} - - -Room::Room(std::string& roomName, std::string& roomId) {} -void Room::addLink(std::string& newLink) {} -void Room::removeLink(std::string& linkName) {} -void Room::addParticipant(std::string& newPart) {} -void Room::removeParticipant(std::string& partName) {} -std::string Room::archiveLink(std::string& linkName) {return "str";} - - - -Link::Link(std::string& name, std::string& url, std::string& tag) {} -Link::~Link() {} -std::string Link::addSnapshot() { return "str"; } - - -Application::Application() : client(), info(), rooms() {} -Application::~Application() {} \ No newline at end of file diff --git a/client/src/client.cpp b/client/src/client.cpp index 9a95756..975d908 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -1,15 +1,17 @@ #include "client.hpp" -Client::Client() : sock() {} +Client::Client(const std::string& _host, int _port) : host(_host), port(_port), sock(-1) {} -Client::~Client() {} +void Client::Connect() { + sock.Connect(host, port); +} -void Client::connect(const std::string& host, int port) {} +std::string Client::readFromServer() { + return sock.Recv(); +} -std::string Client::readFromServer() { return "str"; } - -void Client::writeToServer(std::string& req) {} - -void Client::closeCon() {} +void Client::writeToServer(std::string& req) { + sock.Send(req); +} diff --git a/client/src/link.cpp b/client/src/link.cpp new file mode 100644 index 0000000..663cc9d --- /dev/null +++ b/client/src/link.cpp @@ -0,0 +1,37 @@ +#include + +#include "client.hpp" +#include "link.hpp" + + +class LinkImpl { +public: + LinkImpl(std::string name, std::string url); + const std::string getLinkName(); + void addSnaphotPath(std::string path); +private: + std::string linkname; + std::string url; + std::vector snapshotPaths; +}; + +LinkImpl::LinkImpl(std::string name, std::string url) : linkname(std::move(name)), url(std::move(url)) {} + +const std::string LinkImpl::getLinkName() { + return linkname; +} + +void LinkImpl::addSnaphotPath(std::string path) { + snapshotPaths.push_back(std::move(path)); +} + +Link::Link(std::string& name, std::string& url) : linkImpl(new LinkImpl(name, url)){} +Link::~Link() {} + +const std::string Link::GetLinkName() { + return linkImpl->getLinkName(); +} + +void Link::addSnapshot(std::string& path) { + linkImpl->addSnaphotPath(path); +} \ No newline at end of file diff --git a/client/src/main.cpp b/client/src/main.cpp new file mode 100644 index 0000000..168dc3c --- /dev/null +++ b/client/src/main.cpp @@ -0,0 +1,8 @@ +#include "presenter.hpp" + +int main() { + + Presenter pr; + pr.run(); + return 0; +} \ No newline at end of file diff --git a/client/src/model.cpp b/client/src/model.cpp new file mode 100644 index 0000000..0e5cff2 --- /dev/null +++ b/client/src/model.cpp @@ -0,0 +1,115 @@ +#include +#include + +#include "model.hpp" +#include "requestManager.hpp" +#include "requestHandler.hpp" +#include "room.hpp" +#include "userinfo.hpp" + + +const std::string host = "localhost"; +const size_t port = 5555; + +class ModelImpl { +public: + ModelImpl(); + void passAction(std::string& action, Model& model); + + Client& GetClient() { + return client; + } + + + UserInfo& GetUserInfo() { + return info; + } + + std::shared_ptr GetMainRoom() { + return mainRoom; + } + + std::vector> GetRooms() { + return rooms; + } +private: + std::shared_ptr CreateRequestHandler(std::string& action, Model& model); + + Client client; + UserInfo info; + + std::shared_ptr mainRoom; + std::vector> rooms; +}; + +ModelImpl::ModelImpl() : client(host, port) { + client.Connect(); +} + +std::shared_ptr ModelImpl::CreateRequestHandler(std::string& action, Model& model) { + std::string::size_type typeEndPos = action.find_first_of(","); + std::string type = action.substr(0, typeEndPos); + + std::shared_ptr handler; + //std::cout << typeEndPos << std::endl; + + switch (atoi(type.c_str())) + { + case 1: + handler = std::make_shared(); + break; + case 2: + handler = std::make_shared(); + break; + case 3: + handler = std::make_shared(); + break; + case 4: + handler = std::make_shared(); + break; + case 5: + handler = std::make_shared(); + break; + case 6: + handler = std::make_shared(); + break; + case 7: + handler = std::make_shared(); + break; + default: + break; + } + + handler->FillRequest(action, model); + + return handler; +} + +void ModelImpl::passAction(std::string& action, Model& model) { + + std::shared_ptr handler = CreateRequestHandler(action, model); + + std::string req = handler->GetRequestToSend(); + + //std::cout << "req: " << req; + + client.writeToServer(req); + //std::cout << "*********" << std::endl; + std::string response = client.readFromServer(); + std::cout << response; + if (handler->HandleResponse(response) == SUCCESS) { + handler->DoLogic(model); + } +} + +Model::Model() : modelImpl(new ModelImpl) {} +Model::~Model() {} + +std::string Model::GetMainRoomInfo() { + std::string ret = modelImpl->GetMainRoom()->GetRoomName() + modelImpl->GetMainRoom()->GetRoomHost(); + return ret; +} + +void Model::PassAction(std::string& action) { + modelImpl->passAction(action, *this); +} \ No newline at end of file diff --git a/client/src/presenter.cpp b/client/src/presenter.cpp new file mode 100644 index 0000000..eda1c6e --- /dev/null +++ b/client/src/presenter.cpp @@ -0,0 +1,17 @@ +#include "presenter.hpp" + +#include + +Presenter::Presenter() { +} + +Presenter::~Presenter() { +} + +void Presenter::run() { + std::string action = view.GetRequest(); + while(!action.empty()) { + model.PassAction(action); + action = view.GetRequest(); + } +} \ No newline at end of file diff --git a/client/src/request.cpp b/client/src/request.cpp deleted file mode 100644 index 6446836..0000000 --- a/client/src/request.cpp +++ /dev/null @@ -1,35 +0,0 @@ - -#include "request.hpp" - - -ReqExitStatus LogInReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string LogInReq::RequestToString() {return "a";} - - -ReqExitStatus LogOutReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string LogOutReq::RequestToString() {return "a";} - - -ReqExitStatus CreateRoomReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string CreateRoomReq::RequestToString() {return "a";} - - -ReqExitStatus RemoveRoomReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string RemoveRoomReq::RequestToString() {return "a";} - - -ReqExitStatus AddLinkReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string AddLinkReq::RequestToString() {return "a";} - - -ReqExitStatus RemoveLinkReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string RemoveLinkReq::RequestToString() {return "a";} - - -ReqExitStatus ArchiveLinkReq::buildRequestFromInput(std::istream& s) {return REQ_SUCCESS;} -std::string ArchiveLinkReq::RequestToString() {return "a";} - - - -RequestHandler::RequestHandler() {} -requestPtr RequestHandler::HandleInput(std::string& which) {return _LogInReq;} \ No newline at end of file diff --git a/client/src/requestForm.cpp b/client/src/requestForm.cpp new file mode 100644 index 0000000..e69de29 diff --git a/client/src/requestHandler.cpp b/client/src/requestHandler.cpp new file mode 100644 index 0000000..f1745a5 --- /dev/null +++ b/client/src/requestHandler.cpp @@ -0,0 +1,138 @@ + +#include "requestHandler.hpp" + +std::string& RequestHandler::GetRequestToSend() { + return requestToSend; +} + +ExitStatus RequestHandler::HandleResponse(std::string& responseBody) { + if (responseBody.find("success") != std::string::npos) { + return SUCCESS; + } + return FAILURE; +} + +ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { + size_t separator = action.find("*Users*") + sizeof("*Users*"); + + std::string subStr = action.substr(separator, action.size() - separator); + while(subStr.size()) { + separator = subStr.find_first_of(","); + + users.push_back(subStr.substr(0, separator - 1)); + subStr = subStr.substr(separator, action.size()); + } + + requestToSend += "3," + model.GetMainRoomInfo(); + for(std::vector::iterator it = users.begin(); it != users.end(); ++it) { + requestToSend += "," + (*it); + } + + return SUCCESS; +} + +//ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { return SUCCESS; } +ExitStatus AddUsersReqHandler::DoLogic(Model &model) { + return SUCCESS; +} + + +ExitStatus RemoveUsersReqHandler::FillRequest(std::string action, Model& model) { + size_t separator = action.find("*Users*") + sizeof("*Users*"); + + std::string subStr = action.substr(separator, action.size()); + while(subStr.size()) { + separator = subStr.find_first_of(","); + + users.push_back(subStr.substr(0, separator - 1)); + subStr = subStr.substr(separator, action.size()); + } + + requestToSend += "3," + model.GetMainRoomInfo(); + for(std::vector::iterator it = users.begin(); it != users.end(); ++it) { + requestToSend += "," + (*it); + } + return SUCCESS; +} + +//ExitStatus LogOutReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +ExitStatus RemoveUsersReqHandler::DoLogic(Model &model) { return SUCCESS; } + + +ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { + size_t separator1 = action.find("*Name*") + sizeof("*Name*"); + size_t separator2 = action.find("*Url*"); + linkName = action.substr(separator1, separator2 - (separator1)); + url = action.substr(separator2 + sizeof("*Url*"), action.size()); + + requestToSend += "3," + linkName + "," + url; + + return SUCCESS; +} + +//ExitStatus AddLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +ExitStatus AddLinkReqHandler::DoLogic(Model &model) { + + return SUCCESS; +} + + + +ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { + size_t separator = action.find("*Name*") + sizeof("*Name*"); + linkName = action.substr(separator, action.size()); + + requestToSend += "3," + linkName; + + return SUCCESS; +} +//ExitStatus RemoveLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +ExitStatus RemoveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } + + +ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { + size_t separator = action.find("*Name*") + sizeof("*Name*"); + linkName = action.substr(separator, action.size() - separator); + + requestToSend = "3," + linkName; + + return SUCCESS; +} +ExitStatus ArchiveLinkReqHandler::HandleResponse(std::string &responseBody) { + /* if (responseBody.find("success") == std::string::npos) { + return FAILURE; + } + size_t bodyStartpos = sizeof("success"); + + body = responseBody.substr(++bodyStartpos, responseBody.size()); */ + return SUCCESS; +} +ExitStatus ArchiveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } + + +ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { + size_t separator1 = action.find("*Name*") + sizeof("*Name*"); + size_t separator2 = action.find("*Host*"); + roomName = action.substr(separator1 - 1, separator2 - (separator1 - 1)); + roomHost = action.substr(separator2 + sizeof("*Host*") - 1, action.size()); + + requestToSend += "1," + roomHost + "," + roomName; + + return SUCCESS; +} +//ExitStatus CreateRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS;} +ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } + +ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { + size_t separator1 = action.find("*Name*") + sizeof("*Name*"); + size_t separator2 = action.find("*Host*"); + roomName = action.substr(separator1 - 1, separator2 - (separator1 - 1)); + roomHost = action.substr(separator2 + sizeof("*Host*") - 1, action.size()); + + requestToSend = "2," + roomHost + "," + roomName; + + return SUCCESS; +} +//ExitStatus RemoveRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +ExitStatus RemoveRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } + diff --git a/client/src/requestManager.cpp b/client/src/requestManager.cpp new file mode 100644 index 0000000..b52eb62 --- /dev/null +++ b/client/src/requestManager.cpp @@ -0,0 +1,45 @@ +#include "model.hpp" +#include "requestManager.hpp" +#include "requestHandler.hpp" + +using requestPtr = std::shared_ptr; + +class RequestManagerImpl { +public: + RequestManagerImpl(); + void handle(std::string& action, Model& model); +private: + std::vector requestList; +}; + + +RequestManagerImpl::RequestManagerImpl() {} +/* +void RequestManagerImpl::handle(std::string& action, Model& model) { + std::string::size_type typeEndPos = action.find("#"); + std::string requestType = action.substr(0, typeEndPos); + + std::shared_ptr handler; + if (!requestType.compare("AddLink")) { + handler = std::make_shared(); + } else if (!requestType.compare("RemoveLink")) { + handler = std::make_shared(); + } else if (!requestType.compare("ArchiveLink")) { + handler = std::make_shared(); + } else if (!requestType.compare("CreateRoom")) { + handler = std::make_shared(); + } else if (!requestType.compare("RemoveRoom")) { + handler = std::make_shared(); + } + handler->FillRequest(action); + +} */ + + +RequestManager::RequestManager() : managerImpl(new RequestManagerImpl) {} + +RequestManager::~RequestManager() = default; + +/* void RequestManager::HandleInput(std::string& action, Model& model) { + managerImpl->handle(action, model); +} */ \ No newline at end of file diff --git a/client/src/response.cpp b/client/src/response.cpp deleted file mode 100644 index e934b4f..0000000 --- a/client/src/response.cpp +++ /dev/null @@ -1,35 +0,0 @@ - -#include "response.hpp" - - -RespExitStatus LogInResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus LogInResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -RespExitStatus LogOutResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus LogOutResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -RespExitStatus CreateRoomResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus CreateRoomResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -RespExitStatus RemoveRoomResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus RemoveRoomResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -RespExitStatus AddLinkResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus AddLinkResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -RespExitStatus RemoveLinkResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus RemoveLinkResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -RespExitStatus ArchiveLinkResp::buildResponse(std::string& responseBody) {return RESP_SUCCESS;} -RespExitStatus ArchiveLinkResp::doLogic(const Request& req, Application& app) {return RESP_SUCCESS;} - - -ResponseHandler::ResponseHandler() {} -responsePtr ResponseHandler::HandleInput(std::string& resposeBody) {return _LogInResp;} - diff --git a/client/src/room.cpp b/client/src/room.cpp new file mode 100644 index 0000000..10fb30e --- /dev/null +++ b/client/src/room.cpp @@ -0,0 +1,79 @@ +#include + +#include "link.hpp" +#include "room.hpp" + + + +class RoomImpl { +public: + RoomImpl(std::string roomName/* , std::string roomId */); + std::string getRoomHost(); + std::string getRoomName(); + void addLink(std::string& name, std::string url); + void removeLink(std::string& linkName); + void addParticipant(std::string& newPart); + void removeParticipant(std::string& partName); + std::string archiveLink(std::string& linkName); + +private: + std::string roomName; + /* std::string roomId; */ + std::string roomHost; + std::vector participants; + std::vector links; +}; + + +Room::Room(std::string roomName/* , std::string roomId */) {} +std::string Room::GetRoomHost() { + std::string ret = roomImpl->getRoomHost(); + return ret; +} +std::string Room::GetRoomName() { + std::string ret = roomImpl->getRoomName(); + return ret; +} + +void Room::addLink(std::string& name, std::string& url) { + roomImpl->addLink(name, url); +} + +void Room::removeLink(std::string& linkName) { + roomImpl->removeLink(linkName); +} + +void Room::addParticipant(std::string& newPart) {} +void Room::removeParticipant(std::string& partName) {} + +std::string Room::archiveLink(std::string& linkName) { + return "str"; +} + + +RoomImpl::RoomImpl(std::string roomName/* , std::string roomId */) {} + +std::string RoomImpl::getRoomHost() { + return roomHost; +} + +std::string RoomImpl::getRoomName() { + return roomName; +} + +void RoomImpl::addLink(std::string& name, std::string url) { + Link newLink(name, url); + links.push_back(std::move(newLink)); +} + +void RoomImpl::removeLink(std::string& linkName) { + for(std::vector::iterator it = links.begin(); it != links.end(); ++it) { + if ((*it).GetLinkName() == linkName) { + links.erase(it); + } + } +} + +void RoomImpl::addParticipant(std::string& newPart) {} +void RoomImpl::removeParticipant(std::string& partName) {} +std::string RoomImpl::archiveLink(std::string& linkName) { return "str"; } \ No newline at end of file diff --git a/client/src/socket.cpp b/client/src/socket.cpp index df1c82f..a7e7b2d 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -1,14 +1,99 @@ +#include +#include +#include +#include +#include +#include +#include +#include #include "socket.hpp" +struct sockaddr_in resolve(const char* host, int port) { + struct hostent* hp = gethostbyname(host); + if (hp == NULL) + throw std::runtime_error("resolve error: " + std::string(strerror(errno))); -Socket::Socket() : sd(0) {} + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); + + return addr; +} -Socket::Socket(int _sd) : sd(_sd) {} +void set_non_blocked_impl(int sd, bool opt) { + int flags = fcntl(sd, F_GETFL, 0); + int new_flags = (opt) ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK); + if (fcntl(sd, F_SETFL, new_flags) == -1) + throw std::runtime_error("make nonblocked: " + + std::string(strerror(errno))); +} -Socket::~Socket() {} +void Socket::Connect(const std::string& host, int port) { + struct sockaddr_in addr = resolve(host.data(), port); -void Socket::connect(const std::string& host, int port) {} -void Socket::send(const std::string& str) {} -std::string Socket::recv() {return "str";} -void Socket::close() {} \ No newline at end of file + int _sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (_sd <= 0) + throw std::runtime_error("error to create socket: " + + std::string(strerror(errno))); + + + int connected = ::connect(_sd, (struct sockaddr*)&addr, sizeof(addr)); + if (connected == -1) { + ::close(_sd); + throw std::runtime_error("connect error: " + std::string(strerror(errno))); + } + + //set_non_blocked_impl(_sd, true); + + struct timeval tv; + tv.tv_sec = 3; + tv.tv_usec = 0; + + if (setsockopt(_sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) { + throw std::runtime_error("set rcvtimeout: " + std::string(strerror(errno))); + } + + sd = _sd; +} + +void Socket::Send(const std::string& str) { + size_t left = str.size(); + ssize_t sent = 0; + + int flags = 0; + while (left > 0) { + sent = ::send(sd, str.data() + sent, str.size() - sent, flags); + if (-1 == sent) { + throw std::runtime_error("write failed: " + std::string(strerror(errno))); + } + left -= sent; + } +} + +std::string Socket::Recv() { + char buf[256]; + std::string ret; + + while (true) { + int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); + if (-1 == n && errno != EAGAIN) { + throw std::runtime_error("read failed: " + std::string(strerror(errno))); + } + if (0 == n || -1 == n) { + break; + } + ret.append(buf, n); + while (ret.back() == '\r' || ret.back() == '\n') { + ret.pop_back(); + } + } + + return ret; +} + +void Socket::Close() { + ::close(sd); +} \ No newline at end of file diff --git a/client/src/userinfo.cpp b/client/src/userinfo.cpp new file mode 100644 index 0000000..f66e641 --- /dev/null +++ b/client/src/userinfo.cpp @@ -0,0 +1,6 @@ +#include "userinfo.hpp" + +UserInfo::UserInfo() : name(""), password("") {} +UserInfo::UserInfo(std::string nm, std::string psw) : name(std::move(nm)), password(std::move(psw)){ +} +UserInfo::~UserInfo() {} diff --git a/client/src/view.cpp b/client/src/view.cpp new file mode 100644 index 0000000..8e44a00 --- /dev/null +++ b/client/src/view.cpp @@ -0,0 +1,114 @@ +#include "view.hpp" + +#include + +IView::~IView() {} + + +std::string ConsoleView::GetRequest() { + PrintCommands(); + + std::string inputStr(""), appendStr(" "); + //int key = std::atoi(inputStr.c_str()); + //std::cout << key << std::endl; + int key = 0; + std::cin >> key; + switch (key) { + case 1: { + inputStr += "1,"; + std::cout << "Write name of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr; + std::cout << "Write host of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Host*" + appendStr; + } + break; + case 2: { + inputStr += "2,"; + std::cout << "Write name of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr; + std::cout << "Write host of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Host*" + appendStr; + } + break; + case 3: { + inputStr += "3,"; + /* std::cout << "Write name of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr + ","; + std::cout << "Write host of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Host*" + appendStr + "*Users*"; */ + inputStr += "*Users*"; + std::cout << "Write users" << std::endl; + while(!appendStr.empty()) { + std::cin >> appendStr; + inputStr += appendStr + ","; + } + } + break; + case 4: { + inputStr += "4,"; + /* std::cout << "Write name of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr + ","; + std::cout << "Write host of room" << std::endl; + std::cin >> appendStr; + inputStr += "*Host*" + appendStr + "*Users*"; */ + inputStr += "*Users*"; + std::cout << "Write users" << std::endl; + while(!appendStr.empty()) { + std::cin >> appendStr; + inputStr += appendStr + ","; + } + } + break; + case 5: { + inputStr += "5,"; + std::cout << "Write name of link" << std::endl; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr + ","; + std::cout << "Write url of link" << std::endl; + std::cin >> appendStr; + inputStr += "*Url*" + appendStr; + break; + } + case 6: { + inputStr += "6,"; + std::cout << "Write name of link" << std::endl; + inputStr += "*Name*"; + std::cin >> inputStr; + } + case 7: { + inputStr += "7,"; + std::cout << "Write name of link" << std::endl; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr; + } + break; + case 0: + break; + default: + std::cout << "Wrong command. Try again" << std::endl; + break; + } + + //std::cout << inputStr << std::endl; + return inputStr; +} + +void ConsoleView::PrintCommands() { + std::cout << std::endl; + std::cout << "Please choose command type:" << std::endl; + std::cout << "- 1.Create Room" << std::endl + << "- 2.Remove Room" << std::endl + << "- 3.Add users" << std::endl + << "- 4.Remove users" << std::endl + << "- 5.Add link" << std::endl + << "- 6.Remove link" << std::endl + << "- 7.Archive link" << std::endl + << "- 0.Exit" << std::endl; +} \ No newline at end of file diff --git a/client/test/application.cpp b/client/test/application.cpp index d39a37b..a87c220 100644 --- a/client/test/application.cpp +++ b/client/test/application.cpp @@ -1,18 +1,4 @@ #include "gtest/gtest.h" -#include "application.hpp" +#include "model.hpp" - -TEST(RoomTest, archiveLinkTest) { - std::string name, id, link; - Room room(name, id); - - ASSERT_STREQ(room.archiveLink(link).c_str(), "str"); -} - -TEST(LinkTest, addSnapshot) { - std::string name, url, tag; - Link lk(name, url, tag); - - ASSERT_STREQ(lk.addSnapshot().c_str(), "str"); -} diff --git a/client/test/client.cpp b/client/test/client.cpp index 8207379..5565471 100644 --- a/client/test/client.cpp +++ b/client/test/client.cpp @@ -6,8 +6,8 @@ -TEST(ClientTest, ReadFromServerTest) { +/* TEST(ClientTest, ReadFromServerTest) { Client client; ASSERT_EQ(client.readFromServer(), "str"); -} +} */ diff --git a/client/test/main.cpp b/client/test/main.cpp index b42b721..ff6474f 100644 --- a/client/test/main.cpp +++ b/client/test/main.cpp @@ -1,6 +1,6 @@ #include "gtest/gtest.h" -int main(int argc, char **argv) { +int test_main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } \ No newline at end of file diff --git a/client/test/request.cpp b/client/test/request.cpp index 2d31cb7..37fd370 100644 --- a/client/test/request.cpp +++ b/client/test/request.cpp @@ -1,6 +1,6 @@ #include "gtest/gtest.h" -#include "request.hpp" +#include "requestHandler.hpp" #include #include #include @@ -8,56 +8,49 @@ TEST(RequestTest, logInTest) { std::stringstream is; - LogInReq req; + LogInReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); + //ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } TEST(RequestTest, logOutTest) { std::stringstream is; - LogOutReq req; + LogOutReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); + //ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } TEST(RequestTest, CreateRoomTest) { std::stringstream is; - CreateRoomReq req; + CreateRoomReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); + // ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } TEST(RequestTest, RemoveRoomTest) { std::stringstream is; - RemoveRoomReq req; + RemoveRoomReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); + //ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } TEST(RequestTest, AddLinkTest) { std::stringstream is; - AddLinkReq req; + AddLinkReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); + //ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } TEST(RequestTest, RemoveLinkTest) { std::stringstream is; - RemoveLinkReq req; + RemoveLinkReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); + // ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } TEST(RequestTest, ArchiveLinkTest) { std::stringstream is; - ArchiveLinkReq req; + ArchiveLinkReqHandler req; - ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); -} - -TEST(RequestTest, RequestHandlerTest) { - std::string str; - RequestHandler handler; - - ASSERT_EQ(typeid(handler.HandleInput(str)), typeid(std::shared_ptr)); + // ASSERT_EQ(req.buildRequestFromInput(is), REQ_SUCCESS); } diff --git a/client/test/response.cpp b/client/test/response.cpp deleted file mode 100644 index e058a2e..0000000 --- a/client/test/response.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "gtest/gtest.h" - -#include -#include "request.hpp" -#include "response.hpp" - - -TEST(ResponseTest, logInTest) { - Application app; - LogInReq req; - LogInResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, logOutTest) { - Application app; - LogOutReq req; - LogOutResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, CreateRoomTest) { - Application app; - CreateRoomReq req; - CreateRoomResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, RemoveRoomTest) { - Application app; - RemoveRoomReq req; - RemoveRoomResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, AddLinkTest) { - Application app; - AddLinkReq req; - AddLinkResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, RemoveLinkTest) { - Application app; - RemoveLinkReq req; - RemoveLinkResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, ArchiveLinkTest) { - Application app; - ArchiveLinkReq req; - ArchiveLinkResp resp; - - ASSERT_EQ(resp.doLogic(req, app), RESP_SUCCESS); -} - -TEST(ResponseTest, ResponseHandlerTest) { - std::string str; - ResponseHandler handler; - - ASSERT_EQ(typeid(handler.HandleInput(str)), typeid(std::shared_ptr)); -} - diff --git a/client/test/socket.cpp b/client/test/socket.cpp index a5e1a52..22c5d76 100644 --- a/client/test/socket.cpp +++ b/client/test/socket.cpp @@ -3,9 +3,9 @@ #include "socket.hpp" - +/* TEST(SocketTest, RecvTest) { Socket sock; - ASSERT_STREQ(sock.recv().c_str(), "str"); -} \ No newline at end of file + ASSERT_STREQ(sock.Recv().c_str(), "str"); +} */ \ No newline at end of file From 3339e88c6353a6dcbfec02f86318bb867f195005 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 10 Dec 2020 17:26:37 +0300 Subject: [PATCH 15/36] changed blank.yml --- .github/workflows/blank.yml | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index b6876c5..967b856 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -33,30 +33,30 @@ jobs: cmake .. make - - name: test socket - run: | - cd client/build - valgrind --leak-check=full ./test/socket_tests + #- name: test socket + # run: | + # cd client/build + # valgrind --leak-check=full ./test/socket_tests - - name: test response - run: | - cd client/build - valgrind --leak-check=full ./test/response_tests + #- name: test response + # run: | + # cd client/build + # valgrind --leak-check=full ./test/response_tests - - name: test request - run: | - cd client/build - valgrind --leak-check=full ./test/request_tests + #- name: test request + # run: | + # cd client/build + # valgrind --leak-check=full ./test/request_tests - - name: test client - run: | - cd client/build - valgrind --leak-check=full ./test/client_tests + #- name: test client + # run: | + # cd client/build + # valgrind --leak-check=full ./test/client_tests - - name: test application - run: | - cd client/build - valgrind --leak-check=full ./test/application_tests + #- name: test application + # run: | + # cd client/build + # valgrind --leak-check=full ./test/application_tests - name: coverage run: | From 8dc195d01e3681e648e0e207c8566b59f91feb7d Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 10 Dec 2020 17:52:57 +0300 Subject: [PATCH 16/36] deleted request manager --- client/include/requestManager.hpp | 22 --------------- client/src/CMakeLists.txt | 4 +-- client/src/model.cpp | 1 - client/src/requestManager.cpp | 45 ------------------------------- 4 files changed, 2 insertions(+), 70 deletions(-) delete mode 100644 client/include/requestManager.hpp delete mode 100644 client/src/requestManager.cpp diff --git a/client/include/requestManager.hpp b/client/include/requestManager.hpp deleted file mode 100644 index 33c28ef..0000000 --- a/client/include/requestManager.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - - -#include -#include -#include - - -class RequestManagerImpl; -class Model; - -class RequestManager { -public: - RequestManager(); - ~RequestManager(); - RequestManager& operator=(RequestManager& manager) = delete; - RequestManager(RequestManager& manager) = delete; - /* void HandleInput(std::string& action, Model& model); */ -private: - void addRequest(std::string& action); - std::shared_ptr managerImpl; -}; \ No newline at end of file diff --git a/client/src/CMakeLists.txt b/client/src/CMakeLists.txt index 059d44f..c668f63 100644 --- a/client/src/CMakeLists.txt +++ b/client/src/CMakeLists.txt @@ -1,10 +1,10 @@ add_executable(main main.cpp) include_directories("${PROJECT_SOURCE_DIR}/include") -add_library(client STATIC model.cpp client.cpp requestManager.cpp requestHandler.cpp presenter.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) +add_library(client STATIC model.cpp client.cpp requestHandler.cpp presenter.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") target_link_libraries(main PUBLIC client) -add_definitions(-Wall -Werror -Wpedantic) \ No newline at end of file +add_definitions(-Wall -Werror -Wpedantic) diff --git a/client/src/model.cpp b/client/src/model.cpp index 0e5cff2..fb51a70 100644 --- a/client/src/model.cpp +++ b/client/src/model.cpp @@ -2,7 +2,6 @@ #include #include "model.hpp" -#include "requestManager.hpp" #include "requestHandler.hpp" #include "room.hpp" #include "userinfo.hpp" diff --git a/client/src/requestManager.cpp b/client/src/requestManager.cpp deleted file mode 100644 index b52eb62..0000000 --- a/client/src/requestManager.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "model.hpp" -#include "requestManager.hpp" -#include "requestHandler.hpp" - -using requestPtr = std::shared_ptr; - -class RequestManagerImpl { -public: - RequestManagerImpl(); - void handle(std::string& action, Model& model); -private: - std::vector requestList; -}; - - -RequestManagerImpl::RequestManagerImpl() {} -/* -void RequestManagerImpl::handle(std::string& action, Model& model) { - std::string::size_type typeEndPos = action.find("#"); - std::string requestType = action.substr(0, typeEndPos); - - std::shared_ptr handler; - if (!requestType.compare("AddLink")) { - handler = std::make_shared(); - } else if (!requestType.compare("RemoveLink")) { - handler = std::make_shared(); - } else if (!requestType.compare("ArchiveLink")) { - handler = std::make_shared(); - } else if (!requestType.compare("CreateRoom")) { - handler = std::make_shared(); - } else if (!requestType.compare("RemoveRoom")) { - handler = std::make_shared(); - } - handler->FillRequest(action); - -} */ - - -RequestManager::RequestManager() : managerImpl(new RequestManagerImpl) {} - -RequestManager::~RequestManager() = default; - -/* void RequestManager::HandleInput(std::string& action, Model& model) { - managerImpl->handle(action, model); -} */ \ No newline at end of file From f00d935b854d134631256f08b5c7a374b0d449e1 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 00:55:14 +0300 Subject: [PATCH 17/36] implemented json-like serializator --- client/include/model.hpp | 3 - client/include/requestForm.hpp | 33 ------ client/include/requestHandler.hpp | 4 +- client/include/room.hpp | 2 +- client/include/utils.h | 175 ++++++++++++++++++++++++++++++ client/include/view.hpp | 1 - client/src/model.cpp | 7 +- client/src/requestForm.cpp | 0 client/src/requestHandler.cpp | 104 +++++++++--------- client/src/room.cpp | 6 +- client/src/view.cpp | 43 ++++---- 11 files changed, 256 insertions(+), 122 deletions(-) delete mode 100644 client/include/requestForm.hpp create mode 100644 client/include/utils.h delete mode 100644 client/src/requestForm.cpp diff --git a/client/include/model.hpp b/client/include/model.hpp index d2ed7b0..72a08e0 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -3,8 +3,6 @@ #include #include "client.hpp" -#include "requestForm.hpp" - class ModelImpl; @@ -15,7 +13,6 @@ class Model { ~Model(); std::string GetMainRoomInfo(); void PassAction(std::string& action); - private: std::shared_ptr modelImpl; }; \ No newline at end of file diff --git a/client/include/requestForm.hpp b/client/include/requestForm.hpp deleted file mode 100644 index 24687fd..0000000 --- a/client/include/requestForm.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once -#include - -struct RequestBody {std::string ff = "AAAAAAA";}; - -struct RequestForm { - struct RequestBody Body; - - int Type; -}; - - -struct AddLinkBody : public RequestBody { - std::string linkName; - std::string url; -}; - -struct RemoveLinkBody : public RequestBody { - std::string linkName; -}; - -struct ArchiveLinkBody : public RequestBody { - std::string linkName; -}; - -struct CreateRoomBody : public RequestBody { - std::string roomName; - std::string roomHost; -}; - -struct RemoveRoomBody : public RequestBody { - std::string roomName; -}; \ No newline at end of file diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index 1319200..f4f997c 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -51,8 +51,8 @@ class AddLinkReqHandler : public RequestHandler { //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: - std::string linkName; - std::string url; + std::string linkName = ""; + std::string url = ""; }; class RemoveLinkReqHandler : public RequestHandler { diff --git a/client/include/room.hpp b/client/include/room.hpp index 2f584c3..774475a 100644 --- a/client/include/room.hpp +++ b/client/include/room.hpp @@ -7,7 +7,7 @@ class RoomImpl; class Room { public: - Room(std::string roomName/* , std::string roomId */); + Room(std::string roomName, std::string roomId); std::string GetRoomHost(); std::string GetRoomName(); void addLink(std::string& name, std::string& url); diff --git a/client/include/utils.h b/client/include/utils.h new file mode 100644 index 0000000..36b5bc6 --- /dev/null +++ b/client/include/utils.h @@ -0,0 +1,175 @@ +#pragma once + +#include +#include +#include + +const std::string separator = "{}[]\" "; + + +// Все, что связано с сериализацией в JSON + +std::vector request_split(const std::string& data) { + std::vector elements; + + std::string element = ""; + + bool special_block = false; + + for(auto it = data.begin(); it != data.end(); it++) { + if (*it == '{' || *it == '}' || *it == '[' || *it == ']') { + if (*it == '[' || *it == '{') { + special_block = true; + } else { + special_block = false; + } + } + + if (*it == ',' && !special_block) { + elements.push_back(element); + element = ""; + } else { + element += *it; + } + } + + return elements; +} + + +std::vector splitString(std::string& data) { + if (data.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::cout << data << std::endl; + + std::vector elements; + + std::string element = ""; + std::string tempStr(data); + + auto newEnd = std::remove_if(tempStr.begin(), tempStr.end(), + [](char c) { return separator.find(c) != std::string::npos; }); + tempStr.erase(newEnd, tempStr.end()); + + for(auto it = tempStr.begin(); it != tempStr.end(); it++) { + if ((*it == ':' || *it == ',') /* && !special_block */) { + elements.push_back(element); + element = ""; + } else { + element += *it; + } + } + elements.push_back(element); + + for (auto &i : elements) { + std::cout << i << std::endl; + } + + return elements; +} + + +std::string boolToString(bool value) { + return value ? "true" : "false"; +} + +std::string serialize(const std::string &key, const std::vector vec) { + if (key.empty() || vec.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + + std::string serialized = "\"" + key + "\": [ "; + for (auto &v: vec) { + serialized += "\"" + v + "\", "; + } + serialized.erase(serialized.size() - 2, 1); + serialized += "]"; + + return serialized; +} + +std::string serialize(const std::string &key, const std::string &value) { + if (key.empty() || value.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + + std::string serialized = "\"" + key + "\": \"" + value + "\","; + return serialized; +} + +std::string serialize(const std::string &key, int value) { + if (key.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::string serialized = "\"" + key + "\": " + std::to_string(value) + ","; + return serialized; +} + +std::string serialize(const std::string &key, bool value) { + if (key.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::string serialized = "\"" + key + "\": " + boolToString(value) + ","; + return serialized; +} + +template +std::string serializeData(T key, K value) { + return serialize(key, value); +} + +template +std::string serializeData(T key, K value, Args... args) { + return serialize(key, value) + serializeData(args...); +} + + +template +std::string packToJsonString(T key, K value, Args... args) { + std::string jsonStr(""); + jsonStr = "{"; + jsonStr += serializeData(key, value, args...); + jsonStr += "}"; + return jsonStr; +} + + + +// Все, что связано с распаковкой JSON'а + +void fillData(const std::string& jsonStr, const std::string& key, std::vector* inputVec) { + auto vec = request_split(jsonStr); + for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { + inputVec->push_back(*it); + } +} + + + +void fillData(const std::string& jsonStr, const std::string& key, std::string* inputStr) { + auto vec = request_split(jsonStr); + + std::cout << key << std::endl; + for (auto &i : vec) { + std::cout << i << std::endl; + } + std::cout << std::endl; + + auto it = std::find(vec.begin(), vec.end(), key) + 1; + if (it != vec.end()) { + *inputStr = *it; + } +} + +template +void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal) { + fillData(jsonStr, key, inputVal); +} + +template +void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal, Args... args) { + fillData(jsonStr, key, inputVal); + fillDataFromJson(jsonStr, args...); +} + diff --git a/client/include/view.hpp b/client/include/view.hpp index beeb3f2..efb5d6b 100644 --- a/client/include/view.hpp +++ b/client/include/view.hpp @@ -2,7 +2,6 @@ #include -#include "requestForm.hpp" class IView { public: diff --git a/client/src/model.cpp b/client/src/model.cpp index fb51a70..2e55530 100644 --- a/client/src/model.cpp +++ b/client/src/model.cpp @@ -41,7 +41,7 @@ class ModelImpl { std::vector> rooms; }; -ModelImpl::ModelImpl() : client(host, port) { +ModelImpl::ModelImpl() : client(host, port), mainRoom(std::make_shared("default", "me")) { client.Connect(); } @@ -84,18 +84,17 @@ std::shared_ptr ModelImpl::CreateRequestHandler(std::string& act return handler; } + void ModelImpl::passAction(std::string& action, Model& model) { - std::shared_ptr handler = CreateRequestHandler(action, model); std::string req = handler->GetRequestToSend(); //std::cout << "req: " << req; - client.writeToServer(req); //std::cout << "*********" << std::endl; std::string response = client.readFromServer(); - std::cout << response; + std::cout << std::endl << response << std::endl; if (handler->HandleResponse(response) == SUCCESS) { handler->DoLogic(model); } diff --git a/client/src/requestForm.cpp b/client/src/requestForm.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/client/src/requestHandler.cpp b/client/src/requestHandler.cpp index f1745a5..d7ee1dd 100644 --- a/client/src/requestHandler.cpp +++ b/client/src/requestHandler.cpp @@ -1,10 +1,16 @@ +#include #include "requestHandler.hpp" +#include "utils.h" + + + std::string& RequestHandler::GetRequestToSend() { return requestToSend; } + ExitStatus RequestHandler::HandleResponse(std::string& responseBody) { if (responseBody.find("success") != std::string::npos) { return SUCCESS; @@ -13,20 +19,29 @@ ExitStatus RequestHandler::HandleResponse(std::string& responseBody) { } ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { - size_t separator = action.find("*Users*") + sizeof("*Users*"); + //std::cout << action << std::endl; - std::string subStr = action.substr(separator, action.size() - separator); - while(subStr.size()) { - separator = subStr.find_first_of(","); + /* auto vec = request_split(action); + + for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { + users.push_back(*it); + } */ + /* for (auto &i : vec) { + std::cout << i << std::endl; + } */ - users.push_back(subStr.substr(0, separator - 1)); - subStr = subStr.substr(separator, action.size()); - } + fillDataFromJson(action, "*Users*", &users); - requestToSend += "3," + model.GetMainRoomInfo(); - for(std::vector::iterator it = users.begin(); it != users.end(); ++it) { - requestToSend += "," + (*it); - } + + /* std::string kk = packToJsonString("users", users); + + auto vec2 = splitString(kk); + + for (auto &i : vec2) { + std::cout << i << std::endl; + } */ + + requestToSend = packToJsonString("users", users); return SUCCESS; } @@ -38,20 +53,8 @@ ExitStatus AddUsersReqHandler::DoLogic(Model &model) { ExitStatus RemoveUsersReqHandler::FillRequest(std::string action, Model& model) { - size_t separator = action.find("*Users*") + sizeof("*Users*"); - - std::string subStr = action.substr(separator, action.size()); - while(subStr.size()) { - separator = subStr.find_first_of(","); - - users.push_back(subStr.substr(0, separator - 1)); - subStr = subStr.substr(separator, action.size()); - } - - requestToSend += "3," + model.GetMainRoomInfo(); - for(std::vector::iterator it = users.begin(); it != users.end(); ++it) { - requestToSend += "," + (*it); - } + fillDataFromJson(action, "*Users*", &users); + requestToSend = packToJsonString("users", users); return SUCCESS; } @@ -60,12 +63,24 @@ ExitStatus RemoveUsersReqHandler::DoLogic(Model &model) { return SUCCESS; } ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { - size_t separator1 = action.find("*Name*") + sizeof("*Name*"); - size_t separator2 = action.find("*Url*"); - linkName = action.substr(separator1, separator2 - (separator1)); - url = action.substr(separator2 + sizeof("*Url*"), action.size()); + /* auto vec = splitString(action); + + auto it = std::find(vec.begin(), vec.end(), "*Name*") + 1; + if (it != vec.end()) { + linkName = *it; + } + it = std::find(vec.begin(), vec.end(), "*Url*") + 1; + if (it != vec.end()) { + url = *it; + } */ + + //std::cout << action << std::endl; - requestToSend += "3," + linkName + "," + url; + fillDataFromJson(action, "*Name*", &linkName, "*Url*", &url); + //std::cout << linkName << std::endl; + //std::cout << url << std::endl; + + requestToSend = packToJsonString("linkname", linkName, "url", url); return SUCCESS; } @@ -79,10 +94,8 @@ ExitStatus AddLinkReqHandler::DoLogic(Model &model) { ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { - size_t separator = action.find("*Name*") + sizeof("*Name*"); - linkName = action.substr(separator, action.size()); - - requestToSend += "3," + linkName; + fillDataFromJson(action, "*Name*", &linkName); + requestToSend = packToJsonString("linkname", linkName); return SUCCESS; } @@ -91,10 +104,8 @@ ExitStatus RemoveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { - size_t separator = action.find("*Name*") + sizeof("*Name*"); - linkName = action.substr(separator, action.size() - separator); - - requestToSend = "3," + linkName; + fillDataFromJson(action, "*Name*", &linkName); + requestToSend = packToJsonString("linkname", linkName); return SUCCESS; } @@ -111,12 +122,8 @@ ExitStatus ArchiveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { - size_t separator1 = action.find("*Name*") + sizeof("*Name*"); - size_t separator2 = action.find("*Host*"); - roomName = action.substr(separator1 - 1, separator2 - (separator1 - 1)); - roomHost = action.substr(separator2 + sizeof("*Host*") - 1, action.size()); - - requestToSend += "1," + roomHost + "," + roomName; + fillDataFromJson(action, "*Name*", &roomName, "*Host*", roomHost); + requestToSend = packToJsonString("name", roomName, "host", roomHost); return SUCCESS; } @@ -124,13 +131,8 @@ ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { - size_t separator1 = action.find("*Name*") + sizeof("*Name*"); - size_t separator2 = action.find("*Host*"); - roomName = action.substr(separator1 - 1, separator2 - (separator1 - 1)); - roomHost = action.substr(separator2 + sizeof("*Host*") - 1, action.size()); - - requestToSend = "2," + roomHost + "," + roomName; - + fillDataFromJson(action, "*Name*", &roomName, "*Host*", roomHost); + requestToSend = packToJsonString("name", roomName, "host", roomHost); return SUCCESS; } //ExitStatus RemoveRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } diff --git a/client/src/room.cpp b/client/src/room.cpp index 10fb30e..8731d96 100644 --- a/client/src/room.cpp +++ b/client/src/room.cpp @@ -7,7 +7,7 @@ class RoomImpl { public: - RoomImpl(std::string roomName/* , std::string roomId */); + RoomImpl(std::string name, std::string host); std::string getRoomHost(); std::string getRoomName(); void addLink(std::string& name, std::string url); @@ -25,7 +25,7 @@ class RoomImpl { }; -Room::Room(std::string roomName/* , std::string roomId */) {} +Room::Room(std::string name, std::string host) : roomImpl(new RoomImpl(name, host)) {} std::string Room::GetRoomHost() { std::string ret = roomImpl->getRoomHost(); return ret; @@ -51,7 +51,7 @@ std::string Room::archiveLink(std::string& linkName) { } -RoomImpl::RoomImpl(std::string roomName/* , std::string roomId */) {} +RoomImpl::RoomImpl(std::string name, std::string host) : roomName(name), roomHost(host) {} std::string RoomImpl::getRoomHost() { return roomHost; diff --git a/client/src/view.cpp b/client/src/view.cpp index 8e44a00..7079633 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -9,8 +9,6 @@ std::string ConsoleView::GetRequest() { PrintCommands(); std::string inputStr(""), appendStr(" "); - //int key = std::atoi(inputStr.c_str()); - //std::cout << key << std::endl; int key = 0; std::cin >> key; switch (key) { @@ -36,33 +34,31 @@ std::string ConsoleView::GetRequest() { break; case 3: { inputStr += "3,"; - /* std::cout << "Write name of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Name*" + appendStr + ","; - std::cout << "Write host of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Host*" + appendStr + "*Users*"; */ - inputStr += "*Users*"; + std::cout << "Write amount of users" << std::endl; + size_t amount = 0; + std::cin >> amount; + inputStr += "*Users*,"; std::cout << "Write users" << std::endl; - while(!appendStr.empty()) { + while(amount > 0) { + //std::getline(std::cin, appendStr); std::cin >> appendStr; inputStr += appendStr + ","; + --amount; + /* std::cout << amount<< std::endl; */ } } break; case 4: { inputStr += "4,"; - /* std::cout << "Write name of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Name*" + appendStr + ","; - std::cout << "Write host of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Host*" + appendStr + "*Users*"; */ - inputStr += "*Users*"; + std::cout << "Write amount of users" << std::endl; + size_t amount = 0; + std::cin >> amount; + inputStr += "*Users*,"; std::cout << "Write users" << std::endl; - while(!appendStr.empty()) { + while(amount > 0) { std::cin >> appendStr; inputStr += appendStr + ","; + --amount; } } break; @@ -70,18 +66,19 @@ std::string ConsoleView::GetRequest() { inputStr += "5,"; std::cout << "Write name of link" << std::endl; std::cin >> appendStr; - inputStr += "*Name*" + appendStr + ","; + inputStr += "*Name*," + appendStr + ","; std::cout << "Write url of link" << std::endl; std::cin >> appendStr; - inputStr += "*Url*" + appendStr; + inputStr += "*Url*," + appendStr + ","; break; } case 6: { inputStr += "6,"; std::cout << "Write name of link" << std::endl; - inputStr += "*Name*"; - std::cin >> inputStr; + std::cin >> appendStr; + inputStr += "*Name*" + appendStr; } + break; case 7: { inputStr += "7,"; std::cout << "Write name of link" << std::endl; @@ -95,8 +92,6 @@ std::string ConsoleView::GetRequest() { std::cout << "Wrong command. Try again" << std::endl; break; } - - //std::cout << inputStr << std::endl; return inputStr; } From 82043c94509546717eafc6881c7de872d24a9eae Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 11:11:10 +0300 Subject: [PATCH 18/36] added boost parser and moved send and recieve to presenter --- client/CMakeLists.txt | 2 + client/include/model.hpp | 16 +-- client/include/model.tpp | 145 +++++++++++++++++++++++++ client/include/presenter.hpp | 11 +- client/include/presenter.tpp | 30 ++++++ client/include/requestHandler.hpp | 58 +++++----- client/include/requestHandler.tpp | 154 +++++++++++++++++++++++++++ client/include/responseParser.hpp | 22 ++++ client/include/utils.h | 170 +++++------------------------- client/include/utils.tpp | 41 +++++++ client/src/CMakeLists.txt | 7 +- client/src/main.cpp | 55 +++++++++- client/src/model.cpp | 113 -------------------- client/src/presenter.cpp | 17 --- client/src/requestHandler.cpp | 140 ------------------------ client/src/utils.cpp | 133 +++++++++++++++++++++++ client/src/view.cpp | 13 +-- 17 files changed, 668 insertions(+), 459 deletions(-) create mode 100644 client/include/model.tpp create mode 100644 client/include/presenter.tpp create mode 100644 client/include/requestHandler.tpp create mode 100644 client/include/responseParser.hpp create mode 100644 client/include/utils.tpp delete mode 100644 client/src/model.cpp delete mode 100644 client/src/presenter.cpp delete mode 100644 client/src/requestHandler.cpp create mode 100644 client/src/utils.cpp diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index f78872f..8256b2f 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.16.3) project(link_share) + + add_subdirectory(src) enable_testing() diff --git a/client/include/model.hpp b/client/include/model.hpp index 72a08e0..93b4ed8 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -2,17 +2,21 @@ #include -#include "client.hpp" - +//#include "client.hpp" +template class ModelImpl; - +template class Model { public: Model(); ~Model(); std::string GetMainRoomInfo(); - void PassAction(std::string& action); + //void PassAction(std::string& action); + std::string FormRequest(std::string& action); + void HandleResponse(std::string& response); private: - std::shared_ptr modelImpl; -}; \ No newline at end of file + std::shared_ptr> modelImpl; +}; + +#include "model.tpp" \ No newline at end of file diff --git a/client/include/model.tpp b/client/include/model.tpp new file mode 100644 index 0000000..bbf0be7 --- /dev/null +++ b/client/include/model.tpp @@ -0,0 +1,145 @@ +#pragma once + +#include +#include + +#include "model.hpp" +#include "requestHandler.hpp" +#include "room.hpp" +#include "userinfo.hpp" +//#include "utils.h" + + +/* const std::string host = "localhost"; +const size_t port = 5555; */ + +template +class ModelImpl { +public: + ModelImpl(); + //void passAction(std::string& action, Model& model); + + /* Client& GetClient() { + return client; + } */ + + std::string formRequest(std::string& action, Model& model); + void handleResponse(std::string& response, Model& model); + + UserInfo& GetUserInfo() { + return info; + } + + std::shared_ptr GetMainRoom() { + return mainRoom; + } + + std::vector> GetRooms() { + return rooms; + } +private: + std::shared_ptr> CreateRequestHandler(std::string& action, Model& model); + std::shared_ptr> currentHandler; + + + UserInfo info; + + std::shared_ptr mainRoom; + std::vector> rooms; +}; + +template +ModelImpl::ModelImpl() : mainRoom(std::make_shared("default", "me")) { +} + +template +std::shared_ptr> ModelImpl::CreateRequestHandler(std::string& action, Model& model) { + std::string::size_type typeEndPos = action.find_first_of(","); + std::string type = action.substr(0, typeEndPos); + + std::shared_ptr> handler; + //std::cout << typeEndPos << std::endl; + + switch (atoi(type.c_str())) + { + case 1: + handler = std::make_shared>(); + break; + case 2: + handler = std::make_shared>(); + break; + case 3: + handler = std::make_shared>(); + break; + case 4: + handler = std::make_shared>(); + break; + case 5: + handler = std::make_shared>(); + break; + case 6: + handler = std::make_shared>(); + break; + case 7: + handler = std::make_shared>(); + break; + default: + break; + } + + handler->FillRequest(action, model); + + return handler; +} + + +/* void ModelImpl::passAction(std::string& action, Model& model) { + std::shared_ptr handler = CreateRequestHandler(action, model); + + std::string req = handler->GetRequestToSend(); + + client.writeToServer(req); + std::string response = client.readFromServer(); + std::cout << std::endl << response << std::endl; + if (handler->HandleResponse(response) == SUCCESS) { + handler->DoLogic(model); + } +} */ + +template +std::string ModelImpl::formRequest(std::string& action, Model& model) { + currentHandler = CreateRequestHandler(action, model); + std::string req = currentHandler->GetRequestToSend(); + return req; +} + +template +void ModelImpl::handleResponse(std::string& response, Model& model) { + currentHandler->DoLogic(model); +} + +template +Model::Model() : modelImpl(new ModelImpl) {} +template +Model::~Model() {} + +template +std::string Model::GetMainRoomInfo() { + std::string ret = modelImpl->GetMainRoom()->GetRoomName() + modelImpl->GetMainRoom()->GetRoomHost(); + return ret; +} + +/* void Model::PassAction(std::string& action) { + modelImpl->passAction(action, *this); +} */ + +template +std::string Model::FormRequest(std::string& action) { + std::string ret = modelImpl->formRequest(action, *this); + return ret; +} + +template +void Model::HandleResponse(std::string& response) { + modelImpl->handleResponse(response, *this); +} \ No newline at end of file diff --git a/client/include/presenter.hpp b/client/include/presenter.hpp index 93f5534..e2ffee4 100644 --- a/client/include/presenter.hpp +++ b/client/include/presenter.hpp @@ -2,17 +2,22 @@ #include +#include "client.hpp" #include "model.hpp" #include "view.hpp" +template class Presenter { public: - Presenter(); + Presenter(const std::string& host, const size_t port); Presenter(Presenter& pr) = delete; Presenter& operator=(Presenter& pr) = delete; ~Presenter(); void run(); private: - Model model; + Client client; + Model model; ConsoleView view; -}; \ No newline at end of file +}; + +#include "presenter.tpp" \ No newline at end of file diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp new file mode 100644 index 0000000..87d707f --- /dev/null +++ b/client/include/presenter.tpp @@ -0,0 +1,30 @@ +/* #include "presenter.hpp" */ +#pragma once + +#include + +template +Presenter::Presenter(const std::string& host, const size_t port) : +client(host, port) { + client.Connect(); +} + +template +Presenter::~Presenter() { +} + +template +void Presenter::run() { + std::string action = view.GetRequest(); + while(!action.empty()) { + //model.PassAction(action); + std::string request = model.FormRequest(action); + client.writeToServer(request); + std::string response = client.readFromServer(); + + std::cout << std::endl << response << std::endl; + + model.HandleResponse(response); + action = view.GetRequest(); + } +} \ No newline at end of file diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index f4f997c..08b48b1 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -12,88 +12,98 @@ typedef enum ExitStatus { FAILURE } ExitStatus; - +template class RequestHandler { public: - virtual ExitStatus FillRequest(std::string action, Model& model) = 0; + virtual ExitStatus FillRequest(std::string action, Model& model) = 0; virtual ExitStatus HandleResponse(std::string& responseBody); - virtual ExitStatus DoLogic(Model& app) = 0; + virtual ExitStatus DoLogic(Model& app) = 0; std::string& GetRequestToSend(); protected: std::string requestToSend; + std::shared_ptr parser; }; -class AddUsersReqHandler : public RequestHandler { +template +class AddUsersReqHandler : public RequestHandler { public: AddUsersReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::vector users; }; -class RemoveUsersReqHandler : public RequestHandler { +template +class RemoveUsersReqHandler : public RequestHandler { public: RemoveUsersReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::vector users; }; -class AddLinkReqHandler : public RequestHandler { +template +class AddLinkReqHandler : public RequestHandler { public: AddLinkReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::string linkName = ""; std::string url = ""; }; -class RemoveLinkReqHandler : public RequestHandler { +template +class RemoveLinkReqHandler : public RequestHandler { public: RemoveLinkReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::string linkName; }; -class ArchiveLinkReqHandler : public RequestHandler { +template +class ArchiveLinkReqHandler : public RequestHandler { public: ArchiveLinkReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::string linkName; std::string body; }; -class CreateRoomReqHandler : public RequestHandler { +template +class CreateRoomReqHandler : public RequestHandler { public: CreateRoomReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::string roomName; std::string roomHost; }; -class RemoveRoomReqHandler : public RequestHandler { +template +class RemoveRoomReqHandler : public RequestHandler { public: RemoveRoomReqHandler() = default; - ExitStatus FillRequest(std::string action, Model& model); + ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); - ExitStatus DoLogic(Model& app); + ExitStatus DoLogic(Model& app); private: std::string roomName; std::string roomHost; }; + +#include "requestHandler.tpp" \ No newline at end of file diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp new file mode 100644 index 0000000..a612d33 --- /dev/null +++ b/client/include/requestHandler.tpp @@ -0,0 +1,154 @@ +#pragma once +/* #include "requestHandler.hpp" */ +#include "utils.h" + + +template +std::string& RequestHandler::GetRequestToSend() { + return requestToSend; +} + +template +ExitStatus RequestHandler::HandleResponse(std::string& responseBody) { + if (responseBody.find("success") != std::string::npos) { + return SUCCESS; + } + return FAILURE; +} + +template +ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { + //std::cout << action << std::endl; + + /* auto vec = request_split(action); + + for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { + users.push_back(*it); + } */ + /* for (auto &i : vec) { + std::cout << i << std::endl; + } */ + + fillDataFromJson(action, "*Users*", &users); + + + /* std::string kk = packToJsonString("users", users); + + auto vec2 = splitString(kk); + + for (auto &i : vec2) { + std::cout << i << std::endl; + } */ + + RequestHandler::requestToSend = packToJsonString("users", users); + + return SUCCESS; +} + + + +//ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { return SUCCESS; } + +template +ExitStatus AddUsersReqHandler::DoLogic(Model &model) { + return SUCCESS; +} + +template +ExitStatus RemoveUsersReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "*Users*", &users); + RequestHandler::requestToSend = packToJsonString("users", users); + return SUCCESS; +} + +//ExitStatus LogOutReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +template +ExitStatus RemoveUsersReqHandler::DoLogic(Model &model) { return SUCCESS; } + +template +ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { + /* auto vec = splitString(action); + + auto it = std::find(vec.begin(), vec.end(), "*Name*") + 1; + if (it != vec.end()) { + linkName = *it; + } + it = std::find(vec.begin(), vec.end(), "*Url*") + 1; + if (it != vec.end()) { + url = *it; + } */ + + //std::cout << action << std::endl; + + fillDataFromJson(action, "*Name*", &linkName, "*Url*", &url); + //std::cout << linkName << std::endl; + //std::cout << url << std::endl; + + RequestHandler::requestToSend = packToJsonString("linkname", linkName, "url", url); + + return SUCCESS; +} + +//ExitStatus AddLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +template +ExitStatus AddLinkReqHandler::DoLogic(Model &model) { + + return SUCCESS; +} + + +template +ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "*Name*", &linkName); + RequestHandler::requestToSend = packToJsonString("linkname", linkName); + + return SUCCESS; +} +//ExitStatus RemoveLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +template +ExitStatus RemoveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } + +template +ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "*Name*", &linkName); + RequestHandler::requestToSend = packToJsonString("linkname", linkName); + + return SUCCESS; +} + +template +ExitStatus ArchiveLinkReqHandler::HandleResponse(std::string &responseBody) { + /* if (responseBody.find("success") == std::string::npos) { + return FAILURE; + } + size_t bodyStartpos = sizeof("success"); + + body = responseBody.substr(++bodyStartpos, responseBody.size()); */ + return SUCCESS; +} + +template +ExitStatus ArchiveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } + +template +ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "*Name*", &roomName, "*Host*", &roomHost); + RequestHandler::requestToSend = packToJsonString("name", roomName, "host", roomHost); + + return SUCCESS; +} +//ExitStatus CreateRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS;} +template +ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } + +template +ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "*Name*", &roomName, "*Host*", &roomHost); + RequestHandler::requestToSend = packToJsonString("name", roomName, "host", roomHost); + return SUCCESS; +} +//ExitStatus RemoveRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } + +template +ExitStatus RemoveRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } + diff --git a/client/include/responseParser.hpp b/client/include/responseParser.hpp new file mode 100644 index 0000000..b89ec6e --- /dev/null +++ b/client/include/responseParser.hpp @@ -0,0 +1,22 @@ +#pragma once + +/* class AbstractResponseParser { +public: + virtual ~AbstractResponseParser() {}; + + virtual std::shared_ptr> parse(std::string data) = 0; +}; + +template +class TcpStringBodyParser : public AbstractResponseParser { + public: + TcpStringBodyParser(); + ~TcpStringBodyParser() {}; + + std::shared_ptr> parse(std::string data) override; + + private: + std::vector>> registeredRequestFormers; +}; + +#include "bodyParser.tpp" */ \ No newline at end of file diff --git a/client/include/utils.h b/client/include/utils.h index 36b5bc6..16da237 100644 --- a/client/include/utils.h +++ b/client/include/utils.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include #include @@ -9,167 +11,43 @@ const std::string separator = "{}[]\" "; // Все, что связано с сериализацией в JSON -std::vector request_split(const std::string& data) { - std::vector elements; - - std::string element = ""; - - bool special_block = false; - - for(auto it = data.begin(); it != data.end(); it++) { - if (*it == '{' || *it == '}' || *it == '[' || *it == ']') { - if (*it == '[' || *it == '{') { - special_block = true; - } else { - special_block = false; - } - } - - if (*it == ',' && !special_block) { - elements.push_back(element); - element = ""; - } else { - element += *it; - } - } - - return elements; -} - - -std::vector splitString(std::string& data) { - if (data.empty()) { - throw std::runtime_error("Empty values in serialization"); - } - std::cout << data << std::endl; - - std::vector elements; - - std::string element = ""; - std::string tempStr(data); - - auto newEnd = std::remove_if(tempStr.begin(), tempStr.end(), - [](char c) { return separator.find(c) != std::string::npos; }); - tempStr.erase(newEnd, tempStr.end()); - - for(auto it = tempStr.begin(); it != tempStr.end(); it++) { - if ((*it == ':' || *it == ',') /* && !special_block */) { - elements.push_back(element); - element = ""; - } else { - element += *it; - } - } - elements.push_back(element); - - for (auto &i : elements) { - std::cout << i << std::endl; - } - - return elements; -} - - -std::string boolToString(bool value) { - return value ? "true" : "false"; -} - -std::string serialize(const std::string &key, const std::vector vec) { - if (key.empty() || vec.empty()) { - throw std::runtime_error("Empty values in serialization"); - } - - std::string serialized = "\"" + key + "\": [ "; - for (auto &v: vec) { - serialized += "\"" + v + "\", "; - } - serialized.erase(serialized.size() - 2, 1); - serialized += "]"; - - return serialized; -} - -std::string serialize(const std::string &key, const std::string &value) { - if (key.empty() || value.empty()) { - throw std::runtime_error("Empty values in serialization"); - } - - std::string serialized = "\"" + key + "\": \"" + value + "\","; - return serialized; -} - -std::string serialize(const std::string &key, int value) { - if (key.empty()) { - throw std::runtime_error("Empty values in serialization"); - } - std::string serialized = "\"" + key + "\": " + std::to_string(value) + ","; - return serialized; -} - -std::string serialize(const std::string &key, bool value) { - if (key.empty()) { - throw std::runtime_error("Empty values in serialization"); - } - std::string serialized = "\"" + key + "\": " + boolToString(value) + ","; - return serialized; -} +std::vector request_split(const std::string& data); -template -std::string serializeData(T key, K value) { - return serialize(key, value); -} +std::vector splitString(std::string& data); -template -std::string serializeData(T key, K value, Args... args) { - return serialize(key, value) + serializeData(args...); -} +std::string boolToString(bool value); -template -std::string packToJsonString(T key, K value, Args... args) { - std::string jsonStr(""); - jsonStr = "{"; - jsonStr += serializeData(key, value, args...); - jsonStr += "}"; - return jsonStr; -} +std::string serialize(const std::string &key, const std::vector vec); +std::string serialize(const std::string &key, const std::string &value); +std::string serialize(const std::string &key, int value); -// Все, что связано с распаковкой JSON'а +std::string serialize(const std::string &key, bool value); -void fillData(const std::string& jsonStr, const std::string& key, std::vector* inputVec) { - auto vec = request_split(jsonStr); - for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { - inputVec->push_back(*it); - } -} +template +std::string serializeData(T key, K value); +template +std::string serializeData(T key, K value, Args... args); +template +std::string packToJsonString(T key, K value, Args... args); + + + +// Все, что связано с распаковкой JSON'а -void fillData(const std::string& jsonStr, const std::string& key, std::string* inputStr) { - auto vec = request_split(jsonStr); - std::cout << key << std::endl; - for (auto &i : vec) { - std::cout << i << std::endl; - } - std::cout << std::endl; +void fillData(const std::string& jsonStr, const std::string& key, std::vector* inputVec); - auto it = std::find(vec.begin(), vec.end(), key) + 1; - if (it != vec.end()) { - *inputStr = *it; - } -} +void fillData(const std::string& jsonStr, const std::string& key, std::string* inputStr); template -void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal) { - fillData(jsonStr, key, inputVal); -} +void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal); template -void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal, Args... args) { - fillData(jsonStr, key, inputVal); - fillDataFromJson(jsonStr, args...); -} +void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal, Args... args); +#include "utils.tpp" \ No newline at end of file diff --git a/client/include/utils.tpp b/client/include/utils.tpp new file mode 100644 index 0000000..0f1767b --- /dev/null +++ b/client/include/utils.tpp @@ -0,0 +1,41 @@ +#pragma once + + + +template +std::string serializeData(T key, K value) { + return serialize(key, value); +} + +template +std::string serializeData(T key, K value, Args... args) { + return serialize(key, value) + serializeData(args...); +} + + +template +std::string packToJsonString(T key, K value, Args... args) { + std::string jsonStr(""); + jsonStr = "{"; + jsonStr += serializeData(key, value, args...); + jsonStr += "}"; + return jsonStr; +} + + + +// Все, что связано с распаковкой JSON'а + + + +template +void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal) { + fillData(jsonStr, key, inputVal); +} + +template +void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal, Args... args) { + fillData(jsonStr, key, inputVal); + fillDataFromJson(jsonStr, args...); +} + diff --git a/client/src/CMakeLists.txt b/client/src/CMakeLists.txt index c668f63..72397c5 100644 --- a/client/src/CMakeLists.txt +++ b/client/src/CMakeLists.txt @@ -1,10 +1,11 @@ add_executable(main main.cpp) include_directories("${PROJECT_SOURCE_DIR}/include") -add_library(client STATIC model.cpp client.cpp requestHandler.cpp presenter.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) +add_library(client STATIC utils.cpp client.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") - -target_link_libraries(main PUBLIC client) +find_package(Boost REQUIRED COMPONENTS system thread) +message(STATUS "Boost version: ${Boost_VERSION}") +target_link_libraries(main PUBLIC ${Boost_LIBRARIES} PUBLIC client) add_definitions(-Wall -Werror -Wpedantic) diff --git a/client/src/main.cpp b/client/src/main.cpp index 168dc3c..b048484 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -1,8 +1,61 @@ #include "presenter.hpp" +#include +#include +#include +#include +#include +#include + + +const std::string host = "localhost"; +const size_t port = 5555; + +class JsonParser { +public: + explicit JsonParser(std::string& json_str) { + std::stringstream ss; + + ss << json_str; + + try { + boost::property_tree::read_json(ss, pt); + } + catch (...) { + throw std::runtime_error("Failed to parse json!"); + } + } + + template + bool get_value(const char* key, T& value) { + if (boost::optional v = pt.get_optional(key)) { + value = *v; + return true; + } else { + return false; + } + } + + bool get_value(const char* key, std::vector& value) { + if (pt.get_child_optional(key)) { + BOOST_FOREACH (boost::property_tree::ptree::value_type& field, pt.get_child(key)) + { + value.push_back(field.second.data()); + } + return true; + } + + return false; + } + +private: + boost::property_tree::ptree pt; +}; + + int main() { - Presenter pr; + Presenter pr(host, port); pr.run(); return 0; } \ No newline at end of file diff --git a/client/src/model.cpp b/client/src/model.cpp deleted file mode 100644 index 2e55530..0000000 --- a/client/src/model.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include - -#include "model.hpp" -#include "requestHandler.hpp" -#include "room.hpp" -#include "userinfo.hpp" - - -const std::string host = "localhost"; -const size_t port = 5555; - -class ModelImpl { -public: - ModelImpl(); - void passAction(std::string& action, Model& model); - - Client& GetClient() { - return client; - } - - - UserInfo& GetUserInfo() { - return info; - } - - std::shared_ptr GetMainRoom() { - return mainRoom; - } - - std::vector> GetRooms() { - return rooms; - } -private: - std::shared_ptr CreateRequestHandler(std::string& action, Model& model); - - Client client; - UserInfo info; - - std::shared_ptr mainRoom; - std::vector> rooms; -}; - -ModelImpl::ModelImpl() : client(host, port), mainRoom(std::make_shared("default", "me")) { - client.Connect(); -} - -std::shared_ptr ModelImpl::CreateRequestHandler(std::string& action, Model& model) { - std::string::size_type typeEndPos = action.find_first_of(","); - std::string type = action.substr(0, typeEndPos); - - std::shared_ptr handler; - //std::cout << typeEndPos << std::endl; - - switch (atoi(type.c_str())) - { - case 1: - handler = std::make_shared(); - break; - case 2: - handler = std::make_shared(); - break; - case 3: - handler = std::make_shared(); - break; - case 4: - handler = std::make_shared(); - break; - case 5: - handler = std::make_shared(); - break; - case 6: - handler = std::make_shared(); - break; - case 7: - handler = std::make_shared(); - break; - default: - break; - } - - handler->FillRequest(action, model); - - return handler; -} - - -void ModelImpl::passAction(std::string& action, Model& model) { - std::shared_ptr handler = CreateRequestHandler(action, model); - - std::string req = handler->GetRequestToSend(); - - //std::cout << "req: " << req; - client.writeToServer(req); - //std::cout << "*********" << std::endl; - std::string response = client.readFromServer(); - std::cout << std::endl << response << std::endl; - if (handler->HandleResponse(response) == SUCCESS) { - handler->DoLogic(model); - } -} - -Model::Model() : modelImpl(new ModelImpl) {} -Model::~Model() {} - -std::string Model::GetMainRoomInfo() { - std::string ret = modelImpl->GetMainRoom()->GetRoomName() + modelImpl->GetMainRoom()->GetRoomHost(); - return ret; -} - -void Model::PassAction(std::string& action) { - modelImpl->passAction(action, *this); -} \ No newline at end of file diff --git a/client/src/presenter.cpp b/client/src/presenter.cpp deleted file mode 100644 index eda1c6e..0000000 --- a/client/src/presenter.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "presenter.hpp" - -#include - -Presenter::Presenter() { -} - -Presenter::~Presenter() { -} - -void Presenter::run() { - std::string action = view.GetRequest(); - while(!action.empty()) { - model.PassAction(action); - action = view.GetRequest(); - } -} \ No newline at end of file diff --git a/client/src/requestHandler.cpp b/client/src/requestHandler.cpp deleted file mode 100644 index d7ee1dd..0000000 --- a/client/src/requestHandler.cpp +++ /dev/null @@ -1,140 +0,0 @@ - -#include -#include "requestHandler.hpp" -#include "utils.h" - - - - -std::string& RequestHandler::GetRequestToSend() { - return requestToSend; -} - - -ExitStatus RequestHandler::HandleResponse(std::string& responseBody) { - if (responseBody.find("success") != std::string::npos) { - return SUCCESS; - } - return FAILURE; -} - -ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { - //std::cout << action << std::endl; - - /* auto vec = request_split(action); - - for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { - users.push_back(*it); - } */ - /* for (auto &i : vec) { - std::cout << i << std::endl; - } */ - - fillDataFromJson(action, "*Users*", &users); - - - /* std::string kk = packToJsonString("users", users); - - auto vec2 = splitString(kk); - - for (auto &i : vec2) { - std::cout << i << std::endl; - } */ - - requestToSend = packToJsonString("users", users); - - return SUCCESS; -} - -//ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { return SUCCESS; } -ExitStatus AddUsersReqHandler::DoLogic(Model &model) { - return SUCCESS; -} - - -ExitStatus RemoveUsersReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Users*", &users); - requestToSend = packToJsonString("users", users); - return SUCCESS; -} - -//ExitStatus LogOutReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } -ExitStatus RemoveUsersReqHandler::DoLogic(Model &model) { return SUCCESS; } - - -ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { - /* auto vec = splitString(action); - - auto it = std::find(vec.begin(), vec.end(), "*Name*") + 1; - if (it != vec.end()) { - linkName = *it; - } - it = std::find(vec.begin(), vec.end(), "*Url*") + 1; - if (it != vec.end()) { - url = *it; - } */ - - //std::cout << action << std::endl; - - fillDataFromJson(action, "*Name*", &linkName, "*Url*", &url); - //std::cout << linkName << std::endl; - //std::cout << url << std::endl; - - requestToSend = packToJsonString("linkname", linkName, "url", url); - - return SUCCESS; -} - -//ExitStatus AddLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } -ExitStatus AddLinkReqHandler::DoLogic(Model &model) { - - return SUCCESS; -} - - - -ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &linkName); - requestToSend = packToJsonString("linkname", linkName); - - return SUCCESS; -} -//ExitStatus RemoveLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } -ExitStatus RemoveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } - - -ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &linkName); - requestToSend = packToJsonString("linkname", linkName); - - return SUCCESS; -} -ExitStatus ArchiveLinkReqHandler::HandleResponse(std::string &responseBody) { - /* if (responseBody.find("success") == std::string::npos) { - return FAILURE; - } - size_t bodyStartpos = sizeof("success"); - - body = responseBody.substr(++bodyStartpos, responseBody.size()); */ - return SUCCESS; -} -ExitStatus ArchiveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } - - -ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &roomName, "*Host*", roomHost); - requestToSend = packToJsonString("name", roomName, "host", roomHost); - - return SUCCESS; -} -//ExitStatus CreateRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS;} -ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } - -ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &roomName, "*Host*", roomHost); - requestToSend = packToJsonString("name", roomName, "host", roomHost); - return SUCCESS; -} -//ExitStatus RemoveRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } -ExitStatus RemoveRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } - diff --git a/client/src/utils.cpp b/client/src/utils.cpp new file mode 100644 index 0000000..4c954b8 --- /dev/null +++ b/client/src/utils.cpp @@ -0,0 +1,133 @@ +#include "utils.h" + +std::vector request_split(const std::string& data) { + std::vector elements; + + std::string element = ""; + + bool special_block = false; + + for(auto it = data.begin(); it != data.end(); it++) { + if (*it == '{' || *it == '}' || *it == '[' || *it == ']') { + if (*it == '[' || *it == '{') { + special_block = true; + } else { + special_block = false; + } + } + + if (*it == ',' && !special_block) { + elements.push_back(element); + element = ""; + } else { + element += *it; + } + } + + return elements; +} + + +std::vector splitString(std::string& data) { + if (data.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::cout << data << std::endl; + + std::vector elements; + + std::string element = ""; + std::string tempStr(data); + + auto newEnd = std::remove_if(tempStr.begin(), tempStr.end(), + [](char c) { return separator.find(c) != std::string::npos; }); + tempStr.erase(newEnd, tempStr.end()); + + for(auto it = tempStr.begin(); it != tempStr.end(); it++) { + if ((*it == ':' || *it == ',') /* && !special_block */) { + elements.push_back(element); + element = ""; + } else { + element += *it; + } + } + elements.push_back(element); + + for (auto &i : elements) { + std::cout << i << std::endl; + } + + return elements; +} + + +std::string boolToString(bool value) { + return value ? "true" : "false"; +} + +std::string serialize(const std::string &key, const std::vector vec) { + if (key.empty() || vec.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + + std::string serialized = "\"" + key + "\": [ "; + for (auto &v: vec) { + serialized += "\"" + v + "\", "; + } + serialized.erase(serialized.size() - 2, 1); + serialized += "]"; + + return serialized; +} + +std::string serialize(const std::string &key, const std::string &value) { + if (key.empty() || value.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + + std::string serialized = "\"" + key + "\": \"" + value + "\","; + return serialized; +} + +std::string serialize(const std::string &key, int value) { + if (key.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::string serialized = "\"" + key + "\": " + std::to_string(value) + ","; + return serialized; +} + +std::string serialize(const std::string &key, bool value) { + if (key.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::string serialized = "\"" + key + "\": " + boolToString(value) + ","; + return serialized; +} + + + + +void fillData(const std::string& jsonStr, const std::string& key, std::vector* inputVec) { + auto vec = request_split(jsonStr); + for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { + inputVec->push_back(*it); + } +} + + + +void fillData(const std::string& jsonStr, const std::string& key, std::string* inputStr) { + auto vec = request_split(jsonStr); + + std::cout << key << std::endl; + for (auto &i : vec) { + std::cout << i << std::endl; + } + std::cout << std::endl; + + auto it = std::find(vec.begin(), vec.end(), key) + 1; + if (it != vec.end()) { + *inputStr = *it; + } +} \ No newline at end of file diff --git a/client/src/view.cpp b/client/src/view.cpp index 7079633..e1eeb14 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -1,4 +1,5 @@ #include "view.hpp" +#include "utils.h" #include @@ -16,20 +17,20 @@ std::string ConsoleView::GetRequest() { inputStr += "1,"; std::cout << "Write name of room" << std::endl; std::cin >> appendStr; - inputStr += "*Name*" + appendStr; + inputStr += "*Name*," + appendStr + ","; std::cout << "Write host of room" << std::endl; std::cin >> appendStr; - inputStr += "*Host*" + appendStr; + inputStr += "*Host*," + appendStr + ","; } break; case 2: { inputStr += "2,"; std::cout << "Write name of room" << std::endl; std::cin >> appendStr; - inputStr += "*Name*" + appendStr; + inputStr += "*Name," + appendStr + ","; std::cout << "Write host of room" << std::endl; std::cin >> appendStr; - inputStr += "*Host*" + appendStr; + inputStr += "*Host*," + appendStr + ","; } break; case 3: { @@ -76,14 +77,14 @@ std::string ConsoleView::GetRequest() { inputStr += "6,"; std::cout << "Write name of link" << std::endl; std::cin >> appendStr; - inputStr += "*Name*" + appendStr; + inputStr += "*Name*," + appendStr + ","; } break; case 7: { inputStr += "7,"; std::cout << "Write name of link" << std::endl; std::cin >> appendStr; - inputStr += "*Name*" + appendStr; + inputStr += "*Name*," + appendStr + ","; } break; case 0: From f14559ef7df715805587c8668eaa23c9e5dcf60e Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 13:53:48 +0300 Subject: [PATCH 19/36] changed consoleView --- client/include/inputUtils.hpp | 25 ++++++ client/include/inputUtils.tpp | 14 ++++ client/include/model.tpp | 35 ++++++--- client/include/presenter.tpp | 1 + client/include/requestHandler.hpp | 24 ++++++ client/include/requestHandler.tpp | 95 ++++++++++++----------- client/include/utils.h | 12 +-- client/include/utils.tpp | 1 + client/src/CMakeLists.txt | 2 +- client/src/inputUtils.cpp | 96 +++++++++++++++++++++++ client/src/utils.cpp | 32 ++++++-- client/src/view.cpp | 122 +++++++++++++----------------- 12 files changed, 320 insertions(+), 139 deletions(-) create mode 100644 client/include/inputUtils.hpp create mode 100644 client/include/inputUtils.tpp create mode 100644 client/src/inputUtils.cpp diff --git a/client/include/inputUtils.hpp b/client/include/inputUtils.hpp new file mode 100644 index 0000000..7db48b6 --- /dev/null +++ b/client/include/inputUtils.hpp @@ -0,0 +1,25 @@ +#pragma once +#include +#include + +template +void writeData(T* key); + +template +void writeData(T* key, Args... args); + +void fillObject(std::string* str); +void fillObject(std::vector* str); + + +std::string createRoomInput(); +std::string deleteRoomInput(); +std::string addUsersInput(); +std::string deleteUsersInput(); +std::string addLinkInput(); +std::string deleteLinkInput(); +std::string makeSnapshotInput(); +std::string logInInput(); +std::string signUpInput(); + +#include "inputUtils.tpp" \ No newline at end of file diff --git a/client/include/inputUtils.tpp b/client/include/inputUtils.tpp new file mode 100644 index 0000000..343d3a9 --- /dev/null +++ b/client/include/inputUtils.tpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +template +void writeData(T* key) { + fillObject(key); +} + +template +void writeData(T* key, Args... args) { + fillObject(key); + writeData(args...); +} \ No newline at end of file diff --git a/client/include/model.tpp b/client/include/model.tpp index bbf0be7..c4eb372 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -7,7 +7,7 @@ #include "requestHandler.hpp" #include "room.hpp" #include "userinfo.hpp" -//#include "utils.h" +#include "utils.h" /* const std::string host = "localhost"; @@ -54,35 +54,44 @@ ModelImpl::ModelImpl() : mainRoom(std::make_shared("defaul template std::shared_ptr> ModelImpl::CreateRequestHandler(std::string& action, Model& model) { - std::string::size_type typeEndPos = action.find_first_of(","); - std::string type = action.substr(0, typeEndPos); + /* std::string::size_type typeEndPos = action.find_first_of(","); + std::string type = action.substr(0, typeEndPos); */ + std::cout << action << std::endl; + std::string type; + fillDataFromJson(action, "command", &type); std::shared_ptr> handler; //std::cout << typeEndPos << std::endl; - switch (atoi(type.c_str())) + switch (atoi(type.c_str()) ) { - case 1: + case 0: handler = std::make_shared>(); break; - case 2: + case 1: handler = std::make_shared>(); break; - case 3: + case 2: handler = std::make_shared>(); break; - case 4: + case 3: handler = std::make_shared>(); break; - case 5: + case 4: handler = std::make_shared>(); break; - case 6: + case 5: handler = std::make_shared>(); break; - case 7: + case 6: handler = std::make_shared>(); break; + case 7: + handler = std::make_shared>(); + break; + case 8: + handler = std::make_shared>(); + break; default: break; } @@ -115,7 +124,9 @@ std::string ModelImpl::formRequest(std::string& action, Model void ModelImpl::handleResponse(std::string& response, Model& model) { - currentHandler->DoLogic(model); + if (currentHandler->HandleResponse(response)) { + currentHandler->DoLogic(model); + } } template diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp index 87d707f..0fc1be5 100644 --- a/client/include/presenter.tpp +++ b/client/include/presenter.tpp @@ -1,6 +1,7 @@ /* #include "presenter.hpp" */ #pragma once +#include #include template diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index 08b48b1..5124ddf 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -106,4 +106,28 @@ class RemoveRoomReqHandler : public RequestHandler { std::string roomHost; }; +template +class LogInReqHandler : public RequestHandler { +public: + LogInReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string login; + std::string password; +}; + +template +class SignUpReqHandler : public RequestHandler { +public: + SignUpReqHandler() = default; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string login; + std::string password; +}; + #include "requestHandler.tpp" \ No newline at end of file diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index a612d33..0b648f0 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -18,36 +18,29 @@ ExitStatus RequestHandler::HandleResponse(std::string& responseB template ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { - //std::cout << action << std::endl; - - /* auto vec = request_split(action); - - for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { - users.push_back(*it); - } */ - /* for (auto &i : vec) { - std::cout << i << std::endl; - } */ - - fillDataFromJson(action, "*Users*", &users); - - - /* std::string kk = packToJsonString("users", users); - - auto vec2 = splitString(kk); - - for (auto &i : vec2) { - std::cout << i << std::endl; - } */ - + fillDataFromJson(action, "users", &users); RequestHandler::requestToSend = packToJsonString("users", users); - return SUCCESS; } +template +ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + std::string password; + if (!RequestHandler::parser->get_value("password", password)) { + throw std::runtime_error("Failed to read JSON values!"); + }; + std::cout << "---->>> " << password << std::endl; -//ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { return SUCCESS; } + + return SUCCESS; +} template ExitStatus AddUsersReqHandler::DoLogic(Model &model) { @@ -56,7 +49,7 @@ ExitStatus AddUsersReqHandler::DoLogic(Model &mo template ExitStatus RemoveUsersReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Users*", &users); + fillDataFromJson(action, "users", &users); RequestHandler::requestToSend = packToJsonString("users", users); return SUCCESS; } @@ -67,22 +60,8 @@ ExitStatus RemoveUsersReqHandler::DoLogic(Model template ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { - /* auto vec = splitString(action); - - auto it = std::find(vec.begin(), vec.end(), "*Name*") + 1; - if (it != vec.end()) { - linkName = *it; - } - it = std::find(vec.begin(), vec.end(), "*Url*") + 1; - if (it != vec.end()) { - url = *it; - } */ - - //std::cout << action << std::endl; - - fillDataFromJson(action, "*Name*", &linkName, "*Url*", &url); - //std::cout << linkName << std::endl; - //std::cout << url << std::endl; + + fillDataFromJson(action, "name", &linkName, "url", &url); RequestHandler::requestToSend = packToJsonString("linkname", linkName, "url", url); @@ -99,7 +78,7 @@ ExitStatus AddLinkReqHandler::DoLogic(Model &mod template ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &linkName); + fillDataFromJson(action, "name", &linkName); RequestHandler::requestToSend = packToJsonString("linkname", linkName); return SUCCESS; @@ -110,7 +89,7 @@ ExitStatus RemoveLinkReqHandler::DoLogic(Model & template ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &linkName); + fillDataFromJson(action, "name", &linkName); RequestHandler::requestToSend = packToJsonString("linkname", linkName); return SUCCESS; @@ -132,7 +111,9 @@ ExitStatus ArchiveLinkReqHandler::DoLogic(Model template ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &roomName, "*Host*", &roomHost); + //std::cout << action << std::endl; + fillDataFromJson(action, "name", &roomName, "host", &roomHost); + RequestHandler::requestToSend = packToJsonString("name", roomName, "host", roomHost); return SUCCESS; @@ -143,7 +124,7 @@ ExitStatus CreateRoomReqHandler::DoLogic(Model & template ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "*Name*", &roomName, "*Host*", &roomHost); + fillDataFromJson(action, "name", &roomName, "host", &roomHost); RequestHandler::requestToSend = packToJsonString("name", roomName, "host", roomHost); return SUCCESS; } @@ -152,3 +133,27 @@ ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, template ExitStatus RemoveRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } + +template +ExitStatus LogInReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "login", &login, "password", &password); + RequestHandler::requestToSend = packToJsonString("login", login, "password", password); + return SUCCESS; +} +//ExitStatus LogInReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } + +template +ExitStatus LogInReqHandler::DoLogic(Model &model) { return SUCCESS; } + + + +template +ExitStatus SignUpReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "login", &login, "password", &password); + RequestHandler::requestToSend = packToJsonString("login", login, "password", password); + return SUCCESS; +} +//ExitStatus LogInReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } + +template +ExitStatus SignUpReqHandler::DoLogic(Model &model) { return SUCCESS; } diff --git a/client/include/utils.h b/client/include/utils.h index 16da237..f289247 100644 --- a/client/include/utils.h +++ b/client/include/utils.h @@ -11,10 +11,6 @@ const std::string separator = "{}[]\" "; // Все, что связано с сериализацией в JSON -std::vector request_split(const std::string& data); - -std::vector splitString(std::string& data); - std::string boolToString(bool value); @@ -24,7 +20,7 @@ std::string serialize(const std::string &key, const std::string &value); std::string serialize(const std::string &key, int value); -std::string serialize(const std::string &key, bool value); +/* std::string serialize(const std::string &key, bool value); */ template std::string serializeData(T key, K value); @@ -40,10 +36,16 @@ std::string packToJsonString(T key, K value, Args... args); // Все, что связано с распаковкой JSON'а +std::vector request_split(const std::string& data); + +std::vector splitString(const std::string& data); + void fillData(const std::string& jsonStr, const std::string& key, std::vector* inputVec); void fillData(const std::string& jsonStr, const std::string& key, std::string* inputStr); +void fillData(const std::string& jsonStr, const std::string& key, int* val); + template void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal); diff --git a/client/include/utils.tpp b/client/include/utils.tpp index 0f1767b..7b9834f 100644 --- a/client/include/utils.tpp +++ b/client/include/utils.tpp @@ -18,6 +18,7 @@ std::string packToJsonString(T key, K value, Args... args) { std::string jsonStr(""); jsonStr = "{"; jsonStr += serializeData(key, value, args...); + jsonStr.pop_back(); jsonStr += "}"; return jsonStr; } diff --git a/client/src/CMakeLists.txt b/client/src/CMakeLists.txt index 72397c5..d6d2c98 100644 --- a/client/src/CMakeLists.txt +++ b/client/src/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable(main main.cpp) include_directories("${PROJECT_SOURCE_DIR}/include") -add_library(client STATIC utils.cpp client.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) +add_library(client STATIC utils.cpp inputUtils.cpp client.cpp view.cpp socket.cpp link.cpp room.cpp userinfo.cpp) target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") find_package(Boost REQUIRED COMPONENTS system thread) diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp new file mode 100644 index 0000000..e3f649e --- /dev/null +++ b/client/src/inputUtils.cpp @@ -0,0 +1,96 @@ +#include "inputUtils.hpp" +#include "utils.h" + +void fillObject(std::string* str) { + std::cin >> *str; +} +void fillObject(std::vector* vec) { + size_t amount = 0; + std::string newObj; + std::cin >> amount; + while(amount > 0) { + std::cin >> newObj; + vec->push_back(newObj); + --amount; + } +} + + +std::string createRoomInput() { + std::string name; + std::string host; + + writeData(&name, &host); + + std::string ret = packToJsonString("command", "0", "name", name, "host", host); + return ret; +} + +std::string deleteRoomInput() { + std::string name; + std::string host; + + writeData(&name, &host); + + std::string ret = packToJsonString("command","1", "name", name, "host", host); + return ret; +} + +std::string addUsersInput() { + std::vector vec; + writeData(&vec); + + std::string ret = packToJsonString("command","2", "users", vec); + return ret; +} + +std::string deleteUsersInput() { + std::vector vec; + writeData(&vec); + + std::string ret = packToJsonString("command","3", "users", vec); + return ret; +} +std::string addLinkInput() { + std::string name; + std::string url; + + writeData(&name, &url); + + std::string ret = packToJsonString("command","4", "name", name, "url", url); + return ret; +} +std::string deleteLinkInput() { + std::string name; + + writeData(&name); + + std::string ret = packToJsonString("command","5", "name", name); + return ret; +} +std::string makeSnapshotInput() { + std::string name; + + writeData(&name); + + std::string ret = packToJsonString("command","6", "name", name); + return ret; +} +std::string logInInput() { + std::string login; + std::string password; + + writeData(&login, &password); + + std::string ret = packToJsonString("command","7", "login", login, "password", password); + return ret; +} +std::string signUpInput() { + std::string login; + std::string password; + + writeData(&login, &password); + + std::string ret = packToJsonString("command","8", "login", login, "password", password); + return ret; +} \ No newline at end of file diff --git a/client/src/utils.cpp b/client/src/utils.cpp index 4c954b8..aede5fe 100644 --- a/client/src/utils.cpp +++ b/client/src/utils.cpp @@ -28,7 +28,7 @@ std::vector request_split(const std::string& data) { } -std::vector splitString(std::string& data) { +std::vector splitString(const std::string& data) { if (data.empty()) { throw std::runtime_error("Empty values in serialization"); } @@ -97,20 +97,20 @@ std::string serialize(const std::string &key, int value) { return serialized; } -std::string serialize(const std::string &key, bool value) { +/* std::string serialize(const std::string &key, bool value) { if (key.empty()) { throw std::runtime_error("Empty values in serialization"); } std::string serialized = "\"" + key + "\": " + boolToString(value) + ","; return serialized; } - + */ void fillData(const std::string& jsonStr, const std::string& key, std::vector* inputVec) { - auto vec = request_split(jsonStr); - for (auto it = std::find(vec.begin(), vec.end(), "*Users*") + 1; it != vec.end(); it++) { + auto vec = splitString(jsonStr); + for (auto it = std::find(vec.begin(), vec.end(), key) + 1; it != vec.end(); it++) { inputVec->push_back(*it); } } @@ -118,16 +118,32 @@ void fillData(const std::string& jsonStr, const std::string& key, std::vector IView::~IView() {} +enum RequestCommand { + CREATE_ROOM, + DELETE_ROOM, + ADD_USERS, + DELETE_USERS, + ADD_LINK, + DELETE_LINK, + MAKE_SNAPSHOT, + LOG_IN_USER, + SIGN_UP_USER, +}; + + std::string ConsoleView::GetRequest() { PrintCommands(); @@ -13,81 +26,52 @@ std::string ConsoleView::GetRequest() { int key = 0; std::cin >> key; switch (key) { - case 1: { - inputStr += "1,"; - std::cout << "Write name of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Name*," + appendStr + ","; - std::cout << "Write host of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Host*," + appendStr + ","; + case CREATE_ROOM: { + std::cout << "Write name and host of room" << std::endl; + inputStr = createRoomInput(); } break; - case 2: { - inputStr += "2,"; - std::cout << "Write name of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Name," + appendStr + ","; - std::cout << "Write host of room" << std::endl; - std::cin >> appendStr; - inputStr += "*Host*," + appendStr + ","; + case DELETE_ROOM: { + std::cout << "Write name and host of room" << std::endl; + inputStr = deleteRoomInput(); } break; - case 3: { - inputStr += "3,"; - std::cout << "Write amount of users" << std::endl; - size_t amount = 0; - std::cin >> amount; - inputStr += "*Users*,"; - std::cout << "Write users" << std::endl; - while(amount > 0) { - //std::getline(std::cin, appendStr); - std::cin >> appendStr; - inputStr += appendStr + ","; - --amount; - /* std::cout << amount<< std::endl; */ - } + case ADD_USERS: { + std::cout << "Write amount of users and list of users" << std::endl; + inputStr = addUsersInput(); } break; - case 4: { - inputStr += "4,"; - std::cout << "Write amount of users" << std::endl; - size_t amount = 0; - std::cin >> amount; - inputStr += "*Users*,"; - std::cout << "Write users" << std::endl; - while(amount > 0) { - std::cin >> appendStr; - inputStr += appendStr + ","; - --amount; - } + case DELETE_USERS: { + std::cout << "Write amount of users and list of users" << std::endl; + inputStr = deleteUsersInput(); } break; - case 5: { - inputStr += "5,"; + case ADD_LINK: { + std::cout << "Write name and url of link" << std::endl; + inputStr = addLinkInput(); + break; + } + case DELETE_LINK: { std::cout << "Write name of link" << std::endl; - std::cin >> appendStr; - inputStr += "*Name*," + appendStr + ","; - std::cout << "Write url of link" << std::endl; - std::cin >> appendStr; - inputStr += "*Url*," + appendStr + ","; + inputStr = deleteLinkInput(); break; } - case 6: { - inputStr += "6,"; + case MAKE_SNAPSHOT: { std::cout << "Write name of link" << std::endl; - std::cin >> appendStr; - inputStr += "*Name*," + appendStr + ","; + inputStr = makeSnapshotInput(); + break; } + case LOG_IN_USER: { + std::cout << "Write login and password" << std::endl; + inputStr = logInInput(); break; - case 7: { - inputStr += "7,"; - std::cout << "Write name of link" << std::endl; - std::cin >> appendStr; - inputStr += "*Name*," + appendStr + ","; } + case SIGN_UP_USER: { + std::cout << "Write login and password" << std::endl; + inputStr = signUpInput(); break; - case 0: + } + case -1: break; default: std::cout << "Wrong command. Try again" << std::endl; @@ -99,12 +83,14 @@ std::string ConsoleView::GetRequest() { void ConsoleView::PrintCommands() { std::cout << std::endl; std::cout << "Please choose command type:" << std::endl; - std::cout << "- 1.Create Room" << std::endl - << "- 2.Remove Room" << std::endl - << "- 3.Add users" << std::endl - << "- 4.Remove users" << std::endl - << "- 5.Add link" << std::endl - << "- 6.Remove link" << std::endl - << "- 7.Archive link" << std::endl - << "- 0.Exit" << std::endl; + std::cout << "- 0.Create Room" << std::endl + << "- 1.Remove Room" << std::endl + << "- 2.Add users" << std::endl + << "- 3.Remove users" << std::endl + << "- 4.Add link" << std::endl + << "- 5.Remove link" << std::endl + << "- 6.Make snapshot" << std::endl + << "- 7.Log in" << std::endl + << "- 8.Sign up" << std::endl + << "- (-1).Exit" << std::endl; } \ No newline at end of file From c0990095178e78348628cba36655a17336548cb4 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 15:22:44 +0300 Subject: [PATCH 20/36] login handler --- client/include/model.hpp | 1 + client/include/model.tpp | 16 ++++++-- client/include/requestHandler.hpp | 1 + client/include/requestHandler.tpp | 67 ++++++++++++++++++++----------- client/include/responseParser.hpp | 22 ---------- client/include/userinfo.hpp | 5 ++- client/src/userinfo.cpp | 27 ++++++++++++- 7 files changed, 87 insertions(+), 52 deletions(-) delete mode 100644 client/include/responseParser.hpp diff --git a/client/include/model.hpp b/client/include/model.hpp index 93b4ed8..aae8ad4 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -13,6 +13,7 @@ class Model { ~Model(); std::string GetMainRoomInfo(); //void PassAction(std::string& action); + void SetUserInfo(const std::string& str); std::string FormRequest(std::string& action); void HandleResponse(std::string& response); private: diff --git a/client/include/model.tpp b/client/include/model.tpp index c4eb372..cb9bd29 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -26,13 +26,18 @@ public: std::string formRequest(std::string& action, Model& model); void handleResponse(std::string& response, Model& model); - UserInfo& GetUserInfo() { - return info; + void SetUserInfoFromStr(const std::string& str) { + return info.setUserInfo(str); + } + + std::string GetUserInfo() { + return info.getInfoStr(); } std::shared_ptr GetMainRoom() { return mainRoom; } + std::vector> GetRooms() { return rooms; @@ -124,7 +129,7 @@ std::string ModelImpl::formRequest(std::string& action, Model void ModelImpl::handleResponse(std::string& response, Model& model) { - if (currentHandler->HandleResponse(response)) { + if (!currentHandler->HandleResponse(response)) { currentHandler->DoLogic(model); } } @@ -140,6 +145,11 @@ std::string Model::GetMainRoomInfo() { return ret; } +template +void Model::SetUserInfo(const std::string& str) { + modelImpl->SetUserInfoFromStr(str); + } + /* void Model::PassAction(std::string& action) { modelImpl->passAction(action, *this); } */ diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index 5124ddf..d601dc6 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -114,6 +114,7 @@ class LogInReqHandler : public RequestHandler { ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: + std::string uuid; std::string login; std::string password; }; diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index 0b648f0..41c4aa4 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -10,35 +10,29 @@ std::string& RequestHandler::GetRequestToSend() { template ExitStatus RequestHandler::HandleResponse(std::string& responseBody) { - if (responseBody.find("success") != std::string::npos) { - return SUCCESS; - } - return FAILURE; -} - -template -ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "users", &users); - RequestHandler::requestToSend = packToJsonString("users", users); - return SUCCESS; -} - - -template -ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { try { RequestHandler::parser = std::make_shared(responseBody); } catch (...) { throw std::runtime_error("Failed to parse JSON!"); } - std::string password; - if (!RequestHandler::parser->get_value("password", password)) { - throw std::runtime_error("Failed to read JSON values!"); - }; - std::cout << "---->>> " << password << std::endl; + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + std::cout << "success!" << std::endl; + } else { + return FAILURE; + } + return SUCCESS; +} +template +ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "users", &users); + RequestHandler::requestToSend = packToJsonString("users", users); return SUCCESS; } @@ -140,10 +134,35 @@ ExitStatus LogInReqHandler::FillRequest(std::string action, Mode RequestHandler::requestToSend = packToJsonString("login", login, "password", password); return SUCCESS; } -//ExitStatus LogInReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +template +ExitStatus LogInReqHandler::HandleResponse(std::string& responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("uuid", uuid)); + std::cout << "success!" << std::endl; + } else { + return FAILURE; + } + + return SUCCESS; +} template -ExitStatus LogInReqHandler::DoLogic(Model &model) { return SUCCESS; } +ExitStatus LogInReqHandler::DoLogic(Model &model) { + std::string info = packToJsonString("login", login, "password", password, "uuid", uuid); + model.SetUserInfo(info); + return SUCCESS; +} @@ -153,7 +172,7 @@ ExitStatus SignUpReqHandler::FillRequest(std::string action, Mod RequestHandler::requestToSend = packToJsonString("login", login, "password", password); return SUCCESS; } -//ExitStatus LogInReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } +//ExitStatus SignUpReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } template ExitStatus SignUpReqHandler::DoLogic(Model &model) { return SUCCESS; } diff --git a/client/include/responseParser.hpp b/client/include/responseParser.hpp deleted file mode 100644 index b89ec6e..0000000 --- a/client/include/responseParser.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -/* class AbstractResponseParser { -public: - virtual ~AbstractResponseParser() {}; - - virtual std::shared_ptr> parse(std::string data) = 0; -}; - -template -class TcpStringBodyParser : public AbstractResponseParser { - public: - TcpStringBodyParser(); - ~TcpStringBodyParser() {}; - - std::shared_ptr> parse(std::string data) override; - - private: - std::vector>> registeredRequestFormers; -}; - -#include "bodyParser.tpp" */ \ No newline at end of file diff --git a/client/include/userinfo.hpp b/client/include/userinfo.hpp index 59e9e80..08a257f 100644 --- a/client/include/userinfo.hpp +++ b/client/include/userinfo.hpp @@ -5,10 +5,13 @@ class UserInfo{ public: UserInfo(); - UserInfo(std::string nm, std::string psw); + UserInfo(std::string name, std::string password, std::string uuid); + std::string getInfoStr(); + void setUserInfo(const std::string& str); ~UserInfo(); private: std::string name; std::string password; + std::string uuid; }; \ No newline at end of file diff --git a/client/src/userinfo.cpp b/client/src/userinfo.cpp index f66e641..dea7e46 100644 --- a/client/src/userinfo.cpp +++ b/client/src/userinfo.cpp @@ -1,6 +1,29 @@ #include "userinfo.hpp" +#include "utils.h" +#include -UserInfo::UserInfo() : name(""), password("") {} -UserInfo::UserInfo(std::string nm, std::string psw) : name(std::move(nm)), password(std::move(psw)){ +UserInfo::UserInfo() : name(""), password(""), uuid("") {} +UserInfo::UserInfo(std::string name, std::string password, std::string uuid) +: name(std::move(name)), + password(std::move(password)), + uuid(std::move(uuid)) { } + +std::string UserInfo::getInfoStr() { + if (name.empty() || name.empty() || name.empty()) { + throw std::runtime_error("User info is empty!"); + } + + std::string ret = packToJsonString("name", name, "password", password, "uuid", uuid); + return ret; +} + +void UserInfo::setUserInfo(const std::string& str) { + if (str.empty()) { + throw std::runtime_error("User info is empty!"); + } + fillDataFromJson(str, "name", &name, "password", &password, "uuid", &uuid); +} + + UserInfo::~UserInfo() {} From 255bc0d711d132b9bf722d17ff91e7f457dd3d88 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 17:23:56 +0300 Subject: [PATCH 21/36] add link handler --- client/include/link.hpp | 5 ++-- client/include/model.hpp | 2 ++ client/include/model.tpp | 21 +++++++++---- client/include/requestHandler.hpp | 7 +++-- client/include/requestHandler.tpp | 39 ++++++++++++++++++++---- client/include/room.hpp | 2 +- client/include/socket.hpp | 1 + client/src/client.cpp | 4 +-- client/src/link.cpp | 21 ++++++++----- client/src/room.cpp | 19 +++++++----- client/src/socket.cpp | 50 ++++++++++++++++++++++++------- client/src/userinfo.cpp | 6 ++-- client/src/view.cpp | 18 +++++------ 13 files changed, 138 insertions(+), 57 deletions(-) diff --git a/client/include/link.hpp b/client/include/link.hpp index ab29643..bee9604 100644 --- a/client/include/link.hpp +++ b/client/include/link.hpp @@ -5,9 +5,10 @@ class LinkImpl; class Link { public: - Link(std::string& name, std::string& url); + Link(std::string& name, std::string& url, std::string& uuid); ~Link(); - const std::string GetLinkName(); + std::string GetLinkInfo(); + void SetLinkInfo(); void addSnapshot(std::string& path); private: diff --git a/client/include/model.hpp b/client/include/model.hpp index aae8ad4..b90d252 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -14,6 +14,8 @@ class Model { std::string GetMainRoomInfo(); //void PassAction(std::string& action); void SetUserInfo(const std::string& str); + std::string GetUserInfo(); + void AddLink(std::string& linkInfo); std::string FormRequest(std::string& action); void HandleResponse(std::string& response); private: diff --git a/client/include/model.tpp b/client/include/model.tpp index cb9bd29..f1d561f 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -10,9 +10,6 @@ #include "utils.h" -/* const std::string host = "localhost"; -const size_t port = 5555; */ - template class ModelImpl { public: @@ -34,6 +31,10 @@ public: return info.getInfoStr(); } + void addLink(std::string& linkInfo) { + mainRoom->addLink(linkInfo); + } + std::shared_ptr GetMainRoom() { return mainRoom; } @@ -59,14 +60,11 @@ ModelImpl::ModelImpl() : mainRoom(std::make_shared("defaul template std::shared_ptr> ModelImpl::CreateRequestHandler(std::string& action, Model& model) { - /* std::string::size_type typeEndPos = action.find_first_of(","); - std::string type = action.substr(0, typeEndPos); */ std::cout << action << std::endl; std::string type; fillDataFromJson(action, "command", &type); std::shared_ptr> handler; - //std::cout << typeEndPos << std::endl; switch (atoi(type.c_str()) ) { @@ -163,4 +161,15 @@ std::string Model::FormRequest(std::string& action) { template void Model::HandleResponse(std::string& response) { modelImpl->handleResponse(response, *this); +} + +template +std::string Model::GetUserInfo() { + std::string ret = modelImpl->GetUserInfo(); + return ret; +} + +template +void Model::AddLink(std::string& linkInfo) { + modelImpl->addLink(linkInfo); } \ No newline at end of file diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index d601dc6..f60d0d0 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -52,11 +52,12 @@ class AddLinkReqHandler : public RequestHandler { public: AddLinkReqHandler() = default; ExitStatus FillRequest(std::string action, Model& model); - //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: - std::string linkName = ""; - std::string url = ""; + std::string uuid; + std::string linkName; + std::string url; }; template diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index 41c4aa4..d14ee8f 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -56,16 +56,43 @@ template ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "name", &linkName, "url", &url); + + std::string info = model.GetUserInfo(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); - RequestHandler::requestToSend = packToJsonString("linkname", linkName, "url", url); + RequestHandler::requestToSend = packToJsonString("command", 4, "login", login, "token", token, "name", linkName, "url", url, "description", " "); return SUCCESS; } -//ExitStatus AddLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } template -ExitStatus AddLinkReqHandler::DoLogic(Model &model) { +ExitStatus AddLinkReqHandler::HandleResponse(std::string &responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("uuid", uuid)); + std::cout << "success!" << std::endl; + } else { + return FAILURE; + } + + return SUCCESS; +} +template +ExitStatus AddLinkReqHandler::DoLogic(Model &model) { + std::string linkInfo = packToJsonString("name", linkName, "url", url, "uuid", uuid); + model.AddLink(linkInfo); return SUCCESS; } @@ -131,7 +158,8 @@ ExitStatus RemoveRoomReqHandler::DoLogic(Model & template ExitStatus LogInReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "login", &login, "password", &password); - RequestHandler::requestToSend = packToJsonString("login", login, "password", password); + RequestHandler::requestToSend = packToJsonString("command", 7,"login", login, "password", password); + return SUCCESS; } template @@ -139,6 +167,7 @@ ExitStatus LogInReqHandler::HandleResponse(std::string& response try { RequestHandler::parser = std::make_shared(responseBody); } + catch (...) { throw std::runtime_error("Failed to parse JSON!"); } @@ -165,11 +194,11 @@ ExitStatus LogInReqHandler::DoLogic(Model &model } - template ExitStatus SignUpReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "login", &login, "password", &password); RequestHandler::requestToSend = packToJsonString("login", login, "password", password); + return SUCCESS; } //ExitStatus SignUpReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } diff --git a/client/include/room.hpp b/client/include/room.hpp index 774475a..e153c26 100644 --- a/client/include/room.hpp +++ b/client/include/room.hpp @@ -10,7 +10,7 @@ class Room { Room(std::string roomName, std::string roomId); std::string GetRoomHost(); std::string GetRoomName(); - void addLink(std::string& name, std::string& url); + void addLink(std::string& linkInfo); void removeLink(std::string& linkName); void addParticipant(std::string& newPart); void removeParticipant(std::string& partName); diff --git a/client/include/socket.hpp b/client/include/socket.hpp index b731332..685b078 100644 --- a/client/include/socket.hpp +++ b/client/include/socket.hpp @@ -21,6 +21,7 @@ class Socket void Connect(const std::string& host, int port); void Send(const std::string& str); std::string Recv(); + std::string RecvFile(bool endFlag); void Close(); private: int sd; diff --git a/client/src/client.cpp b/client/src/client.cpp index 975d908..7c29ab4 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -12,6 +12,4 @@ std::string Client::readFromServer() { void Client::writeToServer(std::string& req) { sock.Send(req); -} - - +} \ No newline at end of file diff --git a/client/src/link.cpp b/client/src/link.cpp index 663cc9d..8f54bac 100644 --- a/client/src/link.cpp +++ b/client/src/link.cpp @@ -6,30 +6,37 @@ class LinkImpl { public: - LinkImpl(std::string name, std::string url); - const std::string getLinkName(); + LinkImpl(std::string name, std::string url, std::string uuid); + std::string getLinkInfo(); + void setLinkInfo(); void addSnaphotPath(std::string path); private: std::string linkname; std::string url; + std::string uuid; std::vector snapshotPaths; }; -LinkImpl::LinkImpl(std::string name, std::string url) : linkname(std::move(name)), url(std::move(url)) {} +LinkImpl::LinkImpl(std::string name, std::string url, std::string uuid) : linkname(std::move(name)), url(std::move(url)), uuid(std::move(uuid)) {} -const std::string LinkImpl::getLinkName() { +std::string LinkImpl::getLinkInfo() { return linkname; } +void LinkImpl::setLinkInfo() { + +} + void LinkImpl::addSnaphotPath(std::string path) { snapshotPaths.push_back(std::move(path)); } -Link::Link(std::string& name, std::string& url) : linkImpl(new LinkImpl(name, url)){} +Link::Link(std::string& name, std::string& url, std::string& uuid) : linkImpl(new LinkImpl(name, url, uuid)){} Link::~Link() {} -const std::string Link::GetLinkName() { - return linkImpl->getLinkName(); +std::string Link::GetLinkInfo() { + std::string ret = linkImpl->getLinkInfo(); + return ret; } void Link::addSnapshot(std::string& path) { diff --git a/client/src/room.cpp b/client/src/room.cpp index 8731d96..251042d 100644 --- a/client/src/room.cpp +++ b/client/src/room.cpp @@ -2,6 +2,7 @@ #include "link.hpp" #include "room.hpp" +#include "utils.h" @@ -10,7 +11,7 @@ class RoomImpl { RoomImpl(std::string name, std::string host); std::string getRoomHost(); std::string getRoomName(); - void addLink(std::string& name, std::string url); + void addLink(std::string& linkInfo); void removeLink(std::string& linkName); void addParticipant(std::string& newPart); void removeParticipant(std::string& partName); @@ -35,8 +36,8 @@ std::string Room::GetRoomName() { return ret; } -void Room::addLink(std::string& name, std::string& url) { - roomImpl->addLink(name, url); +void Room::addLink(std::string& linkInfo) { + roomImpl->addLink(linkInfo); } void Room::removeLink(std::string& linkName) { @@ -61,19 +62,23 @@ std::string RoomImpl::getRoomName() { return roomName; } -void RoomImpl::addLink(std::string& name, std::string url) { - Link newLink(name, url); +void RoomImpl::addLink(std::string& linkInfo) { + std::string name, url, uuid; + fillDataFromJson(linkInfo, "name", &name, "url", &url, "uuid", &uuid); + Link newLink(name, url, uuid); links.push_back(std::move(newLink)); } void RoomImpl::removeLink(std::string& linkName) { - for(std::vector::iterator it = links.begin(); it != links.end(); ++it) { + + /* for(std::vector::iterator it = links.begin(); it != links.end(); ++it) { if ((*it).GetLinkName() == linkName) { links.erase(it); } - } + } */ } + void RoomImpl::addParticipant(std::string& newPart) {} void RoomImpl::removeParticipant(std::string& partName) {} std::string RoomImpl::archiveLink(std::string& linkName) { return "str"; } \ No newline at end of file diff --git a/client/src/socket.cpp b/client/src/socket.cpp index a7e7b2d..23ae1c0 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -9,6 +9,8 @@ #include "socket.hpp" +#define BUFSIZE 400 + struct sockaddr_in resolve(const char* host, int port) { struct hostent* hp = gethostbyname(host); if (hp == NULL) @@ -78,22 +80,48 @@ std::string Socket::Recv() { std::string ret; while (true) { - int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); - if (-1 == n && errno != EAGAIN) { - throw std::runtime_error("read failed: " + std::string(strerror(errno))); - } - if (0 == n || -1 == n) { - break; - } - ret.append(buf, n); - while (ret.back() == '\r' || ret.back() == '\n') { - ret.pop_back(); - } + int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); + if (-1 == n && errno != EAGAIN) { + throw std::runtime_error("read failed: " + std::string(strerror(errno))); + } + if (0 == n || -1 == n) { + break; + } + ret.append(buf, n); + while (ret.back() == '\r' || ret.back() == '\n') { + ret.pop_back(); + } } return ret; } +std::string Socket::RecvFile(bool endFlag) { + char buf[BUFSIZE]; + std::string ret; + + while (true) { + int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); + if (-1 == n && errno != EAGAIN) { + throw std::runtime_error("read failed: " + std::string(strerror(errno))); + } + if (0 == n || -1 == n) { + break; + } + ret.append(buf, 1, n - 1); + while (ret.back() == 26) { + ret.pop_back(); + } + if (buf[0] == 'e') { + break; + } else if (buf[0] == 'f') { + endFlag = true; + break; + } + } + return ret; +} + void Socket::Close() { ::close(sd); } \ No newline at end of file diff --git a/client/src/userinfo.cpp b/client/src/userinfo.cpp index dea7e46..fca9203 100644 --- a/client/src/userinfo.cpp +++ b/client/src/userinfo.cpp @@ -2,7 +2,7 @@ #include "utils.h" #include -UserInfo::UserInfo() : name(""), password(""), uuid("") {} +UserInfo::UserInfo() : name("default"), password("default"), uuid("default") {} UserInfo::UserInfo(std::string name, std::string password, std::string uuid) : name(std::move(name)), password(std::move(password)), @@ -10,9 +10,9 @@ UserInfo::UserInfo(std::string name, std::string password, std::string uuid) } std::string UserInfo::getInfoStr() { - if (name.empty() || name.empty() || name.empty()) { + /* if (name.empty() || name.empty() || name.empty()) { throw std::runtime_error("User info is empty!"); - } + } */ std::string ret = packToJsonString("name", name, "password", password, "uuid", uuid); return ret; diff --git a/client/src/view.cpp b/client/src/view.cpp index ed064a8..459c1b6 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -7,15 +7,15 @@ IView::~IView() {} enum RequestCommand { - CREATE_ROOM, - DELETE_ROOM, - ADD_USERS, - DELETE_USERS, - ADD_LINK, - DELETE_LINK, - MAKE_SNAPSHOT, - LOG_IN_USER, - SIGN_UP_USER, + CREATE_ROOM, + DELETE_ROOM, + ADD_USERS, + DELETE_USERS, + ADD_LINK, + DELETE_LINK, + MAKE_SNAPSHOT, + LOG_IN_USER, + SIGN_UP_USER, }; From 837acb23d0fc910266c28dac511508b8a7e816ac Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 19:09:01 +0300 Subject: [PATCH 22/36] changed connect --- client/include/client.hpp | 1 + client/include/presenter.hpp | 2 ++ client/include/presenter.tpp | 15 ++++++++++++--- client/src/client.cpp | 4 ++++ client/src/socket.cpp | 23 ++++++++++++++++++++++- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/client/include/client.hpp b/client/include/client.hpp index 5d02d50..d5d1395 100644 --- a/client/include/client.hpp +++ b/client/include/client.hpp @@ -12,6 +12,7 @@ class Client { Client(const std::string& _host, int _port); ~Client() {} void Connect(); + void Close(); void writeToServer(std::string& req); std::string readFromServer(); private: diff --git a/client/include/presenter.hpp b/client/include/presenter.hpp index e2ffee4..a5d9bed 100644 --- a/client/include/presenter.hpp +++ b/client/include/presenter.hpp @@ -11,6 +11,8 @@ class Presenter { public: Presenter(const std::string& host, const size_t port); Presenter(Presenter& pr) = delete; + void connect(); + void disconnect(); Presenter& operator=(Presenter& pr) = delete; ~Presenter(); void run(); diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp index 0fc1be5..df9bbc9 100644 --- a/client/include/presenter.tpp +++ b/client/include/presenter.tpp @@ -6,19 +6,27 @@ template Presenter::Presenter(const std::string& host, const size_t port) : -client(host, port) { +client(host, port) { /* client.Connect(); */} + +template +Presenter::~Presenter() { +} + +template +void Presenter::connect() { client.Connect(); } template -Presenter::~Presenter() { +void Presenter::disconnect() { + client.Close(); } template void Presenter::run() { std::string action = view.GetRequest(); while(!action.empty()) { - //model.PassAction(action); + connect(); std::string request = model.FormRequest(action); client.writeToServer(request); std::string response = client.readFromServer(); @@ -27,5 +35,6 @@ void Presenter::run() { model.HandleResponse(response); action = view.GetRequest(); + disconnect(); } } \ No newline at end of file diff --git a/client/src/client.cpp b/client/src/client.cpp index 7c29ab4..7573b12 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -6,6 +6,10 @@ void Client::Connect() { sock.Connect(host, port); } +void Client::Close() { + sock.Close(); +} + std::string Client::readFromServer() { return sock.Recv(); } diff --git a/client/src/socket.cpp b/client/src/socket.cpp index 23ae1c0..0f5deec 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -61,7 +61,7 @@ void Socket::Connect(const std::string& host, int port) { sd = _sd; } -void Socket::Send(const std::string& str) { +/* void Socket::Send(const std::string& str) { size_t left = str.size(); ssize_t sent = 0; @@ -73,8 +73,29 @@ void Socket::Send(const std::string& str) { } left -= sent; } +} */ + +void Socket::Send(const std::string& str) { + std::string temp = "f" + str; + while(temp.size() != 400) { + temp.push_back('\x1A'); + } + + size_t left = temp.size(); + ssize_t sent = 0; + + int flags = 0; + while (left > 0) { + sent = ::send(sd, temp.data() + sent, temp.size() - sent, flags); + if (-1 == sent) { + throw std::runtime_error("write failed: " + std::string(strerror(errno))); + } + left -= sent; + } } + + std::string Socket::Recv() { char buf[256]; std::string ret; From 5043f2ddb5161d3b31d890a6851476fe7c027549 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 19:12:13 +0300 Subject: [PATCH 23/36] -comms --- client/src/utils.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/client/src/utils.cpp b/client/src/utils.cpp index aede5fe..402ab34 100644 --- a/client/src/utils.cpp +++ b/client/src/utils.cpp @@ -32,8 +32,8 @@ std::vector splitString(const std::string& data) { if (data.empty()) { throw std::runtime_error("Empty values in serialization"); } - std::cout << data << std::endl; - + /* std::cout << data << std::endl; + */ std::vector elements; std::string element = ""; @@ -53,9 +53,9 @@ std::vector splitString(const std::string& data) { } elements.push_back(element); - for (auto &i : elements) { + /* for (auto &i : elements) { std::cout << i << std::endl; - } + } */ return elements; } @@ -120,11 +120,11 @@ void fillData(const std::string& jsonStr, const std::string& key, std::vector Date: Thu, 24 Dec 2020 19:31:19 +0300 Subject: [PATCH 24/36] -- --- client/src/client.cpp | 5 +++-- client/src/socket.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client/src/client.cpp b/client/src/client.cpp index 7573b12..887ab31 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -10,8 +10,9 @@ void Client::Close() { sock.Close(); } -std::string Client::readFromServer() { - return sock.Recv(); +std::string Client::readFromServer() { + bool endFlag = false; + return sock.RecvFile(endFlag); } void Client::writeToServer(std::string& req) { diff --git a/client/src/socket.cpp b/client/src/socket.cpp index 0f5deec..42e6dba 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -130,7 +130,7 @@ std::string Socket::RecvFile(bool endFlag) { break; } ret.append(buf, 1, n - 1); - while (ret.back() == 26) { + while (ret.back() == '\x1A') { ret.pop_back(); } if (buf[0] == 'e') { From d90ec82c32cb6a622cae2ecc4555ed6512fc1561 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 19:51:23 +0300 Subject: [PATCH 25/36] ---- --- client/include/requestHandler.tpp | 8 ++++---- client/src/utils.cpp | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index d14ee8f..1cca1ad 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -60,7 +60,7 @@ ExitStatus AddLinkReqHandler::FillRequest(std::string action, Mo std::string info = model.GetUserInfo(); std::string login, token; fillDataFromJson(info, "name", &login, "uuid", &token); - + RequestHandler::requestToSend = packToJsonString("command", 4, "login", login, "token", token, "name", linkName, "url", url, "description", " "); return SUCCESS; @@ -71,7 +71,7 @@ ExitStatus AddLinkReqHandler::HandleResponse(std::string &respon try { RequestHandler::parser = std::make_shared(responseBody); } - + catch (...) { throw std::runtime_error("Failed to parse JSON!"); } @@ -91,12 +91,12 @@ ExitStatus AddLinkReqHandler::HandleResponse(std::string &respon } template ExitStatus AddLinkReqHandler::DoLogic(Model &model) { + std::string linkInfo = packToJsonString("name", linkName, "url", url, "uuid", uuid); model.AddLink(linkInfo); return SUCCESS; } - template ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "name", &linkName); @@ -182,7 +182,7 @@ ExitStatus LogInReqHandler::HandleResponse(std::string& response } else { return FAILURE; } - + return SUCCESS; } diff --git a/client/src/utils.cpp b/client/src/utils.cpp index 402ab34..090f74a 100644 --- a/client/src/utils.cpp +++ b/client/src/utils.cpp @@ -29,9 +29,9 @@ std::vector request_split(const std::string& data) { std::vector splitString(const std::string& data) { - if (data.empty()) { + /* if (data.empty()) { throw std::runtime_error("Empty values in serialization"); - } + } */ /* std::cout << data << std::endl; */ std::vector elements; @@ -66,9 +66,9 @@ std::string boolToString(bool value) { } std::string serialize(const std::string &key, const std::vector vec) { - if (key.empty() || vec.empty()) { + /* if (key.empty() || vec.empty()) { throw std::runtime_error("Empty values in serialization"); - } + } */ std::string serialized = "\"" + key + "\": [ "; for (auto &v: vec) { @@ -81,9 +81,9 @@ std::string serialize(const std::string &key, const std::vector vec } std::string serialize(const std::string &key, const std::string &value) { - if (key.empty() || value.empty()) { + /* if (key.empty() || value.empty()) { throw std::runtime_error("Empty values in serialization"); - } + } */ std::string serialized = "\"" + key + "\": \"" + value + "\","; return serialized; From 20ed769aca5428c5869c0849ce0b4b2d2d17e8a7 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Thu, 24 Dec 2020 20:07:58 +0300 Subject: [PATCH 26/36] ------ --- client/include/requestHandler.tpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index 1cca1ad..b233eaf 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -197,8 +197,7 @@ ExitStatus LogInReqHandler::DoLogic(Model &model template ExitStatus SignUpReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "login", &login, "password", &password); - RequestHandler::requestToSend = packToJsonString("login", login, "password", password); - + RequestHandler::requestToSend = packToJsonString("command", 8, "login", login, "password", password); return SUCCESS; } //ExitStatus SignUpReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } From fe5ff7a3db2f12d6931213206f7db31aa55a3596 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Sat, 26 Dec 2020 22:25:48 +0300 Subject: [PATCH 27/36] improved creating and removing rooms --- client/include/model.hpp | 8 +++- client/include/model.tpp | 43 ++++++++++++++------ client/include/requestHandler.hpp | 5 ++- client/include/requestHandler.tpp | 66 +++++++++++++++++++++++++------ client/include/room.hpp | 7 ++-- client/include/utils.h | 6 ++- client/src/client.cpp | 1 + client/src/inputUtils.cpp | 5 ++- client/src/main.cpp | 2 +- client/src/room.cpp | 20 +++++++--- client/src/utils.cpp | 34 +++++++++++----- client/src/view.cpp | 2 +- 12 files changed, 149 insertions(+), 50 deletions(-) diff --git a/client/include/model.hpp b/client/include/model.hpp index b90d252..ea0a94e 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -1,8 +1,10 @@ #pragma once #include +#include "room.hpp" +#include "userinfo.hpp" + -//#include "client.hpp" template class ModelImpl; @@ -13,9 +15,11 @@ class Model { ~Model(); std::string GetMainRoomInfo(); //void PassAction(std::string& action); - void SetUserInfo(const std::string& str); + void SetUserInfo(std::shared_ptr info); std::string GetUserInfo(); void AddLink(std::string& linkInfo); + void AddRoom(std::shared_ptr newRoom); + void RemoveRoom(const std::string& roomName); std::string FormRequest(std::string& action); void HandleResponse(std::string& response); private: diff --git a/client/include/model.tpp b/client/include/model.tpp index f1d561f..da0337a 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "model.hpp" @@ -23,22 +24,32 @@ public: std::string formRequest(std::string& action, Model& model); void handleResponse(std::string& response, Model& model); - void SetUserInfoFromStr(const std::string& str) { - return info.setUserInfo(str); + void SetUserInfoFromStr(std::shared_ptr uinfo) { + info.swap(uinfo); + //std::swap(uinfo, info); } std::string GetUserInfo() { - return info.getInfoStr(); + return info->getInfoStr(); } void addLink(std::string& linkInfo) { mainRoom->addLink(linkInfo); } + void addRoom(std::shared_ptr newRoom) { + rooms.push_back(newRoom); + } + + void removeRoom(const std::string& roomName) { + auto it = std::find_if(rooms.begin(), rooms.end(), + [roomName](std::shared_ptr room) { return room->GetRoomName() == roomName; }); + rooms.erase(it); + } + std::shared_ptr GetMainRoom() { return mainRoom; } - std::vector> GetRooms() { return rooms; @@ -47,16 +58,15 @@ private: std::shared_ptr> CreateRequestHandler(std::string& action, Model& model); std::shared_ptr> currentHandler; - - UserInfo info; - + std::shared_ptr info; std::shared_ptr mainRoom; std::vector> rooms; }; template -ModelImpl::ModelImpl() : mainRoom(std::make_shared("default", "me")) { -} +ModelImpl::ModelImpl() +: info(std::make_shared()), + mainRoom(std::make_shared()) {} template std::shared_ptr> ModelImpl::CreateRequestHandler(std::string& action, Model& model) { @@ -144,8 +154,8 @@ std::string Model::GetMainRoomInfo() { } template -void Model::SetUserInfo(const std::string& str) { - modelImpl->SetUserInfoFromStr(str); +void Model::SetUserInfo(std::shared_ptr info) { + modelImpl->SetUserInfoFromStr(info); } /* void Model::PassAction(std::string& action) { @@ -172,4 +182,15 @@ std::string Model::GetUserInfo() { template void Model::AddLink(std::string& linkInfo) { modelImpl->addLink(linkInfo); +} + + +template +void Model::AddRoom(std::shared_ptr newRoom) { + modelImpl->addRoom(newRoom); +} + +template +void Model::RemoveRoom(const std::string& roomName) { + modelImpl->removeRoom(roomName); } \ No newline at end of file diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index f60d0d0..856b08a 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -88,11 +88,13 @@ class CreateRoomReqHandler : public RequestHandler { public: CreateRoomReqHandler() = default; ExitStatus FillRequest(std::string action, Model& model); - //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: std::string roomName; std::string roomHost; + std::string uuid; + bool isPrivate; }; template @@ -100,7 +102,6 @@ class RemoveRoomReqHandler : public RequestHandler { public: RemoveRoomReqHandler() = default; ExitStatus FillRequest(std::string action, Model& model); - //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: std::string roomName; diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index b233eaf..6662d34 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -1,6 +1,8 @@ #pragma once /* #include "requestHandler.hpp" */ #include "utils.h" +#include "room.hpp" +#include "userinfo.hpp" template @@ -132,27 +134,65 @@ ExitStatus ArchiveLinkReqHandler::DoLogic(Model template ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { - //std::cout << action << std::endl; - fillDataFromJson(action, "name", &roomName, "host", &roomHost); + fillDataFromJson(action, "name", &roomName, "host", &roomHost, "private", &isPrivate); + + std::string info = model.GetUserInfo(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); - RequestHandler::requestToSend = packToJsonString("name", roomName, "host", roomHost); + RequestHandler::requestToSend = packToJsonString("command", 0, "login", login, "token", token, "name", + roomName, "host", roomHost, "private", isPrivate); return SUCCESS; } -//ExitStatus CreateRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS;} + template -ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } +ExitStatus CreateRoomReqHandler::HandleResponse(std::string &responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("uuid", uuid)); + } else { + return FAILURE; + } + std::cout << "success!" << std::endl; + return SUCCESS; +} + +template +ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { + std::shared_ptr newRoom = std::make_shared(roomHost, roomName, uuid, isPrivate); + model.AddRoom(newRoom); + return SUCCESS; +} template ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "name", &roomName, "host", &roomHost); - RequestHandler::requestToSend = packToJsonString("name", roomName, "host", roomHost); + + std::string info = model.GetUserInfo(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); + + RequestHandler::requestToSend = packToJsonString("command", 1, "login", login, "token", token,"name", roomName, "host", roomHost); return SUCCESS; } //ExitStatus RemoveRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } template -ExitStatus RemoveRoomReqHandler::DoLogic(Model &model) { return SUCCESS; } +ExitStatus RemoveRoomReqHandler::DoLogic(Model &model) { + model.RemoveRoom(roomName); + return SUCCESS; +} template @@ -167,7 +207,6 @@ ExitStatus LogInReqHandler::HandleResponse(std::string& response try { RequestHandler::parser = std::make_shared(responseBody); } - catch (...) { throw std::runtime_error("Failed to parse JSON!"); } @@ -178,18 +217,20 @@ ExitStatus LogInReqHandler::HandleResponse(std::string& response } if (error.empty()) { (RequestHandler::parser->get_value("uuid", uuid)); - std::cout << "success!" << std::endl; } else { return FAILURE; } - + std::cout << "success!" << std::endl; return SUCCESS; } template ExitStatus LogInReqHandler::DoLogic(Model &model) { - std::string info = packToJsonString("login", login, "password", password, "uuid", uuid); - model.SetUserInfo(info); + //std::string info = packToJsonString("login", login, "password", password, "uuid", uuid); + + std::shared_ptr newUserinfo = std::make_shared(login, password, uuid); + + model.SetUserInfo(newUserinfo); return SUCCESS; } @@ -200,6 +241,7 @@ ExitStatus SignUpReqHandler::FillRequest(std::string action, Mod RequestHandler::requestToSend = packToJsonString("command", 8, "login", login, "password", password); return SUCCESS; } + //ExitStatus SignUpReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } template diff --git a/client/include/room.hpp b/client/include/room.hpp index e153c26..3a9b99d 100644 --- a/client/include/room.hpp +++ b/client/include/room.hpp @@ -7,7 +7,8 @@ class RoomImpl; class Room { public: - Room(std::string roomName, std::string roomId); + Room(); + Room(std::string& roomName, std::string& roomId, std::string& uuid, bool isPrivate); std::string GetRoomHost(); std::string GetRoomName(); void addLink(std::string& linkInfo); @@ -19,11 +20,11 @@ class Room { std::shared_ptr roomImpl; }; -class PublicRoom : public Room { +/* class PublicRoom : public Room { }; class PrivateRoom : public Room { -}; +}; */ diff --git a/client/include/utils.h b/client/include/utils.h index f289247..73ad9a7 100644 --- a/client/include/utils.h +++ b/client/include/utils.h @@ -18,9 +18,11 @@ std::string serialize(const std::string &key, const std::vector vec std::string serialize(const std::string &key, const std::string &value); +std::string serialize(const std::string &key, const char* value); + std::string serialize(const std::string &key, int value); -/* std::string serialize(const std::string &key, bool value); */ +std::string serialize(const std::string &key, bool value); template std::string serializeData(T key, K value); @@ -46,6 +48,8 @@ void fillData(const std::string& jsonStr, const std::string& key, std::string* void fillData(const std::string& jsonStr, const std::string& key, int* val); +void fillData(const std::string& jsonStr, const std::string& key, bool* val); + template void fillDataFromJson(const std::string& jsonStr, const T& key, K* inputVal); diff --git a/client/src/client.cpp b/client/src/client.cpp index 887ab31..7ecc55a 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -16,5 +16,6 @@ std::string Client::readFromServer() { } void Client::writeToServer(std::string& req) { + sock.Send(req); } \ No newline at end of file diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index e3f649e..2e9e7ca 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -19,10 +19,11 @@ void fillObject(std::vector* vec) { std::string createRoomInput() { std::string name; std::string host; + std::string isPrivate; - writeData(&name, &host); + writeData(&name, &host, &isPrivate); - std::string ret = packToJsonString("command", "0", "name", name, "host", host); + std::string ret = packToJsonString("command", "0", "name", name, "host", host, "private", isPrivate); return ret; } diff --git a/client/src/main.cpp b/client/src/main.cpp index b048484..82dfa55 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -9,7 +9,7 @@ const std::string host = "localhost"; -const size_t port = 5555; +const size_t port = 13201; class JsonParser { public: diff --git a/client/src/room.cpp b/client/src/room.cpp index 251042d..6280864 100644 --- a/client/src/room.cpp +++ b/client/src/room.cpp @@ -8,7 +8,7 @@ class RoomImpl { public: - RoomImpl(std::string name, std::string host); + RoomImpl(std::string name, std::string host, std::string uuid, bool isPrivate); std::string getRoomHost(); std::string getRoomName(); void addLink(std::string& linkInfo); @@ -18,15 +18,19 @@ class RoomImpl { std::string archiveLink(std::string& linkName); private: - std::string roomName; - /* std::string roomId; */ - std::string roomHost; + std::string roomName = ""; + std::string roomHost = ""; + std::string uuid = ""; + bool isPrivate = true; std::vector participants; std::vector links; }; -Room::Room(std::string name, std::string host) : roomImpl(new RoomImpl(name, host)) {} +Room::Room() {} + +Room::Room(std::string& name, std::string& host, std::string& uuid, bool isPrivate) +: roomImpl(new RoomImpl(name, host, uuid, isPrivate)) {} std::string Room::GetRoomHost() { std::string ret = roomImpl->getRoomHost(); return ret; @@ -52,7 +56,11 @@ std::string Room::archiveLink(std::string& linkName) { } -RoomImpl::RoomImpl(std::string name, std::string host) : roomName(name), roomHost(host) {} +RoomImpl::RoomImpl(std::string name, std::string host, std::string uuid, bool isPrivate) +: roomName(name), +roomHost(host), +uuid(uuid), +isPrivate(isPrivate) {} std::string RoomImpl::getRoomHost() { return roomHost; diff --git a/client/src/utils.cpp b/client/src/utils.cpp index 090f74a..71286af 100644 --- a/client/src/utils.cpp +++ b/client/src/utils.cpp @@ -1,6 +1,6 @@ #include "utils.h" -std::vector request_split(const std::string& data) { +/* std::vector request_split(const std::string& data) { std::vector elements; std::string element = ""; @@ -25,7 +25,7 @@ std::vector request_split(const std::string& data) { } return elements; -} +} */ std::vector splitString(const std::string& data) { @@ -65,6 +65,10 @@ std::string boolToString(bool value) { return value ? "true" : "false"; } +bool stringToBool(const std::string value) { + return (value == "true") ? true : false; +} + std::string serialize(const std::string &key, const std::vector vec) { /* if (key.empty() || vec.empty()) { throw std::runtime_error("Empty values in serialization"); @@ -89,6 +93,14 @@ std::string serialize(const std::string &key, const std::string &value) { return serialized; } +std::string serialize(const std::string &key, const char* value) { + if (key.empty()) { + throw std::runtime_error("Empty values in serialization"); + } + std::string serialized = "\"" + key + "\": \"" + std::string(value) + "\","; + return serialized; +} + std::string serialize(const std::string &key, int value) { if (key.empty()) { throw std::runtime_error("Empty values in serialization"); @@ -97,14 +109,14 @@ std::string serialize(const std::string &key, int value) { return serialized; } -/* std::string serialize(const std::string &key, bool value) { +std::string serialize(const std::string &key, bool value) { if (key.empty()) { throw std::runtime_error("Empty values in serialization"); } std::string serialized = "\"" + key + "\": " + boolToString(value) + ","; return serialized; } - */ + @@ -120,18 +132,22 @@ void fillData(const std::string& jsonStr, const std::string& key, std::vector> key; switch (key) { case CREATE_ROOM: { - std::cout << "Write name and host of room" << std::endl; + std::cout << "Write name, host of room and is it private(true/false)" << std::endl; inputStr = createRoomInput(); } break; From db3d3415ab0b4616cae9b7b231a7563215df2ffa Mon Sep 17 00:00:00 2001 From: Verhushker Date: Sun, 27 Dec 2020 04:28:17 +0300 Subject: [PATCH 28/36] improved adding users and links --- client/include/link.hpp | 3 +- client/include/model.hpp | 13 ++-- client/include/model.tpp | 115 ++++++++++++++++++++++++------ client/include/requestHandler.hpp | 13 ++-- client/include/requestHandler.tpp | 70 +++++++++++++----- client/include/room.hpp | 18 +++-- client/src/inputUtils.cpp | 5 +- client/src/link.cpp | 27 +++++-- client/src/room.cpp | 85 +++++++++++++++++----- client/src/userinfo.cpp | 4 -- client/src/utils.cpp | 7 +- client/src/view.cpp | 2 +- 12 files changed, 274 insertions(+), 88 deletions(-) diff --git a/client/include/link.hpp b/client/include/link.hpp index bee9604..3548dde 100644 --- a/client/include/link.hpp +++ b/client/include/link.hpp @@ -5,8 +5,9 @@ class LinkImpl; class Link { public: - Link(std::string& name, std::string& url, std::string& uuid); + Link(std::string& name, std::string& url, std::string& uuid, std::string& description); ~Link(); + std::string GetLinkName(); std::string GetLinkInfo(); void SetLinkInfo(); void addSnapshot(std::string& path); diff --git a/client/include/model.hpp b/client/include/model.hpp index ea0a94e..9f3bcae 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include "room.hpp" #include "userinfo.hpp" @@ -13,11 +14,15 @@ class Model { public: Model(); ~Model(); - std::string GetMainRoomInfo(); - //void PassAction(std::string& action); + std::string GetRoomInfoStr(const std::string& roomName); + std::string GetCurrentRoomInfoStr(); + std::string GetLinkInfoStr(const std::string& linkName); void SetUserInfo(std::shared_ptr info); - std::string GetUserInfo(); - void AddLink(std::string& linkInfo); + std::string GetUserInfoStr(); + void AddUsers(std::vector users); + void RemoveUsers(std::vector users); + void AddLink(std::shared_ptr newLink); + void RemoveLink(const std::string& linkName); void AddRoom(std::shared_ptr newRoom); void RemoveRoom(const std::string& roomName); std::string FormRequest(std::string& action); diff --git a/client/include/model.tpp b/client/include/model.tpp index da0337a..b7b80af 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -7,6 +7,7 @@ #include "model.hpp" #include "requestHandler.hpp" #include "room.hpp" +#include "link.hpp" #include "userinfo.hpp" #include "utils.h" @@ -15,30 +16,76 @@ template class ModelImpl { public: ModelImpl(); - //void passAction(std::string& action, Model& model); - - /* Client& GetClient() { - return client; - } */ std::string formRequest(std::string& action, Model& model); void handleResponse(std::string& response, Model& model); void SetUserInfoFromStr(std::shared_ptr uinfo) { - info.swap(uinfo); - //std::swap(uinfo, info); + info = uinfo; } - std::string GetUserInfo() { + std::string getUserInfoStr() { return info->getInfoStr(); } - void addLink(std::string& linkInfo) { - mainRoom->addLink(linkInfo); + + std::string getCurrentRoomInfoStr() { + std::string ret = currentRoom->GetRoomInfoStr(); + return ret; + } + + std::string getLinkInfoStr(const std::string& linkName) { + std::string ret = currentRoom->GetLinkInfoStr(linkName); + return ret; + } + + std::string getRoomInfoStr(const std::string& roomName) { + std::string ret; + /* if (mainRoom->GetRoomName() == roomName) { + ret = mainRoom->GetRoomInfoStr(); + } else { + for (auto i : rooms) { + if (i->GetRoomName() == roomName) { + ret = i->GetRoomInfoStr(); + break; + } + } + } */ + for (auto i : rooms) { + if (i->GetRoomName() == roomName) { + ret = i->GetRoomInfoStr(); + break; + } + } + if (ret.empty()) { + throw std::runtime_error("Room is not found"); + } + + return ret; + } + + void addUsers(std::vector users) { + currentRoom->AddUsers(users); + } + + void removeUsers(std::vector users) { + currentRoom->RemoveUsers(users); + } + + void addLink(std::shared_ptr newLink) { + currentRoom->AddLink(newLink); + } + + void removeLink(const std::string& linkName) { + currentRoom->RemoveLink(linkName); } void addRoom(std::shared_ptr newRoom) { - rooms.push_back(newRoom); + if (currentRoom == nullptr) { + currentRoom = newRoom; + } else { + rooms.push_back(newRoom); + } } void removeRoom(const std::string& roomName) { @@ -60,13 +107,15 @@ private: std::shared_ptr info; std::shared_ptr mainRoom; + std::shared_ptr currentRoom; std::vector> rooms; }; template ModelImpl::ModelImpl() -: info(std::make_shared()), - mainRoom(std::make_shared()) {} +: info(nullptr), + mainRoom(std::make_shared()), + currentRoom(nullptr) {} template std::shared_ptr> ModelImpl::CreateRequestHandler(std::string& action, Model& model) { @@ -114,7 +163,6 @@ std::shared_ptr> ModelImpl::Creat return handler; } - /* void ModelImpl::passAction(std::string& action, Model& model) { std::shared_ptr handler = CreateRequestHandler(action, model); @@ -148,8 +196,20 @@ template Model::~Model() {} template -std::string Model::GetMainRoomInfo() { - std::string ret = modelImpl->GetMainRoom()->GetRoomName() + modelImpl->GetMainRoom()->GetRoomHost(); +std::string Model::GetCurrentRoomInfoStr() { + std::string ret = modelImpl->getCurrentRoomInfoStr(); + return ret; +} + +template +std::string Model::GetLinkInfoStr(const std::string& linkName) { + std::string ret = modelImpl->getLinkInfoStr(linkName); + return ret; +} + +template +std::string Model::GetRoomInfoStr(const std::string& roomName) { + std::string ret = modelImpl->getRoomInfoStr(roomName); return ret; } @@ -174,17 +234,32 @@ void Model::HandleResponse(std::string& response) { } template -std::string Model::GetUserInfo() { - std::string ret = modelImpl->GetUserInfo(); +std::string Model::GetUserInfoStr() { + std::string ret = modelImpl->getUserInfoStr(); return ret; } template -void Model::AddLink(std::string& linkInfo) { - modelImpl->addLink(linkInfo); +void Model::AddUsers(std::vector users) { + modelImpl->addUsers(users); +} + +template +void Model::RemoveUsers(std::vector users) { + modelImpl->removeUsers(users); +} + +template +void Model::AddLink(std::shared_ptr newLink) { + modelImpl->addLink(newLink); } +template +void Model::RemoveLink(const std::string& linkName) { + modelImpl->removeLink(linkName); +} + template void Model::AddRoom(std::shared_ptr newRoom) { modelImpl->addRoom(newRoom); diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index 856b08a..a945b28 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -29,10 +29,10 @@ class AddUsersReqHandler : public RequestHandler { public: AddUsersReqHandler() = default; ExitStatus FillRequest(std::string action, Model& model); - //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: std::vector users; + std::string uuid; }; template @@ -40,11 +40,10 @@ class RemoveUsersReqHandler : public RequestHandler { public: RemoveUsersReqHandler() = default; ExitStatus FillRequest(std::string action, Model& model); - //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: -std::vector users; - + std::vector users; + std::string uuid; }; template @@ -58,6 +57,7 @@ class AddLinkReqHandler : public RequestHandler { std::string uuid; std::string linkName; std::string url; + std::string description; }; template @@ -65,10 +65,10 @@ class RemoveLinkReqHandler : public RequestHandler { public: RemoveLinkReqHandler() = default; ExitStatus FillRequest(std::string action, Model& model); - //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: std::string linkName; + std::string uuid; }; template @@ -83,6 +83,7 @@ class ArchiveLinkReqHandler : public RequestHandler { std::string body; }; + template class CreateRoomReqHandler : public RequestHandler { public: @@ -105,7 +106,7 @@ class RemoveRoomReqHandler : public RequestHandler { ExitStatus DoLogic(Model& app); private: std::string roomName; - std::string roomHost; + std::string uuid; }; template diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index 6662d34..b8feabf 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -34,36 +34,59 @@ ExitStatus RequestHandler::HandleResponse(std::string& responseB template ExitStatus AddUsersReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "users", &users); - RequestHandler::requestToSend = packToJsonString("users", users); + + std::string userInfo = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(userInfo, "name", &login, "uuid", &token); + + std::string roomInfo = model.GetCurrentRoomInfoStr(); + fillDataFromJson(roomInfo, "uuid", &uuid); + + RequestHandler::requestToSend = packToJsonString("command", 2, "login", login, "token", token, "uuid", uuid, "users", users); return SUCCESS; } template ExitStatus AddUsersReqHandler::DoLogic(Model &model) { + model.AddUsers(users); return SUCCESS; } template ExitStatus RemoveUsersReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "users", &users); - RequestHandler::requestToSend = packToJsonString("users", users); + + std::string userInfo = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(userInfo, "name", &login, "uuid", &token); + + std::string roomInfo = model.GetCurrentRoomInfoStr(); + fillDataFromJson(roomInfo, "uuid", &uuid); + + RequestHandler::requestToSend = packToJsonString("command", 3, "login", login, "token", token, "uuid", uuid, "users", users); return SUCCESS; } //ExitStatus LogOutReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } template -ExitStatus RemoveUsersReqHandler::DoLogic(Model &model) { return SUCCESS; } +ExitStatus RemoveUsersReqHandler::DoLogic(Model &model) { + model.RemoveUsers(users); + return SUCCESS; +} template ExitStatus AddLinkReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "name", &linkName, "url", &url); + fillDataFromJson(action, "name", &linkName, "url", &url, "description", &description); - std::string info = model.GetUserInfo(); + std::string info = model.GetUserInfoStr(); std::string login, token; fillDataFromJson(info, "name", &login, "uuid", &token); - RequestHandler::requestToSend = packToJsonString("command", 4, "login", login, "token", token, "name", linkName, "url", url, "description", " "); + std::string roomInfo = model.GetCurrentRoomInfoStr(); + fillDataFromJson(roomInfo, "uuid", &uuid); + + RequestHandler::requestToSend = packToJsonString("command", 4, "login", login, "token", token, "uuid", uuid, "name", linkName, "url", url, "description", description); return SUCCESS; } @@ -93,22 +116,32 @@ ExitStatus AddLinkReqHandler::HandleResponse(std::string &respon } template ExitStatus AddLinkReqHandler::DoLogic(Model &model) { + std::shared_ptr newLink = std::make_shared(linkName, url, uuid, description); - std::string linkInfo = packToJsonString("name", linkName, "url", url, "uuid", uuid); - model.AddLink(linkInfo); + model.AddLink(newLink); return SUCCESS; } template ExitStatus RemoveLinkReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "name", &linkName); - RequestHandler::requestToSend = packToJsonString("linkname", linkName); + + std::string info = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); + + std::string linkInfo = model.GetLinkInfoStr(linkName); + fillDataFromJson(linkInfo, "uuid", &uuid); + RequestHandler::requestToSend = packToJsonString("command", 5, "login", login, "token", token, "uuid", uuid); return SUCCESS; } -//ExitStatus RemoveLinkReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } + template -ExitStatus RemoveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } +ExitStatus RemoveLinkReqHandler::DoLogic(Model &model) { + model.RemoveLink(linkName); + return SUCCESS; +} template ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { @@ -136,7 +169,7 @@ template ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "name", &roomName, "host", &roomHost, "private", &isPrivate); - std::string info = model.GetUserInfo(); + std::string info = model.GetUserInfoStr(); std::string login, token; fillDataFromJson(info, "name", &login, "uuid", &token); @@ -170,20 +203,23 @@ ExitStatus CreateRoomReqHandler::HandleResponse(std::string &res template ExitStatus CreateRoomReqHandler::DoLogic(Model &model) { - std::shared_ptr newRoom = std::make_shared(roomHost, roomName, uuid, isPrivate); + std::shared_ptr newRoom = std::make_shared(roomName, roomHost, uuid, isPrivate); model.AddRoom(newRoom); return SUCCESS; } template ExitStatus RemoveRoomReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "name", &roomName, "host", &roomHost); + fillDataFromJson(action, "name", &roomName); - std::string info = model.GetUserInfo(); + std::string userInfo = model.GetUserInfoStr(); std::string login, token; - fillDataFromJson(info, "name", &login, "uuid", &token); + fillDataFromJson(userInfo, "name", &login, "uuid", &token); + + std::string roomInfo = model.GetRoomInfoStr(roomName); + fillDataFromJson(roomInfo, "uuid", &uuid); - RequestHandler::requestToSend = packToJsonString("command", 1, "login", login, "token", token,"name", roomName, "host", roomHost); + RequestHandler::requestToSend = packToJsonString("command", 1, "login", login, "token", token, "uuid", uuid); return SUCCESS; } //ExitStatus RemoveRoomReqHandler::HandleResponse(std::string &responseBody) { return SUCCESS; } diff --git a/client/include/room.hpp b/client/include/room.hpp index 3a9b99d..2024ab6 100644 --- a/client/include/room.hpp +++ b/client/include/room.hpp @@ -3,16 +3,22 @@ #include #include +#include "link.hpp" + class RoomImpl; class Room { public: Room(); Room(std::string& roomName, std::string& roomId, std::string& uuid, bool isPrivate); + std::string GetRoomInfoStr(); std::string GetRoomHost(); std::string GetRoomName(); - void addLink(std::string& linkInfo); - void removeLink(std::string& linkName); + std::string GetLinkInfoStr(const std::string& linkName); + void AddUsers(std::vector users); + void RemoveUsers(std::vector users); + void AddLink(std::shared_ptr newLink); + void RemoveLink(const std::string& linkName); void addParticipant(std::string& newPart); void removeParticipant(std::string& partName); std::string archiveLink(std::string& linkName); @@ -20,11 +26,3 @@ class Room { std::shared_ptr roomImpl; }; -/* class PublicRoom : public Room { - -}; - -class PrivateRoom : public Room { - -}; */ - diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index 2e9e7ca..cf69cc1 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -55,10 +55,11 @@ std::string deleteUsersInput() { std::string addLinkInput() { std::string name; std::string url; + std::string description; - writeData(&name, &url); + writeData(&name, &url, &description); - std::string ret = packToJsonString("command","4", "name", name, "url", url); + std::string ret = packToJsonString("command","4", "name", name, "url", url, "description", description); return ret; } std::string deleteLinkInput() { diff --git a/client/src/link.cpp b/client/src/link.cpp index 8f54bac..210f3ca 100644 --- a/client/src/link.cpp +++ b/client/src/link.cpp @@ -2,11 +2,13 @@ #include "client.hpp" #include "link.hpp" +#include "utils.h" class LinkImpl { public: - LinkImpl(std::string name, std::string url, std::string uuid); + LinkImpl(std::string name, std::string url, std::string uuid, std::string description); + std::string getLinkName(); std::string getLinkInfo(); void setLinkInfo(); void addSnaphotPath(std::string path); @@ -14,15 +16,25 @@ class LinkImpl { std::string linkname; std::string url; std::string uuid; + std::string description; std::vector snapshotPaths; }; -LinkImpl::LinkImpl(std::string name, std::string url, std::string uuid) : linkname(std::move(name)), url(std::move(url)), uuid(std::move(uuid)) {} +LinkImpl::LinkImpl(std::string name, std::string url, std::string uuid, std::string description) +: linkname(std::move(name)), +url(std::move(url)), +uuid(std::move(uuid)), +description(std::move(description)) {} -std::string LinkImpl::getLinkInfo() { +std::string LinkImpl::getLinkName() { return linkname; } +std::string LinkImpl::getLinkInfo() { + std::string ret = packToJsonString("linkname", linkname, "url", url, "uuid", uuid, "description", description); + return ret; +} + void LinkImpl::setLinkInfo() { } @@ -31,9 +43,16 @@ void LinkImpl::addSnaphotPath(std::string path) { snapshotPaths.push_back(std::move(path)); } -Link::Link(std::string& name, std::string& url, std::string& uuid) : linkImpl(new LinkImpl(name, url, uuid)){} +Link::Link(std::string& name, std::string& url, std::string& uuid, std::string& description) +: linkImpl(new LinkImpl(name, url, uuid, description)){} Link::~Link() {} + +std::string Link::GetLinkName() { + std::string ret = linkImpl->getLinkName(); + return ret; +} + std::string Link::GetLinkInfo() { std::string ret = linkImpl->getLinkInfo(); return ret; diff --git a/client/src/room.cpp b/client/src/room.cpp index 6280864..e2471a8 100644 --- a/client/src/room.cpp +++ b/client/src/room.cpp @@ -9,10 +9,14 @@ class RoomImpl { public: RoomImpl(std::string name, std::string host, std::string uuid, bool isPrivate); + std::string getRoomInfoStr(); std::string getRoomHost(); std::string getRoomName(); - void addLink(std::string& linkInfo); - void removeLink(std::string& linkName); + std::string getLinkInfoStr(const std::string& linkName); + void addUsers(std::vector users); + void removeUsers(std::vector users); + void addLink(std::shared_ptr newLink); + void removeLink(const std::string& linkName); void addParticipant(std::string& newPart); void removeParticipant(std::string& partName); std::string archiveLink(std::string& linkName); @@ -23,7 +27,7 @@ class RoomImpl { std::string uuid = ""; bool isPrivate = true; std::vector participants; - std::vector links; + std::vector> links; }; @@ -31,6 +35,11 @@ Room::Room() {} Room::Room(std::string& name, std::string& host, std::string& uuid, bool isPrivate) : roomImpl(new RoomImpl(name, host, uuid, isPrivate)) {} + +std::string Room::GetRoomInfoStr() { + return roomImpl->getRoomInfoStr(); +} + std::string Room::GetRoomHost() { std::string ret = roomImpl->getRoomHost(); return ret; @@ -40,11 +49,24 @@ std::string Room::GetRoomName() { return ret; } -void Room::addLink(std::string& linkInfo) { - roomImpl->addLink(linkInfo); +std::string Room::GetLinkInfoStr(const std::string& linkName) { + std::string ret = roomImpl->getLinkInfoStr(linkName); + return ret; +} + +void Room::AddUsers(std::vector users) { + roomImpl->addUsers(users); } -void Room::removeLink(std::string& linkName) { +void Room::RemoveUsers(std::vector users) { + roomImpl->removeUsers(users); +} + +void Room::AddLink(std::shared_ptr newLink) { + roomImpl->addLink(newLink); +} + +void Room::RemoveLink(const std::string& linkName) { roomImpl->removeLink(linkName); } @@ -62,6 +84,11 @@ roomHost(host), uuid(uuid), isPrivate(isPrivate) {} +std::string RoomImpl::getRoomInfoStr() { + std::string ret = packToJsonString("name", roomName, "password", roomHost, "uuid", uuid, "private", isPrivate); + return ret; +} + std::string RoomImpl::getRoomHost() { return roomHost; } @@ -70,20 +97,44 @@ std::string RoomImpl::getRoomName() { return roomName; } -void RoomImpl::addLink(std::string& linkInfo) { - std::string name, url, uuid; - fillDataFromJson(linkInfo, "name", &name, "url", &url, "uuid", &uuid); - Link newLink(name, url, uuid); - links.push_back(std::move(newLink)); +std::string RoomImpl::getLinkInfoStr(const std::string& linkName) { + std::string ret; + for(auto i : links) { + if (i->GetLinkName() == linkName) { + ret = i->GetLinkInfo(); + } + } + if (ret.empty()) { + throw std::runtime_error("Link was not found");; + } + return ret; } -void RoomImpl::removeLink(std::string& linkName) { - - /* for(std::vector::iterator it = links.begin(); it != links.end(); ++it) { - if ((*it).GetLinkName() == linkName) { - links.erase(it); +void RoomImpl::addUsers(std::vector users) { + for (auto i : users) { + auto it = std::find(participants.begin(), participants.end(), i); + if (it == participants.end()) { + participants.push_back(i); } - } */ + } +} + +void RoomImpl::removeUsers(std::vector users) { + for (auto i : users) { + participants.erase(std::remove(participants.begin(), participants.end(), i), participants.end()); + } +} + +void RoomImpl::addLink(std::shared_ptr newLink) { + links.push_back(newLink); +} + +void RoomImpl::removeLink(const std::string& linkName) { + + auto it = std::find_if(links.begin(), links.end(), + [linkName](std::shared_ptr link) { return link->GetLinkName() == linkName; }); + links.erase(it); + } diff --git a/client/src/userinfo.cpp b/client/src/userinfo.cpp index fca9203..135b2c2 100644 --- a/client/src/userinfo.cpp +++ b/client/src/userinfo.cpp @@ -10,10 +10,6 @@ UserInfo::UserInfo(std::string name, std::string password, std::string uuid) } std::string UserInfo::getInfoStr() { - /* if (name.empty() || name.empty() || name.empty()) { - throw std::runtime_error("User info is empty!"); - } */ - std::string ret = packToJsonString("name", name, "password", password, "uuid", uuid); return ret; } diff --git a/client/src/utils.cpp b/client/src/utils.cpp index 71286af..0b63650 100644 --- a/client/src/utils.cpp +++ b/client/src/utils.cpp @@ -78,8 +78,10 @@ std::string serialize(const std::string &key, const std::vector vec for (auto &v: vec) { serialized += "\"" + v + "\", "; } - serialized.erase(serialized.size() - 2, 1); - serialized += "]"; + //serialized.erase(serialized.size() - 2, 1); + serialized.pop_back(); + serialized.pop_back(); + serialized += " ] "; return serialized; } @@ -138,6 +140,7 @@ void fillData(const std::string& jsonStr, const std::string& key, std::string* } } + void fillData(const std::string& jsonStr, const std::string& key, bool* val) { auto vec = splitString(jsonStr); diff --git a/client/src/view.cpp b/client/src/view.cpp index 6140206..0b022a2 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -47,7 +47,7 @@ std::string ConsoleView::GetRequest() { } break; case ADD_LINK: { - std::cout << "Write name and url of link" << std::endl; + std::cout << "Write name, url and description of link" << std::endl; inputStr = addLinkInput(); break; } From c9caa36cccf41da7d0e7a88cfc2142f2f7ddbb1e Mon Sep 17 00:00:00 2001 From: Verhushker Date: Sun, 27 Dec 2020 20:59:12 +0300 Subject: [PATCH 29/36] recieving files --- client/include/client.hpp | 5 +- client/include/inputUtils.hpp | 1 + client/include/link.hpp | 4 +- client/include/model.hpp | 12 ++++ client/include/model.tpp | 82 +++++++++++++++------ client/include/presenter.tpp | 22 +++++- client/include/requestHandler.hpp | 43 ++++++++--- client/include/requestHandler.tpp | 114 +++++++++++++++++++++++++++--- client/include/room.hpp | 2 + client/include/socket.hpp | 4 +- client/include/utils.tpp | 2 - client/src/CMakeLists.txt | 4 +- client/src/client.cpp | 7 +- client/src/inputUtils.cpp | 15 +++- client/src/link.cpp | 26 +++++-- client/src/room.cpp | 32 +++++++++ client/src/socket.cpp | 40 ++++++++++- client/src/view.cpp | 9 ++- 18 files changed, 357 insertions(+), 67 deletions(-) diff --git a/client/include/client.hpp b/client/include/client.hpp index d5d1395..a845b17 100644 --- a/client/include/client.hpp +++ b/client/include/client.hpp @@ -6,7 +6,7 @@ #include "socket.hpp" - + class Client { public: Client(const std::string& _host, int _port); @@ -14,7 +14,8 @@ class Client { void Connect(); void Close(); void writeToServer(std::string& req); - std::string readFromServer(); + std::string readFromServer(bool* endFlag); + std::vector readFileBodyFromServer(bool* endFlag); private: const std::string& host; int port; diff --git a/client/include/inputUtils.hpp b/client/include/inputUtils.hpp index 7db48b6..5010bcb 100644 --- a/client/include/inputUtils.hpp +++ b/client/include/inputUtils.hpp @@ -21,5 +21,6 @@ std::string deleteLinkInput(); std::string makeSnapshotInput(); std::string logInInput(); std::string signUpInput(); +std::string downloadSnapshotInput(); #include "inputUtils.tpp" \ No newline at end of file diff --git a/client/include/link.hpp b/client/include/link.hpp index 3548dde..6f2d3e2 100644 --- a/client/include/link.hpp +++ b/client/include/link.hpp @@ -9,9 +9,9 @@ class Link { ~Link(); std::string GetLinkName(); std::string GetLinkInfo(); + std::string GetSnapshotUuid(); void SetLinkInfo(); - void addSnapshot(std::string& path); - + void AddSnapshot(const std::string& uuid); private: std::shared_ptr linkImpl; }; diff --git a/client/include/model.hpp b/client/include/model.hpp index 9f3bcae..9447463 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -6,6 +6,13 @@ #include "userinfo.hpp" +typedef struct recFile +{ + std::string name; + std::vector body; +} recFile; + + template class ModelImpl; @@ -17,9 +24,11 @@ class Model { std::string GetRoomInfoStr(const std::string& roomName); std::string GetCurrentRoomInfoStr(); std::string GetLinkInfoStr(const std::string& linkName); + std::string GetLinkSnapshotInfoStr(const std::string& linkName); void SetUserInfo(std::shared_ptr info); std::string GetUserInfoStr(); void AddUsers(std::vector users); + void AddSnapshotUuid(const std::string& linkname, const std::string& uuid); void RemoveUsers(std::vector users); void AddLink(std::shared_ptr newLink); void RemoveLink(const std::string& linkName); @@ -27,6 +36,9 @@ class Model { void RemoveRoom(const std::string& roomName); std::string FormRequest(std::string& action); void HandleResponse(std::string& response); + void HandleFile(recFile& newFile); + bool IsHandlerRecievingFiles(); + bool IsServRequired(); private: std::shared_ptr> modelImpl; }; diff --git a/client/include/model.tpp b/client/include/model.tpp index b7b80af..a1f219d 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -5,10 +5,10 @@ #include #include "model.hpp" -#include "requestHandler.hpp" #include "room.hpp" #include "link.hpp" #include "userinfo.hpp" +#include "requestHandler.hpp" #include "utils.h" @@ -19,6 +19,7 @@ public: std::string formRequest(std::string& action, Model& model); void handleResponse(std::string& response, Model& model); + void handleFile(recFile& newFile); void SetUserInfoFromStr(std::shared_ptr uinfo) { info = uinfo; @@ -39,18 +40,13 @@ public: return ret; } + std::string getLinkSnapshotInfoStr(const std::string& linkName) { + std::string ret = currentRoom->GetLinkSnapshotInfoStr(linkName); + return ret; + } + std::string getRoomInfoStr(const std::string& roomName) { std::string ret; - /* if (mainRoom->GetRoomName() == roomName) { - ret = mainRoom->GetRoomInfoStr(); - } else { - for (auto i : rooms) { - if (i->GetRoomName() == roomName) { - ret = i->GetRoomInfoStr(); - break; - } - } - } */ for (auto i : rooms) { if (i->GetRoomName() == roomName) { ret = i->GetRoomInfoStr(); @@ -68,6 +64,10 @@ public: currentRoom->AddUsers(users); } + void addSnapshotUuid(const std::string& linkname, const std::string& uuid) { + currentRoom->AddSnapshot(linkname, uuid); + } + void removeUsers(std::vector users) { currentRoom->RemoveUsers(users); } @@ -101,6 +101,14 @@ public: std::vector> GetRooms() { return rooms; } + + bool isHandlerRecievingFiles() { + return currentHandler->RecievingFiles(); + } + + bool isServRequired() { + return currentHandler->ServRequired(); + } private: std::shared_ptr> CreateRequestHandler(std::string& action, Model& model); std::shared_ptr> currentHandler; @@ -128,31 +136,34 @@ std::shared_ptr> ModelImpl::Creat switch (atoi(type.c_str()) ) { case 0: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); break; case 1: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); break; case 2: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); break; case 3: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); break; case 4: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); break; case 5: - handler = std::make_shared>(); + handler = std::make_shared>(true, true); break; case 6: - handler = std::make_shared>(); + handler = std::make_shared>(true, true); break; case 7: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); break; case 8: - handler = std::make_shared>(); + handler = std::make_shared>(false, true); + break; + case 9: + handler = std::make_shared>(true, true); break; default: break; @@ -190,6 +201,11 @@ void ModelImpl::handleResponse(std::string& response, Model +void ModelImpl::handleFile(recFile& newFile) { + currentHandler->RecieveFile(newFile); +} + template Model::Model() : modelImpl(new ModelImpl) {} template @@ -207,6 +223,12 @@ std::string Model::GetLinkInfoStr(const std::string& linkName) { return ret; } +template +std::string Model::GetLinkSnapshotInfoStr(const std::string& linkName) { + std::string ret = modelImpl->getLinkSnapshotInfoStr(linkName); + return ret; +} + template std::string Model::GetRoomInfoStr(const std::string& roomName) { std::string ret = modelImpl->getRoomInfoStr(roomName); @@ -233,6 +255,11 @@ void Model::HandleResponse(std::string& response) { modelImpl->handleResponse(response, *this); } +template +void Model::HandleFile(recFile& newFile) { + modelImpl->handleFile(newFile); +} + template std::string Model::GetUserInfoStr() { std::string ret = modelImpl->getUserInfoStr(); @@ -244,6 +271,11 @@ void Model::AddUsers(std::vector users) { modelImpl->addUsers(users); } +template +void Model::AddSnapshotUuid(const std::string& linkname, const std::string& uuid) { + modelImpl->addSnapshotUuid(linkname, uuid); +} + template void Model::RemoveUsers(std::vector users) { modelImpl->removeUsers(users); @@ -268,4 +300,14 @@ void Model::AddRoom(std::shared_ptr newRoom) { template void Model::RemoveRoom(const std::string& roomName) { modelImpl->removeRoom(roomName); +} + +template +bool Model::IsHandlerRecievingFiles() { + return modelImpl->isHandlerRecievingFiles(); +} + +template +bool Model::IsServRequired() { + return modelImpl->isServRequired(); } \ No newline at end of file diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp index df9bbc9..31279bf 100644 --- a/client/include/presenter.tpp +++ b/client/include/presenter.tpp @@ -4,6 +4,8 @@ #include #include +#include "requestHandler.hpp" + template Presenter::Presenter(const std::string& host, const size_t port) : client(host, port) { /* client.Connect(); */} @@ -29,11 +31,27 @@ void Presenter::run() { connect(); std::string request = model.FormRequest(action); client.writeToServer(request); - std::string response = client.readFromServer(); + bool endFlag = false; + std::string response = client.readFromServer(&endFlag); std::cout << std::endl << response << std::endl; - + model.HandleResponse(response); + while(!endFlag) { + if (model.IsHandlerRecievingFiles()) { + recFile newFile; + newFile.name = client.readFromServer(&endFlag); + newFile.body = client.readFileBodyFromServer(&endFlag); + std::cout << newFile.body.size() << std::endl; + model.HandleFile(newFile); + } else { + std::string response = client.readFromServer(&endFlag); + + std::cout << std::endl << response << std::endl; + + model.HandleResponse(response); + } + } action = view.GetRequest(); disconnect(); } diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index a945b28..01d4be0 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -12,22 +12,29 @@ typedef enum ExitStatus { FAILURE } ExitStatus; + template class RequestHandler { public: + RequestHandler(bool recvFiles, bool servIsReq) : recievingFiles(recvFiles), servIsRequired(servIsReq) {} virtual ExitStatus FillRequest(std::string action, Model& model) = 0; virtual ExitStatus HandleResponse(std::string& responseBody); virtual ExitStatus DoLogic(Model& app) = 0; + virtual ExitStatus RecieveFile(recFile& newFile) { return SUCCESS;} + bool RecievingFiles() { return recievingFiles; } + bool ServRequired() { return servIsRequired; } std::string& GetRequestToSend(); protected: std::string requestToSend; std::shared_ptr parser; + bool recievingFiles; + bool servIsRequired; }; template class AddUsersReqHandler : public RequestHandler { public: - AddUsersReqHandler() = default; + AddUsersReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -38,7 +45,7 @@ class AddUsersReqHandler : public RequestHandler { template class RemoveUsersReqHandler : public RequestHandler { public: - RemoveUsersReqHandler() = default; + RemoveUsersReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -49,7 +56,7 @@ class RemoveUsersReqHandler : public RequestHandler { template class AddLinkReqHandler : public RequestHandler { public: - AddLinkReqHandler() = default; + AddLinkReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -63,7 +70,7 @@ class AddLinkReqHandler : public RequestHandler { template class RemoveLinkReqHandler : public RequestHandler { public: - RemoveLinkReqHandler() = default; + RemoveLinkReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -72,22 +79,22 @@ class RemoveLinkReqHandler : public RequestHandler { }; template -class ArchiveLinkReqHandler : public RequestHandler { +class MakeSnapshotReqHandler : public RequestHandler { public: - ArchiveLinkReqHandler() = default; + MakeSnapshotReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); private: std::string linkName; - std::string body; + std::string uuid; }; template class CreateRoomReqHandler : public RequestHandler { public: - CreateRoomReqHandler() = default; + CreateRoomReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -101,7 +108,7 @@ class CreateRoomReqHandler : public RequestHandler { template class RemoveRoomReqHandler : public RequestHandler { public: - RemoveRoomReqHandler() = default; + RemoveRoomReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -112,7 +119,7 @@ class RemoveRoomReqHandler : public RequestHandler { template class LogInReqHandler : public RequestHandler { public: - LogInReqHandler() = default; + LogInReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -125,7 +132,7 @@ class LogInReqHandler : public RequestHandler { template class SignUpReqHandler : public RequestHandler { public: - SignUpReqHandler() = default; + SignUpReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -134,4 +141,18 @@ class SignUpReqHandler : public RequestHandler { std::string password; }; +template +class DownloadSnapshotReqHandler : public RequestHandler { +public: + DownloadSnapshotReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + ExitStatus FillRequest(std::string action, Model& model); + //ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); + ExitStatus RecieveFile(recFile& newFile); +private: + std::string linkName; + std::string uuid; + std::string filesdir; +}; + #include "requestHandler.tpp" \ No newline at end of file diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index b8feabf..ebded6b 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -3,6 +3,9 @@ #include "utils.h" #include "room.hpp" #include "userinfo.hpp" +#include "model.hpp" +#include +#include template @@ -144,38 +147,64 @@ ExitStatus RemoveLinkReqHandler::DoLogic(Model & } template -ExitStatus ArchiveLinkReqHandler::FillRequest(std::string action, Model& model) { +ExitStatus MakeSnapshotReqHandler::FillRequest(std::string action, Model& model) { fillDataFromJson(action, "name", &linkName); - RequestHandler::requestToSend = packToJsonString("linkname", linkName); + + std::string info = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); + + std::string linkInfo = model.GetLinkInfoStr(linkName); + fillDataFromJson(linkInfo, "uuid", &uuid); + + RequestHandler::requestToSend = packToJsonString("command", 6, "login", login, "token", token, "uuid", uuid); return SUCCESS; } template -ExitStatus ArchiveLinkReqHandler::HandleResponse(std::string &responseBody) { - /* if (responseBody.find("success") == std::string::npos) { +ExitStatus MakeSnapshotReqHandler::HandleResponse(std::string &responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("uuid", uuid)); + std::cout << "success!" << std::endl; + } else { return FAILURE; } - size_t bodyStartpos = sizeof("success"); - body = responseBody.substr(++bodyStartpos, responseBody.size()); */ return SUCCESS; } + template -ExitStatus ArchiveLinkReqHandler::DoLogic(Model &model) { return SUCCESS; } +ExitStatus MakeSnapshotReqHandler::DoLogic(Model &model) { + model.AddSnapshotUuid(linkName, uuid); + return SUCCESS; +} + template ExitStatus CreateRoomReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "name", &roomName, "host", &roomHost, "private", &isPrivate); + fillDataFromJson(action, "name", &roomName, "private", &isPrivate); std::string info = model.GetUserInfoStr(); std::string login, token; fillDataFromJson(info, "name", &login, "uuid", &token); + roomHost = login; RequestHandler::requestToSend = packToJsonString("command", 0, "login", login, "token", token, "name", - roomName, "host", roomHost, "private", isPrivate); - + roomName, "private", isPrivate); return SUCCESS; } @@ -282,3 +311,68 @@ ExitStatus SignUpReqHandler::FillRequest(std::string action, Mod template ExitStatus SignUpReqHandler::DoLogic(Model &model) { return SUCCESS; } + + +template +ExitStatus DownloadSnapshotReqHandler::FillRequest(std::string action, Model& model) { + fillDataFromJson(action, "name", &linkName, "filesdir", &filesdir); + + std::string info = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); + + uuid = model.GetLinkSnapshotInfoStr(linkName); + + RequestHandler::requestToSend = packToJsonString("command", 9, "login", login, "token", token, "uuid", uuid); + + return SUCCESS; +} + +/* template +ExitStatus MakeSnapshotReqHandler::HandleResponse(std::string &responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("files_dir", filesdir)); + std::cout << "success!" << std::endl; + } else { + return FAILURE; + } + + return SUCCESS; +} */ + + +template +ExitStatus DownloadSnapshotReqHandler::DoLogic(Model &model) { + + return SUCCESS; +} + +template +ExitStatus DownloadSnapshotReqHandler::RecieveFile(recFile& newFile) { + std::string mainDir(filesdir); + std::experimental::filesystem::create_directories(mainDir); + + std::string staticDir = mainDir + "/static"; + std::experimental::filesystem::create_directories(staticDir); + + std::string pathToNewFile(mainDir); + pathToNewFile += newFile.name; + + std::ofstream file(pathToNewFile, std::ios::binary); + for (auto i : newFile.body) { + file.write(&i, 1); + } + return SUCCESS; +} \ No newline at end of file diff --git a/client/include/room.hpp b/client/include/room.hpp index 2024ab6..b0c9db7 100644 --- a/client/include/room.hpp +++ b/client/include/room.hpp @@ -15,7 +15,9 @@ class Room { std::string GetRoomHost(); std::string GetRoomName(); std::string GetLinkInfoStr(const std::string& linkName); + std::string GetLinkSnapshotInfoStr(const std::string& linkName); void AddUsers(std::vector users); + void AddSnapshot(const std::string& linkname, const std::string& uuid); void RemoveUsers(std::vector users); void AddLink(std::shared_ptr newLink); void RemoveLink(const std::string& linkName); diff --git a/client/include/socket.hpp b/client/include/socket.hpp index 685b078..08c792e 100644 --- a/client/include/socket.hpp +++ b/client/include/socket.hpp @@ -3,6 +3,7 @@ #include // close() #include #include +#include class Socket { @@ -21,7 +22,8 @@ class Socket void Connect(const std::string& host, int port); void Send(const std::string& str); std::string Recv(); - std::string RecvFile(bool endFlag); + std::string RecvFile(bool* endFlag); + std::vector RecvFileVec(bool* endFlag); void Close(); private: int sd; diff --git a/client/include/utils.tpp b/client/include/utils.tpp index 7b9834f..6765fe0 100644 --- a/client/include/utils.tpp +++ b/client/include/utils.tpp @@ -23,8 +23,6 @@ std::string packToJsonString(T key, K value, Args... args) { return jsonStr; } - - // Все, что связано с распаковкой JSON'а diff --git a/client/src/CMakeLists.txt b/client/src/CMakeLists.txt index d6d2c98..7267751 100644 --- a/client/src/CMakeLists.txt +++ b/client/src/CMakeLists.txt @@ -6,6 +6,6 @@ target_include_directories(client PUBLIC "${PROJECT_SOURCE_DIR}/include") find_package(Boost REQUIRED COMPONENTS system thread) message(STATUS "Boost version: ${Boost_VERSION}") -target_link_libraries(main PUBLIC ${Boost_LIBRARIES} PUBLIC client) +target_link_libraries(main PUBLIC ${Boost_LIBRARIES} PUBLIC client stdc++fs) -add_definitions(-Wall -Werror -Wpedantic) +add_definitions(-Wall -Werror -Wpedantic -g) diff --git a/client/src/client.cpp b/client/src/client.cpp index 7ecc55a..59421ef 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -10,11 +10,14 @@ void Client::Close() { sock.Close(); } -std::string Client::readFromServer() { - bool endFlag = false; +std::string Client::readFromServer(bool* endFlag) { return sock.RecvFile(endFlag); } +std::vector Client::readFileBodyFromServer(bool* endFlag) { + return sock.RecvFileVec(endFlag); +} + void Client::writeToServer(std::string& req) { sock.Send(req); diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index cf69cc1..f633031 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -18,12 +18,11 @@ void fillObject(std::vector* vec) { std::string createRoomInput() { std::string name; - std::string host; std::string isPrivate; - writeData(&name, &host, &isPrivate); + writeData(&name, &isPrivate); - std::string ret = packToJsonString("command", "0", "name", name, "host", host, "private", isPrivate); + std::string ret = packToJsonString("command", "0", "name", name, "private", isPrivate); return ret; } @@ -95,4 +94,14 @@ std::string signUpInput() { std::string ret = packToJsonString("command","8", "login", login, "password", password); return ret; +} + + +std::string downloadSnapshotInput() { + std::string name, filesdir; + + writeData(&name, &filesdir); + + std::string ret = packToJsonString("command","9", "name", name, "filesdir", filesdir); + return ret; } \ No newline at end of file diff --git a/client/src/link.cpp b/client/src/link.cpp index 210f3ca..002fba2 100644 --- a/client/src/link.cpp +++ b/client/src/link.cpp @@ -10,14 +10,15 @@ class LinkImpl { LinkImpl(std::string name, std::string url, std::string uuid, std::string description); std::string getLinkName(); std::string getLinkInfo(); + std::string getSnapshotUuid(); void setLinkInfo(); - void addSnaphotPath(std::string path); + void addSnaphot(const std::string& uuid); private: std::string linkname; std::string url; std::string uuid; std::string description; - std::vector snapshotPaths; + std::string snapshotPath; }; LinkImpl::LinkImpl(std::string name, std::string url, std::string uuid, std::string description) @@ -35,12 +36,17 @@ std::string LinkImpl::getLinkInfo() { return ret; } +std::string LinkImpl::getSnapshotUuid() { + std::string ret = snapshotPath; + return ret; +} + void LinkImpl::setLinkInfo() { } -void LinkImpl::addSnaphotPath(std::string path) { - snapshotPaths.push_back(std::move(path)); +void LinkImpl::addSnaphot(const std::string& uuid) { + snapshotPath = uuid; } Link::Link(std::string& name, std::string& url, std::string& uuid, std::string& description) @@ -58,6 +64,12 @@ std::string Link::GetLinkInfo() { return ret; } -void Link::addSnapshot(std::string& path) { - linkImpl->addSnaphotPath(path); -} \ No newline at end of file +std::string Link::GetSnapshotUuid() { + std::string ret = linkImpl->getSnapshotUuid(); + return ret; +} + +void Link::AddSnapshot(const std::string& uuid) { + linkImpl->addSnaphot(uuid); +} + diff --git a/client/src/room.cpp b/client/src/room.cpp index e2471a8..3e403ed 100644 --- a/client/src/room.cpp +++ b/client/src/room.cpp @@ -13,7 +13,9 @@ class RoomImpl { std::string getRoomHost(); std::string getRoomName(); std::string getLinkInfoStr(const std::string& linkName); + std::string getLinkSnapshotInfoStr(const std::string& linkName); void addUsers(std::vector users); + void addSnapshot(const std::string& linkname, const std::string& uuid); void removeUsers(std::vector users); void addLink(std::shared_ptr newLink); void removeLink(const std::string& linkName); @@ -54,6 +56,11 @@ std::string Room::GetLinkInfoStr(const std::string& linkName) { return ret; } +std::string Room::GetLinkSnapshotInfoStr(const std::string& linkName) { + std::string ret = roomImpl->getLinkSnapshotInfoStr(linkName); + return ret; +} + void Room::AddUsers(std::vector users) { roomImpl->addUsers(users); } @@ -66,6 +73,10 @@ void Room::AddLink(std::shared_ptr newLink) { roomImpl->addLink(newLink); } +void Room::AddSnapshot(const std::string& linkname, const std::string& uuid) { + roomImpl->addSnapshot(linkname, uuid); +} + void Room::RemoveLink(const std::string& linkName) { roomImpl->removeLink(linkName); } @@ -110,6 +121,19 @@ std::string RoomImpl::getLinkInfoStr(const std::string& linkName) { return ret; } +std::string RoomImpl::getLinkSnapshotInfoStr(const std::string& linkName) { + std::string ret; + for(auto i : links) { + if (i->GetLinkName() == linkName) { + ret = i->GetSnapshotUuid(); + } + } + if (ret.empty()) { + throw std::runtime_error("Link was not found");; + } + return ret; +} + void RoomImpl::addUsers(std::vector users) { for (auto i : users) { auto it = std::find(participants.begin(), participants.end(), i); @@ -119,12 +143,20 @@ void RoomImpl::addUsers(std::vector users) { } } +void RoomImpl::addSnapshot(const std::string& linkname, const std::string& uuid) { + auto it = std::find_if(links.begin(), links.end(), + [linkname](std::shared_ptr link) { return link->GetLinkName() == linkname; }); + (*it)->AddSnapshot(uuid); +} + void RoomImpl::removeUsers(std::vector users) { for (auto i : users) { participants.erase(std::remove(participants.begin(), participants.end(), i), participants.end()); } } + + void RoomImpl::addLink(std::shared_ptr newLink) { links.push_back(newLink); } diff --git a/client/src/socket.cpp b/client/src/socket.cpp index 42e6dba..749f0ce 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include "socket.hpp" @@ -117,7 +119,7 @@ std::string Socket::Recv() { return ret; } -std::string Socket::RecvFile(bool endFlag) { +std::string Socket::RecvFile(bool* endFlag) { char buf[BUFSIZE]; std::string ret; @@ -126,6 +128,9 @@ std::string Socket::RecvFile(bool endFlag) { if (-1 == n && errno != EAGAIN) { throw std::runtime_error("read failed: " + std::string(strerror(errno))); } + if (-1 == n && ret.empty()) { + continue; + } if (0 == n || -1 == n) { break; } @@ -136,7 +141,38 @@ std::string Socket::RecvFile(bool endFlag) { if (buf[0] == 'e') { break; } else if (buf[0] == 'f') { - endFlag = true; + *endFlag = true; + break; + } + } + return ret; +} + + +std::vector Socket::RecvFileVec(bool* endFlag) { + char buf[BUFSIZE]; + std::vector ret; + + while (true) { + int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); + if (-1 == n && errno != EAGAIN) { + throw std::runtime_error("read failed: " + std::string(strerror(errno))); + } + if (0 == n && ret.empty()) { + continue; + } + if (0 == n || -1 == n) { + break; + } + + std::copy(&buf[1], &buf[n-1], std::back_inserter(ret)); + while (ret.back() == '\x1A') { + ret.pop_back(); + } + if (buf[0] == 'e') { + break; + } else if (buf[0] == 'f') { + *endFlag = true; break; } } diff --git a/client/src/view.cpp b/client/src/view.cpp index 0b022a2..1e54976 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -16,6 +16,7 @@ enum RequestCommand { MAKE_SNAPSHOT, LOG_IN_USER, SIGN_UP_USER, + DOWNLOAD_SNAPSHOT }; @@ -27,7 +28,7 @@ std::string ConsoleView::GetRequest() { std::cin >> key; switch (key) { case CREATE_ROOM: { - std::cout << "Write name, host of room and is it private(true/false)" << std::endl; + std::cout << "Write name of room and is it private(true/false)" << std::endl; inputStr = createRoomInput(); } break; @@ -71,6 +72,11 @@ std::string ConsoleView::GetRequest() { inputStr = signUpInput(); break; } + case DOWNLOAD_SNAPSHOT: { + std::cout << "Write name of link and directory " << std::endl; + inputStr = downloadSnapshotInput(); + break; + } case -1: break; default: @@ -92,5 +98,6 @@ void ConsoleView::PrintCommands() { << "- 6.Make snapshot" << std::endl << "- 7.Log in" << std::endl << "- 8.Sign up" << std::endl + << "- 9.Download snapshot" << std::endl << "- (-1).Exit" << std::endl; } \ No newline at end of file From d03341a7b35b3bdeee52ce42195e73396820fbf2 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Sun, 27 Dec 2020 21:47:25 +0300 Subject: [PATCH 30/36] rec --- client/include/requestHandler.tpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index ebded6b..e7d45f3 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -355,20 +355,24 @@ ExitStatus MakeSnapshotReqHandler::HandleResponse(std::string &r template ExitStatus DownloadSnapshotReqHandler::DoLogic(Model &model) { + std::string mainDir(filesdir); + std::experimental::filesystem::create_directories(mainDir); + std::string staticDir = mainDir + "/static"; + std::experimental::filesystem::create_directories(staticDir); return SUCCESS; } template ExitStatus DownloadSnapshotReqHandler::RecieveFile(recFile& newFile) { std::string mainDir(filesdir); - std::experimental::filesystem::create_directories(mainDir); - std::string staticDir = mainDir + "/static"; - std::experimental::filesystem::create_directories(staticDir); - std::string pathToNewFile(mainDir); - pathToNewFile += newFile.name; + if (newFile.name.find("index") != std::string::npos) { + pathToNewFile += newFile.name; + } else { + pathToNewFile += "/static" + newFile.name; + } std::ofstream file(pathToNewFile, std::ios::binary); for (auto i : newFile.body) { From a4f47cf59ec65dfc1c0b8fc7a05dc5dc3b7466eb Mon Sep 17 00:00:00 2001 From: Verhushker Date: Sun, 27 Dec 2020 23:00:03 +0300 Subject: [PATCH 31/36] fixed serialization --- client/src/utils.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/client/src/utils.cpp b/client/src/utils.cpp index 0b63650..dfa9d92 100644 --- a/client/src/utils.cpp +++ b/client/src/utils.cpp @@ -43,10 +43,25 @@ std::vector splitString(const std::string& data) { [](char c) { return separator.find(c) != std::string::npos; }); tempStr.erase(newEnd, tempStr.end()); + bool elemStarted = false; + for(auto it = tempStr.begin(); it != tempStr.end(); it++) { if ((*it == ':' || *it == ',') /* && !special_block */) { - elements.push_back(element); - element = ""; + if (*it == ':') { + if (!elemStarted) { + elements.push_back(element); + element = ""; + elemStarted = true; + } else { + element += *it; + } + } + if (*it == ',') { + elemStarted = false; + elements.push_back(element); + element = ""; + } + } else { element += *it; } From 2fb936573588eb8fcd2db1fb561b72dfbda5dd18 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Sun, 27 Dec 2020 23:27:53 +0300 Subject: [PATCH 32/36] debug try --- client/src/socket.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client/src/socket.cpp b/client/src/socket.cpp index 749f0ce..e2bb1b3 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -169,6 +169,9 @@ std::vector Socket::RecvFileVec(bool* endFlag) { while (ret.back() == '\x1A') { ret.pop_back(); } + //debug + ret.push_back('@'); + //debug if (buf[0] == 'e') { break; } else if (buf[0] == 'f') { From 7f9d74c3b8d3ff0930c3e68d2200f4553cb615b6 Mon Sep 17 00:00:00 2001 From: Verhushker Date: Mon, 28 Dec 2020 09:46:03 +0300 Subject: [PATCH 33/36] getting room from server --- client/include/inputUtils.hpp | 3 + client/include/link.hpp | 2 + client/include/model.tpp | 24 ++--- client/include/presenter.tpp | 4 +- client/include/requestHandler.hpp | 38 ++++++++ client/include/requestHandler.tpp | 155 ++++++++++++++++++++++++++---- client/include/room.hpp | 1 + client/src/client.cpp | 2 +- client/src/inputUtils.cpp | 21 +++- client/src/link.cpp | 11 ++- client/src/main.cpp | 32 ++++-- client/src/room.cpp | 8 +- client/src/socket.cpp | 106 ++++++++++++++------ client/src/view.cpp | 22 ++++- 14 files changed, 346 insertions(+), 83 deletions(-) diff --git a/client/include/inputUtils.hpp b/client/include/inputUtils.hpp index 5010bcb..82214bd 100644 --- a/client/include/inputUtils.hpp +++ b/client/include/inputUtils.hpp @@ -22,5 +22,8 @@ std::string makeSnapshotInput(); std::string logInInput(); std::string signUpInput(); std::string downloadSnapshotInput(); +std::string getUserRoomInput(); +std::string getUserLinksInput(); +std::string getLinkSnapshotsInput(); #include "inputUtils.tpp" \ No newline at end of file diff --git a/client/include/link.hpp b/client/include/link.hpp index 6f2d3e2..d7a1b06 100644 --- a/client/include/link.hpp +++ b/client/include/link.hpp @@ -6,9 +6,11 @@ class LinkImpl; class Link { public: Link(std::string& name, std::string& url, std::string& uuid, std::string& description); + Link(Link& link); ~Link(); std::string GetLinkName(); std::string GetLinkInfo(); + std::shared_ptr getLinkImpl(); std::string GetSnapshotUuid(); void SetLinkInfo(); void AddSnapshot(const std::string& uuid); diff --git a/client/include/model.tpp b/client/include/model.tpp index a1f219d..308f753 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -127,7 +127,7 @@ ModelImpl::ModelImpl() template std::shared_ptr> ModelImpl::CreateRequestHandler(std::string& action, Model& model) { - std::cout << action << std::endl; + //std::cout << action << std::endl; std::string type; fillDataFromJson(action, "command", &type); @@ -165,6 +165,15 @@ std::shared_ptr> ModelImpl::Creat case 9: handler = std::make_shared>(true, true); break; + case 10: + handler = std::make_shared>(true, true); + break; + case 11: + handler = std::make_shared>(true, true); + break; + case 12: + handler = std::make_shared>(true, true); + break; default: break; } @@ -174,19 +183,6 @@ std::shared_ptr> ModelImpl::Creat return handler; } -/* void ModelImpl::passAction(std::string& action, Model& model) { - std::shared_ptr handler = CreateRequestHandler(action, model); - - std::string req = handler->GetRequestToSend(); - - client.writeToServer(req); - std::string response = client.readFromServer(); - std::cout << std::endl << response << std::endl; - if (handler->HandleResponse(response) == SUCCESS) { - handler->DoLogic(model); - } -} */ - template std::string ModelImpl::formRequest(std::string& action, Model& model) { currentHandler = CreateRequestHandler(action, model); diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp index 31279bf..424c1ca 100644 --- a/client/include/presenter.tpp +++ b/client/include/presenter.tpp @@ -30,6 +30,7 @@ void Presenter::run() { while(!action.empty()) { connect(); std::string request = model.FormRequest(action); + client.writeToServer(request); bool endFlag = false; std::string response = client.readFromServer(&endFlag); @@ -41,8 +42,9 @@ void Presenter::run() { if (model.IsHandlerRecievingFiles()) { recFile newFile; newFile.name = client.readFromServer(&endFlag); + std::cout << "FILENAME: " << newFile.name.substr(0, 80) << std::endl << std::endl << std::endl; newFile.body = client.readFileBodyFromServer(&endFlag); - std::cout << newFile.body.size() << std::endl; + model.HandleFile(newFile); } else { std::string response = client.readFromServer(&endFlag); diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index 01d4be0..873ad50 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "model.hpp" @@ -155,4 +156,41 @@ class DownloadSnapshotReqHandler : public RequestHandler { std::string filesdir; }; +template +class GetUserRoomReqHandler : public RequestHandler { +public: + GetUserRoomReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + ExitStatus FillRequest(std::string action, Model& model); + ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string uuid; +}; + +template +class GetUserLinksReqHandler : public RequestHandler { +public: + GetUserLinksReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + ExitStatus FillRequest(std::string action, Model& model); + ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::vector> map; + std::vector links; + std::string uuid; +}; + +template +class GetLinkSnapshotReqHandler : public RequestHandler { +public: + GetLinkSnapshotReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + ExitStatus FillRequest(std::string action, Model& model); + ExitStatus HandleResponse(std::string& responseBody); + ExitStatus DoLogic(Model& app); +private: + std::string linkName; + std::string uuid; + std::vector> map; +}; + #include "requestHandler.tpp" \ No newline at end of file diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index e7d45f3..f24828e 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -6,6 +6,8 @@ #include "model.hpp" #include #include +#include + template @@ -328,12 +330,95 @@ ExitStatus DownloadSnapshotReqHandler::FillRequest(std::string a return SUCCESS; } -/* template -ExitStatus MakeSnapshotReqHandler::HandleResponse(std::string &responseBody) { + +template +ExitStatus DownloadSnapshotReqHandler::DoLogic(Model &model) { + std::string mainDir(filesdir); + std::experimental::filesystem::create_directories(mainDir); + + std::string staticDir = mainDir + "/static"; + std::experimental::filesystem::create_directories(staticDir); + return SUCCESS; +} + +template +ExitStatus DownloadSnapshotReqHandler::RecieveFile(recFile& newFile) { + std::string mainDir(filesdir); + std::string staticDir = mainDir + "/static"; + std::string pathToNewFile(mainDir); + if (newFile.name.find("index") != std::string::npos) { + pathToNewFile += newFile.name; + } else { + pathToNewFile += "/static" + newFile.name; + } + + std::cout << "PATH TO NEW FILE: " << pathToNewFile.substr(0, 80) << std::endl; + + std::ofstream file(pathToNewFile, std::ios::binary); + for (auto i : newFile.body) { + file.write(&i, 1); + } + return SUCCESS; +} + +template +ExitStatus GetUserRoomReqHandler::FillRequest(std::string action, Model& model) { + std::string info = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); + RequestHandler::requestToSend = packToJsonString("command", 10, "login", login, "token", token); + return SUCCESS; +} + +template +ExitStatus GetUserRoomReqHandler::HandleResponse(std::string &responseBody) { try { RequestHandler::parser = std::make_shared(responseBody); } + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("uuid", uuid)); + } else { + return FAILURE; + } + std::cout << "success!" << std::endl; + return SUCCESS; +} + +template +ExitStatus GetUserRoomReqHandler::DoLogic(Model &model) { + std::string roomName = "roomName", roomHost = "roomHost"; + std::shared_ptr newRoom = std::make_shared(roomName, roomHost, uuid, true); + model.AddRoom(newRoom); + return SUCCESS; +} + +template +ExitStatus GetUserLinksReqHandler::FillRequest(std::string action, Model& model) { + std::string info = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); + + std::string roomInfo = model.GetCurrentRoomInfoStr(); + fillDataFromJson(roomInfo, "uuid", &uuid); + + RequestHandler::requestToSend = packToJsonString("command", 11, "login", login, "token", token, "uuid", uuid); + return SUCCESS; +} + +template +ExitStatus GetUserLinksReqHandler::HandleResponse(std::string &responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } catch (...) { throw std::runtime_error("Failed to parse JSON!"); } @@ -343,40 +428,70 @@ ExitStatus MakeSnapshotReqHandler::HandleResponse(std::string &r throw std::runtime_error("Failed to parse JSON!"); } if (error.empty()) { - (RequestHandler::parser->get_value("files_dir", filesdir)); - std::cout << "success!" << std::endl; + (RequestHandler::parser->get_value("objects", map)); } else { return FAILURE; } + std::cout << "success!" << std::endl; + return SUCCESS; +} +template +ExitStatus GetUserLinksReqHandler::DoLogic(Model &model) { + for (auto i : map) { + std::string description = i.find("description")->second; + std::string uuid = i.find("link_uuid")->second; + std::string linkname = i.find("name")->second; + std::string url = i.find("url")->second; + + std::shared_ptr newLink = std::make_shared(linkname, url, uuid, description); + model.AddLink(newLink); + } return SUCCESS; -} */ +} template -ExitStatus DownloadSnapshotReqHandler::DoLogic(Model &model) { - std::string mainDir(filesdir); - std::experimental::filesystem::create_directories(mainDir); +ExitStatus GetLinkSnapshotReqHandler::FillRequest(std::string action, Model& model) { + std::string info = model.GetUserInfoStr(); + std::string login, token; + fillDataFromJson(info, "name", &login, "uuid", &token); - std::string staticDir = mainDir + "/static"; - std::experimental::filesystem::create_directories(staticDir); + fillDataFromJson(action, "linkname", &linkName); + std::string linkInfo = model.GetLinkInfoStr(linkName); + fillDataFromJson(linkInfo, "uuid", &uuid); + + RequestHandler::requestToSend = packToJsonString("command", 12, "login", login, "token", token, "uuid", uuid); return SUCCESS; } template -ExitStatus DownloadSnapshotReqHandler::RecieveFile(recFile& newFile) { - std::string mainDir(filesdir); - std::string staticDir = mainDir + "/static"; - std::string pathToNewFile(mainDir); - if (newFile.name.find("index") != std::string::npos) { - pathToNewFile += newFile.name; +ExitStatus GetLinkSnapshotReqHandler::HandleResponse(std::string &responseBody) { + try { + RequestHandler::parser = std::make_shared(responseBody); + } + catch (...) { + throw std::runtime_error("Failed to parse JSON!"); + } + + std::string error; + if (!RequestHandler::parser->get_value("error", error)) { + throw std::runtime_error("Failed to parse JSON!"); + } + if (error.empty()) { + (RequestHandler::parser->get_value("objects", map)); } else { - pathToNewFile += "/static" + newFile.name; + return FAILURE; } + return SUCCESS; +} - std::ofstream file(pathToNewFile, std::ios::binary); - for (auto i : newFile.body) { - file.write(&i, 1); +template +ExitStatus GetLinkSnapshotReqHandler::DoLogic(Model &model) { + for (auto i : map) { + std::string snapuuid = i.find("snapshot_uuid")->second; + + model.AddSnapshotUuid(linkName, snapuuid); } return SUCCESS; } \ No newline at end of file diff --git a/client/include/room.hpp b/client/include/room.hpp index b0c9db7..0907c6a 100644 --- a/client/include/room.hpp +++ b/client/include/room.hpp @@ -10,6 +10,7 @@ class RoomImpl; class Room { public: Room(); + Room(std::string& roomName, std::string& roomId, std::string& uuid, bool isPrivate); std::string GetRoomInfoStr(); std::string GetRoomHost(); diff --git a/client/src/client.cpp b/client/src/client.cpp index 59421ef..d2e18d1 100644 --- a/client/src/client.cpp +++ b/client/src/client.cpp @@ -1,4 +1,5 @@ #include "client.hpp" +#include Client::Client(const std::string& _host, int _port) : host(_host), port(_port), sock(-1) {} @@ -19,6 +20,5 @@ std::vector Client::readFileBodyFromServer(bool* endFlag) { } void Client::writeToServer(std::string& req) { - sock.Send(req); } \ No newline at end of file diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index f633031..43c284f 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -104,4 +104,23 @@ std::string downloadSnapshotInput() { std::string ret = packToJsonString("command","9", "name", name, "filesdir", filesdir); return ret; -} \ No newline at end of file +} + + +std::string getUserRoomInput() { + std::string ret = packToJsonString("command","10"); + return ret; +} + +std::string getUserLinksInput() { + std::string ret = packToJsonString("command","11"); + return ret; +} + +std::string getLinkSnapshotsInput() { + std::string linkName; + + writeData(&linkName); + std::string ret = packToJsonString("command","12", "linkname", linkName); + return ret; +} diff --git a/client/src/link.cpp b/client/src/link.cpp index 002fba2..de7b1df 100644 --- a/client/src/link.cpp +++ b/client/src/link.cpp @@ -18,7 +18,7 @@ class LinkImpl { std::string url; std::string uuid; std::string description; - std::string snapshotPath; + std::vector snapshotPath; }; LinkImpl::LinkImpl(std::string name, std::string url, std::string uuid, std::string description) @@ -37,7 +37,7 @@ std::string LinkImpl::getLinkInfo() { } std::string LinkImpl::getSnapshotUuid() { - std::string ret = snapshotPath; + std::string ret = snapshotPath[0]; return ret; } @@ -46,13 +46,18 @@ void LinkImpl::setLinkInfo() { } void LinkImpl::addSnaphot(const std::string& uuid) { - snapshotPath = uuid; + snapshotPath.push_back(uuid); } Link::Link(std::string& name, std::string& url, std::string& uuid, std::string& description) : linkImpl(new LinkImpl(name, url, uuid, description)){} Link::~Link() {} +Link::Link(Link& link) : linkImpl(link.getLinkImpl()) {} + +std::shared_ptr Link::getLinkImpl() { + return linkImpl; +} std::string Link::GetLinkName() { std::string ret = linkImpl->getLinkName(); diff --git a/client/src/main.cpp b/client/src/main.cpp index 82dfa55..47d5cdd 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -38,15 +39,32 @@ class JsonParser { bool get_value(const char* key, std::vector& value) { if (pt.get_child_optional(key)) { - BOOST_FOREACH (boost::property_tree::ptree::value_type& field, pt.get_child(key)) - { - value.push_back(field.second.data()); - } - return true; + BOOST_FOREACH (boost::property_tree::ptree::value_type& field, pt.get_child(key)) + { + value.push_back(field.second.data()); + } + return true; + } + return false; } - return false; - } + bool get_value(const char* key, std::vector>& value) { + if (pt.get_child_optional(key)) { + BOOST_FOREACH (boost::property_tree::ptree::value_type& objs, pt.get_child(key)) + { + std::map object; + + BOOST_FOREACH (boost::property_tree::ptree::value_type& obj, objs.second) { + + object[obj.first] = obj.second.data(); + } + value.push_back(object); + } + + return true; + } + return false; + } private: boost::property_tree::ptree pt; diff --git a/client/src/room.cpp b/client/src/room.cpp index 3e403ed..6cc0c24 100644 --- a/client/src/room.cpp +++ b/client/src/room.cpp @@ -8,7 +8,7 @@ class RoomImpl { public: - RoomImpl(std::string name, std::string host, std::string uuid, bool isPrivate); + RoomImpl(std::string& name, std::string& host, std::string& uuid, bool isPrivate); std::string getRoomInfoStr(); std::string getRoomHost(); std::string getRoomName(); @@ -36,7 +36,9 @@ class RoomImpl { Room::Room() {} Room::Room(std::string& name, std::string& host, std::string& uuid, bool isPrivate) -: roomImpl(new RoomImpl(name, host, uuid, isPrivate)) {} +/* : roomImpl(std::make_shared(name, host, uuid, isPrivate)) */ { + roomImpl = std::make_shared(name, host, uuid, isPrivate); +} std::string Room::GetRoomInfoStr() { return roomImpl->getRoomInfoStr(); @@ -89,7 +91,7 @@ std::string Room::archiveLink(std::string& linkName) { } -RoomImpl::RoomImpl(std::string name, std::string host, std::string uuid, bool isPrivate) +RoomImpl::RoomImpl(std::string& name, std::string& host, std::string& uuid, bool isPrivate) : roomName(name), roomHost(host), uuid(uuid), diff --git a/client/src/socket.cpp b/client/src/socket.cpp index e2bb1b3..41fc4a0 100644 --- a/client/src/socket.cpp +++ b/client/src/socket.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "socket.hpp" @@ -63,41 +64,64 @@ void Socket::Connect(const std::string& host, int port) { sd = _sd; } -/* void Socket::Send(const std::string& str) { - size_t left = str.size(); - ssize_t sent = 0; - int flags = 0; - while (left > 0) { - sent = ::send(sd, str.data() + sent, str.size() - sent, flags); - if (-1 == sent) { - throw std::runtime_error("write failed: " + std::string(strerror(errno))); - } - left -= sent; - } -} */ +std::vector form_packages(std::string data, char status) { + std::vector pkgs; -void Socket::Send(const std::string& str) { - std::string temp = "f" + str; - while(temp.size() != 400) { - temp.push_back('\x1A'); + std::string pkg; + if (data.size() <= BUFSIZE-1) { + pkg += status; + pkg += data; + + for (size_t i = 0; i < BUFSIZE - 1 - data.size(); i++) { + pkg += '\x1A'; + } + pkgs.push_back(pkg); + pkg.clear(); + } else { + while(!data.empty()) { + if (data.size() <= BUFSIZE-1) { + pkg += status; + } else { + pkg += 'c'; + } + + pkg += data.substr(0, BUFSIZE-1); + data.erase(0, BUFSIZE-1); + + while (pkg.size() < BUFSIZE) { + pkg += '\x1A'; + } + + pkgs.push_back(pkg); + pkg.clear(); } + } - size_t left = temp.size(); - ssize_t sent = 0; + return pkgs; +} + +void Socket::Send(const std::string& data) { + std::vector pkgs = form_packages(data, 'f'); + for(auto& str : pkgs) { + size_t left = str.size(); + + ssize_t sent = 0; - int flags = 0; - while (left > 0) { - sent = ::send(sd, temp.data() + sent, temp.size() - sent, flags); - if (-1 == sent) { + while (left > 0) { + sent = ::send(sd, str.data() + sent, str.size() - sent, 0); + if (-1 == sent) throw std::runtime_error("write failed: " + std::string(strerror(errno))); + + left -= sent; } - left -= sent; - } + } } + + std::string Socket::Recv() { char buf[256]; std::string ret; @@ -124,7 +148,14 @@ std::string Socket::RecvFile(bool* endFlag) { std::string ret; while (true) { - int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); + int n = ::recv(sd, buf, BUFSIZE, MSG_NOSIGNAL); + std::string str(buf); + if (n > 0) + if (n != BUFSIZE && n != -1) { + char* bufPtr = buf + n; + n = ::recv(sd, bufPtr, BUFSIZE - n, MSG_NOSIGNAL); + } + if (-1 == n && errno != EAGAIN) { throw std::runtime_error("read failed: " + std::string(strerror(errno))); } @@ -134,11 +165,14 @@ std::string Socket::RecvFile(bool* endFlag) { if (0 == n || -1 == n) { break; } - ret.append(buf, 1, n - 1); + ret.append(buf, 1, BUFSIZE - 1); while (ret.back() == '\x1A') { ret.pop_back(); } + + if (buf[0] == 'e') { + break; } else if (buf[0] == 'f') { *endFlag = true; @@ -152,26 +186,34 @@ std::string Socket::RecvFile(bool* endFlag) { std::vector Socket::RecvFileVec(bool* endFlag) { char buf[BUFSIZE]; std::vector ret; - + size_t i = 0; while (true) { int n = ::recv(sd, buf, sizeof(buf), MSG_NOSIGNAL); + std::string str(buf); + if (n > 0) { + } + + if (n != BUFSIZE && n != -1) { + char* bufPtr = buf + n; + n = ::recv(sd, bufPtr, BUFSIZE - n, MSG_NOSIGNAL); + } if (-1 == n && errno != EAGAIN) { throw std::runtime_error("read failed: " + std::string(strerror(errno))); } - if (0 == n && ret.empty()) { + if (-1 == n && ret.empty() && i < 3) { + sleep(2); + i++; continue; } if (0 == n || -1 == n) { break; } - std::copy(&buf[1], &buf[n-1], std::back_inserter(ret)); + std::copy(&buf[1], &buf[BUFSIZE], std::back_inserter(ret)); while (ret.back() == '\x1A') { ret.pop_back(); } - //debug - ret.push_back('@'); - //debug + if (buf[0] == 'e') { break; } else if (buf[0] == 'f') { diff --git a/client/src/view.cpp b/client/src/view.cpp index 1e54976..983b889 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -16,7 +16,10 @@ enum RequestCommand { MAKE_SNAPSHOT, LOG_IN_USER, SIGN_UP_USER, - DOWNLOAD_SNAPSHOT + DOWNLOAD_SNAPSHOT, + GET_USER_ROOM, + GET_USER_LINKS, + GET_LINK_SNAPSHOTS }; @@ -77,6 +80,20 @@ std::string ConsoleView::GetRequest() { inputStr = downloadSnapshotInput(); break; } + case GET_USER_ROOM: { + //std::cout << "Write name of link and directory " << std::endl; + inputStr = getUserRoomInput(); + break; + } + case GET_USER_LINKS: { + inputStr = getUserLinksInput(); + break; + } + case GET_LINK_SNAPSHOTS: { + std::cout << "Write name of link" << std::endl; + inputStr = getLinkSnapshotsInput(); + break; + } case -1: break; default: @@ -99,5 +116,8 @@ void ConsoleView::PrintCommands() { << "- 7.Log in" << std::endl << "- 8.Sign up" << std::endl << "- 9.Download snapshot" << std::endl + << "- 10.Get user room" << std::endl + << "- 11.Get user links" << std::endl + << "- 12.Get link snapshots" << std::endl << "- (-1).Exit" << std::endl; } \ No newline at end of file From 64cf698d5dd6036d648e2b2f6869ba655090ec8c Mon Sep 17 00:00:00 2001 From: Verhushker Date: Mon, 28 Dec 2020 11:43:04 +0300 Subject: [PATCH 34/36] new commit --- client/include/model.hpp | 1 + client/include/model.tpp | 35 +++++++++----- client/include/presenter.tpp | 79 ++++++++++++++++++++++++++++--- client/include/requestHandler.hpp | 30 ++++++------ client/include/requestHandler.tpp | 16 +++---- client/src/inputUtils.cpp | 16 +++---- client/src/view.cpp | 36 ++++++++++++-- 7 files changed, 158 insertions(+), 55 deletions(-) diff --git a/client/include/model.hpp b/client/include/model.hpp index 9447463..071d1fe 100644 --- a/client/include/model.hpp +++ b/client/include/model.hpp @@ -39,6 +39,7 @@ class Model { void HandleFile(recFile& newFile); bool IsHandlerRecievingFiles(); bool IsServRequired(); + bool IsLogin(); private: std::shared_ptr> modelImpl; }; diff --git a/client/include/model.tpp b/client/include/model.tpp index 308f753..f478616 100644 --- a/client/include/model.tpp +++ b/client/include/model.tpp @@ -109,6 +109,10 @@ public: bool isServRequired() { return currentHandler->ServRequired(); } + + bool isLogin() { + return currentHandler->IsLogin(); + } private: std::shared_ptr> CreateRequestHandler(std::string& action, Model& model); std::shared_ptr> currentHandler; @@ -136,43 +140,43 @@ std::shared_ptr> ModelImpl::Creat switch (atoi(type.c_str()) ) { case 0: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, false); break; case 1: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, false); break; case 2: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, false); break; case 3: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, false); break; case 4: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, false); break; case 5: - handler = std::make_shared>(true, true); + handler = std::make_shared>(true, true, false); break; case 6: - handler = std::make_shared>(true, true); + handler = std::make_shared>(true, true, false); break; case 7: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, true); break; case 8: - handler = std::make_shared>(false, true); + handler = std::make_shared>(false, true, false); break; case 9: - handler = std::make_shared>(true, true); + handler = std::make_shared>(true, true, false); break; case 10: - handler = std::make_shared>(true, true); + handler = std::make_shared>(true, true, false); break; case 11: - handler = std::make_shared>(true, true); + handler = std::make_shared>(true, true, false); break; case 12: - handler = std::make_shared>(true, true); + handler = std::make_shared>(true, true, false); break; default: break; @@ -306,4 +310,9 @@ bool Model::IsHandlerRecievingFiles() { template bool Model::IsServRequired() { return modelImpl->isServRequired(); +} + +template +bool Model::IsLogin() { + return modelImpl->isLogin(); } \ No newline at end of file diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp index 424c1ca..99603ef 100644 --- a/client/include/presenter.tpp +++ b/client/include/presenter.tpp @@ -4,23 +4,28 @@ #include #include +#include "utils.h" #include "requestHandler.hpp" template -Presenter::Presenter(const std::string& host, const size_t port) : -client(host, port) { /* client.Connect(); */} +Presenter::Presenter(const std::string &host, const size_t port) : client(host, port) +{ /* client.Connect(); */ +} template -Presenter::~Presenter() { +Presenter::~Presenter() +{ } template -void Presenter::connect() { +void Presenter::connect() +{ client.Connect(); } template -void Presenter::disconnect() { +void Presenter::disconnect() +{ client.Close(); } @@ -49,7 +54,7 @@ void Presenter::run() { } else { std::string response = client.readFromServer(&endFlag); - std::cout << std::endl << response << std::endl; + //std::cout << std::endl << response << std::endl; model.HandleResponse(response); } @@ -57,4 +62,64 @@ void Presenter::run() { action = view.GetRequest(); disconnect(); } -} \ No newline at end of file +} + + +/* template +void Presenter::run() +{ + std::string action = view.GetRequest(); + bool loginFlag = false; + std::string request, response; + while (!action.empty()) + { + bool endFlag = false; + if (loginFlag) + { + connect(); + std::string login, token; + fillDataFromJson(request, "login", &login); + fillDataFromJson(response, "uuid", &token); + action = "{\"command\": 10,\"login\": \"" + login + "\",\"token\": \"" + token + "\"}"; + request = model.FormRequest(action); + client.writeToServer(request); + response = client.readFromServer(&endFlag); + model.HandleResponse(response); + loginFlag = false; + disconnect(); + } + if (!loginFlag) { + connect(); + request = model.FormRequest(action); + client.writeToServer(request); + response = client.readFromServer(&endFlag); + model.HandleResponse(response); + + if (model.IsLogin()) + { + loginFlag = true; + } + + while (!endFlag) + { + if (model.IsHandlerRecievingFiles()) + { + recFile newFile; + newFile.name = client.readFromServer(&endFlag); + newFile.body = client.readFileBodyFromServer(&endFlag); + + model.HandleFile(newFile); + } + else + { + std::string response = client.readFromServer(&endFlag); + + model.HandleResponse(response); + } + } + disconnect(); + } + action = view.GetRequest(); + } +} + */ \ No newline at end of file diff --git a/client/include/requestHandler.hpp b/client/include/requestHandler.hpp index 873ad50..b3c18c1 100644 --- a/client/include/requestHandler.hpp +++ b/client/include/requestHandler.hpp @@ -17,25 +17,27 @@ typedef enum ExitStatus { template class RequestHandler { public: - RequestHandler(bool recvFiles, bool servIsReq) : recievingFiles(recvFiles), servIsRequired(servIsReq) {} + RequestHandler(bool recvFiles, bool servIsReq, bool isLogin) : recievingFiles(recvFiles), servIsRequired(servIsReq) , isLogin(isLogin){} virtual ExitStatus FillRequest(std::string action, Model& model) = 0; virtual ExitStatus HandleResponse(std::string& responseBody); virtual ExitStatus DoLogic(Model& app) = 0; virtual ExitStatus RecieveFile(recFile& newFile) { return SUCCESS;} bool RecievingFiles() { return recievingFiles; } bool ServRequired() { return servIsRequired; } + bool IsLogin() { return isLogin; } std::string& GetRequestToSend(); protected: std::string requestToSend; std::shared_ptr parser; bool recievingFiles; bool servIsRequired; + bool isLogin; }; template class AddUsersReqHandler : public RequestHandler { public: - AddUsersReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + AddUsersReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -46,7 +48,7 @@ class AddUsersReqHandler : public RequestHandler { template class RemoveUsersReqHandler : public RequestHandler { public: - RemoveUsersReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + RemoveUsersReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -57,7 +59,7 @@ class RemoveUsersReqHandler : public RequestHandler { template class AddLinkReqHandler : public RequestHandler { public: - AddLinkReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + AddLinkReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -71,7 +73,7 @@ class AddLinkReqHandler : public RequestHandler { template class RemoveLinkReqHandler : public RequestHandler { public: - RemoveLinkReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + RemoveLinkReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -82,7 +84,7 @@ class RemoveLinkReqHandler : public RequestHandler { template class MakeSnapshotReqHandler : public RequestHandler { public: - MakeSnapshotReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + MakeSnapshotReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -95,7 +97,7 @@ class MakeSnapshotReqHandler : public RequestHandler { template class CreateRoomReqHandler : public RequestHandler { public: - CreateRoomReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + CreateRoomReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -109,7 +111,7 @@ class CreateRoomReqHandler : public RequestHandler { template class RemoveRoomReqHandler : public RequestHandler { public: - RemoveRoomReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + RemoveRoomReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus DoLogic(Model& app); private: @@ -120,7 +122,7 @@ class RemoveRoomReqHandler : public RequestHandler { template class LogInReqHandler : public RequestHandler { public: - LogInReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + LogInReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -133,7 +135,7 @@ class LogInReqHandler : public RequestHandler { template class SignUpReqHandler : public RequestHandler { public: - SignUpReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + SignUpReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -145,7 +147,7 @@ class SignUpReqHandler : public RequestHandler { template class DownloadSnapshotReqHandler : public RequestHandler { public: - DownloadSnapshotReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + DownloadSnapshotReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); //ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -159,7 +161,7 @@ class DownloadSnapshotReqHandler : public RequestHandler { template class GetUserRoomReqHandler : public RequestHandler { public: - GetUserRoomReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + GetUserRoomReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -170,7 +172,7 @@ class GetUserRoomReqHandler : public RequestHandler { template class GetUserLinksReqHandler : public RequestHandler { public: - GetUserLinksReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + GetUserLinksReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); @@ -183,7 +185,7 @@ class GetUserLinksReqHandler : public RequestHandler { template class GetLinkSnapshotReqHandler : public RequestHandler { public: - GetLinkSnapshotReqHandler(bool recvFiles, bool servIsReq) : RequestHandler(recvFiles, servIsReq) {}; + GetLinkSnapshotReqHandler(bool recvFiles, bool servIsReq, bool isLogin) : RequestHandler(recvFiles, servIsReq, isLogin) {}; ExitStatus FillRequest(std::string action, Model& model); ExitStatus HandleResponse(std::string& responseBody); ExitStatus DoLogic(Model& app); diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index f24828e..cb37656 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -28,7 +28,7 @@ ExitStatus RequestHandler::HandleResponse(std::string& responseB throw std::runtime_error("Failed to parse JSON!"); } if (error.empty()) { - std::cout << "success!" << std::endl; + //std::cout << "success!" << std::endl; } else { return FAILURE; } @@ -112,7 +112,7 @@ ExitStatus AddLinkReqHandler::HandleResponse(std::string &respon } if (error.empty()) { (RequestHandler::parser->get_value("uuid", uuid)); - std::cout << "success!" << std::endl; + // std::cout << "success!" << std::endl; } else { return FAILURE; } @@ -180,7 +180,7 @@ ExitStatus MakeSnapshotReqHandler::HandleResponse(std::string &r } if (error.empty()) { (RequestHandler::parser->get_value("uuid", uuid)); - std::cout << "success!" << std::endl; + //!" << std::endl; } else { return FAILURE; } @@ -228,7 +228,7 @@ ExitStatus CreateRoomReqHandler::HandleResponse(std::string &res } else { return FAILURE; } - std::cout << "success!" << std::endl; + // std::cout << "success!" << std::endl; return SUCCESS; } @@ -287,7 +287,7 @@ ExitStatus LogInReqHandler::HandleResponse(std::string& response } else { return FAILURE; } - std::cout << "success!" << std::endl; + //std::cout << "success!" << std::endl; return SUCCESS; } @@ -352,8 +352,6 @@ ExitStatus DownloadSnapshotReqHandler::RecieveFile(recFile& newF pathToNewFile += "/static" + newFile.name; } - std::cout << "PATH TO NEW FILE: " << pathToNewFile.substr(0, 80) << std::endl; - std::ofstream file(pathToNewFile, std::ios::binary); for (auto i : newFile.body) { file.write(&i, 1); @@ -388,7 +386,7 @@ ExitStatus GetUserRoomReqHandler::HandleResponse(std::string &re } else { return FAILURE; } - std::cout << "success!" << std::endl; + //std::cout << "success!" << std::endl; return SUCCESS; } @@ -432,7 +430,7 @@ ExitStatus GetUserLinksReqHandler::HandleResponse(std::string &r } else { return FAILURE; } - std::cout << "success!" << std::endl; + //std::cout << "success!" << std::endl; return SUCCESS; } diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index 43c284f..bf1f81e 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -16,7 +16,7 @@ void fillObject(std::vector* vec) { } -std::string createRoomInput() { +/* std::string createRoomInput() { std::string name; std::string isPrivate; @@ -24,9 +24,9 @@ std::string createRoomInput() { std::string ret = packToJsonString("command", "0", "name", name, "private", isPrivate); return ret; -} +} */ -std::string deleteRoomInput() { +/* std::string deleteRoomInput() { std::string name; std::string host; @@ -34,23 +34,23 @@ std::string deleteRoomInput() { std::string ret = packToJsonString("command","1", "name", name, "host", host); return ret; -} +} */ -std::string addUsersInput() { +/* std::string addUsersInput() { std::vector vec; writeData(&vec); std::string ret = packToJsonString("command","2", "users", vec); return ret; -} +} */ -std::string deleteUsersInput() { +/* std::string deleteUsersInput() { std::vector vec; writeData(&vec); std::string ret = packToJsonString("command","3", "users", vec); return ret; -} +} */ std::string addLinkInput() { std::string name; std::string url; diff --git a/client/src/view.cpp b/client/src/view.cpp index 983b889..760017a 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -6,7 +6,7 @@ IView::~IView() {} -enum RequestCommand { +/* enum RequestCommand { CREATE_ROOM, DELETE_ROOM, ADD_USERS, @@ -20,6 +20,18 @@ enum RequestCommand { GET_USER_ROOM, GET_USER_LINKS, GET_LINK_SNAPSHOTS +}; */ + +enum RequestCommand { + ADD_LINK, + DELETE_LINK, + MAKE_SNAPSHOT, + LOG_IN_USER, + SIGN_UP_USER, + DOWNLOAD_SNAPSHOT, + GET_USER_ROOM, + GET_USER_LINKS, + GET_LINK_SNAPSHOTS }; @@ -30,7 +42,7 @@ std::string ConsoleView::GetRequest() { int key = 0; std::cin >> key; switch (key) { - case CREATE_ROOM: { + /* case CREATE_ROOM: { std::cout << "Write name of room and is it private(true/false)" << std::endl; inputStr = createRoomInput(); } @@ -49,7 +61,7 @@ std::string ConsoleView::GetRequest() { std::cout << "Write amount of users and list of users" << std::endl; inputStr = deleteUsersInput(); } - break; + break; */ case ADD_LINK: { std::cout << "Write name, url and description of link" << std::endl; inputStr = addLinkInput(); @@ -103,7 +115,7 @@ std::string ConsoleView::GetRequest() { return inputStr; } -void ConsoleView::PrintCommands() { +/* void ConsoleView::PrintCommands() { std::cout << std::endl; std::cout << "Please choose command type:" << std::endl; std::cout << "- 0.Create Room" << std::endl @@ -120,4 +132,20 @@ void ConsoleView::PrintCommands() { << "- 11.Get user links" << std::endl << "- 12.Get link snapshots" << std::endl << "- (-1).Exit" << std::endl; +} */ + + +void ConsoleView::PrintCommands() { + std::cout << std::endl; + std::cout << "Please choose command type:" << std::endl; + std::cout << "- 0.Add link" << std::endl + << "- 1.Remove link" << std::endl + << "- 2.Make snapshot" << std::endl + << "- 3.Log in" << std::endl + << "- 4.Sign up" << std::endl + << "- 5.Download snapshot" << std::endl + << "- 6.Create link storage" << std::endl + << "- 7.Get user links" << std::endl + << "- 8.Get link snapshots" << std::endl + << "- (-1).Exit" << std::endl; } \ No newline at end of file From f80127faaf692a7ca8c06ee75fa8f8b9fbf5841d Mon Sep 17 00:00:00 2001 From: Verhushker Date: Mon, 28 Dec 2020 13:11:07 +0300 Subject: [PATCH 35/36] new --- client/include/presenter.tpp | 4 +- client/src/inputUtils.cpp | 98 +++++++++++++++++++++++------------- client/src/view.cpp | 3 ++ 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/client/include/presenter.tpp b/client/include/presenter.tpp index 99603ef..4c4feca 100644 --- a/client/include/presenter.tpp +++ b/client/include/presenter.tpp @@ -40,14 +40,14 @@ void Presenter::run() { bool endFlag = false; std::string response = client.readFromServer(&endFlag); - std::cout << std::endl << response << std::endl; + //std::cout << std::endl << response << std::endl; model.HandleResponse(response); while(!endFlag) { if (model.IsHandlerRecievingFiles()) { recFile newFile; newFile.name = client.readFromServer(&endFlag); - std::cout << "FILENAME: " << newFile.name.substr(0, 80) << std::endl << std::endl << std::endl; + //std::cout << "FILENAME: " << newFile.name.substr(0, 80) << std::endl << std::endl << std::endl; newFile.body = client.readFileBodyFromServer(&endFlag); model.HandleFile(newFile); diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index bf1f81e..3adea8c 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -1,21 +1,42 @@ #include "inputUtils.hpp" #include "utils.h" - -void fillObject(std::string* str) { - std::cin >> *str; +#include + +void fillObject(std::string *str) +{ + //std::cin >> *str; + char c = 'a'; + while ((c = getchar()) != '\n') + { + //c = getchar(); + //std::cout << "Char = " << c << '\n'; + (*str).push_back(c); + } + //std::cout << "String = " << (*str) << '\n'; + } -void fillObject(std::vector* vec) { +void fillObject(std::vector *vec) +{ size_t amount = 0; std::string newObj; std::cin >> amount; - while(amount > 0) { - std::cin >> newObj; - vec->push_back(newObj); - --amount; + while (amount > 0) + { + //std::getline(std::cin, newObj/* , '\n' */); + //std::cout << newObj << std::endl; + char c = 'a'; + while ((c = getchar()) != '\n') + { + // = getchar(); + newObj.push_back(c); } + //std::cin.getline(*str); + //std::cin >> newObj; + vec->push_back(newObj); + --amount; + } } - /* std::string createRoomInput() { std::string name; std::string isPrivate; @@ -51,76 +72,83 @@ void fillObject(std::vector* vec) { std::string ret = packToJsonString("command","3", "users", vec); return ret; } */ -std::string addLinkInput() { +std::string addLinkInput() +{ std::string name; std::string url; std::string description; writeData(&name, &url, &description); - - std::string ret = packToJsonString("command","4", "name", name, "url", url, "description", description); + + std::string ret = packToJsonString("command", "4", "name", name, "url", url, "description", description); return ret; } -std::string deleteLinkInput() { +std::string deleteLinkInput() +{ std::string name; writeData(&name); - - std::string ret = packToJsonString("command","5", "name", name); + + std::string ret = packToJsonString("command", "5", "name", name); return ret; } -std::string makeSnapshotInput() { +std::string makeSnapshotInput() +{ std::string name; writeData(&name); - - std::string ret = packToJsonString("command","6", "name", name); + + std::string ret = packToJsonString("command", "6", "name", name); return ret; } -std::string logInInput() { +std::string logInInput() +{ std::string login; std::string password; writeData(&login, &password); - - std::string ret = packToJsonString("command","7", "login", login, "password", password); + + std::string ret = packToJsonString("command", "7", "login", login, "password", password); return ret; } -std::string signUpInput() { +std::string signUpInput() +{ std::string login; std::string password; writeData(&login, &password); - - std::string ret = packToJsonString("command","8", "login", login, "password", password); + + std::string ret = packToJsonString("command", "8", "login", login, "password", password); return ret; } - -std::string downloadSnapshotInput() { +std::string downloadSnapshotInput() +{ std::string name, filesdir; writeData(&name, &filesdir); - - std::string ret = packToJsonString("command","9", "name", name, "filesdir", filesdir); + + std::string ret = packToJsonString("command", "9", "name", name, "filesdir", filesdir); return ret; } - -std::string getUserRoomInput() { - std::string ret = packToJsonString("command","10"); +std::string getUserRoomInput() +{ + std::string ret = packToJsonString("command", "10"); return ret; } -std::string getUserLinksInput() { - std::string ret = packToJsonString("command","11"); +std::string getUserLinksInput() +{ + std::string ret = packToJsonString("command", "11"); return ret; } -std::string getLinkSnapshotsInput() { +std::string getLinkSnapshotsInput() +{ std::string linkName; writeData(&linkName); - std::string ret = packToJsonString("command","12", "linkname", linkName); + std::string ret = packToJsonString("command", "12", "linkname", linkName); return ret; } diff --git a/client/src/view.cpp b/client/src/view.cpp index 760017a..38a1a51 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -41,6 +41,9 @@ std::string ConsoleView::GetRequest() { std::string inputStr(""), appendStr(" "); int key = 0; std::cin >> key; + getchar(); + /* int a = 0; + std::cin >> a; */ switch (key) { /* case CREATE_ROOM: { std::cout << "Write name of room and is it private(true/false)" << std::endl; From ea87c4522d966c3c946d2107eaaefae11a8c888a Mon Sep 17 00:00:00 2001 From: Verhushker Date: Mon, 28 Dec 2020 13:48:25 +0300 Subject: [PATCH 36/36] new2 --- client/include/requestHandler.tpp | 33 +++++++++++++++++++------------ client/src/inputUtils.cpp | 12 +++++------ client/src/view.cpp | 2 +- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/client/include/requestHandler.tpp b/client/include/requestHandler.tpp index cb37656..308ed12 100644 --- a/client/include/requestHandler.tpp +++ b/client/include/requestHandler.tpp @@ -28,7 +28,7 @@ ExitStatus RequestHandler::HandleResponse(std::string& responseB throw std::runtime_error("Failed to parse JSON!"); } if (error.empty()) { - //std::cout << "success!" << std::endl; + std::cout << "success!" << std::endl; } else { return FAILURE; } @@ -112,7 +112,7 @@ ExitStatus AddLinkReqHandler::HandleResponse(std::string &respon } if (error.empty()) { (RequestHandler::parser->get_value("uuid", uuid)); - // std::cout << "success!" << std::endl; + std::cout << "success!" << std::endl; } else { return FAILURE; } @@ -228,7 +228,7 @@ ExitStatus CreateRoomReqHandler::HandleResponse(std::string &res } else { return FAILURE; } - // std::cout << "success!" << std::endl; + std::cout << "success!" << std::endl; return SUCCESS; } @@ -287,7 +287,7 @@ ExitStatus LogInReqHandler::HandleResponse(std::string& response } else { return FAILURE; } - //std::cout << "success!" << std::endl; + std::cout << "success!" << std::endl; return SUCCESS; } @@ -317,14 +317,12 @@ ExitStatus SignUpReqHandler::DoLogic(Model &mode template ExitStatus DownloadSnapshotReqHandler::FillRequest(std::string action, Model& model) { - fillDataFromJson(action, "name", &linkName, "filesdir", &filesdir); + fillDataFromJson(action, "uuid", &uuid, "filesdir", &filesdir); std::string info = model.GetUserInfoStr(); std::string login, token; fillDataFromJson(info, "name", &login, "uuid", &token); - uuid = model.GetLinkSnapshotInfoStr(linkName); - RequestHandler::requestToSend = packToJsonString("command", 9, "login", login, "token", token, "uuid", uuid); return SUCCESS; @@ -386,7 +384,7 @@ ExitStatus GetUserRoomReqHandler::HandleResponse(std::string &re } else { return FAILURE; } - //std::cout << "success!" << std::endl; + std::cout << "success!" << std::endl; return SUCCESS; } @@ -426,11 +424,14 @@ ExitStatus GetUserLinksReqHandler::HandleResponse(std::string &r throw std::runtime_error("Failed to parse JSON!"); } if (error.empty()) { - (RequestHandler::parser->get_value("objects", map)); + if (!(RequestHandler::parser->get_value("objects", map))) { + std::cout << "You don't have links" << std::endl; + return FAILURE; + } } else { return FAILURE; } - //std::cout << "success!" << std::endl; + std::cout << "success!" << std::endl; return SUCCESS; } @@ -442,6 +443,8 @@ ExitStatus GetUserLinksReqHandler::DoLogic(Model std::string linkname = i.find("name")->second; std::string url = i.find("url")->second; + std::cout << "Name: " << linkname << std::endl; + std::shared_ptr newLink = std::make_shared(linkname, url, uuid, description); model.AddLink(newLink); } @@ -455,7 +458,7 @@ ExitStatus GetLinkSnapshotReqHandler::FillRequest(std::string ac std::string login, token; fillDataFromJson(info, "name", &login, "uuid", &token); - fillDataFromJson(action, "linkname", &linkName); + fillDataFromJson(action, "name", &linkName); std::string linkInfo = model.GetLinkInfoStr(linkName); fillDataFromJson(linkInfo, "uuid", &uuid); @@ -477,7 +480,10 @@ ExitStatus GetLinkSnapshotReqHandler::HandleResponse(std::string throw std::runtime_error("Failed to parse JSON!"); } if (error.empty()) { - (RequestHandler::parser->get_value("objects", map)); + if(!RequestHandler::parser->get_value("objects", map)) { + std::cout << "You don't have snapshots" < ExitStatus GetLinkSnapshotReqHandler::DoLogic(Model &model) { for (auto i : map) { std::string snapuuid = i.find("snapshot_uuid")->second; - + std::string snapdate = i.find("snapshot_date")->second; + std::cout << "Snap: "<< snapuuid << " Date: " << snapdate << std::endl; model.AddSnapshotUuid(linkName, snapuuid); } return SUCCESS; diff --git a/client/src/inputUtils.cpp b/client/src/inputUtils.cpp index 3adea8c..f39b7c5 100644 --- a/client/src/inputUtils.cpp +++ b/client/src/inputUtils.cpp @@ -124,11 +124,11 @@ std::string signUpInput() std::string downloadSnapshotInput() { - std::string name, filesdir; + std::string uuid, filesdir; - writeData(&name, &filesdir); + writeData(&uuid, &filesdir); - std::string ret = packToJsonString("command", "9", "name", name, "filesdir", filesdir); + std::string ret = packToJsonString("command", "9", "uuid", uuid, "filesdir", filesdir); return ret; } @@ -146,9 +146,9 @@ std::string getUserLinksInput() std::string getLinkSnapshotsInput() { - std::string linkName; + std::string name; - writeData(&linkName); - std::string ret = packToJsonString("command", "12", "linkname", linkName); + writeData(&name); + std::string ret = packToJsonString("command", "12", "name", name); return ret; } diff --git a/client/src/view.cpp b/client/src/view.cpp index 38a1a51..5656c9c 100644 --- a/client/src/view.cpp +++ b/client/src/view.cpp @@ -91,7 +91,7 @@ std::string ConsoleView::GetRequest() { break; } case DOWNLOAD_SNAPSHOT: { - std::cout << "Write name of link and directory " << std::endl; + std::cout << "Write uuid of snapshot and directory " << std::endl; inputStr = downloadSnapshotInput(); break; }