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

worker: Remove ugly "static" Channel singleton.

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