提交 749ce475 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Starting Notifications (events) from worker processes to Node process.

上级 d786c5c4
...@@ -20,6 +20,7 @@ class Channel extends EventEmitter ...@@ -20,6 +20,7 @@ class Channel extends EventEmitter
logger.debug('constructor()'); logger.debug('constructor()');
super(); super();
this.setMaxListeners(Infinity);
// Unix Socket instance // Unix Socket instance
this._socket = socket; this._socket = socket;
...@@ -258,9 +259,20 @@ class Channel extends EventEmitter ...@@ -258,9 +259,20 @@ class Channel extends EventEmitter
} }
} }
// If a Notification emit it to the corresponding entity // If a Notification emit it to the corresponding entity
else if (json.targetId && json.eventName)
{
let targetId = json.targetId;
let eventName = json.eventName;
logger.debug('received Notification [targetId:%s, eventName:%s]', targetId, eventName);
// Emit it
this.emit(targetId, eventName, json.eventData);
}
// Otherwise unexpected message
else else
{ {
// TODO logger.error('received message is not a Response nor a Notification');
} }
} }
} }
......
...@@ -27,6 +27,10 @@ class Room extends EventEmitter ...@@ -27,6 +27,10 @@ class Room extends EventEmitter
this._closed = false; this._closed = false;
// TODO: subscribe to channel notifications from the worker // TODO: subscribe to channel notifications from the worker
this._channel.on(this._internal.roomId, (eventName, eventData) =>
{
logger.debug('----- room %s received event "%s" [data:%o]', this._internal.roomId, eventName, eventData);
});
} }
get closed() get closed()
...@@ -71,6 +75,8 @@ class Room extends EventEmitter ...@@ -71,6 +75,8 @@ class Room extends EventEmitter
}); });
} }
// TODO: unsubscribe from Channel
this.emit('close', error); this.emit('close', error);
} }
......
#ifndef MS_CHANNEL_NOTIFIER_H
#define MS_CHANNEL_NOTIFIER_H
#include "Channel/UnixStreamSocket.h"
#include <string>
#include <json/json.h>
namespace Channel
{
class Notifier
{
public:
Notifier(Channel::UnixStreamSocket* channel);
virtual ~Notifier();
void Close();
void Emit(unsigned int targetId, std::string eventName);
void Emit(unsigned int targetId, std::string eventName, Json::Value& eventData);
public:
// Passed by argument.
Channel::UnixStreamSocket* channel = nullptr;
};
}
#endif
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "handles/SignalsHandler.h" #include "handles/SignalsHandler.h"
#include "Channel/UnixStreamSocket.h" #include "Channel/UnixStreamSocket.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/Notifier.h"
#include "RTC/Room.h" #include "RTC/Room.h"
#include <unordered_map> #include <unordered_map>
...@@ -41,6 +42,7 @@ private: ...@@ -41,6 +42,7 @@ private:
SignalsHandler* signalsHandler = nullptr; SignalsHandler* signalsHandler = nullptr;
// Others. // Others.
bool closed = false; bool closed = false;
Channel::Notifier* notifier = nullptr;
typedef std::unordered_map<unsigned int, RTC::Room*> Rooms; typedef std::unordered_map<unsigned int, RTC::Room*> Rooms;
Rooms rooms; Rooms rooms;
}; };
......
#define MS_CLASS "Channel::Notifier"
#include "Channel/Notifier.h"
#include "Logger.h"
namespace Channel
{
/* Static variables. */
static Json::Value empty_data(Json::objectValue);
/* Instance methods. */
Notifier::Notifier(Channel::UnixStreamSocket* channel) :
channel(channel)
{
MS_TRACE();
}
Notifier::~Notifier()
{
MS_TRACE();
}
void Notifier::Close()
{
MS_TRACE();
delete this;
}
void Notifier::Emit(unsigned int targetId, std::string eventName)
{
MS_TRACE();
Emit(targetId, eventName, empty_data);
}
void Notifier::Emit(unsigned int targetId, std::string eventName, Json::Value &eventData)
{
MS_TRACE();
static const Json::StaticString k_targetId("targetId");
static const Json::StaticString k_eventName("eventName");
static const Json::StaticString k_eventData("eventData");
Json::Value json;
json[k_targetId] = targetId;
json[k_eventName] = eventName;
if (eventData.isObject())
json[k_eventData] = eventData;
else
json[k_eventData] = empty_data;
this->channel->Send(json);
}
}
...@@ -22,6 +22,9 @@ Loop::Loop(Channel::UnixStreamSocket* channel) : ...@@ -22,6 +22,9 @@ Loop::Loop(Channel::UnixStreamSocket* channel) :
// Set us as Channel's listener. // Set us as Channel's listener.
this->channel->SetListener(this); this->channel->SetListener(this);
// Create the Notifier instance.
this->notifier = new Channel::Notifier(this->channel);
// Set the signals handler. // Set the signals handler.
this->signalsHandler = new SignalsHandler(this); this->signalsHandler = new SignalsHandler(this);
...@@ -76,6 +79,9 @@ void Loop::Close() ...@@ -76,6 +79,9 @@ void Loop::Close()
room->Close(); room->Close();
} }
// Close the Notifier.
this->notifier->Close();
// Close the Channel socket. // Close the Channel socket.
if (this->channel) if (this->channel)
this->channel->Close(); this->channel->Close();
...@@ -206,6 +212,9 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -206,6 +212,9 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
MS_DEBUG("Room created [roomId:%u]", roomId); MS_DEBUG("Room created [roomId:%u]", roomId);
request->Accept(); request->Accept();
// TODO: TESTING
this->notifier->Emit(roomId, "created");
break; break;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论