[edit] Deprecate gRPC in favor of MongoDB

This commit is contained in:
Newe
2021-05-24 17:46:50 +02:00
parent 9c9c948ae9
commit a5d98b9dcc
7 changed files with 75 additions and 74 deletions
+12 -3
View File
@@ -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<rtcPeerHandler>();
auto rpcHandler = std::unique_ptr<rpcStub>(new rpcStub(commsHandler, 8057));
auto mongoHandler = std::make_unique<mongoStub>();
mongocxx::options::change_stream options;
mongocxx::change_stream colCs = mongoHandler->getCol().watch(options);
//Check for new messages in the collection
for (;;){
std::vector<std::string> 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;
}
+26
View File
@@ -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::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));
}
return retVec;
}
+29
View File
@@ -0,0 +1,29 @@
#ifndef MONGOSTUB_HPP
#define MONGOSTUB_HPP
#include <boost/utility.hpp>
#include <cstdint>
#include <iostream>
#include <vector>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/v_noabi/mongocxx/change_stream.hpp>
#include <bsoncxx/json.hpp>
class mongoStub : boost::noncopyable {
public:
mongoStub();
std::vector<std::string> 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
-32
View File
@@ -1,32 +0,0 @@
#include "rpcStub.hpp"
class fossCordInternalsImpl final : public fosscordMedia::fosscordInternals::Service {
std::shared_ptr<rtcPeerHandler> ph;
fossCordInternalsImpl(std::shared_ptr<rtcPeerHandler> 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<rtcPeerHandler> 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;
}
-15
View File
@@ -1,15 +0,0 @@
#include <grpc++/grpc++.h>
#include "protodefs/include/protos.grpc.pb.h"
#include "rtcPeerHandler.hpp"
#ifndef RPCSTUB
#define RPCSTUB
class rpcStub{
public:
rpcStub(std::shared_ptr<rtcPeerHandler> peerHandler, int port);
std::unique_ptr<grpc::Server> server;
private:
std::shared_ptr<rtcPeerHandler> ph;
};
#endif