1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

removed dependency to jansson

This commit is contained in:
Falk Werner 2020-07-12 11:09:50 +02:00
parent 59105c291e
commit df52f1b753
6 changed files with 174 additions and 193 deletions

View File

@ -2,6 +2,8 @@
#include "webfuse_provider/test_util/webfuse_server.hpp" #include "webfuse_provider/test_util/webfuse_server.hpp"
#include "webfuse_provider/mocks/mock_provider_client.hpp" #include "webfuse_provider/mocks/mock_provider_client.hpp"
#include "webfuse_provider/test_util/client.hpp" #include "webfuse_provider/test_util/client.hpp"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/impl/json/node.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <future> #include <future>
@ -146,20 +148,22 @@ TEST(Client, Lookup)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
json_t * response = server.Lookup(1, "foo"); std::string response_text = server.Lookup(1, "foo");
ASSERT_TRUE(json_is_object(response)); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
json_t * result = json_object_get(response, "result"); wfp_json const * response = wfp_impl_json_root(doc);
ASSERT_TRUE(wfp_impl_json_is_object(response));
wfp_json const * result = wfp_impl_json_object_get(response, "result");
json_t * inode = json_object_get(result, "inode"); wfp_json const * inode = wfp_impl_json_object_get(result, "inode");
ASSERT_EQ(42, json_integer_value(inode)); ASSERT_EQ(42, wfp_impl_json_get_int(inode));
json_t * mode = json_object_get(result, "mode"); wfp_json const * mode = wfp_impl_json_object_get(result, "mode");
ASSERT_EQ(0644, json_integer_value(mode)); ASSERT_EQ(0644, wfp_impl_json_get_int(mode));
json_t * type = json_object_get(result, "type"); wfp_json const * type = wfp_impl_json_object_get(result, "type");
ASSERT_STREQ("file", json_string_value(type)); ASSERT_STREQ("file", wfp_impl_json_get_string(type));
json_decref(response); wfp_impl_json_dispose(doc);
client.Disconnect(); client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
@ -194,14 +198,17 @@ TEST(Client, LookupFail)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
json_t * response = server.Lookup(1, "foo"); std::string response_text = server.Lookup(1, "foo");
ASSERT_TRUE(json_is_object(response)); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
json_t * error = json_object_get(response, "error"); wfp_json const * response = wfp_impl_json_root(doc);
json_t * code = json_object_get(error, "code"); ASSERT_TRUE(wfp_impl_json_is_object(response));
ASSERT_NE(0, json_integer_value(code)); wfp_json const * error = wfp_impl_json_object_get(response, "error");
json_decref(response); wfp_json const * code = wfp_impl_json_object_get(error, "code");
ASSERT_NE(0, wfp_impl_json_get_int(code));
wfp_impl_json_dispose(doc);
client.Disconnect(); client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
@ -236,14 +243,17 @@ TEST(Client, Open)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
json_t * response = server.Open(1, 0); std::string response_text = server.Open(1, 0);
ASSERT_TRUE(json_is_object(response)); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
json_t * result = json_object_get(response, "result"); wfp_json const * response = wfp_impl_json_root(doc);
json_t * handle = json_object_get(result, "handle"); ASSERT_TRUE(wfp_impl_json_is_object(response));
ASSERT_EQ(4711, json_integer_value(handle)); wfp_json const * result = wfp_impl_json_object_get(response, "result");
json_decref(response); wfp_json const * handle = wfp_impl_json_object_get(result, "handle");
ASSERT_EQ(4711, wfp_impl_json_get_int(handle));
wfp_impl_json_dispose(doc);
client.Disconnect(); client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
@ -279,20 +289,23 @@ TEST(Client, Read)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
json_t * response = server.Read(42, 5, 0, 1); std::string response_text = server.Read(42, 5, 0, 1);
ASSERT_TRUE(json_is_object(response)); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
json_t * result = json_object_get(response, "result"); wfp_json const * response = wfp_impl_json_root(doc);
json_t * format = json_object_get(result, "format"); ASSERT_TRUE(wfp_impl_json_is_object(response));
ASSERT_STREQ("base64", json_string_value(format)); wfp_json const * result = wfp_impl_json_object_get(response, "result");
json_t * count = json_object_get(result, "count"); wfp_json const * format = wfp_impl_json_object_get(result, "format");
ASSERT_EQ(1, json_integer_value(count)); ASSERT_STREQ("base64", wfp_impl_json_get_string(format));
json_t * data = json_object_get(result, "data"); wfp_json const * count = wfp_impl_json_object_get(result, "count");
ASSERT_STREQ("Kg==", json_string_value(data)); ASSERT_EQ(1, wfp_impl_json_get_int(count));
json_decref(response); wfp_json const * data = wfp_impl_json_object_get(result, "data");
ASSERT_STREQ("Kg==", wfp_impl_json_get_string(data));
wfp_impl_json_dispose(doc);
client.Disconnect(); client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
@ -329,14 +342,17 @@ TEST(Client, ReadDir)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
json_t * response = server.ReadDir(42); std::string response_text = server.ReadDir(42);
ASSERT_TRUE(json_is_object(response)); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
json_t * result = json_object_get(response, "result"); wfp_json const * response = wfp_impl_json_root(doc);
ASSERT_TRUE(json_is_array(result)); ASSERT_TRUE(wfp_impl_json_is_object(response));
ASSERT_EQ(3, json_array_size(result)); wfp_json const * result = wfp_impl_json_object_get(response, "result");
json_decref(response); ASSERT_TRUE(wfp_impl_json_is_array(result));
ASSERT_EQ(3, wfp_impl_json_array_size(result));
wfp_impl_json_dispose(doc);
client.Disconnect(); client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));

View File

@ -7,12 +7,14 @@
#include "webfuse_provider/mocks/mock_provider_client.hpp" #include "webfuse_provider/mocks/mock_provider_client.hpp"
#include "webfuse_provider/protocol_names.h" #include "webfuse_provider/protocol_names.h"
#include "webfuse_provider/test_util/timeout_watcher.hpp" #include "webfuse_provider/test_util/timeout_watcher.hpp"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/impl/json/node.h"
#include <libwebsockets.h> #include <libwebsockets.h>
#include <cstring> #include <cstring>
#include <sstream>
#include <thread> #include <thread>
#include <atomic>
using webfuse_test::WsServer; using webfuse_test::WsServer;
using webfuse_test::MockProviderClient; using webfuse_test::MockProviderClient;
@ -77,16 +79,16 @@ public:
wfp_client_protocol_disconnect(protocol); wfp_client_protocol_disconnect(protocol);
} }
void SendToClient(json_t * request) void SendToClient(std::string const & request)
{ {
server->SendMessage(request); server->SendMessage(request);
} }
json_t * ReceiveMessageFromClient() std::string ReceiveMessageFromClient()
{ {
TimeoutWatcher watcher(DEFAULT_TIMEOUT); TimeoutWatcher watcher(DEFAULT_TIMEOUT);
json_t * result = server->ReceiveMessage(); std::string result = server->ReceiveMessage();
while (nullptr == result) while (result.empty())
{ {
watcher.check(); watcher.check();
lws_service(context, 0); lws_service(context, 0);
@ -100,73 +102,71 @@ public:
std::string const & expected_username, std::string const & expected_username,
std::string const & expected_password) std::string const & expected_password)
{ {
json_t * request = ReceiveMessageFromClient(); std::string request_text = ReceiveMessageFromClient();
ASSERT_TRUE(json_is_object(request)); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char *>(request_text.data()));
wfp_json const * request = wfp_impl_json_root(doc);
ASSERT_TRUE(wfp_impl_json_is_object(request));
json_t * method = json_object_get(request, "method"); wfp_json const * method = wfp_impl_json_object_get(request, "method");
ASSERT_TRUE(json_is_string(method)); ASSERT_TRUE(wfp_impl_json_is_string(method));
ASSERT_STREQ("authenticate", json_string_value(method)); ASSERT_STREQ("authenticate", wfp_impl_json_get_string(method));
json_t * id = json_object_get(request, "id"); wfp_json const * id = wfp_impl_json_object_get(request, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wfp_impl_json_is_int(id));
json_t * params = json_object_get(request, "params"); wfp_json const * params = wfp_impl_json_object_get(request, "params");
ASSERT_TRUE(json_is_array(params)); ASSERT_TRUE(wfp_impl_json_is_array(params));
ASSERT_EQ(2, json_array_size(params)); ASSERT_EQ(2, wfp_impl_json_array_size(params));
json_t * type = json_array_get(params, 0); wfp_json const * type = wfp_impl_json_array_get(params, 0);
ASSERT_TRUE(json_is_string(type)); ASSERT_TRUE(wfp_impl_json_is_string(type));
ASSERT_STREQ("username", json_string_value(type)); ASSERT_STREQ("username", wfp_impl_json_get_string(type));
json_t * credentials = json_array_get(params, 1); wfp_json const * credentials = wfp_impl_json_array_get(params, 1);
ASSERT_TRUE(json_is_object(credentials)); ASSERT_TRUE(wfp_impl_json_is_object(credentials));
json_t * username = json_object_get(credentials, "username"); wfp_json const * username = wfp_impl_json_object_get(credentials, "username");
ASSERT_TRUE(json_is_string(username)); ASSERT_TRUE(wfp_impl_json_is_string(username));
ASSERT_STREQ(expected_username.c_str(), json_string_value(username)); ASSERT_STREQ(expected_username.c_str(), wfp_impl_json_get_string(username));
json_t * password = json_object_get(credentials, "password"); wfp_json const * password = wfp_impl_json_object_get(credentials, "password");
ASSERT_TRUE(json_is_string(password)); ASSERT_TRUE(wfp_impl_json_is_string(password));
ASSERT_STREQ(expected_password.c_str(), json_string_value(password)); ASSERT_STREQ(expected_password.c_str(), wfp_impl_json_get_string(password));
json_t * response = json_object(); std::ostringstream response;
json_object_set_new(response, "result", json_object()); response << "{\"result\": {}, \"id\": " << wfp_impl_json_get_int(id) << "}";
json_object_set(response, "id", id); SendToClient(response.str());
SendToClient(response);
json_decref(request); wfp_impl_json_dispose(doc);
} }
void AwaitAddFilesystem(std::string& filesystemName) void AwaitAddFilesystem(std::string& filesystemName)
{ {
json_t * addFilesystemRequest = ReceiveMessageFromClient(); std::string request_text = ReceiveMessageFromClient();
ASSERT_NE(nullptr, addFilesystemRequest); wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char *>(request_text.data()));
ASSERT_TRUE(json_is_object(addFilesystemRequest)); wfp_json const * request = wfp_impl_json_root(doc);
ASSERT_TRUE(wfp_impl_json_is_object(request));
json_t * method = json_object_get(addFilesystemRequest, "method"); wfp_json const * method = wfp_impl_json_object_get(request, "method");
ASSERT_TRUE(json_is_string(method)); ASSERT_TRUE(wfp_impl_json_is_string(method));
ASSERT_STREQ("add_filesystem", json_string_value(method)); ASSERT_STREQ("add_filesystem", wfp_impl_json_get_string(method));
json_t * params = json_object_get(addFilesystemRequest, "params"); wfp_json const * params = wfp_impl_json_object_get(request, "params");
ASSERT_TRUE(json_is_array(params)); ASSERT_TRUE(wfp_impl_json_is_array(params));
ASSERT_EQ(1, json_array_size(params)); ASSERT_EQ(1, wfp_impl_json_array_size(params));
json_t * filesystem = json_array_get(params, 0); wfp_json const * filesystem = wfp_impl_json_array_get(params, 0);
ASSERT_TRUE(json_is_string(filesystem)); ASSERT_TRUE(wfp_impl_json_is_string(filesystem));
filesystemName = json_string_value(filesystem);
json_t * id = json_object_get(addFilesystemRequest, "id"); wfp_json const * id = wfp_impl_json_object_get(request, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wfp_impl_json_is_int(id));
json_t * response = json_object(); std::ostringstream response;
json_t * result = json_object(); response << "{\"result\": {\"id\": \"" << wfp_impl_json_get_string(filesystem) << "\"}, \"id\": " << wfp_impl_json_get_int(id) << "}";
json_object_set(result, "id", filesystem);
json_object_set_new(response, "result", result);
json_object_set(response, "id", id);
SendToClient(response); SendToClient(response.str());
json_decref(addFilesystemRequest); wfp_impl_json_dispose(doc);
} }
private: private:
@ -250,19 +250,12 @@ TEST(client_protocol, getattr)
fixture.AwaitAddFilesystem(filesystem); fixture.AwaitAddFilesystem(filesystem);
if (HasFatalFailure()) { return; } if (HasFatalFailure()) { return; }
json_t * params = json_array(); std::ostringstream request;
json_array_append_new(params, json_string(filesystem.c_str())); request << "{\"method\": \"getattr\", \"params\": [\"" << filesystem << "\", 1], \"id\": 42}";
json_array_append_new(params, json_integer(1));
json_t * request = json_object();
json_object_set_new(request, "method", json_string("getattr"));
json_object_set_new(request, "params", params);
json_object_set_new(request, "id", json_integer(42));
fixture.SendToClient(request); fixture.SendToClient(request.str());
json_t * response = fixture.ReceiveMessageFromClient(); std::string response = fixture.ReceiveMessageFromClient();
ASSERT_TRUE(json_is_object(response)); ASSERT_FALSE(response.empty());
json_decref(response);
fixture.Disconnect(); fixture.Disconnect();
} }

View File

@ -1,6 +1,8 @@
#include "webfuse_provider/test_util/webfuse_server.hpp" #include "webfuse_provider/test_util/webfuse_server.hpp"
#include "webfuse_provider/impl/util/lws_log.h" #include "webfuse_provider/impl/util/lws_log.h"
#include "webfuse_provider/protocol_names.h" #include "webfuse_provider/protocol_names.h"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/impl/json/node.h"
#include <libwebsockets.h> #include <libwebsockets.h>
#include <stdexcept> #include <stdexcept>
@ -10,6 +12,7 @@
#include <sstream> #include <sstream>
#include <queue> #include <queue>
#include <chrono> #include <chrono>
#include <sstream>
#define TIMEOUT (std::chrono::seconds(10)) #define TIMEOUT (std::chrono::seconds(10))
@ -22,7 +25,7 @@ public:
virtual ~IServer() = default; virtual ~IServer() = default;
virtual void OnConnected(lws * wsi) = 0; virtual void OnConnected(lws * wsi) = 0;
virtual void OnConnectionClosed(lws * wsi) = 0; virtual void OnConnectionClosed(lws * wsi) = 0;
virtual void OnMessageReceived(lws * wsi, char const * data, size_t length) = 0; virtual void OnMessageReceived(lws * wsi, char * data, size_t length) = 0;
virtual void OnWritable(lws * wsi) = 0; virtual void OnWritable(lws * wsi) = 0;
}; };
@ -54,7 +57,7 @@ static int wfp_test_utils_webfuse_server_callback(
break; break;
case LWS_CALLBACK_RECEIVE: case LWS_CALLBACK_RECEIVE:
{ {
auto * data = reinterpret_cast<char const*>(in); auto * data = reinterpret_cast<char*>(in);
server->OnMessageReceived(wsi, data, len); server->OnMessageReceived(wsi, data, len);
} }
break; break;
@ -143,7 +146,7 @@ public:
return filesystem; return filesystem;
} }
json_t * Invoke(std::string const & method, json_t * params) std::string Invoke(std::string const & method, std::string const & params)
{ {
std::promise<std::string> response; std::promise<std::string> response;
{ {
@ -151,27 +154,23 @@ public:
message = &response; message = &response;
id++; id++;
json_t * request = json_object(); std::ostringstream request;
json_object_set_new(request, "method", json_string(method.c_str())); request << "{"
json_object_set_new(request, "params", params); << "\"method\": \"" << method << "\","
json_object_set_new(request, "id", json_integer(id)); << "\"params\": " << params << ","
<< "\"id\": " << id
<< "}";
char * request_text = json_dumps(request, 0); write_queue.push(request.str());
write_queue.push(request_text);
free(request_text);
json_decref(request);
} }
lws_callback_on_writable(client); lws_callback_on_writable(client);
json_t * result = nullptr; std::string result;
auto future = response.get_future(); auto future = response.get_future();
auto state = future.wait_for(TIMEOUT); auto state = future.wait_for(TIMEOUT);
if (std::future_status::ready == state) if (std::future_status::ready == state)
{ {
std::string response_text = future.get(); result = future.get();
result = json_loadb(response_text.c_str(), response_text.size(), 0, nullptr);
std::unique_lock<std::mutex> lock(mutex);
message = nullptr;
} }
return result; return result;
@ -189,7 +188,7 @@ public:
client = nullptr; client = nullptr;
} }
void OnMessageReceived(lws * wsi, char const * data, size_t length) override void OnMessageReceived(lws * wsi, char * data, size_t length) override
{ {
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
@ -199,35 +198,30 @@ public:
} }
} }
json_t * message = json_loadb(data, length, 0, nullptr); wfp_json_doc * doc = wfp_impl_json_parse_buffer(data, length);
if (message) if (doc)
{ {
json_t * method = json_object_get(message, "method"); wfp_json const * message = wfp_impl_json_root(doc);
if (json_is_string(method)) wfp_json const * method = wfp_impl_json_object_get(message, "method");
if (wfp_impl_json_is_string(method))
{ {
if (0 == strcmp("add_filesystem", json_string_value(method))) if (0 == strcmp("add_filesystem", wfp_impl_json_get_string(method)))
{ {
json_t * id = json_object_get(message, "id"); wfp_json const * id = wfp_impl_json_object_get(message, "id");
json_t * response = json_object(); std::ostringstream response;
json_t * result = json_object(); response << "{\"result\": {\"id\": \"" << GetFilesystem() << "\"}, "
json_object_set_new(result, "id", json_string(GetFilesystem().c_str())); << "\"id\": " << wfp_impl_json_get_int(id) << "}";
json_object_set_new(response, "result", result);
json_object_set(response, "id", id);
char * response_text = json_dumps(response, 0);
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
write_queue.push(response_text); write_queue.push(response.str());
} }
free(response_text);
json_decref(response);
lws_callback_on_writable(wsi); lws_callback_on_writable(wsi);
} }
} }
json_decref(message); wfp_impl_json_dispose(doc);
} }
} }
@ -306,56 +300,41 @@ std::string const & WebfuseServer::GetUrl()
return d->GetUrl(); return d->GetUrl();
} }
json_t * WebfuseServer::Invoke(std::string const & method, json_t * params) std::string WebfuseServer::Invoke(std::string const & method, std::string const & params)
{ {
return d->Invoke(method, params); return d->Invoke(method, params);
} }
json_t * WebfuseServer::Invoke(std::string const & method, std::string const & params) std::string WebfuseServer::Lookup(int parent, std::string const & name)
{ {
json_t * params_json = json_loads(params.c_str(), 0, nullptr); std::ostringstream params;
return d->Invoke(method, params_json); params << "[\"" << d->GetFilesystem() << "\", " << parent << ", \"" << name << "\"]";
return d->Invoke("lookup", params.str());
} }
json_t * WebfuseServer::Lookup(int parent, std::string const & name) std::string WebfuseServer::Open(int inode, int flags)
{ {
json_t * params = json_array(); std::ostringstream params;
json_array_append_new(params, json_string(d->GetFilesystem().c_str())); params << "[\"" << d->GetFilesystem() << "\", " << inode << ", " << flags << "]";
json_array_append_new(params, json_integer(parent));
json_array_append_new(params, json_string(name.c_str()));
return d->Invoke("lookup", params); return d->Invoke("open", params.str());
} }
json_t * WebfuseServer::Open(int inode, int flags) std::string WebfuseServer::Read(int inode, int handle, int offset, int length)
{ {
json_t * params = json_array(); std::ostringstream params;
json_array_append_new(params, json_string(d->GetFilesystem().c_str())); params << "[\"" << d->GetFilesystem() << "\", " << inode << ", " << handle << ", " << offset << ", " << length << "]";
json_array_append_new(params, json_integer(inode));
json_array_append_new(params, json_integer(flags));
return d->Invoke("open", params); return d->Invoke("read", params.str());
} }
json_t * WebfuseServer::Read(int inode, int handle, int offset, int length) std::string WebfuseServer::ReadDir(int inode)
{ {
json_t * params = json_array(); std::ostringstream params;
json_array_append_new(params, json_string(d->GetFilesystem().c_str())); params << "[\"" << d->GetFilesystem() << "\", " << inode << "]";
json_array_append_new(params, json_integer(inode));
json_array_append_new(params, json_integer(handle));
json_array_append_new(params, json_integer(offset));
json_array_append_new(params, json_integer(length));
return d->Invoke("read", params); return d->Invoke("readdir", params.str());
}
json_t * WebfuseServer::ReadDir(int inode)
{
json_t * params = json_array();
json_array_append_new(params, json_string(d->GetFilesystem().c_str()));
json_array_append_new(params, json_integer(inode));
return d->Invoke("readdir", params);
} }
} }

View File

@ -2,7 +2,6 @@
#define WFP_TEST_UTIL_WEBFUSE_SERVER_HPP #define WFP_TEST_UTIL_WEBFUSE_SERVER_HPP
#include <string> #include <string>
#include <jansson.h>
namespace webfuse_test namespace webfuse_test
{ {
@ -15,12 +14,11 @@ public:
WebfuseServer(bool use_tls = false); WebfuseServer(bool use_tls = false);
~WebfuseServer(); ~WebfuseServer();
std::string const & GetUrl(); std::string const & GetUrl();
json_t * Invoke(std::string const & method, json_t * params); std::string Invoke(std::string const & method, std::string const & params);
json_t * Invoke(std::string const & method, std::string const & params); std::string Lookup(int parent, std::string const & name);
json_t * Lookup(int parent, std::string const & name); std::string Open(int inode, int flags);
json_t * Open(int inode, int flags); std::string Read(int inode, int handle, int offset, int length);
json_t * Read(int inode, int handle, int offset, int length); std::string ReadDir(int inode);
json_t * ReadDir(int inode);
private: private:
class Private; class Private;
Private * d; Private * d;

View File

@ -35,8 +35,8 @@ public:
~Private(); ~Private();
bool IsConnected(); bool IsConnected();
std::string GetUrl() const; std::string GetUrl() const;
void SendMessage(json_t * message); void SendMessage(std::string const & message);
json_t * ReceiveMessage(); std::string ReceiveMessage();
void OnConnected(lws * wsi) override; void OnConnected(lws * wsi) override;
void OnConnectionClosed(lws * wsi) override; void OnConnectionClosed(lws * wsi) override;
void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override; void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override;
@ -123,12 +123,12 @@ bool WsServer::IsConnected()
return d->IsConnected(); return d->IsConnected();
} }
void WsServer::SendMessage(json_t * message) void WsServer::SendMessage(std::string const & message)
{ {
d->SendMessage(message); d->SendMessage(message);
} }
json_t * WsServer::ReceiveMessage() std::string WsServer::ReceiveMessage()
{ {
return d->ReceiveMessage(); return d->ReceiveMessage();
} }
@ -248,7 +248,7 @@ void WsServer::Private::OnWritable(struct lws * wsi)
} }
void WsServer::Private::SendMessage(json_t * message) void WsServer::Private::SendMessage(std::string const & message)
{ {
lws * wsi = nullptr; lws * wsi = nullptr;
@ -257,10 +257,7 @@ void WsServer::Private::SendMessage(json_t * message)
if (nullptr != wsi_) if (nullptr != wsi_)
{ {
char* message_text = json_dumps(message, JSON_COMPACT); writeQueue.push(message);
writeQueue.push(message_text);
json_decref(message);
free(message_text);
wsi = wsi_; wsi = wsi_;
} }
} }
@ -280,19 +277,18 @@ void WsServer::Private::OnMessageReceived(struct lws * wsi, char const * data, s
} }
} }
json_t * WsServer::Private::ReceiveMessage() std::string WsServer::Private::ReceiveMessage()
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
json_t * result = nullptr; std::string message;
if (!recvQueue.empty()) if (!recvQueue.empty())
{ {
std::string const & message_text = recvQueue.front(); message = recvQueue.front();
result = json_loads(message_text.c_str(), JSON_DECODE_ANY, nullptr);
recvQueue.pop(); recvQueue.pop();
} }
return result; return message;
} }
std::string WsServer::Private::GetUrl() const std::string WsServer::Private::GetUrl() const

View File

@ -1,7 +1,6 @@
#ifndef WFP_TEST_UTILS_WS_SERVER_HPP #ifndef WFP_TEST_UTILS_WS_SERVER_HPP
#define WFP_TEST_UTILS_WS_SERVER_HPP #define WFP_TEST_UTILS_WS_SERVER_HPP
#include <jansson.h>
#include <string> #include <string>
namespace webfuse_test namespace webfuse_test
@ -16,8 +15,8 @@ public:
~WsServer(); ~WsServer();
bool IsConnected(); bool IsConnected();
std::string GetUrl() const; std::string GetUrl() const;
void SendMessage(json_t * message); void SendMessage(std::string const & message);
json_t * ReceiveMessage(); std::string ReceiveMessage();
private: private:
class Private; class Private;
Private * d; Private * d;