2019-02-25 19:17:13 +00:00
|
|
|
#include "wsfs/provider/client.h"
|
2019-02-16 15:08:17 +00:00
|
|
|
|
2019-02-17 13:31:04 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2019-02-18 22:30:39 +00:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2019-02-25 19:17:13 +00:00
|
|
|
#include "wsfs/provider/provider.h"
|
|
|
|
#include "wsfs/provider/client_protocol_intern.h"
|
|
|
|
#include "wsfs/provider/url.h"
|
2019-02-18 22:30:39 +00:00
|
|
|
|
2019-02-23 15:20:30 +00:00
|
|
|
#define WSFSP_PROTOCOL ("fs")
|
2019-02-18 22:30:39 +00:00
|
|
|
#define WSFSP_DISABLE_LWS_LOG 0
|
|
|
|
#define WSFSP_CLIENT_PROTOCOL_COUNT 2
|
|
|
|
#define WSFSP_CLIENT_TIMEOUT (1 * 1000)
|
2019-02-17 13:31:04 +00:00
|
|
|
|
|
|
|
struct wsfsp_client
|
|
|
|
{
|
|
|
|
volatile bool is_running;
|
|
|
|
struct wsfsp_provider provider;
|
2019-02-18 22:30:39 +00:00
|
|
|
struct wsfsp_client_protocol protocol;
|
|
|
|
struct lws_context_creation_info info;
|
|
|
|
struct lws_protocols protocols[WSFSP_CLIENT_PROTOCOL_COUNT];
|
|
|
|
struct lws_context * context;
|
2019-02-17 13:31:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct wsfsp_client * wsfsp_client_create(
|
|
|
|
struct wsfsp_provider * provider,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-02-18 22:30:39 +00:00
|
|
|
lws_set_log_level(WSFSP_DISABLE_LWS_LOG, NULL);
|
|
|
|
|
2019-02-17 13:31:04 +00:00
|
|
|
struct wsfsp_client * client = malloc(sizeof(struct wsfsp_client));
|
|
|
|
if (NULL != client)
|
|
|
|
{
|
|
|
|
client->is_running = true;
|
2019-02-18 22:30:39 +00:00
|
|
|
wsfsp_client_protocol_init(&client->protocol, provider, user_data);
|
|
|
|
|
|
|
|
memset(client->protocols, 0, sizeof(struct lws_protocols) * WSFSP_CLIENT_PROTOCOL_COUNT);
|
2019-02-24 17:03:22 +00:00
|
|
|
client->protocols[0].name = "fs";
|
2019-02-18 22:30:39 +00:00
|
|
|
wsfsp_client_protocol_init_lws(&client->protocol, &client->protocols[0]);
|
|
|
|
|
|
|
|
memset(&client->info, 0, sizeof(struct lws_context_creation_info));
|
|
|
|
client->info.port = CONTEXT_PORT_NO_LISTEN;
|
|
|
|
client->info.protocols = client->protocols;
|
|
|
|
client->info.uid = -1;
|
|
|
|
client->info.gid = -1;
|
|
|
|
|
|
|
|
client->context = lws_create_context(&client->info);
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_dispose(
|
|
|
|
struct wsfsp_client * client)
|
|
|
|
{
|
2019-02-18 22:30:39 +00:00
|
|
|
lws_context_destroy(client->context);
|
|
|
|
wsfsp_client_protocol_cleanup(&client->protocol);
|
2019-02-17 13:31:04 +00:00
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_connect(
|
|
|
|
struct wsfsp_client * client,
|
|
|
|
char const * url)
|
|
|
|
{
|
2019-02-23 15:20:30 +00:00
|
|
|
struct wsfsp_url url_data;
|
|
|
|
bool const success = wsfsp_url_init(&url_data, url);
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
struct lws_client_connect_info info;
|
|
|
|
memset(&info, 0, sizeof(struct lws_client_connect_info));
|
|
|
|
info.context = client->context;
|
|
|
|
info.port = url_data.port;
|
|
|
|
info.address = url_data.host;
|
|
|
|
info.path = url_data.path;
|
|
|
|
info.host = info.address;
|
|
|
|
info.origin = info.address;
|
|
|
|
info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0;
|
|
|
|
info.protocol = WSFSP_PROTOCOL;
|
2019-02-26 14:42:59 +00:00
|
|
|
info.pwsi = &client->protocol.wsi;
|
2019-02-23 15:20:30 +00:00
|
|
|
|
|
|
|
lws_client_connect_via_info(&info);
|
|
|
|
|
|
|
|
wsfsp_url_cleanup(&url_data);
|
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_disconnect(
|
|
|
|
struct wsfsp_client * client)
|
|
|
|
{
|
|
|
|
(void) client;
|
|
|
|
|
|
|
|
// ToDo: implement me
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_settimeout(
|
|
|
|
struct wsfsp_client * client,
|
|
|
|
unsigned int timepoint)
|
|
|
|
{
|
|
|
|
(void) client;
|
|
|
|
(void) timepoint;
|
|
|
|
|
|
|
|
// ToDo: implement me
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wsfsp_client_run(
|
|
|
|
struct wsfsp_client * client)
|
|
|
|
{
|
|
|
|
while (client->is_running)
|
|
|
|
{
|
2019-02-18 22:30:39 +00:00
|
|
|
lws_service(client->context, WSFSP_CLIENT_TIMEOUT);
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_shutdown(
|
|
|
|
struct wsfsp_client * client)
|
|
|
|
{
|
|
|
|
client->is_running = false;
|
|
|
|
}
|
|
|
|
|