From 7d40250c11077d6717388412479a65bb8826960b Mon Sep 17 00:00:00 2001 From: Newe Date: Fri, 21 May 2021 11:13:17 +0200 Subject: [add] Basic project structure --- src/main.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main.cpp (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 00000000..3f131512 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,36 @@ +// $$$$$$\ $$\ +// $$ __$$\ $$ | +// $$ / \__|$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | +// $$$$\ $$ __$$\ $$ _____|$$ _____|$$ _____|$$ __$$\ $$ __$$\ $$ __$$ | +// $$ _| $$ / $$ |\$$$$$$\ \$$$$$$\ $$ / $$ / $$ |$$ | \__|$$ / $$ | +// $$ | $$ | $$ | \____$$\ \____$$\ $$ | $$ | $$ |$$ | $$ | $$ | +// $$ | \$$$$$$ |$$$$$$$ |$$$$$$$ |\$$$$$$$\ \$$$$$$ |$$ | \$$$$$$$ | +// \__| \______/ \_______/ \_______/ \_______| \______/ \__| \_______| +// +// +// +// $$\ $$$$$$\ +// \__| $$ __$$\ +// $$\ $$\ $$$$$$\ $$\ $$$$$$$\ $$$$$$\ $$ / \__| $$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$\ +// \$$\ $$ |$$ __$$\ $$ |$$ _____|$$ __$$\ \$$$$$$\ $$ __$$\ $$ __$$\\$$\ $$ |$$ __$$\ $$ __$$\ +// \$$\$$ / $$ / $$ |$$ |$$ / $$$$$$$$ | \____$$\ $$$$$$$$ |$$ | \__|\$$\$$ / $$$$$$$$ |$$ | \__| +// \$$$ / $$ | $$ |$$ |$$ | $$ ____|$$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | +// \$ / \$$$$$$ |$$ |\$$$$$$$\ \$$$$$$$\ \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | +// \_/ \______/ \__| \_______| \_______| \______/ \_______|\__| \_/ \_______|\__| +// +// +// + + +#include "rtcPeerHandler.hpp" //HAndle peer connection requests +#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements + +int main (int argc, char** argv){ + + auto handler = std::make_shared(); + auto rpcHandler = std::unique_ptr(); + + std::cout << "Server created" < Date: Fri, 21 May 2021 11:19:56 +0200 Subject: [edit] Set rpcStub.server to public --- src/main.cpp | 3 ++- src/rpcStub.hpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 3f131512..88adc97c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,10 +27,11 @@ int main (int argc, char** argv){ - auto handler = std::make_shared(); + auto commsHandler = std::make_shared(); auto rpcHandler = std::unique_ptr(); std::cout << "Server created" <server->Wait(); //blocking, this will need to be threaded return 0; } \ No newline at end of file diff --git a/src/rpcStub.hpp b/src/rpcStub.hpp index 9da80cda..99d6d582 100644 --- a/src/rpcStub.hpp +++ b/src/rpcStub.hpp @@ -4,7 +4,8 @@ class rpcStub{ public: rpcStub(int port); - private: std::unique_ptr server; + private: + }; \ No newline at end of file -- cgit 1.5.1 From f8eceb3124ac4149f180b496082d9965f6b8c1b6 Mon Sep 17 00:00:00 2001 From: Newe Date: Fri, 21 May 2021 15:48:56 +0200 Subject: [edit] Hypothetical gRPC handshake --- .gitignore | 4 ++ src/main.cpp | 45 +++++++++++----------- src/protodefs/protos.proto | 4 +- src/rpcStub.cpp | 38 +++++++++++-------- src/rpcStub.hpp | 7 ++-- src/rtcPeerHandler.cpp | 94 +++++++++++++++++++++++++++++----------------- src/rtcPeerHandler.hpp | 5 ++- 7 files changed, 119 insertions(+), 78 deletions(-) (limited to 'src/main.cpp') diff --git a/.gitignore b/.gitignore index 788c5adb..539bb001 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,10 @@ # Executables *.o +# Protobuffer builds +*.pb.cc +*.pb.h + # Directories build/ .vscode/ diff --git a/src/main.cpp b/src/main.cpp index 88adc97c..2fdeceee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,37 +1,36 @@ // $$$$$$\ $$\ -// $$ __$$\ $$ | -// $$ / \__|$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | -// $$$$\ $$ __$$\ $$ _____|$$ _____|$$ _____|$$ __$$\ $$ __$$\ $$ __$$ | -// $$ _| $$ / $$ |\$$$$$$\ \$$$$$$\ $$ / $$ / $$ |$$ | \__|$$ / $$ | -// $$ | $$ | $$ | \____$$\ \____$$\ $$ | $$ | $$ |$$ | $$ | $$ | -// $$ | \$$$$$$ |$$$$$$$ |$$$$$$$ |\$$$$$$$\ \$$$$$$ |$$ | \$$$$$$$ | -// \__| \______/ \_______/ \_______/ \_______| \______/ \__| \_______| -// -// -// +// $$ __$$\ $$ | +// $$ / \__|$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | +// $$$$\ $$ __$$\ $$ _____|$$ _____|$$ _____|$$ __$$\ $$ __$$\ $$ __$$ | +// $$ _| $$ / $$ |\$$$$$$\ \$$$$$$\ $$ / $$ / $$ |$$ | \__|$$ / $$ | +// $$ | $$ | $$ | \____$$\ \____$$\ $$ | $$ | $$ |$$ | $$ | $$ | +// $$ | \$$$$$$ |$$$$$$$ |$$$$$$$ |\$$$$$$$\ \$$$$$$ |$$ | \$$$$$$$ | +// \__| \______/ \_______/ \_______/ \_______| \______/ \__| \_______| +// +// +// // $$\ $$$$$$\ // \__| $$ __$$\ // $$\ $$\ $$$$$$\ $$\ $$$$$$$\ $$$$$$\ $$ / \__| $$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$\ // \$$\ $$ |$$ __$$\ $$ |$$ _____|$$ __$$\ \$$$$$$\ $$ __$$\ $$ __$$\\$$\ $$ |$$ __$$\ $$ __$$\ // \$$\$$ / $$ / $$ |$$ |$$ / $$$$$$$$ | \____$$\ $$$$$$$$ |$$ | \__|\$$\$$ / $$$$$$$$ |$$ | \__| -// \$$$ / $$ | $$ |$$ |$$ | $$ ____|$$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | -// \$ / \$$$$$$ |$$ |\$$$$$$$\ \$$$$$$$\ \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | -// \_/ \______/ \__| \_______| \_______| \______/ \_______|\__| \_/ \_______|\__| -// -// -// +// \$$$ / $$ | $$ |$$ |$$ | $$ ____|$$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | +// \$ / \$$$$$$ |$$ |\$$$$$$$\ \$$$$$$$\ \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | +// \_/ \______/ \__| \_______| \_______| \______/ \_______|\__| \_/ \_______|\__| +// +// +// +#include "rtcPeerHandler.hpp" //Handle peer connection requests +#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements -#include "rtcPeerHandler.hpp" //HAndle peer connection requests -#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements - -int main (int argc, char** argv){ +int main(int argc, char **argv){ auto commsHandler = std::make_shared(); - auto rpcHandler = std::unique_ptr(); + auto rpcHandler = std::unique_ptr(new rpcStub(commsHandler, 8057)); - std::cout << "Server created" <server->Wait(); //blocking, this will need to be threaded - return 0; + return 0; } \ No newline at end of file diff --git a/src/protodefs/protos.proto b/src/protodefs/protos.proto index a8c9f845..11face5f 100644 --- a/src/protodefs/protos.proto +++ b/src/protodefs/protos.proto @@ -3,13 +3,13 @@ syntax = "proto3"; package fosscordMedia; service fosscordInternals{ - rpc requestProtocol(voiceRequest) returns (voiceAnswer) {} + rpc vRequest(voiceRequest) returns (voiceAnswer) {} } message voiceRequest{ //OP1 from gw uint64 userid = 1; uint64 guildid = 2; - string IP=3; + string ip=3; uint32 port=4; string protocol=5; string rtcConnectionId=6; diff --git a/src/rpcStub.cpp b/src/rpcStub.cpp index 3d848924..1633aab8 100644 --- a/src/rpcStub.cpp +++ b/src/rpcStub.cpp @@ -1,24 +1,32 @@ #include "rpcStub.hpp" -class fossCordInternalsImpl final : public fosscordMedia::fosscordInternals::Service{ - grpc::Status sendRequest( - grpc::ServerContext* ctx, - const fosscordMedia::rpcRequest* req, - fosscordMedia::rpcResponse* resp - ) override{ - resp->set_b(333); - return grpc::Status::OK; +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(int port){ - grpc::ServerBuilder builder; +rpcStub::rpcStub(std::shared_ptr handler, int port) { + if (not port) { + port = 8057; + } + this->ph = handler; - fossCordInternalsImpl* service; - builder.AddListeningPort("0.0.0.0:8057", grpc::InsecureServerCredentials() ); - builder.RegisterService(service); + fossCordInternalsImpl* service; + grpc::ServerBuilder builder; + builder.AddListeningPort("0.0.0.0:" + std::to_string(port), + grpc::InsecureServerCredentials()); + builder.RegisterService(service); - std::unique_ptr server(builder.BuildAndStart()); - std::cout << "Server listening on port 8057 " << std::endl; + 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 index 99d6d582..f567cd97 100644 --- a/src/rpcStub.hpp +++ b/src/rpcStub.hpp @@ -1,11 +1,12 @@ #include #include "protodefs/include/protos.grpc.pb.h" +#include "rtcPeerHandler.hpp" class rpcStub{ public: - rpcStub(int port); + rpcStub(std::shared_ptr peerHandler, int port); std::unique_ptr server; - private: - + private: + std::shared_ptr ph; }; \ No newline at end of file diff --git a/src/rtcPeerHandler.cpp b/src/rtcPeerHandler.cpp index 4696c4c7..9bfc6466 100644 --- a/src/rtcPeerHandler.cpp +++ b/src/rtcPeerHandler.cpp @@ -1,45 +1,44 @@ #include "rtcPeerHandler.hpp" -rtcPeerHandler::rtcPeerHandler() -{ - rtc::InitLogger(rtc::LogLevel::Verbose, NULL); -} -void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) -{ - //Socket connection between client and server - SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - sockaddr_in addr; - addr.sin_addr.s_addr = inet_addr(peerIP.c_str()); - addr.sin_port = htons(peerPort); - addr.sin_family = AF_INET; +rtcPeerHandler::rtcPeerHandler() { + rtc::InitLogger(rtc::LogLevel::Verbose, NULL); +} - rtc::Configuration conf; - conf.enableIceTcp = false; - conf.disableAutoNegotiation = false; +void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) { + // Socket connection between client and server + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + sockaddr_in addr; + addr.sin_addr.s_addr = inet_addr(peerIP.c_str()); + addr.sin_port = htons(peerPort); + addr.sin_family = AF_INET; - auto pc = std::make_shared(conf); + rtc::Configuration conf; + conf.enableIceTcp = false; + conf.disableAutoNegotiation = false; - rtc::Description::Audio media("audio", rtc::Description::Direction::SendRecv); - media.addOpusCodec(96); - media.setBitrate(64); + auto pc = std::make_shared(conf); + rtc::Description::Audio media("audio", + rtc::Description::Direction::SendRecv); + media.addOpusCodec(96); + media.setBitrate(64); - auto track = pc->addTrack(media); + auto track = pc->addTrack(media); - //auto session = std::make_shared(); + // auto session = std::make_shared(); - //track->setMediaHandler(session); + // track->setMediaHandler(session); - rtc::Reliability rtcRel; - rtcRel.unordered = true; - rtcRel.type = rtc::Reliability::Type::Timed; - rtcRel.rexmit = 500; + rtc::Reliability rtcRel; + rtcRel.unordered = true; + rtcRel.type = rtc::Reliability::Type::Timed; + rtcRel.rexmit = 500; - rtc::DataChannelInit rtcConf; - rtcConf.reliability = rtcRel; - rtcConf.negotiated = false; + rtc::DataChannelInit rtcConf; + rtcConf.reliability = rtcRel; + rtcConf.negotiated = false; - pc->onStateChange([](rtc::PeerConnection::State state) { + pc->onStateChange([](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; if (state == rtc::PeerConnection::State::Disconnected || state == rtc::PeerConnection::State::Failed || @@ -48,10 +47,37 @@ void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) } }); - pc->onGatheringStateChange( - [](rtc::PeerConnection::GatheringState state) { std::cout << "Gathering State: " << state << std::endl; }); + pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) { + std::cout << "Gathering State: " << state << std::endl; + }); + + /*std::tuple addAudio( + + const std::shared_ptr pc, + const uint8_t payloadType, const uint32_t ssrc, const std::string cname, + const std::string msid, const std::function onOpen) { + auto audio = Description::Audio(cname); + audio.addOpusCodec(payloadType); + audio.addSSRC(ssrc, cname, msid, cname); + auto track = pc->addTrack(audio); + // create RTP configuration + auto rtpConfig = make_shared( + ssrc, cname, payloadType, OpusRtpPacketizer::defaultClockRate); + // create packetizer + auto packetizer = make_shared(rtpConfig); + // create opus handler + auto opusHandler = make_shared(packetizer); + + // add RTCP SR handler + auto srReporter = make_shared(rtpConfig); + opusHandler->addToChain(srReporter); + // set handler + track->setMediaHandler(opusHandler); + track->onOpen(onOpen); + auto trackData = make_shared(track, srReporter); + return trackData; + }*/ - pc->createDataChannel("Fosscord voice connection", rtcConf); + pc->createDataChannel("Fosscord voice connection", rtcConf); } - \ No newline at end of file diff --git a/src/rtcPeerHandler.hpp b/src/rtcPeerHandler.hpp index 030f41fa..3ba32a83 100644 --- a/src/rtcPeerHandler.hpp +++ b/src/rtcPeerHandler.hpp @@ -13,6 +13,8 @@ typedef int SOCKET; using json = nlohmann::json; +#ifndef RTCPEERHANDLER +#define RTCPEERHANDLER class rtcPeerHandler{ public: rtcPeerHandler(); @@ -26,4 +28,5 @@ public: private: std::map clients; -}; \ No newline at end of file +}; +#endif \ No newline at end of file -- cgit 1.5.1 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 (limited to 'src/main.cpp') 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.5.1 From 63407d96e37df8457e8350e9184b9e3de376fd1f Mon Sep 17 00:00:00 2001 From: Newe Date: Tue, 25 May 2021 12:41:28 +0200 Subject: [edit] Retrieve the event name from mongoDB event payload --- src/main.cpp | 8 +++++--- src/mongoStub.cpp | 26 ++++++++++++++++++++++---- src/mongoStub.hpp | 17 +++++++++++++---- 3 files changed, 40 insertions(+), 11 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index bd4ebbec..372eaa00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,16 +30,18 @@ int main(int argc, char **argv){ auto mongoHandler = std::make_unique(); mongocxx::options::change_stream options; + //voiceEvents collection watcher mongocxx::change_stream colCs = mongoHandler->getCol().watch(options); + std::cout << "Server created and listening for events" << std::endl; + //Check for new messages in the collection for (;;){ - std::vector t = mongoHandler->getNewMessages(&colCs); + std::vector t = mongoHandler->getNewMessages(&colCs); for(auto &i : t){ - std::cout << i << std::endl; + std::cout << "[" << i.eventName << "] " << std::endl; } } - std::cout << "Server created" << std::endl; return 0; } \ No newline at end of file diff --git a/src/mongoStub.cpp b/src/mongoStub.cpp index 50312fc6..3e7ae3df 100644 --- a/src/mongoStub.cpp +++ b/src/mongoStub.cpp @@ -17,10 +17,28 @@ mongoStub::mongoStub() { } } -std::vectormongoStub::getNewMessages(mongocxx::change_stream* colCs) { - std::vector retVec; - for (const auto& event : *colCs) { - retVec.push_back(bsoncxx::to_json(event)); +//Too slow for my liking +std::vector mongoStub::getNewMessages(mongocxx::change_stream* colCs) { + std::vector retVec; + + for (auto&& event : *colCs) { + mongoStub::mongoMessage returnValue; + + /*if(event["fullDocument"]["data"]){ + returnValue.data.push_back(event["fullDocument"]["data"].get_utf8().value.to_string()); + }*/ + + + + std::cout << bsoncxx::to_json(event) << std::endl; + + //Oly listen to insert events (to avoid "precondition failed: data" exception) + if(event["operationType"].get_utf8().value.to_string()!="insert"){ + continue; + } + returnValue.eventName = event["fullDocument"]["event"].get_utf8().value.to_string(); + retVec.push_back(returnValue); } + return retVec; } diff --git a/src/mongoStub.hpp b/src/mongoStub.hpp index 3cee472c..71bac792 100644 --- a/src/mongoStub.hpp +++ b/src/mongoStub.hpp @@ -7,23 +7,32 @@ #include #include #include -#include +#include #include +#include -class mongoStub : boost::noncopyable { +class mongoStub{ public: mongoStub(); - std::vector getNewMessages(mongocxx::change_stream* colCs); + + struct mongoMessage{ + std::string eventName; + std::vector data; + }; + + 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; + mongocxx::change_stream* colCs = nullptr; }; #endif -- cgit 1.5.1