Files
UNSWebServerCore_LinuxSO/ProcessorAdapter.h
T

197 lines
5.4 KiB
C++

#pragma once
#include "IPTable.h"
#include <webcc/view.h>
#include "FileReceiver.h"
#include "ServerLogger.h"
#include "ServerProcessor.h"
#include "SyncFileReceiver.h"
#include "HTTPObjectsBridge.h" // 确保能看到 Request::Impl 结构体
#include "UNSResponseBuilder.h"
namespace uns
{
class IBlockedIpUpdatable
{
public:
virtual ~IBlockedIpUpdatable() = default;
// 统一的库内更新接口
virtual void ApplyIpUpdate(IPTablePtr blocked_ips) = 0;
};
// 隐藏在动态库内部的适配器,对外部不可见
class ServerProcessorAdapter : public webcc::View, public IBlockedIpUpdatable
{
private:
std::shared_ptr<ServerProcessor> user_processor;
public:
explicit ServerProcessorAdapter(std::shared_ptr<ServerProcessor> processor) : user_processor(processor)
{
}
// 完美的把 webcc 的驱动流,翻译给用户的纯净业务类
webcc::ResponsePtr Handle(webcc::RequestPtr request) final
{
try
{
auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request)));
// 调用用户的业务类
uns::ResponsePtr uns_res = user_processor->Handle(uns_req);
return uns_res->GetImpl()->webcc_res;
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(Handle): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(Handle)");
}
return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500
}
bool Stream(const std::string& method) final
{
try
{
return user_processor->Stream(method);
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(Stream): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(Stream)");
}
return false; //Default false
}
void ApplyIpUpdate(IPTablePtr blocked_ips) override
{
try
{
user_processor->UpdateBlockedIPList(blocked_ips);
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(ApplyIpUpdate): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(ApplyIpUpdate)");
}
}
};
class FileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable
{
private:
std::shared_ptr<::FileReceiver> user_reciver; // 持有用户的派生类对象
public:
explicit FileReceiverAdapter(std::shared_ptr<FileReceiver> reciver) : user_reciver(reciver)
{
}
// 完美的把 webcc 的驱动流,翻译给用户的纯净业务类
webcc::ResponsePtr Handle(webcc::RequestPtr request) final
{
try
{
auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request)));
// 调用用户的业务类
uns::ResponsePtr uns_res = user_reciver->Execute(uns_req);
return uns_res->GetImpl()->webcc_res;
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(Handle): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(Handle)");
}
return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500
}
bool Stream(const std::string& method) final
{
// 所有数据都不能由webcc进行串流,否则将无法从request中获取文件
return false;
}
void ApplyIpUpdate(IPTablePtr blocked_ips) override
{
try
{
user_reciver->UpdateBlockedIPs(blocked_ips);
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(ApplyIpUpdate): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(ApplyIpUpdate)");
}
}
};
class SyncFileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable
{
private:
std::shared_ptr<SyncFileReceiver> user_reciver; // 持有用户的派生类对象
public:
explicit SyncFileReceiverAdapter(std::shared_ptr<SyncFileReceiver> reciver) : user_reciver(reciver)
{
}
// 完美的把 webcc 的驱动流,翻译给用户的纯净业务类
webcc::ResponsePtr Handle(webcc::RequestPtr request) final
{
try
{
auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request)));
// 调用用户的业务类
uns::ResponsePtr uns_res = user_reciver->Execute(uns_req);
return uns_res->GetImpl()->webcc_res;
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(Handle): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(Handle)");
}
return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500
}
bool Stream(const std::string& method) final
{
// 所有数据都不能由webcc进行串流,否则将无法从request中获取文件
return false;
}
void ApplyIpUpdate(IPTablePtr blocked_ips) override
{
try
{
user_reciver->UpdateBlockedIPs(blocked_ips);
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(ApplyIpUpdate): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(ApplyIpUpdate)");
}
}
};
}