2020-02-20 16:15:13 +00:00
|
|
|
#include "webfuse/tests/integration/provider.hpp"
|
2019-05-19 12:33:42 +00:00
|
|
|
#include "webfuse_provider.h"
|
|
|
|
#include "webfuse/provider/impl/client.h"
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
2020-03-01 16:23:59 +00:00
|
|
|
#include <chrono>
|
2019-05-19 12:33:42 +00:00
|
|
|
#include <string>
|
2020-02-20 17:54:29 +00:00
|
|
|
#include "webfuse/utils/static_filesystem.h"
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-01 16:23:59 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2020-06-06 08:59:06 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
enum class ConnectionState
|
|
|
|
{
|
|
|
|
disconnected,
|
|
|
|
connected,
|
|
|
|
connecting
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
|
|
|
void
|
|
|
|
webfuse_test_provider_onconnected(
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
auto * fs = reinterpret_cast<wfp_static_filesystem*>(user_data);
|
|
|
|
auto * connection_state = reinterpret_cast<ConnectionState*>(wfp_static_filesystem_get_user_data(fs));
|
|
|
|
*connection_state = ConnectionState::connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
webfuse_test_provider_ondisconnected(
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
auto * fs = reinterpret_cast<wfp_static_filesystem*>(user_data);
|
|
|
|
auto * connection_state = reinterpret_cast<ConnectionState*>(wfp_static_filesystem_get_user_data(fs));
|
|
|
|
*connection_state = ConnectionState::disconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:33:42 +00:00
|
|
|
namespace webfuse_test
|
|
|
|
{
|
|
|
|
|
|
|
|
class Provider::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Private(char const * url)
|
|
|
|
: is_shutdown_requested(false)
|
|
|
|
{
|
2020-06-06 08:59:06 +00:00
|
|
|
ConnectionState connection_state = ConnectionState::connecting;
|
|
|
|
|
2019-05-19 12:33:42 +00:00
|
|
|
config = wfp_client_config_create();
|
2020-04-01 19:42:50 +00:00
|
|
|
wfp_client_config_set_certpath(config, "client-cert.pem");
|
|
|
|
wfp_client_config_set_keypath(config, "client-key.pem");
|
|
|
|
wfp_client_config_set_ca_filepath(config, "server-cert.pem");
|
2020-06-06 08:59:06 +00:00
|
|
|
wfp_client_config_set_onconnected(config, &webfuse_test_provider_onconnected);
|
|
|
|
wfp_client_config_set_ondisconnected(config, &webfuse_test_provider_ondisconnected);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
fs = wfp_static_filesystem_create(config);
|
2020-06-06 08:59:06 +00:00
|
|
|
wfp_static_filesystem_set_user_data(fs, reinterpret_cast<void*>(&connection_state));
|
2019-05-19 12:33:42 +00:00
|
|
|
wfp_static_filesystem_add_text(fs, "hello.txt", 0444, "Hello, World");
|
|
|
|
|
|
|
|
client = wfp_client_create(config);
|
|
|
|
wfp_client_connect(client, url);
|
2020-06-06 08:59:06 +00:00
|
|
|
while (ConnectionState::connecting == connection_state)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-04-06 18:44:18 +00:00
|
|
|
wfp_client_service(client);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
2020-06-06 08:59:06 +00:00
|
|
|
|
|
|
|
if (ConnectionState::connected == connection_state)
|
|
|
|
{
|
|
|
|
thread = std::thread(Run, this);
|
|
|
|
std::this_thread::sleep_for(200ms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_client_dispose(client);
|
|
|
|
|
|
|
|
wfp_static_filesystem_dispose(fs);
|
|
|
|
wfp_client_config_dispose(config);
|
|
|
|
|
|
|
|
throw std::runtime_error("unable to connect");
|
|
|
|
}
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
RequestShutdown();
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
wfp_client_dispose(client);
|
|
|
|
|
|
|
|
wfp_static_filesystem_dispose(fs);
|
|
|
|
wfp_client_config_dispose(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShutdownRequested()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(shutdown_lock);
|
|
|
|
return is_shutdown_requested;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void RequestShutdown()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(shutdown_lock);
|
|
|
|
is_shutdown_requested = true;
|
2020-03-07 16:13:16 +00:00
|
|
|
wfp_client_interrupt(client);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Run(Provider::Private * context)
|
|
|
|
{
|
|
|
|
while (!context->IsShutdownRequested())
|
|
|
|
{
|
2020-04-06 18:44:18 +00:00
|
|
|
wfp_client_service(context->client);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
2020-04-07 18:37:50 +00:00
|
|
|
|
|
|
|
wfp_client_disconnect(context->client);
|
|
|
|
while (wfp_impl_client_is_connected(context->client))
|
|
|
|
{
|
|
|
|
wfp_client_service(context->client);
|
|
|
|
}
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex shutdown_lock;
|
|
|
|
std::thread thread;
|
|
|
|
bool is_shutdown_requested;
|
|
|
|
|
|
|
|
wfp_client_config * config;
|
|
|
|
wfp_static_filesystem * fs;
|
|
|
|
public:
|
|
|
|
wfp_client * client;
|
|
|
|
};
|
|
|
|
|
|
|
|
Provider::Provider(char const * url)
|
|
|
|
: d(new Provider::Private(url))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Provider::~Provider()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|