1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/test/webfuse_provider/test_util/webfuse_server.cc

340 lines
8.7 KiB
C++
Raw Normal View History

2020-06-24 20:03:38 +00:00
#include "webfuse_provider/test_util/webfuse_server.hpp"
#include "webfuse_provider/impl/util/lws_log.h"
#include "webfuse_provider/protocol_names.h"
#include "webfuse_provider/impl/json/doc.h"
2020-07-12 09:09:50 +00:00
#include "webfuse_provider/impl/json/node.h"
2020-06-24 20:03:38 +00:00
#include <libwebsockets.h>
#include <stdexcept>
#include <thread>
2020-06-25 19:19:45 +00:00
#include <future>
2020-06-24 20:03:38 +00:00
#include <mutex>
#include <sstream>
#include <queue>
2020-06-25 19:19:45 +00:00
#include <chrono>
2020-07-12 09:09:50 +00:00
#include <sstream>
2020-06-25 19:19:45 +00:00
#define TIMEOUT (std::chrono::seconds(10))
2020-06-24 20:03:38 +00:00
namespace
{
class IServer
{
public:
virtual ~IServer() = default;
virtual void OnConnected(lws * wsi) = 0;
virtual void OnConnectionClosed(lws * wsi) = 0;
2020-07-12 09:09:50 +00:00
virtual void OnMessageReceived(lws * wsi, char * data, size_t length) = 0;
2020-06-24 20:03:38 +00:00
virtual void OnWritable(lws * wsi) = 0;
};
}
extern "C"
{
static int wfp_test_utils_webfuse_server_callback(
lws * wsi,
lws_callback_reasons reason,
void * user,
void * in,
size_t len)
{
int result = 0;
lws_protocols const * protocol = lws_get_protocol(wsi);
auto * server = reinterpret_cast<IServer*>(nullptr != protocol ? protocol->user : nullptr);
if (nullptr != server)
{
switch (reason)
{
case LWS_CALLBACK_ESTABLISHED:
server->OnConnected(wsi);
break;
case LWS_CALLBACK_CLOSED:
server->OnConnectionClosed(wsi);
break;
case LWS_CALLBACK_RECEIVE:
{
2020-07-12 09:09:50 +00:00
auto * data = reinterpret_cast<char*>(in);
2020-06-24 20:03:38 +00:00
server->OnMessageReceived(wsi, data, len);
}
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
server->OnWritable(wsi);
break;
default:
break;
}
}
return result;
}
}
namespace webfuse_test
{
class WebfuseServer::Private: public IServer
{
public:
2020-06-25 16:36:51 +00:00
Private(bool use_tls)
2020-06-25 19:19:45 +00:00
: id(0)
, is_shutdown_requested(false)
, message(nullptr)
2020-06-24 20:03:38 +00:00
, client(nullptr)
{
wfp_impl_lwslog_disable();
IServer * server = this;
memset(protocols, 0, sizeof(struct lws_protocols) * 2 );
protocols[0].name = WFP_PROTOCOL_NAME_ADAPTER_SERVER;
protocols[0].callback = &wfp_test_utils_webfuse_server_callback;
protocols[0].per_session_data_size = 0;
protocols[0].user = reinterpret_cast<void*>(server);
memset(&info, 0, sizeof(struct lws_context_creation_info));
info.port = 0;
info.mounts = NULL;
info.protocols = protocols;
info.vhost_name = "localhost";
info.ws_ping_pong_interval = 10;
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
context = lws_create_context(&info);
2020-06-25 16:36:51 +00:00
if (use_tls)
{
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
info.ssl_cert_filepath = "server-cert.pem";
info.ssl_private_key_filepath = "server-key.pem";
}
2020-06-24 20:03:38 +00:00
struct lws_vhost * vhost = lws_create_vhost(context, &info);
int port = lws_get_vhost_port(vhost);
2020-06-25 16:36:51 +00:00
std::ostringstream stream;
stream << (use_tls ? "wss://" : "ws://") << "localhost:" << port << "/";
2020-06-24 20:03:38 +00:00
url = stream.str();
thread = std::thread(&Run, this);
}
~Private() override
{
{
std::unique_lock<std::mutex> lock(mutex);
is_shutdown_requested = true;
}
lws_cancel_service(context);
thread.join();
lws_context_destroy(context);
}
std::string const & GetUrl() const
{
return url;
}
2020-06-25 19:19:45 +00:00
std::string const & GetFilesystem() const
{
return filesystem;
}
2020-07-12 09:09:50 +00:00
std::string Invoke(std::string const & method, std::string const & params)
2020-06-24 20:03:38 +00:00
{
2020-06-25 19:19:45 +00:00
std::promise<std::string> response;
{
std::unique_lock<std::mutex> lock(mutex);
message = &response;
id++;
2020-07-12 09:09:50 +00:00
std::ostringstream request;
request << "{"
<< "\"method\": \"" << method << "\","
<< "\"params\": " << params << ","
<< "\"id\": " << id
<< "}";
2020-06-25 19:19:45 +00:00
2020-07-12 09:09:50 +00:00
write_queue.push(request.str());
2020-06-25 19:19:45 +00:00
}
lws_callback_on_writable(client);
2020-07-12 09:09:50 +00:00
std::string result;
2020-06-25 19:19:45 +00:00
auto future = response.get_future();
auto state = future.wait_for(TIMEOUT);
if (std::future_status::ready == state)
{
2020-07-12 09:09:50 +00:00
result = future.get();
2020-06-25 19:19:45 +00:00
}
return result;
2020-06-24 20:03:38 +00:00
}
void OnConnected(lws * wsi) override
{
std::unique_lock<std::mutex> lock(mutex);
client = wsi;
}
void OnConnectionClosed(lws * wsi) override
{
std::unique_lock<std::mutex> lock(mutex);
client = nullptr;
}
2020-07-12 09:09:50 +00:00
void OnMessageReceived(lws * wsi, char * data, size_t length) override
2020-06-24 20:03:38 +00:00
{
2020-06-25 19:19:45 +00:00
{
std::unique_lock<std::mutex> lock(mutex);
if (nullptr != message)
{
message->set_value(std::string(data, length));
}
}
wfp_json_doc * doc = wfp_impl_json_doc_loadb(data, length);
2020-07-12 09:09:50 +00:00
if (doc)
2020-06-24 20:03:38 +00:00
{
wfp_json const * message = wfp_impl_json_doc_root(doc);
2020-07-12 09:09:50 +00:00
wfp_json const * method = wfp_impl_json_object_get(message, "method");
if (wfp_impl_json_is_string(method))
2020-06-24 20:03:38 +00:00
{
if (0 == strcmp("add_filesystem", wfp_impl_json_string_get(method)))
2020-06-24 20:03:38 +00:00
{
2020-07-12 09:09:50 +00:00
wfp_json const * id = wfp_impl_json_object_get(message, "id");
2020-06-24 20:03:38 +00:00
2020-07-12 09:09:50 +00:00
std::ostringstream response;
response << "{\"result\": {\"id\": \"" << GetFilesystem() << "\"}, "
<< "\"id\": " << wfp_impl_json_int_get(id) << "}";
2020-06-24 20:03:38 +00:00
{
std::unique_lock<std::mutex> lock(mutex);
2020-07-12 09:09:50 +00:00
write_queue.push(response.str());
2020-06-24 20:03:38 +00:00
}
lws_callback_on_writable(wsi);
}
}
2020-06-25 19:19:45 +00:00
wfp_impl_json_doc_dispose(doc);
2020-06-24 20:03:38 +00:00
}
}
void OnWritable(lws * wsi) override
{
bool notify = false;
{
std::unique_lock<std::mutex> lock(mutex);
if (!write_queue.empty())
{
std::string const & message = write_queue.front();
unsigned char * data = new unsigned char[LWS_PRE + message.size()];
memcpy(&data[LWS_PRE], message.c_str(), message.size());
lws_write(wsi, &data[LWS_PRE], message.size(), LWS_WRITE_TEXT);
delete[] data;
write_queue.pop();
notify = !write_queue.empty();
}
}
if (notify)
{
lws_callback_on_writable(wsi);
}
}
private:
static void Run(Private * self)
{
bool is_running = true;
while (is_running)
{
lws_service(self->context, 0);
{
std::unique_lock<std::mutex> lock(self->mutex);
is_running = !self->is_shutdown_requested;
}
}
}
2020-06-25 19:19:45 +00:00
int id;
2020-06-24 20:03:38 +00:00
bool is_shutdown_requested;
2020-06-25 19:19:45 +00:00
std::promise<std::string> * message;
2020-06-24 20:03:38 +00:00
lws * client;
std::string url;
lws_context * context;
lws_protocols protocols[2];
lws_context_creation_info info;
std::thread thread;
std::mutex mutex;
std::queue<std::string> write_queue;
2020-06-25 19:19:45 +00:00
std::string filesystem = "fs";
2020-06-24 20:03:38 +00:00
};
2020-06-25 16:36:51 +00:00
WebfuseServer::WebfuseServer(bool use_tls)
: d(new Private(use_tls))
2020-06-24 20:03:38 +00:00
{
}
WebfuseServer::~WebfuseServer()
{
delete d;
}
std::string const & WebfuseServer::GetUrl()
{
return d->GetUrl();
}
2020-07-12 09:09:50 +00:00
std::string WebfuseServer::Invoke(std::string const & method, std::string const & params)
2020-06-24 20:03:38 +00:00
{
return d->Invoke(method, params);
}
2020-07-12 09:09:50 +00:00
std::string WebfuseServer::Lookup(int parent, std::string const & name)
2020-06-25 19:19:45 +00:00
{
2020-07-12 09:09:50 +00:00
std::ostringstream params;
params << "[\"" << d->GetFilesystem() << "\", " << parent << ", \"" << name << "\"]";
2020-06-25 19:19:45 +00:00
2020-07-12 09:09:50 +00:00
return d->Invoke("lookup", params.str());
2020-06-25 19:19:45 +00:00
}
2020-07-12 09:09:50 +00:00
std::string WebfuseServer::Open(int inode, int flags)
2020-06-25 20:22:26 +00:00
{
2020-07-12 09:09:50 +00:00
std::ostringstream params;
params << "[\"" << d->GetFilesystem() << "\", " << inode << ", " << flags << "]";
2020-06-25 20:22:26 +00:00
2020-07-12 09:09:50 +00:00
return d->Invoke("open", params.str());
2020-06-25 20:22:26 +00:00
}
2020-07-12 09:09:50 +00:00
std::string WebfuseServer::Read(int inode, int handle, int offset, int length)
2020-06-26 16:11:10 +00:00
{
2020-07-12 09:09:50 +00:00
std::ostringstream params;
params << "[\"" << d->GetFilesystem() << "\", " << inode << ", " << handle << ", " << offset << ", " << length << "]";
return d->Invoke("read", params.str());
2020-06-26 16:11:10 +00:00
}
2020-07-12 09:09:50 +00:00
std::string WebfuseServer::ReadDir(int inode)
2020-06-26 16:11:10 +00:00
{
2020-07-12 09:09:50 +00:00
std::ostringstream params;
params << "[\"" << d->GetFilesystem() << "\", " << inode << "]";
2020-06-26 16:11:10 +00:00
2020-07-12 09:09:50 +00:00
return d->Invoke("readdir", params.str());
2020-06-26 16:11:10 +00:00
}
2020-06-24 20:03:38 +00:00
}