mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
refactor: merged code structure
This commit is contained in:
149
test/webfuse_provider/utils/adapter_client.cc
Normal file
149
test/webfuse_provider/utils/adapter_client.cc
Normal file
@@ -0,0 +1,149 @@
|
||||
#include "webfuse/utils/adapter_client.hpp"
|
||||
#include "webfuse/utils/tempdir.hpp"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
enum class Command
|
||||
{
|
||||
run,
|
||||
shutdown,
|
||||
connect,
|
||||
disconnect,
|
||||
authenticate,
|
||||
add_filesystem
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class AdapterClient::Private
|
||||
{
|
||||
public:
|
||||
Private(
|
||||
wf_client_callback_fn * callback,
|
||||
void * user_data,
|
||||
std::string const & url)
|
||||
: client(wf_client_create(callback, user_data))
|
||||
, url_(url)
|
||||
, command(Command::run)
|
||||
, tempdir("webfuse_adpter_client")
|
||||
{
|
||||
thread = std::thread(&Run, this);
|
||||
}
|
||||
|
||||
~Private()
|
||||
{
|
||||
ApplyCommand(Command::shutdown);
|
||||
thread.join();
|
||||
wf_client_dispose(client);
|
||||
}
|
||||
|
||||
void ApplyCommand(Command actual_command)
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
command = actual_command;
|
||||
}
|
||||
|
||||
wf_client_interrupt(client);
|
||||
}
|
||||
|
||||
std::string GetDir()
|
||||
{
|
||||
return tempdir.path();
|
||||
}
|
||||
|
||||
private:
|
||||
static void Run(Private * self)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
Command actual_command;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
actual_command = self->command;
|
||||
self->command = Command::run;
|
||||
}
|
||||
|
||||
switch (actual_command)
|
||||
{
|
||||
case Command::run:
|
||||
wf_client_service(self->client);
|
||||
break;
|
||||
case Command::connect:
|
||||
wf_client_connect(self->client, self->url_.c_str());
|
||||
break;
|
||||
case Command::disconnect:
|
||||
wf_client_disconnect(self->client);
|
||||
break;
|
||||
case Command::authenticate:
|
||||
wf_client_authenticate(self->client);
|
||||
break;
|
||||
case Command::add_filesystem:
|
||||
wf_client_add_filesystem(self->client, self->tempdir.path(), "test");
|
||||
break;
|
||||
case Command::shutdown:
|
||||
// fall-through
|
||||
default:
|
||||
is_running = false;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
wf_client * client;
|
||||
std::string url_;
|
||||
Command command;
|
||||
TempDir tempdir;
|
||||
std::thread thread;
|
||||
std::mutex mutex;
|
||||
};
|
||||
|
||||
AdapterClient::AdapterClient(
|
||||
wf_client_callback_fn * callback,
|
||||
void * user_data,
|
||||
std::string const & url)
|
||||
: d(new Private(callback, user_data, url))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
AdapterClient::~AdapterClient()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void AdapterClient::Connect()
|
||||
{
|
||||
d->ApplyCommand(Command::connect);
|
||||
}
|
||||
|
||||
void AdapterClient::Disconnect()
|
||||
{
|
||||
d->ApplyCommand(Command::disconnect);
|
||||
}
|
||||
|
||||
void AdapterClient::Authenticate()
|
||||
{
|
||||
d->ApplyCommand(Command::authenticate);
|
||||
}
|
||||
|
||||
void AdapterClient::AddFileSystem()
|
||||
{
|
||||
d->ApplyCommand(Command::add_filesystem);
|
||||
}
|
||||
|
||||
std::string AdapterClient::GetDir() const
|
||||
{
|
||||
return d->GetDir();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
32
test/webfuse_provider/utils/adapter_client.hpp
Normal file
32
test/webfuse_provider/utils/adapter_client.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef WF_UTILS_ADAPTER_CLIENT_HPP
|
||||
#define WF_UTILS_APAPTER_CLIENT_HPP
|
||||
|
||||
#include "webfuse/adapter/client.h"
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class AdapterClient
|
||||
{
|
||||
AdapterClient(AdapterClient const &) = delete;
|
||||
AdapterClient& operator=(AdapterClient const &) = delete;
|
||||
public:
|
||||
AdapterClient(
|
||||
wf_client_callback_fn * callback,
|
||||
void * user_data,
|
||||
std::string const & url);
|
||||
~AdapterClient();
|
||||
void Connect();
|
||||
void Disconnect();
|
||||
void Authenticate();
|
||||
void AddFileSystem();
|
||||
std::string GetDir() const;
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
38
test/webfuse_provider/utils/file_utils.cc
Normal file
38
test/webfuse_provider/utils/file_utils.cc
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "webfuse_provider/utils/file_utils.hpp"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
bool is_dir(std::string const & path)
|
||||
{
|
||||
struct stat info;
|
||||
int rc = stat(path.c_str(), &info);
|
||||
|
||||
return (0 == rc) && (S_ISDIR(info.st_mode));
|
||||
}
|
||||
|
||||
bool is_symlink(std::string const & path)
|
||||
{
|
||||
struct stat info;
|
||||
int rc = lstat(path.c_str(), &info);
|
||||
|
||||
return (0 == rc) && (S_ISLNK(info.st_mode));
|
||||
}
|
||||
|
||||
bool is_same_path(std::string const & path, std::string const & other)
|
||||
{
|
||||
struct stat info;
|
||||
int rc = stat(path.c_str(), &info);
|
||||
|
||||
struct stat info_other;
|
||||
int rc_other = stat(other.c_str(), &info_other);
|
||||
|
||||
return (0 == rc) && (0 == rc_other) && (info.st_ino == info_other.st_ino);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
17
test/webfuse_provider/utils/file_utils.hpp
Normal file
17
test/webfuse_provider/utils/file_utils.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef WF_TEST_FILE_UTILS_HPP
|
||||
#define WF_TEST_FILE_UTILS_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
bool is_dir(std::string const & path);
|
||||
|
||||
bool is_symlink(std::string const & path);
|
||||
|
||||
bool is_same_path(std::string const & path, std::string const & other);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
16
test/webfuse_provider/utils/jansson_test_environment.cc
Normal file
16
test/webfuse_provider/utils/jansson_test_environment.cc
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <jansson.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class JanssonTestEnvironment: public ::testing::Environment
|
||||
{
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
json_object_seed(0);
|
||||
}
|
||||
};
|
||||
#
|
||||
}
|
||||
113
test/webfuse_provider/utils/path.c
Normal file
113
test/webfuse_provider/utils/path.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "webfuse_provider/utils/path.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WF_PATH_DEFAULT_CAPACITY (8)
|
||||
|
||||
struct wf_path
|
||||
{
|
||||
char * * elements;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
static void
|
||||
wf_path_add(
|
||||
struct wf_path * path,
|
||||
char const * element,
|
||||
size_t element_size)
|
||||
{
|
||||
if (0 < element_size)
|
||||
{
|
||||
if (path->count >= path->capacity)
|
||||
{
|
||||
size_t new_capacity = 2 * path->capacity;
|
||||
size_t new_size = sizeof(char*) * new_capacity;
|
||||
|
||||
char * * elements = realloc(path->elements, new_size);
|
||||
if (NULL != elements)
|
||||
{
|
||||
path->elements = elements;
|
||||
path->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
if (path->count < path->capacity)
|
||||
{
|
||||
path->elements[path->count] = strndup(element, element_size);
|
||||
path->count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct wf_path *
|
||||
wf_path_create(
|
||||
char const * value)
|
||||
{
|
||||
struct wf_path * path = malloc(sizeof(struct wf_path));
|
||||
path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY);
|
||||
path->capacity = WF_PATH_DEFAULT_CAPACITY;
|
||||
path->count = 0;
|
||||
|
||||
char const * remainder = value;
|
||||
char const * pos = strchr(remainder, '/');
|
||||
while (NULL != pos)
|
||||
{
|
||||
wf_path_add(path, remainder, (pos - remainder));
|
||||
remainder = pos + 1;
|
||||
pos = strchr(remainder, '/');
|
||||
}
|
||||
|
||||
wf_path_add(path, remainder, strlen(remainder));
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
void
|
||||
wf_path_dispose(
|
||||
struct wf_path * path)
|
||||
{
|
||||
for(size_t i = 0; i < path->count; i++)
|
||||
{
|
||||
free(path->elements[i]);
|
||||
}
|
||||
|
||||
free(path->elements);
|
||||
free(path);
|
||||
(void) path;
|
||||
}
|
||||
|
||||
size_t
|
||||
wf_path_element_count(
|
||||
struct wf_path * path)
|
||||
{
|
||||
return path->count;
|
||||
}
|
||||
|
||||
char const *
|
||||
wf_path_get_element(
|
||||
struct wf_path * path,
|
||||
size_t i)
|
||||
{
|
||||
char const * result = NULL;
|
||||
if (i < path->count)
|
||||
{
|
||||
result = path->elements[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char const *
|
||||
wf_path_get_filename(
|
||||
struct wf_path * path)
|
||||
{
|
||||
char const * result = NULL;
|
||||
|
||||
if (0 < path->count)
|
||||
{
|
||||
result = path->elements[path->count - 1];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
43
test/webfuse_provider/utils/path.h
Normal file
43
test/webfuse_provider/utils/path.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WF_PATH_H
|
||||
#define WF_PATH_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using ::std::size_t;
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_path;
|
||||
|
||||
extern struct wf_path *
|
||||
wf_path_create(
|
||||
char const * value);
|
||||
|
||||
extern void
|
||||
wf_path_dispose(
|
||||
struct wf_path * path);
|
||||
|
||||
extern size_t
|
||||
wf_path_element_count(
|
||||
struct wf_path * path);
|
||||
|
||||
extern char const *
|
||||
wf_path_get_element(
|
||||
struct wf_path * path,
|
||||
size_t i);
|
||||
|
||||
extern char const *
|
||||
wf_path_get_filename(
|
||||
struct wf_path * path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
421
test/webfuse_provider/utils/static_filesystem.c
Normal file
421
test/webfuse_provider/utils/static_filesystem.c
Normal file
@@ -0,0 +1,421 @@
|
||||
#include "webfuse_provider/utils/static_filesystem.h"
|
||||
#include "webfuse_provider/client_config.h"
|
||||
#include "webfuse_provider/dirbuffer.h"
|
||||
#include "webfuse_provider/operation/error.h"
|
||||
|
||||
#include "webfuse_provider/utils/path.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY (16)
|
||||
#define WFP_STATIC_FILSYSTEM_INODE_ROOT (1)
|
||||
#define WFP_STATIC_FILESYSTEM_MAX_READ_SIZE (4 * 1024)
|
||||
|
||||
struct wfp_static_filesystem_entry
|
||||
{
|
||||
size_t inode;
|
||||
size_t parent;
|
||||
char * name;
|
||||
bool is_file;
|
||||
int mode;
|
||||
size_t size;
|
||||
char * content;
|
||||
wfp_static_filesystem_read_fn * read;
|
||||
wfp_static_filesystem_get_info_fn * get_info;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
struct wfp_static_filesystem
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entries;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
static struct wfp_static_filesystem_entry *
|
||||
wfp_static_filesystem_get_entry(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t inode)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = NULL;
|
||||
|
||||
if ((0 < inode) && (inode <= filesystem->size))
|
||||
{
|
||||
entry = &filesystem->entries[inode - 1];
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static struct wfp_static_filesystem_entry *
|
||||
wfp_static_filesystem_get_entry_by_name(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t parent,
|
||||
char const * name)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = NULL;
|
||||
for(size_t i = 0; i < filesystem->size; i++)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * current = &filesystem->entries[i];
|
||||
if ((parent == current->parent) && (0 == strcmp(name, current->name)))
|
||||
{
|
||||
entry = current;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static struct wfp_static_filesystem_entry *
|
||||
wfp_static_filesystem_add_entry(
|
||||
struct wfp_static_filesystem * filesystem)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = NULL;
|
||||
|
||||
if (filesystem->size >= filesystem->capacity)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entries;
|
||||
|
||||
size_t new_capacity = 2 * filesystem->capacity;
|
||||
size_t new_size = new_capacity * sizeof(struct wfp_static_filesystem_entry);
|
||||
entries = realloc(filesystem->entries, new_size);
|
||||
|
||||
if (NULL != entries)
|
||||
{
|
||||
filesystem->entries = entries;
|
||||
filesystem->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
if (filesystem->size < filesystem->capacity)
|
||||
{
|
||||
entry = &filesystem->entries[filesystem->size];
|
||||
entry->inode = filesystem->size + 1;
|
||||
filesystem->size++;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_entry_read(
|
||||
size_t offset,
|
||||
char * buffer,
|
||||
size_t buffer_size,
|
||||
void * user_data)
|
||||
{
|
||||
size_t result = 0;
|
||||
struct wfp_static_filesystem_entry * entry = user_data;
|
||||
if (offset < entry->size)
|
||||
{
|
||||
size_t remaining = (entry->size - offset);
|
||||
result = (buffer_size < remaining) ? buffer_size : remaining;
|
||||
memcpy(buffer, entry->content, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_static_filesystem_entry_get_info(
|
||||
void * user_data,
|
||||
int * result_mode,
|
||||
size_t * result_size)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = user_data;
|
||||
*result_mode = entry->mode;
|
||||
*result_size = entry->size;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_add_dir(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t parent,
|
||||
char const * name
|
||||
)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
||||
if (NULL == entry)
|
||||
{
|
||||
entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = false;
|
||||
entry->mode = 0555;
|
||||
entry->name = strdup(name);
|
||||
entry->user_data = entry;
|
||||
entry->read = &wfp_static_filesystem_entry_read;
|
||||
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
||||
entry->size = 0;
|
||||
entry->content = NULL;
|
||||
}
|
||||
|
||||
return entry->inode;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_make_parent(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
struct wf_path * path)
|
||||
{
|
||||
size_t result = WFP_STATIC_FILSYSTEM_INODE_ROOT;
|
||||
|
||||
size_t count = wf_path_element_count(path);
|
||||
if (0 < count)
|
||||
{
|
||||
for(size_t i = 0; i < (count - 1); i++)
|
||||
{
|
||||
char const * name = wf_path_get_element(path, i);
|
||||
result = wfp_static_filesystem_add_dir(filesystem, result, name);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_static_filesystem_stat(
|
||||
struct wfp_static_filesystem_entry * entry,
|
||||
struct stat * stat
|
||||
)
|
||||
{
|
||||
memset(stat, 0, sizeof(struct stat));
|
||||
|
||||
int mode;
|
||||
size_t size;
|
||||
entry->get_info(entry->user_data, &mode, &size);
|
||||
|
||||
stat->st_ino = entry->inode;
|
||||
stat->st_size = entry->size;
|
||||
stat->st_mode = entry->mode & 0777;
|
||||
stat->st_mode |= (entry->is_file) ? S_IFREG: S_IFDIR;
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_lookup(
|
||||
struct wfp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
||||
|
||||
if (NULL != entry)
|
||||
{
|
||||
struct stat stat;
|
||||
wfp_static_filesystem_stat(entry, &stat);
|
||||
wfp_respond_lookup(request, &stat);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void wfp_static_filesystem_getattr(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if (NULL != entry)
|
||||
{
|
||||
struct stat stat;
|
||||
wfp_static_filesystem_stat(entry, &stat);
|
||||
wfp_respond_getattr(request, &stat);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_readdir(
|
||||
struct wfp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * dir = wfp_static_filesystem_get_entry(filesystem, directory);
|
||||
|
||||
if ((NULL != dir) && (!dir->is_file))
|
||||
{
|
||||
struct wfp_dirbuffer * buffer = wfp_dirbuffer_create();
|
||||
wfp_dirbuffer_add(buffer, ".", dir->inode);
|
||||
wfp_dirbuffer_add(buffer, "..", dir->inode);
|
||||
|
||||
for(size_t i = 0; i < filesystem->size; i++)
|
||||
{
|
||||
struct wfp_static_filesystem_entry const * entry = &filesystem->entries[i];
|
||||
if (directory == entry->parent)
|
||||
{
|
||||
wfp_dirbuffer_add(buffer, entry->name, entry->inode);
|
||||
}
|
||||
}
|
||||
|
||||
wfp_respond_readdir(request, buffer);
|
||||
wfp_dirbuffer_dispose(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_open(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if ((NULL != entry) && (entry->is_file))
|
||||
{
|
||||
if (O_RDONLY == (flags & O_ACCMODE))
|
||||
{
|
||||
wfp_respond_open(request, 0U);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_ACCESS_DENIED);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_read(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t WF_UNUSED_PARAM(handle),
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if ((NULL != entry) && (entry->is_file))
|
||||
{
|
||||
char buffer[WFP_STATIC_FILESYSTEM_MAX_READ_SIZE];
|
||||
size_t max_size = (length < WFP_STATIC_FILESYSTEM_MAX_READ_SIZE) ? length : WFP_STATIC_FILESYSTEM_MAX_READ_SIZE;
|
||||
|
||||
size_t count = entry->read(offset, buffer, max_size, entry->user_data);
|
||||
wfp_respond_read(request, buffer, count);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct wfp_static_filesystem *
|
||||
wfp_static_filesystem_create(
|
||||
struct wfp_client_config * config)
|
||||
{
|
||||
(void) config;
|
||||
|
||||
struct wfp_static_filesystem * filesystem = malloc(sizeof(struct wfp_static_filesystem));
|
||||
filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY);
|
||||
filesystem->size = 0;
|
||||
filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY;
|
||||
filesystem->user_data = NULL;
|
||||
|
||||
wfp_static_filesystem_add_dir(filesystem, 0, "<root>");
|
||||
|
||||
wfp_client_config_set_userdata(config, filesystem);
|
||||
wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup);
|
||||
wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr);
|
||||
wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir);
|
||||
wfp_client_config_set_onopen(config, &wfp_static_filesystem_open);
|
||||
wfp_client_config_set_onread(config, &wfp_static_filesystem_read);
|
||||
|
||||
return filesystem;
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_dispose(
|
||||
struct wfp_static_filesystem * filesystem)
|
||||
{
|
||||
for(size_t i = 0; i < filesystem->size; i++)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = &filesystem->entries[i];
|
||||
free(entry->name);
|
||||
free(entry->content);
|
||||
}
|
||||
|
||||
free(filesystem->entries);
|
||||
free(filesystem);
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content,
|
||||
size_t length)
|
||||
{
|
||||
struct wf_path * path_ = wf_path_create(path);
|
||||
if (NULL != path_)
|
||||
{
|
||||
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = true;
|
||||
entry->name = strdup(wf_path_get_filename(path_));
|
||||
entry->mode = mode;
|
||||
entry->size = length;
|
||||
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
||||
entry->read = &wfp_static_filesystem_entry_read;
|
||||
entry->user_data = entry;
|
||||
|
||||
entry->content = malloc(length);
|
||||
memcpy(entry->content, content, length);
|
||||
|
||||
wf_path_dispose(path_);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add_text(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content)
|
||||
{
|
||||
size_t length = strlen(content);
|
||||
wfp_static_filesystem_add(filesystem, path, mode, content, length);
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_set_user_data(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
void * user_data)
|
||||
{
|
||||
filesystem->user_data = user_data;
|
||||
}
|
||||
|
||||
void *
|
||||
wfp_static_filesystem_get_user_data(
|
||||
struct wfp_static_filesystem * filesystem)
|
||||
{
|
||||
return filesystem->user_data;
|
||||
}
|
||||
72
test/webfuse_provider/utils/static_filesystem.h
Normal file
72
test/webfuse_provider/utils/static_filesystem.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef WFP_STATIC_FILESYSTEM_H
|
||||
#define WFP_STATIC_FILESYSTEM_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using ::std::size_t;
|
||||
#endif
|
||||
|
||||
#include <webfuse_provider/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_client_config;
|
||||
|
||||
struct wfp_static_filesystem;
|
||||
|
||||
typedef size_t
|
||||
wfp_static_filesystem_read_fn(
|
||||
size_t offset,
|
||||
char * buffer,
|
||||
size_t buffer_size,
|
||||
void * user_data);
|
||||
|
||||
typedef void
|
||||
wfp_static_filesystem_get_info_fn(
|
||||
void * user_data,
|
||||
int * result_mode,
|
||||
size_t * result_size);
|
||||
|
||||
|
||||
extern WFP_API struct wfp_static_filesystem *
|
||||
wfp_static_filesystem_create(
|
||||
struct wfp_client_config * config);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_dispose(
|
||||
struct wfp_static_filesystem * filesystem);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content,
|
||||
size_t length);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add_text(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content);
|
||||
|
||||
extern void
|
||||
wfp_static_filesystem_set_user_data(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
void * user_data);
|
||||
|
||||
extern void *
|
||||
wfp_static_filesystem_get_user_data(
|
||||
struct wfp_static_filesystem * filesystem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
33
test/webfuse_provider/utils/tempdir.cc
Normal file
33
test/webfuse_provider/utils/tempdir.cc
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "webfuse_provider/utils/tempdir.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
TempDir::TempDir(char const * prefix)
|
||||
{
|
||||
asprintf(&path_, "/tmp/%s_XXXXXX", prefix);
|
||||
char * result = mkdtemp(path_);
|
||||
if (NULL == result)
|
||||
{
|
||||
throw std::runtime_error("unable to create temp dir");
|
||||
}
|
||||
}
|
||||
|
||||
TempDir::~TempDir()
|
||||
{
|
||||
rmdir(path_);
|
||||
free(path_);
|
||||
}
|
||||
|
||||
char const * TempDir::path()
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
21
test/webfuse_provider/utils/tempdir.hpp
Normal file
21
test/webfuse_provider/utils/tempdir.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef WF_TEST_TEMPDIR_HPP
|
||||
#define WF_TEST_TEMPDIR_HPP
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class TempDir
|
||||
{
|
||||
TempDir(TempDir const &) = delete;
|
||||
TempDir & operator=(TempDir const &) = delete;
|
||||
public:
|
||||
explicit TempDir(char const * prefix);
|
||||
~TempDir();
|
||||
char const * path();
|
||||
private:
|
||||
char * path_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
58
test/webfuse_provider/utils/timeout_watcher.cc
Normal file
58
test/webfuse_provider/utils/timeout_watcher.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "webfuse_provider/utils/timeout_watcher.hpp"
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
|
||||
using std::chrono::milliseconds;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::steady_clock;
|
||||
|
||||
namespace
|
||||
{
|
||||
milliseconds now()
|
||||
{
|
||||
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch());
|
||||
}
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
TimeoutWatcher::TimeoutWatcher(milliseconds timeout)
|
||||
: startedAt(now())
|
||||
, timeout_(timeout)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TimeoutWatcher::~TimeoutWatcher()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool TimeoutWatcher::isTimeout()
|
||||
{
|
||||
return (now() - startedAt) > timeout_;
|
||||
}
|
||||
|
||||
void TimeoutWatcher::check()
|
||||
{
|
||||
if (isTimeout())
|
||||
{
|
||||
throw std::runtime_error("timeout");
|
||||
}
|
||||
}
|
||||
|
||||
bool TimeoutWatcher::waitUntil(std::function<bool()> predicate)
|
||||
{
|
||||
bool result = predicate();
|
||||
while ((!result) && (!isTimeout()))
|
||||
{
|
||||
std::this_thread::yield();
|
||||
result = predicate();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
test/webfuse_provider/utils/timeout_watcher.hpp
Normal file
28
test/webfuse_provider/utils/timeout_watcher.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef WF_TEST_TIMEOUT_WATCHER_HPP
|
||||
#define WF_TEST_TIMEOUT_WATCHER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class TimeoutWatcher final
|
||||
{
|
||||
TimeoutWatcher(TimeoutWatcher const & other) = delete;
|
||||
TimeoutWatcher& operator=(TimeoutWatcher const & other) = delete;
|
||||
public:
|
||||
explicit TimeoutWatcher(std::chrono::milliseconds timeout);
|
||||
~TimeoutWatcher();
|
||||
bool isTimeout();
|
||||
void check();
|
||||
bool waitUntil(std::function<bool()> predicate);
|
||||
private:
|
||||
std::chrono::milliseconds startedAt;
|
||||
std::chrono::milliseconds timeout_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
132
test/webfuse_provider/utils/wrap.hpp
Normal file
132
test/webfuse_provider/utils/wrap.hpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#ifndef WF_WRAP_HPP
|
||||
#define WF_WRAP_HPP
|
||||
|
||||
#define WF_WRAP_FUNC0( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME () \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC1( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC2( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC4( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC5( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC6( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5, ARG6_TYPE arg6) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5, arg6); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5, arg6); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#define WF_WRAP_VFUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, va_list); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, va_list args) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, args); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_VFUNC5( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, va_list); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5, va_list args) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5, args); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
306
test/webfuse_provider/utils/ws_server.cc
Normal file
306
test/webfuse_provider/utils/ws_server.cc
Normal file
@@ -0,0 +1,306 @@
|
||||
#include "webfuse_provider/utils/ws_server.h"
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <queue>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class IServer
|
||||
{
|
||||
public:
|
||||
virtual ~IServer() = default;
|
||||
virtual void OnConnected(lws * wsi) = 0;
|
||||
virtual void OnConnectionClosed(lws * wsi) = 0;
|
||||
virtual void OnMessageReceived(struct lws * wsi, char const * data, size_t length) = 0;
|
||||
virtual void OnWritable(struct lws * wsi) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class WsServer::Private : IServer
|
||||
{
|
||||
Private(Private const &) = delete;
|
||||
Private & operator=(Private const &) = delete;
|
||||
public:
|
||||
Private(std::string const & protocol, int port);
|
||||
~Private();
|
||||
bool IsConnected();
|
||||
std::string GetUrl() const;
|
||||
void SendMessage(json_t * message);
|
||||
json_t * ReceiveMessage();
|
||||
void OnConnected(lws * wsi) override;
|
||||
void OnConnectionClosed(lws * wsi) override;
|
||||
void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override;
|
||||
void OnWritable(struct lws * wsi) override;
|
||||
|
||||
private:
|
||||
static void run(Private * self);
|
||||
std::string protocol_;
|
||||
int port_;
|
||||
bool is_connected;
|
||||
bool is_shutdown_requested;
|
||||
lws * wsi_;
|
||||
lws_context * ws_context;
|
||||
lws_protocols ws_protocols[2];
|
||||
lws_context_creation_info info;
|
||||
std::thread context;
|
||||
std::mutex mutex;
|
||||
std::queue<std::string> writeQueue;
|
||||
std::queue<std::string> recvQueue;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
static int wf_test_utils_ws_server_callback(
|
||||
struct lws * wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void * user,
|
||||
void * in,
|
||||
size_t len)
|
||||
{
|
||||
int result = 0;
|
||||
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
||||
auto * server = reinterpret_cast<IServer*>(nullptr != ws_protocol ? ws_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:
|
||||
{
|
||||
auto * data = reinterpret_cast<char const *>(in);
|
||||
server->OnMessageReceived(wsi, data, len);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
server->OnWritable(wsi);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
WsServer::WsServer(std::string const & protocol, int port)
|
||||
: d(new Private(protocol, port))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WsServer::~WsServer()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool WsServer::IsConnected()
|
||||
{
|
||||
return d->IsConnected();
|
||||
}
|
||||
|
||||
void WsServer::SendMessage(json_t * message)
|
||||
{
|
||||
d->SendMessage(message);
|
||||
}
|
||||
|
||||
json_t * WsServer::ReceiveMessage()
|
||||
{
|
||||
return d->ReceiveMessage();
|
||||
}
|
||||
|
||||
std::string WsServer::GetUrl() const
|
||||
{
|
||||
return d->GetUrl();
|
||||
}
|
||||
|
||||
|
||||
WsServer::Private::Private(std::string const & protocol, int port)
|
||||
: protocol_(protocol)
|
||||
, port_(port)
|
||||
, is_connected(false)
|
||||
, is_shutdown_requested(false)
|
||||
, wsi_(nullptr)
|
||||
{
|
||||
wf_lwslog_disable();
|
||||
IServer * server = this;
|
||||
memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2 );
|
||||
|
||||
ws_protocols[0].name = protocol_.c_str();
|
||||
ws_protocols[0].callback = &wf_test_utils_ws_server_callback;
|
||||
ws_protocols[0].per_session_data_size = 0;
|
||||
ws_protocols[0].user = reinterpret_cast<void*>(server);
|
||||
|
||||
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
||||
info.port = port;
|
||||
info.mounts = NULL;
|
||||
info.protocols =ws_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;
|
||||
|
||||
ws_context = lws_create_context(&info);
|
||||
|
||||
struct lws_vhost * vhost = lws_create_vhost(ws_context, &info);
|
||||
port_ = lws_get_vhost_port(vhost);
|
||||
|
||||
context = std::thread(&run, this);
|
||||
}
|
||||
|
||||
WsServer::Private::~Private()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_shutdown_requested = true;
|
||||
}
|
||||
|
||||
lws_cancel_service(ws_context);
|
||||
context.join();
|
||||
lws_context_destroy(ws_context);
|
||||
}
|
||||
|
||||
void WsServer::Private::run(Private * self)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
lws_service(self->ws_context, 0);
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
is_running = !self->is_shutdown_requested;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WsServer::Private::IsConnected()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
return is_connected;
|
||||
}
|
||||
|
||||
void WsServer::Private::OnConnected(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_connected = true;
|
||||
wsi_ = wsi;
|
||||
}
|
||||
|
||||
void WsServer::Private::OnConnectionClosed(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (wsi == wsi_)
|
||||
{
|
||||
is_connected = false;
|
||||
wsi_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer::Private::OnWritable(struct lws * wsi)
|
||||
{
|
||||
bool notify = false;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (!writeQueue.empty())
|
||||
{
|
||||
std::string const & message = writeQueue.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;
|
||||
|
||||
writeQueue.pop();
|
||||
notify = !writeQueue.empty();
|
||||
}
|
||||
}
|
||||
|
||||
if (notify)
|
||||
{
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void WsServer::Private::SendMessage(json_t * message)
|
||||
{
|
||||
lws * wsi = nullptr;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (nullptr != wsi_)
|
||||
{
|
||||
char* message_text = json_dumps(message, JSON_COMPACT);
|
||||
writeQueue.push(message_text);
|
||||
json_decref(message);
|
||||
free(message_text);
|
||||
wsi = wsi_;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullptr != wsi)
|
||||
{
|
||||
lws_callback_on_writable(wsi_);
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (wsi == wsi_)
|
||||
{
|
||||
recvQueue.push(std::string(data, length));
|
||||
}
|
||||
}
|
||||
|
||||
json_t * WsServer::Private::ReceiveMessage()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
json_t * result = nullptr;
|
||||
if (!recvQueue.empty())
|
||||
{
|
||||
std::string const & message_text = recvQueue.front();
|
||||
result = json_loads(message_text.c_str(), JSON_DECODE_ANY, nullptr);
|
||||
recvQueue.pop();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string WsServer::Private::GetUrl() const
|
||||
{
|
||||
std::ostringstream stream;
|
||||
stream << "ws://localhost:" << port_ << "/";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
29
test/webfuse_provider/utils/ws_server.h
Normal file
29
test/webfuse_provider/utils/ws_server.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WF_TEST_UTILS_WS_SERVER_HPP
|
||||
#define WF_TEST_UTILS_WS_SERVER_HPP
|
||||
|
||||
#include <jansson.h>
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class WsServer
|
||||
{
|
||||
WsServer(WsServer const &) = delete;
|
||||
WsServer & operator=(WsServer const &) = delete;
|
||||
public:
|
||||
WsServer(std::string const & protocol, int port = 0);
|
||||
~WsServer();
|
||||
bool IsConnected();
|
||||
std::string GetUrl() const;
|
||||
void SendMessage(json_t * message);
|
||||
json_t * ReceiveMessage();
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
331
test/webfuse_provider/utils/ws_server2.cc
Normal file
331
test/webfuse_provider/utils/ws_server2.cc
Normal file
@@ -0,0 +1,331 @@
|
||||
#include "webfuse_provider/utils/ws_server2.hpp"
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <queue>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class IServer
|
||||
{
|
||||
public:
|
||||
virtual ~IServer() = default;
|
||||
virtual void OnConnected(lws * wsi) = 0;
|
||||
virtual void OnConnectionClosed(lws * wsi) = 0;
|
||||
virtual void OnMessageReceived(struct lws * wsi, char const * data, size_t length) = 0;
|
||||
virtual void OnWritable(struct lws * wsi) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
static int wf_test_utils_ws_server_callback(
|
||||
struct lws * wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void * user,
|
||||
void * in,
|
||||
size_t len)
|
||||
{
|
||||
int result = 0;
|
||||
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
||||
auto * server = reinterpret_cast<IServer*>(nullptr != ws_protocol ? ws_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:
|
||||
{
|
||||
auto * data = reinterpret_cast<char const *>(in);
|
||||
server->OnMessageReceived(wsi, data, len);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
server->OnWritable(wsi);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class WsServer2::Private : public IServer
|
||||
{
|
||||
Private(Private const &) = delete;
|
||||
Private & operator=(Private const &) = delete;
|
||||
public:
|
||||
Private(IIvokationHandler & handler, std::string const & protocol, int port, bool enable_tls);
|
||||
~Private();
|
||||
bool IsConnected();
|
||||
std::string const & GetUrl() const;
|
||||
void OnConnected(lws * wsi) override;
|
||||
void OnConnectionClosed(lws * wsi) override;
|
||||
void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override;
|
||||
void OnWritable(struct lws * wsi) override;
|
||||
|
||||
void SendMessage(char const * message);
|
||||
void SendMessage(json_t * message);
|
||||
private:
|
||||
static void Run(Private * self);
|
||||
|
||||
IIvokationHandler & handler_;
|
||||
std::string protocol_;
|
||||
bool is_connected;
|
||||
bool is_shutdown_requested;
|
||||
lws * wsi_;
|
||||
lws_context * ws_context;
|
||||
lws_protocols ws_protocols[2];
|
||||
lws_context_creation_info info;
|
||||
std::string url;
|
||||
std::thread context;
|
||||
std::mutex mutex;
|
||||
std::queue<std::string> writeQueue;
|
||||
};
|
||||
|
||||
WsServer2::WsServer2(
|
||||
IIvokationHandler& handler,
|
||||
std::string const & protocol,
|
||||
int port,
|
||||
bool enable_tls)
|
||||
: d(new WsServer2::Private(handler, protocol, port, enable_tls))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WsServer2::~WsServer2()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool WsServer2::IsConnected()
|
||||
{
|
||||
return d->IsConnected();
|
||||
}
|
||||
|
||||
std::string const & WsServer2::GetUrl() const
|
||||
{
|
||||
return d->GetUrl();
|
||||
}
|
||||
|
||||
void WsServer2::SendMessage(char const * message)
|
||||
{
|
||||
d->SendMessage(message);
|
||||
}
|
||||
|
||||
void WsServer2::SendMessage(json_t * message)
|
||||
{
|
||||
d->SendMessage(message);
|
||||
}
|
||||
|
||||
|
||||
WsServer2::Private::Private(
|
||||
IIvokationHandler & handler,
|
||||
std::string const & protocol,
|
||||
int port,
|
||||
bool enable_tls)
|
||||
: handler_(handler)
|
||||
, protocol_(protocol)
|
||||
, is_connected(false)
|
||||
, is_shutdown_requested(false)
|
||||
, wsi_(nullptr)
|
||||
{
|
||||
wf_lwslog_disable();
|
||||
IServer * server = this;
|
||||
memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2 );
|
||||
|
||||
ws_protocols[0].name = protocol_.c_str();
|
||||
ws_protocols[0].callback = &wf_test_utils_ws_server_callback;
|
||||
ws_protocols[0].per_session_data_size = 0;
|
||||
ws_protocols[0].user = reinterpret_cast<void*>(server);
|
||||
|
||||
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
||||
info.port = port;
|
||||
info.mounts = NULL;
|
||||
info.protocols =ws_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;
|
||||
|
||||
if (enable_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";
|
||||
}
|
||||
|
||||
ws_context = lws_create_context(&info);
|
||||
|
||||
std::ostringstream stream;
|
||||
struct lws_vhost * vhost = lws_create_vhost(ws_context, &info);
|
||||
stream << (enable_tls ? "wss://" : "ws://")
|
||||
<< "localhost:" << lws_get_vhost_port(vhost) << "/";
|
||||
url = stream.str();
|
||||
|
||||
context = std::thread(&Run, this);
|
||||
}
|
||||
|
||||
WsServer2::Private::~Private()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_shutdown_requested = true;
|
||||
}
|
||||
|
||||
lws_cancel_service(ws_context);
|
||||
context.join();
|
||||
lws_context_destroy(ws_context);
|
||||
}
|
||||
|
||||
void WsServer2::Private::Run(Private * self)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
lws_service(self->ws_context, 0);
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
is_running = !self->is_shutdown_requested;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WsServer2::Private::IsConnected()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
return is_connected;
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnConnected(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_connected = true;
|
||||
wsi_ = wsi;
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnConnectionClosed(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (wsi == wsi_)
|
||||
{
|
||||
is_connected = false;
|
||||
wsi_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnWritable(struct lws * wsi)
|
||||
{
|
||||
bool notify = false;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (!writeQueue.empty())
|
||||
{
|
||||
std::string const & message = writeQueue.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;
|
||||
|
||||
writeQueue.pop();
|
||||
notify = !writeQueue.empty();
|
||||
}
|
||||
}
|
||||
|
||||
if (notify)
|
||||
{
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer2::Private::SendMessage(char const * message)
|
||||
{
|
||||
lws * wsi = nullptr;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (nullptr != wsi_)
|
||||
{
|
||||
writeQueue.push(message);
|
||||
wsi = wsi_;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullptr != wsi)
|
||||
{
|
||||
lws_callback_on_writable(wsi_);
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer2::Private::SendMessage(json_t * message)
|
||||
{
|
||||
char* message_text = json_dumps(message, JSON_COMPACT);
|
||||
SendMessage(message_text);
|
||||
json_decref(message);
|
||||
free(message_text);
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length)
|
||||
{
|
||||
(void) wsi;
|
||||
|
||||
json_t * request = json_loadb(data, length, JSON_DECODE_ANY, nullptr);
|
||||
json_t * method = json_object_get(request, "method");
|
||||
json_t * params = json_object_get(request, "params");
|
||||
json_t * id = json_object_get(request, "id");
|
||||
|
||||
if (json_is_string(method) && json_is_array(params) && json_is_integer(id))
|
||||
{
|
||||
json_t * response = json_object();
|
||||
|
||||
try
|
||||
{
|
||||
std::string result_text = handler_.Invoke(json_string_value(method), params);
|
||||
json_t * result = json_loads(result_text.c_str(), JSON_DECODE_ANY, nullptr);
|
||||
json_object_set_new(response, "result", result);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(1));
|
||||
json_object_set_new(response, "error", error);
|
||||
}
|
||||
|
||||
json_object_set(response, "id", id);
|
||||
SendMessage(response);
|
||||
}
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
std::string const & WsServer2::Private::GetUrl() const
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
39
test/webfuse_provider/utils/ws_server2.hpp
Normal file
39
test/webfuse_provider/utils/ws_server2.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef WF_TEST_UTILS_WS_SERVER2_HPP
|
||||
#define WF_TEST_UTILS_WS_SERVER2_HPP
|
||||
|
||||
#include <jansson.h>
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class IIvokationHandler
|
||||
{
|
||||
public:
|
||||
virtual ~IIvokationHandler() = default;
|
||||
virtual std::string Invoke(char const * method, json_t * params) = 0;
|
||||
};
|
||||
|
||||
class WsServer2
|
||||
{
|
||||
WsServer2(WsServer2 const &) = delete;
|
||||
WsServer2 & operator=(WsServer2 const & ) = delete;
|
||||
public:
|
||||
WsServer2(
|
||||
IIvokationHandler& handler,
|
||||
std::string const & protocol,
|
||||
int port = 0,
|
||||
bool enable_tls = false);
|
||||
virtual ~WsServer2();
|
||||
bool IsConnected();
|
||||
std::string const & GetUrl() const;
|
||||
void SendMessage(char const * message);
|
||||
void SendMessage(json_t * message);
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user