eqemu-server/world/web_interface.cpp
Michael Cook (mackal) 7a46a6595c
[Cleanup] use std::make_unique (#1259)
* Convert common/eq_limits.cpp to use make_unique

* Convert common/net/console_server.cpp to use make_unique

* Convert common/net/servertalk_client_connection.cpp to use make_unique

* Convert common/net/servertalk_legacy_client_connection.cpp to use make_unique

* Convert common/net/servertalk_server.cpp to use make_unique

* Convert common/net/websocket_server.cpp to use make_unique

* Convert common/net/websocket_server_connection.cpp to use make_unique

* Convert common/shareddb.cpp to use make_unique

* Convert eqlaunch/worldserver.cpp to use make_unique

* Convert loginserver/server_manager.cpp to use make_unique

* Convert loginserver/world_server.cpp to use make_unique

* Convert queryserv/worldserver.cpp to use make_unique

* Convert ucs/worldserver.cpp to use make_unique

* Convert world/clientlist.cpp to use make_unique

* Convert world/expedition.cpp to use make_unique

* Convert world/launcher_link.cpp to use make_unique

* Convert world/login_server.cpp to use make_unique

* Convert world/main.cpp to use make_unique

* Convert world/ucs.cpp to use make_unique

* Convert world/web_interface.cpp to use make_unique

* Convert world/zonelist.cpp to use make_unique

* Convert world/zoneserver.cpp to use make_unique

* Convert zone/client.cpp to use make_unique

* Convert zone/corpse.cpp to use make_unique

* Convert zone/dynamiczone.cpp to use make_unique

* Convert zone/expedition.cpp to use make_unique

* Convert zone/main.cpp to use make_unique

* Convert zone/mob_ai.cpp to use make_unique

* Convert zone/mob_movement_manager.cpp to use make_unique

* Convert zone/pathfinder_nav_mesh.cpp to use make_unique

* Convert zone/worldserver.cpp to use make_unique
2021-02-23 18:30:46 -06:00

187 lines
3.9 KiB
C++

#include "web_interface.h"
#include "../common/json/json.h"
#include "web_interface_eqw.h"
#include <sstream>
WebInterface::WebInterface(std::shared_ptr<EQ::Net::ServertalkServerConnection> connection)
{
m_connection = connection;
m_connection->OnMessage(ServerOP_WebInterfaceCall, std::bind(&WebInterface::OnCall, this, std::placeholders::_1, std::placeholders::_2));
RegisterEQW(this);
}
WebInterface::~WebInterface()
{
}
void WebInterface::OnCall(uint16 opcode, EQ::Net::Packet &p)
{
Json::Value root;
try {
auto json_str = p.GetCString(0);
std::stringstream ss(json_str);
ss >> root;
}
catch (std::exception &) {
SendError("Could not parse request");
return;
}
std::string method;
Json::Value params;
std::string id;
try {
method = root["method"].asString();
if (method.length() == 0) {
SendError("Invalid request: method not supplied");
return;
}
}
catch (std::exception &) {
SendError("Invalid request: method not supplied");
return;
}
//optional "params" -> Json::Value
try {
params = root["params"];
}
catch (std::exception &) {
params = nullptr;
}
//optional "id" needs to be string
try {
id = root["id"].asString();
}
catch (std::exception &) {
id = "";
}
//check for registered method
auto iter = m_calls.find(method);
if (iter == m_calls.end()) {
//if not exist then error
SendError("Invalid request: method not found", id);
return;
}
iter->second(this, method, id, params);
}
void WebInterface::Send(const Json::Value &value)
{
try {
std::stringstream ss;
ss << value;
EQ::Net::DynamicPacket p;
p.PutString(0, ss.str());
m_connection->Send(ServerOP_WebInterfaceCall, p);
}
catch (std::exception &) {
//Log error
}
}
void WebInterface::SendError(const std::string &message)
{
Json::Value error;
error["error"] = Json::Value();
error["error"]["message"] = message;
Send(error);
}
void WebInterface::SendError(const std::string &message, const std::string &id)
{
Json::Value error;
error["id"] = id;
error["error"] = Json::Value();
error["error"]["message"] = message;
Send(error);
}
void WebInterface::SendEvent(const Json::Value &value)
{
try {
std::stringstream ss;
ss << value;
EQ::Net::DynamicPacket p;
p.PutString(0, ss.str());
m_connection->Send(ServerOP_WebInterfaceEvent, p);
}
catch (std::exception &) {
//Log error
}
}
void WebInterface::AddCall(const std::string &method, WebInterfaceCall call)
{
m_calls.insert(std::make_pair(method, call));
}
void WebInterface::SendResponse(const std::string &id, const Json::Value &response)
{
Json::Value out;
if(!id.empty())
out["id"] = id;
out["response"] = response;
Send(out);
}
WebInterfaceList::WebInterfaceList()
{
}
WebInterfaceList::~WebInterfaceList()
{
}
void WebInterfaceList::AddConnection(std::shared_ptr<EQ::Net::ServertalkServerConnection> connection)
{
m_interfaces.insert(std::make_pair(connection->GetUUID(), std::make_unique<WebInterface>(connection)));
}
void WebInterfaceList::RemoveConnection(std::shared_ptr<EQ::Net::ServertalkServerConnection> connection)
{
auto iter = m_interfaces.find(connection->GetUUID());
if (iter != m_interfaces.end()) {
m_interfaces.erase(iter);
return;
}
}
void WebInterfaceList::SendResponse(const std::string &uuid, std::string &id, const Json::Value &response) {
auto iter = m_interfaces.find(uuid);
if (iter != m_interfaces.end()) {
iter->second->SendResponse(id, response);
}
}
void WebInterfaceList::SendEvent(const Json::Value &value) {
for (auto &i : m_interfaces) {
i.second->SendEvent(value);
}
}
void WebInterfaceList::SendError(const std::string &uuid, const std::string &message) {
auto iter = m_interfaces.find(uuid);
if (iter != m_interfaces.end()) {
iter->second->SendError(message);
}
}
void WebInterfaceList::SendError(const std::string &uuid, const std::string &message, const std::string &id) {
auto iter = m_interfaces.find(uuid);
if (iter != m_interfaces.end()) {
iter->second->SendError(message, id);
}
}