2020-06-13 16:24:45 +00:00
|
|
|
#include "webfuse/utils/adapter_client.hpp"
|
2020-06-14 08:39:33 +00:00
|
|
|
#include "webfuse/utils/tempdir.hpp"
|
2020-06-13 16:24:45 +00:00
|
|
|
#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)
|
2020-06-14 08:39:33 +00:00
|
|
|
, tempdir("webfuse_adpter_client")
|
2020-06-13 16:24:45 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:39:33 +00:00
|
|
|
std::string GetDir()
|
|
|
|
{
|
|
|
|
return tempdir.path();
|
|
|
|
}
|
|
|
|
|
2020-06-13 16:24:45 +00:00
|
|
|
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:
|
2020-06-14 08:39:33 +00:00
|
|
|
wf_client_add_filesystem(self->client, self->tempdir.path(), "test");
|
2020-06-13 16:24:45 +00:00
|
|
|
break;
|
|
|
|
case Command::shutdown:
|
|
|
|
// fall-through
|
|
|
|
default:
|
|
|
|
is_running = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wf_client * client;
|
|
|
|
std::string url_;
|
|
|
|
Command command;
|
2020-06-14 08:39:33 +00:00
|
|
|
TempDir tempdir;
|
2020-06-13 16:24:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:39:33 +00:00
|
|
|
std::string AdapterClient::GetDir() const
|
|
|
|
{
|
|
|
|
return d->GetDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-13 16:24:45 +00:00
|
|
|
}
|