Merge pull request #21 from ItsNewe/master

Mongo payload data retrieval
This commit is contained in:
Flam3rboy 2021-05-25 23:38:06 +02:00 committed by GitHub
commit d2fb1ee458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 37 deletions

View File

@ -5,7 +5,7 @@ A Fosscord media (voice and video) server
## Installation
### Prerequisites
- Install the [libdatachannel](https://github.com/paullouisageneau/libdatachannel) library
- Install the [limbongocxx]() driver
- Install the [libmongocxx](http://mongocxx.org/mongocxx-v3/installation/) driver and its requirements
### Building

View File

@ -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;
}

View File

@ -17,10 +17,68 @@ 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;
std::cout << bsoncxx::to_json(event) << std::endl;
// Only listen to insert events (to avoid "precondition failed: data"
// exception)
if (event["operationType"].get_utf8().value.to_string() != "insert") {
continue;
}
std::string evName = event["fullDocument"]["event"].get_utf8().value.to_string();
if(evName.substr(0, 7)=="VSERVER"){ continue; } //Ignore the event if it's been emited by a voice server
if (evName == "UDP_CONNECTION") {
handleUdpRequest(
event["fullDocument"]["data"]["d"]["address"].get_utf8().value.to_string(),
event["fullDocument"]["data"]["d"]["port"].get_int32().value,
event["fullDocument"]["data"]["d"]["mode"].get_utf8().value.to_string()
);
} else if (evName == "VOICE_REQUEST") {
//TODO
continue;
}
returnValue.eventName = evName;
retVec.push_back(returnValue);
}
return retVec;
}
void mongoStub::handleUdpRequest(std::string address, int port, std::string mode) {
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::sub_array;
using bsoncxx::builder::basic::sub_document;
auto builder = bsoncxx::builder::basic::document{};
//Handle UDP socket stuff (later tho)
builder.append(kvp("event", "VSERVER_UDP_RESPONSE"));
builder.append(kvp("op", "4"));
builder.append(kvp("d", [](sub_document subdoc) {
subdoc.append(kvp("mode", "CRYPT_MODE")),
subdoc.append(kvp("secret_key", [](sub_array subarr) {
subarr.append(1, 2, 3, 5); // HOW DO I GEN A SKEY?
}));
}));
bsoncxx::stdx::optional<mongocxx::result::insert_one> r= col.insert_one(builder.view());
}
void mongoStub::handleVoiceRequest() {
//Is this really needed? idk
}

View File

@ -7,23 +7,35 @@
#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;
void handleUdpRequest(std::string address, int port, std::string mode);
void handleVoiceRequest();
};
#endif

View File

@ -1,24 +0,0 @@
syntax = "proto3";
package fosscordMedia;
service fosscordInternals{
rpc vRequest(voiceRequest) returns (voiceAnswer) {}
}
message voiceRequest{ //OP1 from gw
uint64 userid = 1;
uint64 guildid = 2;
string ip=3;
uint32 port=4;
string protocol=5;
string rtcConnectionId=6;
}
message voiceAnswer{//OP2 and OP4 to gw
string ip=1;
uint32 port=3;
repeated string modes=2;
int32 ssrc=4;
string audioCodec=5;
}