From a5d98b9dccc149e75161b33ea3d1c4121029747f Mon Sep 17 00:00:00 2001 From: Newe Date: Mon, 24 May 2021 17:46:50 +0200 Subject: [edit] Deprecate gRPC in favor of MongoDB --- CMakeLists.txt | 28 ++++++---------------------- README.md | 2 +- src/main.cpp | 15 ++++++++++++--- src/mongoStub.cpp | 26 ++++++++++++++++++++++++++ src/mongoStub.hpp | 29 +++++++++++++++++++++++++++++ src/rpcStub.cpp | 32 -------------------------------- src/rpcStub.hpp | 15 --------------- 7 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 src/mongoStub.cpp create mode 100644 src/mongoStub.hpp delete mode 100644 src/rpcStub.cpp delete mode 100644 src/rpcStub.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cebd3adf..2cf5c0a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,32 +1,16 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.2) project(fosscord-media) set(CMAKE_CXX_STANDARD 17) find_package(Threads REQUIRED) -find_package(Protobuf REQUIRED) -find_package(gRPC CONFIG REQUIRED) -find_package(absl REQUIRED) -find_package(nlohmann_json REQUIRED) -file(GLOB SourceFiles ${PROJECT_SOURCE_DIR}/src/*.cpp) - -file(GLOB ProtoFiles ${PROJECT_SOURCE_DIR}/src/protodefs/*.proto) -set(PROTOBUF_INPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src/protodefs) -set(PROTOBUF_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src/protodefs/include) - -foreach(file ${ProtoFiles}) - execute_process(COMMAND "LD_LIBRARY_PATH=/usr/local/lib protoc --proto_path=\"${PROTOBUF_INPUT_DIRECTORY}\" - --cpp_out=\"${PROJECT_SOURCE_DIR}/src/protodefs/include\" --grpc_out=\"${PROJECT_SOURCE_DIR}/src/protodefs/include\" - --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin protos.proto" - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) -endforeach() +find_package(mongocxx REQUIRED) +find_package(Boost REQUIRED) -include_directories(${Protobuf_INCLUDE_DIRS}) - -#protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ProtoFiles) - +file(GLOB SourceFiles ${PROJECT_SOURCE_DIR}/src/*.cpp) +#include_directories("bsoncxx/v_noabi/bsoncxx/") add_executable(${CMAKE_PROJECT_NAME} ${SourceFiles}) -target_link_libraries(${CMAKE_PROJECT_NAME} datachannel gRPC::grpc++ absl::base absl::synchronization absl::strings ${Protobuf_LIBRARIES} nlohmann_json::nlohmann_json) \ No newline at end of file +target_link_libraries(${CMAKE_PROJECT_NAME} datachannel mongo::mongocxx_shared Boost::boost) \ No newline at end of file diff --git a/README.md b/README.md index 4ddd4708..a1ff3c08 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A Fosscord media (voice and video) server ## Installation ### Prerequisites - Install the [libdatachannel](https://github.com/paullouisageneau/libdatachannel) library -- Install the [gRPC](https://github.com/grpc/grpc) library +- Install the [limbongocxx]() driver ### Building diff --git a/src/main.cpp b/src/main.cpp index 2fdeceee..bd4ebbec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,15 +22,24 @@ // #include "rtcPeerHandler.hpp" //Handle peer connection requests -#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements +#include "mongoStub.hpp" //Handle communication with the MongoDB server int main(int argc, char **argv){ auto commsHandler = std::make_shared(); - auto rpcHandler = std::unique_ptr(new rpcStub(commsHandler, 8057)); + auto mongoHandler = std::make_unique(); + mongocxx::options::change_stream options; + mongocxx::change_stream colCs = mongoHandler->getCol().watch(options); + + //Check for new messages in the collection + for (;;){ + std::vector t = mongoHandler->getNewMessages(&colCs); + for(auto &i : t){ + std::cout << i << std::endl; + } + } std::cout << "Server created" << std::endl; - //rpcHandler->server->Wait(); //blocking, this will need to be threaded return 0; } \ No newline at end of file diff --git a/src/mongoStub.cpp b/src/mongoStub.cpp new file mode 100644 index 00000000..50312fc6 --- /dev/null +++ b/src/mongoStub.cpp @@ -0,0 +1,26 @@ +#include "mongoStub.hpp" + +mongoStub::mongoStub() { + if (this->client) { + this->db = client["fosscord"]; + + if (this->db) { + this->col = db["events"]; + + } else { + std::cout << "db not found"; + exit(-1); + } + } else { + std::cout << "Client couldn't be initialized"; + exit(-1); + } +} + +std::vectormongoStub::getNewMessages(mongocxx::change_stream* colCs) { + std::vector retVec; + for (const auto& event : *colCs) { + retVec.push_back(bsoncxx::to_json(event)); + } + return retVec; +} diff --git a/src/mongoStub.hpp b/src/mongoStub.hpp new file mode 100644 index 00000000..3cee472c --- /dev/null +++ b/src/mongoStub.hpp @@ -0,0 +1,29 @@ +#ifndef MONGOSTUB_HPP +#define MONGOSTUB_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + + +class mongoStub : boost::noncopyable { + public: + mongoStub(); + std::vector getNewMessages(mongocxx::change_stream* colCs); + + mongocxx::collection getCol() const { return col; } + + private: + mongocxx::instance instance; + mongocxx::client client{mongocxx::uri{}}; + mongocxx::database db; + mongocxx::collection col; + mongocxx::change_stream* colCs = nullptr; +}; + +#endif diff --git a/src/rpcStub.cpp b/src/rpcStub.cpp deleted file mode 100644 index 1633aab8..00000000 --- a/src/rpcStub.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "rpcStub.hpp" - -class fossCordInternalsImpl final : public fosscordMedia::fosscordInternals::Service { - std::shared_ptr ph; - fossCordInternalsImpl(std::shared_ptr handler){ - this->ph= handler; - } - grpc::Status vRequest(grpc::ServerContext* ctx, - const fosscordMedia::voiceRequest* req, - fosscordMedia::voiceAnswer* resp) override { - - this->ph->initiateConnection(req->ip(), req->port()); - return grpc::Status::OK; - } -}; - -rpcStub::rpcStub(std::shared_ptr handler, int port) { - if (not port) { - port = 8057; - } - this->ph = handler; - - fossCordInternalsImpl* service; - grpc::ServerBuilder builder; - builder.AddListeningPort("0.0.0.0:" + std::to_string(port), - grpc::InsecureServerCredentials()); - builder.RegisterService(service); - - this->server = builder.BuildAndStart(); - - std::cout << "RPC stub listening on port " << port << std::endl; -} \ No newline at end of file diff --git a/src/rpcStub.hpp b/src/rpcStub.hpp deleted file mode 100644 index d183cf3c..00000000 --- a/src/rpcStub.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "protodefs/include/protos.grpc.pb.h" -#include "rtcPeerHandler.hpp" - -#ifndef RPCSTUB -#define RPCSTUB -class rpcStub{ - public: - rpcStub(std::shared_ptr peerHandler, int port); - std::unique_ptr server; - - private: - std::shared_ptr ph; -}; -#endif \ No newline at end of file -- cgit 1.4.1