diff options
author | Newe <speedy.wolfy@outlook.com> | 2021-05-25 12:41:28 +0200 |
---|---|---|
committer | Newe <speedy.wolfy@outlook.com> | 2021-05-25 12:41:28 +0200 |
commit | 63407d96e37df8457e8350e9184b9e3de376fd1f (patch) | |
tree | ddc85cc13bcc72a33f1d6716c1c4c16c2b5195bd | |
parent | [edit] Edited README (diff) | |
download | server-63407d96e37df8457e8350e9184b9e3de376fd1f.tar.xz |
[edit] Retrieve the event name from mongoDB event payload
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/mongoStub.cpp | 26 | ||||
-rw-r--r-- | src/mongoStub.hpp | 17 |
3 files changed, 40 insertions, 11 deletions
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<mongoStub>(); 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<std::string> t = mongoHandler->getNewMessages(&colCs); + std::vector<mongoStub::mongoMessage> 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::vector<std::string>mongoStub::getNewMessages(mongocxx::change_stream* colCs) { - std::vector<std::string> retVec; - for (const auto& event : *colCs) { - retVec.push_back(bsoncxx::to_json(event)); +//Too slow for my liking +std::vector<mongoStub::mongoMessage> mongoStub::getNewMessages(mongocxx::change_stream* colCs) { + std::vector<mongoStub::mongoMessage> 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 <vector> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> -#include <mongocxx/v_noabi/mongocxx/change_stream.hpp> +#include <mongocxx/change_stream.hpp> #include <bsoncxx/json.hpp> +#include <bsoncxx/document/element.hpp> -class mongoStub : boost::noncopyable { +class mongoStub{ public: mongoStub(); - std::vector<std::string> getNewMessages(mongocxx::change_stream* colCs); + + struct mongoMessage{ + std::string eventName; + std::vector<std::string> data; + }; + + std::vector<mongoMessage> 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 |