提交 5d779b79 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

worker: Remove ugly "static" Channel singleton.

上级 0febb7e8
#ifndef MS_CHANNEL_REQUEST_H #ifndef MS_CHANNEL_REQUEST_H
#define MS_CHANNEL_REQUEST_H #define MS_CHANNEL_REQUEST_H
#include "Channel/UnixStreamSocket.h"
#include <string> #include <string>
#include <json/json.h> #include <json/json.h>
...@@ -9,19 +10,22 @@ namespace Channel ...@@ -9,19 +10,22 @@ namespace Channel
class Request class Request
{ {
public: public:
static Channel::Request* Factory(Json::Value& msg); static Channel::Request* Factory(Channel::UnixStreamSocket* channel, Json::Value& msg);
public: public:
Request(std::string& id, std::string& method, Json::Value& data); Request(Channel::UnixStreamSocket* channel, std::string& id, std::string& method, Json::Value& data);
virtual ~Request(); virtual ~Request();
void Reply(int status); void Reply(int status);
public: public:
// Passed by argument: // Passed by argument:
Channel::UnixStreamSocket* channel = nullptr;
std::string id; std::string id;
std::string method; std::string method;
Json::Value data; Json::Value data;
// Others:
bool replied = false;
}; };
} }
......
...@@ -37,9 +37,6 @@ namespace Channel ...@@ -37,9 +37,6 @@ namespace Channel
// Others: // Others:
size_t msgStart = 0; // Where the latest message starts. size_t msgStart = 0; // Where the latest message starts.
}; };
// Channel singleton.
extern UnixStreamSocket* channel;
} }
#endif #endif
#define MS_CLASS "Channel::Request" #define MS_CLASS "Channel::Request"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/UnixStreamSocket.h"
#include "Logger.h" #include "Logger.h"
namespace Channel namespace Channel
{ {
/* Static methods. */ /* Static methods. */
Channel::Request* Request::Factory(Json::Value& msg) Channel::Request* Request::Factory(Channel::UnixStreamSocket* channel, Json::Value& msg)
{ {
MS_TRACE(); MS_TRACE();
...@@ -23,12 +22,13 @@ namespace Channel ...@@ -23,12 +22,13 @@ namespace Channel
std::string id = msg["id"].asString(); std::string id = msg["id"].asString();
std::string method = msg["method"].asString(); std::string method = msg["method"].asString();
return new Request(id, method, msg["data"]); return new Request(channel, id, method, msg["data"]);
} }
/* Instance methods. */ /* Instance methods. */
Request::Request(std::string& id, std::string& method, Json::Value& data) : Request::Request(Channel::UnixStreamSocket* channel, std::string& id, std::string& method, Json::Value& data) :
channel(channel),
id(id), id(id),
method(method), method(method),
data(data) data(data)
...@@ -45,11 +45,19 @@ namespace Channel ...@@ -45,11 +45,19 @@ namespace Channel
{ {
MS_TRACE(); MS_TRACE();
if (this->replied)
{
MS_ERROR("already replied");
return;
}
this->replied = true;
Json::Value json; Json::Value json;
json["id"] = this->id; json["id"] = this->id;
json["status"] = status; json["status"] = status;
Channel::channel->Send(json); this->channel->Send(json);
} }
} }
...@@ -13,10 +13,6 @@ ...@@ -13,10 +13,6 @@
namespace Channel namespace Channel
{ {
/* Singletons. */
UnixStreamSocket* channel = nullptr;
/* Static variables. */ /* Static variables. */
MS_BYTE UnixStreamSocket::writeBuffer[MESSAGE_MAX_SIZE]; MS_BYTE UnixStreamSocket::writeBuffer[MESSAGE_MAX_SIZE];
...@@ -28,10 +24,6 @@ namespace Channel ...@@ -28,10 +24,6 @@ namespace Channel
listener(listener) listener(listener)
{ {
MS_TRACE(); MS_TRACE();
// Store a singleton.
MS_ASSERT(Channel::channel == nullptr, "Channel singleton already set");
Channel::channel = this;
} }
UnixStreamSocket::~UnixStreamSocket() UnixStreamSocket::~UnixStreamSocket()
...@@ -163,7 +155,7 @@ namespace Channel ...@@ -163,7 +155,7 @@ namespace Channel
MS_THROW_ERROR("invalid JSON"); MS_THROW_ERROR("invalid JSON");
} }
Channel::Request* request = Request::Factory(json); Channel::Request* request = Request::Factory(this, json);
if (request) if (request)
{ {
...@@ -181,6 +173,7 @@ namespace Channel ...@@ -181,6 +173,7 @@ namespace Channel
request->Reply(500); request->Reply(500);
} }
// Delete the Request.
delete request; delete request;
} }
else else
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论