1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

adds session and session_manager

This commit is contained in:
Falk Werner 2019-03-22 21:20:35 +01:00
parent 988af6c2b9
commit fb465bf9a9
7 changed files with 172 additions and 24 deletions

View File

@ -60,6 +60,8 @@ set(WSFS_ADAPTER_SOURCES
lib/wsfs/adapter/server.c lib/wsfs/adapter/server.c
lib/wsfs/adapter/server_config.c lib/wsfs/adapter/server_config.c
lib/wsfs/adapter/server_protocol.c lib/wsfs/adapter/server_protocol.c
lib/wsfs/adapter/session.c
lib/wsfs/adapter/session_manager.c
lib/wsfs/adapter/authenticator.c lib/wsfs/adapter/authenticator.c
lib/wsfs/adapter/authenticators.c lib/wsfs/adapter/authenticators.c
lib/wsfs/adapter/credentials.c lib/wsfs/adapter/credentials.c

View File

@ -19,6 +19,7 @@ static int wsfs_server_protocol_callback(
struct wsfs_server_protocol * protocol = ws_protocol->user; struct wsfs_server_protocol * protocol = ws_protocol->user;
wsfs_timeout_manager_check(&protocol->timeout_manager); wsfs_timeout_manager_check(&protocol->timeout_manager);
struct wsfs_session * session = wsfs_session_manager_get(&protocol->session_manager, wsi);
switch (reason) switch (reason)
{ {
@ -33,28 +34,24 @@ static int wsfs_server_protocol_callback(
} }
break; break;
case LWS_CALLBACK_ESTABLISHED: case LWS_CALLBACK_ESTABLISHED:
if (NULL == protocol->wsi)
{ {
protocol->wsi = wsi; bool is_authenticated = wsfs_authenticators_authenticate(&protocol->authenticators, NULL);
protocol->is_authenticated = wsfs_authenticators_authenticate(&protocol->authenticators, NULL); wsfs_session_manager_add(&protocol->session_manager, wsi, is_authenticated);
} }
break; break;
case LWS_CALLBACK_CLOSED: case LWS_CALLBACK_CLOSED:
if (wsi == protocol->wsi)
{ {
protocol->wsi = NULL; wsfs_session_manager_remove(&protocol->session_manager, wsi);
protocol->is_authenticated = false;
wsfs_message_queue_cleanup(&protocol->queue);
} }
break; break;
case LWS_CALLBACK_SERVER_WRITEABLE: case LWS_CALLBACK_SERVER_WRITEABLE:
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue))) if ((NULL != session) && (!wsfs_message_queue_empty(&session->queue)))
{ {
struct wsfs_message * message = wsfs_message_queue_pop(&protocol->queue); struct wsfs_message * message = wsfs_message_queue_pop(&session->queue);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT); lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wsfs_message_dispose(message); wsfs_message_dispose(message);
if (!wsfs_message_queue_empty(&protocol->queue)) if (!wsfs_message_queue_empty(&session->queue))
{ {
lws_callback_on_writable(wsi); lws_callback_on_writable(wsi);
} }
@ -79,14 +76,15 @@ static bool wsfs_server_protocol_invoke(
{ {
bool result = false; bool result = false;
struct wsfs_server_protocol * protocol = user_data; struct wsfs_server_protocol * protocol = user_data;
struct wsfs_session * session = &protocol->session_manager.session;
if ((protocol->is_authenticated) && (NULL != protocol->wsi)) if ((session->is_authenticated) && (NULL != session->wsi))
{ {
struct wsfs_message * message = wsfs_message_create(request); struct wsfs_message * message = wsfs_message_create(request);
if (NULL != message) if (NULL != message)
{ {
wsfs_message_queue_push(&protocol->queue, message); wsfs_message_queue_push(&session->queue, message);
lws_callback_on_writable(protocol->wsi); lws_callback_on_writable(session->wsi);
result = true; result = true;
} }
@ -132,10 +130,8 @@ bool wsfs_server_protocol_init(
struct wsfs_server_protocol * protocol, struct wsfs_server_protocol * protocol,
char * mount_point) char * mount_point)
{ {
protocol->wsi = NULL;
protocol->is_authenticated = false;
wsfs_message_queue_init(&protocol->queue);
wsfs_timeout_manager_init(&protocol->timeout_manager); wsfs_timeout_manager_init(&protocol->timeout_manager);
wsfs_session_manager_init(&protocol->session_manager);
wsfs_authenticators_init(&protocol->authenticators); wsfs_authenticators_init(&protocol->authenticators);
wsfs_jsonrpc_server_init(&protocol->rpc, &protocol->timeout_manager); wsfs_jsonrpc_server_init(&protocol->rpc, &protocol->timeout_manager);
@ -154,7 +150,7 @@ bool wsfs_server_protocol_init(
wsfs_jsonrpc_server_cleanup(&protocol->rpc); wsfs_jsonrpc_server_cleanup(&protocol->rpc);
wsfs_authenticators_cleanup(&protocol->authenticators); wsfs_authenticators_cleanup(&protocol->authenticators);
wsfs_timeout_manager_cleanup(&protocol->timeout_manager); wsfs_timeout_manager_cleanup(&protocol->timeout_manager);
wsfs_message_queue_cleanup(&protocol->queue); wsfs_session_manager_cleanup(&protocol->session_manager);
} }
return success; return success;
@ -166,9 +162,8 @@ void wsfs_server_protocol_cleanup(
wsfs_filesystem_cleanup(&protocol->filesystem); wsfs_filesystem_cleanup(&protocol->filesystem);
wsfs_jsonrpc_server_cleanup(&protocol->rpc); wsfs_jsonrpc_server_cleanup(&protocol->rpc);
wsfs_timeout_manager_cleanup(&protocol->timeout_manager); wsfs_timeout_manager_cleanup(&protocol->timeout_manager);
wsfs_message_queue_cleanup(&protocol->queue);
wsfs_authenticators_cleanup(&protocol->authenticators); wsfs_authenticators_cleanup(&protocol->authenticators);
protocol->wsi = NULL; wsfs_session_manager_cleanup(&protocol->session_manager);
} }
void wsfs_server_protocol_add_authenticator( void wsfs_server_protocol_add_authenticator(

View File

@ -1,23 +1,20 @@
#ifndef WSFS_ADAPTER_SERVER_PROTOCOL_INTERN_H #ifndef WSFS_ADAPTER_SERVER_PROTOCOL_INTERN_H
#define WSFS_ADAPTER_SERVER_PROTOCOL_INTERN_H #define WSFS_ADAPTER_SERVER_PROTOCOL_INTERN_H
#include "wsfs/message_queue.h"
#include "wsfs/adapter/server_protocol.h" #include "wsfs/adapter/server_protocol.h"
#include "wsfs/adapter/filesystem.h" #include "wsfs/adapter/filesystem.h"
#include "wsfs/adapter/jsonrpc/server.h" #include "wsfs/adapter/jsonrpc/server.h"
#include "wsfs/adapter/time/timeout_manager.h" #include "wsfs/adapter/time/timeout_manager.h"
#include "wsfs/adapter/authenticators.h" #include "wsfs/adapter/authenticators.h"
#include "wsfs/adapter/session_manager.h"
struct wsfs_server_protocol struct wsfs_server_protocol
{ {
struct wsfs_timeout_manager timeout_manager; struct wsfs_timeout_manager timeout_manager;
struct wsfs_filesystem filesystem; struct wsfs_filesystem filesystem;
struct wsfs_jsonrpc_server rpc; struct wsfs_jsonrpc_server rpc;
struct wsfs_message_queue queue;
struct wsfs_authenticators authenticators; struct wsfs_authenticators authenticators;
struct lws * wsi; struct wsfs_session_manager session_manager;
bool is_authenticated;
}; };
extern bool wsfs_server_protocol_init( extern bool wsfs_server_protocol_init(

View File

@ -0,0 +1,20 @@
#include "wsfs/adapter/session.h"
#include <stddef.h>
void wsfs_session_init(
struct wsfs_session * session,
struct lws * wsi,
bool is_authenticated)
{
session->wsi = wsi;
session->is_authenticated = is_authenticated;
wsfs_message_queue_init(&session->queue);
}
void wsfs_session_cleanup(
struct wsfs_session * session)
{
wsfs_message_queue_cleanup(&session->queue);
session->is_authenticated = false;
session->wsi = NULL;
}

View File

@ -0,0 +1,37 @@
#ifndef WSFS_ADAPTER_SESSION_H
#define WSFS_ADAPTER_SESSION_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "wsfs/message_queue.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct lws;
struct wsfs_session
{
struct lws * wsi;
bool is_authenticated;
struct wsfs_message_queue queue;
};
extern void wsfs_session_init(
struct wsfs_session * session,
struct lws * wsi,
bool is_authenticated);
extern void wsfs_session_cleanup(
struct wsfs_session * session);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,52 @@
#include "wsfs/adapter/session_manager.h"
#include <stddef.h>
void wsfs_session_manager_init(
struct wsfs_session_manager * manager)
{
wsfs_session_init(&manager->session, NULL, false);
}
void wsfs_session_manager_cleanup(
struct wsfs_session_manager * manager)
{
wsfs_session_cleanup(&manager->session);
}
bool wsfs_session_manager_add(
struct wsfs_session_manager * manager,
struct lws * wsi,
bool is_authenticated)
{
bool const result = (NULL == manager->session.wsi);
if (result)
{
wsfs_session_init(&manager->session, wsi, is_authenticated);
}
return result;
}
struct wsfs_session * wsfs_session_manager_get(
struct wsfs_session_manager * manager,
struct lws * wsi)
{
struct wsfs_session * session = NULL;
if (wsi == manager->session.wsi)
{
session = &manager->session;
}
return session;
}
void wsfs_session_manager_remove(
struct wsfs_session_manager * manager,
struct lws * wsi)
{
if (wsi == manager->session.wsi)
{
wsfs_session_cleanup(&manager->session);
manager->session.wsi = NULL;
}
}

View File

@ -0,0 +1,45 @@
#ifndef WSFS_ADAPTER_SESSION_MANAGER_H
#define WSFS_ADAPTER_SESSION_MANAGER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "wsfs/adapter/session.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct lws;
struct wsfs_session_manager
{
struct wsfs_session session;
};
extern void wsfs_session_manager_init(
struct wsfs_session_manager * manager);
extern void wsfs_session_manager_cleanup(
struct wsfs_session_manager * manager);
extern bool wsfs_session_manager_add(
struct wsfs_session_manager * manager,
struct lws * wsi,
bool is_authenticated);
extern struct wsfs_session * wsfs_session_manager_get(
struct wsfs_session_manager * manager,
struct lws * wsi);
extern void wsfs_session_manager_remove(
struct wsfs_session_manager * manager,
struct lws * wsi);
#ifdef __cplusplus
}
#endif
#endif