forked from cory/tildefriends
Rooms JS => C.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4107 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
289
src/ssb.rpc.c
289
src/ssb.rpc.c
@ -11,6 +11,10 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#if !defined(_countof)
|
||||
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
|
||||
#endif
|
||||
|
||||
static void _tf_ssb_rpc_gossip_ping(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||
{
|
||||
char buffer[256];
|
||||
@ -52,7 +56,6 @@ static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags
|
||||
const int k_send_max = 8192;
|
||||
if (tf_ssb_db_blob_get(ssb, id, &blob, &size))
|
||||
{
|
||||
printf("sending %s (%zd)\n", id, size);
|
||||
for (size_t offset = 0; offset < size; offset += k_send_max)
|
||||
{
|
||||
tf_ssb_connection_rpc_send(
|
||||
@ -178,9 +181,65 @@ void _tf_ssb_rpc_tunnel_cleanup(tf_ssb_t* ssb, void* user_data)
|
||||
tf_free(user_data);
|
||||
}
|
||||
|
||||
static bool _get_global_setting_bool(tf_ssb_t* ssb, const char* name, bool default_value)
|
||||
{
|
||||
bool result = default_value;
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
sqlite3* db = tf_ssb_get_db(ssb);
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_step(statement) == SQLITE_ROW)
|
||||
{
|
||||
JSValue value = JS_ParseJSON(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0), NULL);
|
||||
JSValue property = JS_GetPropertyStr(context, value, name);
|
||||
if (JS_IsBool(property))
|
||||
{
|
||||
result = JS_ToBool(context, property);
|
||||
}
|
||||
JS_FreeValue(context, property);
|
||||
JS_FreeValue(context, value);
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool _get_global_setting_string(tf_ssb_t* ssb, const char* name, char* out_value, size_t size)
|
||||
{
|
||||
bool result = false;
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
sqlite3* db = tf_ssb_get_db(ssb);
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_step(statement) == SQLITE_ROW)
|
||||
{
|
||||
JSValue value = JS_ParseJSON(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0), NULL);
|
||||
JSValue property = JS_GetPropertyStr(context, value, name);
|
||||
const char* value_string = JS_ToCString(context, property);
|
||||
if (value_string)
|
||||
{
|
||||
snprintf(out_value, size, "%s", value_string);
|
||||
result = true;
|
||||
}
|
||||
JS_FreeValue(context, property);
|
||||
JS_FreeValue(context, value);
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||
{
|
||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||
if (!_get_global_setting_bool(ssb, "room", true))
|
||||
{
|
||||
tf_ssb_connection_rpc_send_error_method_not_allowed(connection, flags, -request_number);
|
||||
return;
|
||||
}
|
||||
|
||||
JSContext* context = tf_ssb_connection_get_context(connection);
|
||||
JSValue arg_array = JS_GetPropertyStr(context, args, "args");
|
||||
JSValue arg = JS_GetPropertyUint32(context, arg_array, 0);
|
||||
@ -241,8 +300,9 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
|
||||
.connection = connection,
|
||||
.request_number = -request_number,
|
||||
};
|
||||
tf_ssb_connection_add_request(connection, -request_number, _tf_ssb_rpc_tunnel_callback, _tf_ssb_rpc_tunnel_cleanup, data0, connection);
|
||||
tf_ssb_connection_add_request(target_connection, tunnel_request_number, _tf_ssb_rpc_tunnel_callback, _tf_ssb_rpc_tunnel_cleanup, data1, target_connection);
|
||||
printf("MAKE TUNNEL %p %p\n", connection, target_connection);
|
||||
tf_ssb_connection_add_request(connection, -request_number, _tf_ssb_rpc_tunnel_callback, _tf_ssb_rpc_tunnel_cleanup, data0, target_connection);
|
||||
tf_ssb_connection_add_request(target_connection, tunnel_request_number, _tf_ssb_rpc_tunnel_callback, _tf_ssb_rpc_tunnel_cleanup, data1, connection);
|
||||
|
||||
JS_FreeValue(context, message_val);
|
||||
JS_FreeValue(context, message);
|
||||
@ -256,6 +316,7 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
|
||||
const char* origin_str = JS_ToCString(context, origin);
|
||||
const char* portal_str = JS_ToCString(context, portal);
|
||||
const char* target_str = JS_ToCString(context, target);
|
||||
printf("TUNNEL CREATE\n");
|
||||
tf_ssb_connection_tunnel_create(ssb, portal_str, -request_number, origin_str);
|
||||
JS_FreeCString(context, origin_str);
|
||||
JS_FreeCString(context, portal_str);
|
||||
@ -269,30 +330,6 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
|
||||
JS_FreeValue(context, arg_array);
|
||||
}
|
||||
|
||||
static bool _get_global_setting_bool(tf_ssb_t* ssb, const char* name, bool default_value)
|
||||
{
|
||||
bool result = default_value;
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
sqlite3* db = tf_ssb_get_db(ssb);
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_step(statement) == SQLITE_ROW)
|
||||
{
|
||||
JSValue value = JS_ParseJSON(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0), NULL);
|
||||
JSValue property = JS_GetPropertyStr(context, value, name);
|
||||
if (JS_IsBool(property))
|
||||
{
|
||||
result = JS_ToBool(context, property);
|
||||
}
|
||||
JS_FreeValue(context, property);
|
||||
JS_FreeValue(context, value);
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void _tf_ssb_rpc_tunnel_is_room(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||
{
|
||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||
@ -300,8 +337,10 @@ static void _tf_ssb_rpc_tunnel_is_room(tf_ssb_connection_t* connection, uint8_t
|
||||
JSValue response = JS_FALSE;
|
||||
if (_get_global_setting_bool(ssb, "room", true))
|
||||
{
|
||||
char room_name[1024] = "tilde friends tunnel";
|
||||
_get_global_setting_string(ssb, "room_name", room_name, sizeof(room_name));
|
||||
response = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, response, "name", JS_NewString(context, "tilde friends tunnel"));
|
||||
JS_SetPropertyStr(context, response, "name", JS_NewString(context, room_name));
|
||||
JS_SetPropertyStr(context, response, "membership", JS_FALSE);
|
||||
JSValue features = JS_NewArray(context);
|
||||
JS_SetPropertyUint32(context, features, 0, JS_NewString(context, "tunnel"));
|
||||
@ -326,6 +365,56 @@ static void _tf_ssb_rpc_tunnel_is_room(tf_ssb_connection_t* connection, uint8_t
|
||||
JS_FreeValue(context, response);
|
||||
}
|
||||
|
||||
static void _tf_ssb_rpc_room_attendants(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||
{
|
||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||
if (!_get_global_setting_bool(ssb, "room", true))
|
||||
{
|
||||
tf_ssb_connection_rpc_send_error_method_not_allowed(connection, flags, -request_number);
|
||||
return;
|
||||
}
|
||||
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
JSValue joined = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, joined, "type", JS_NewString(context, "joined"));
|
||||
char my_id[k_id_base64_len] = "";
|
||||
if (tf_ssb_connection_get_id(connection, my_id, sizeof(my_id)))
|
||||
{
|
||||
JS_SetPropertyStr(context, joined, "id", JS_NewString(context, my_id));
|
||||
}
|
||||
|
||||
JSValue state = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, state, "type", JS_NewString(context, "state"));
|
||||
JSValue ids = JS_NewArray(context);
|
||||
int id_count = 0;
|
||||
tf_ssb_connection_t* connections[1024];
|
||||
int count = tf_ssb_get_connections(ssb, connections, _countof(connections));
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
char id[k_id_base64_len] = { 0 };
|
||||
if (tf_ssb_connection_is_attendant(connections[i]) &&
|
||||
tf_ssb_connection_get_id(connections[i], id, sizeof(id)))
|
||||
{
|
||||
JS_SetPropertyUint32(context, ids, id_count++, JS_NewString(context, id));
|
||||
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connections[i],
|
||||
flags,
|
||||
-tf_ssb_connection_get_attendant_request_number(connections[i]),
|
||||
joined,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
JS_SetPropertyStr(context, state, "ids", ids);
|
||||
tf_ssb_connection_rpc_send_json(connection, flags, -request_number, state, NULL, NULL, NULL);
|
||||
JS_FreeValue(context, joined);
|
||||
JS_FreeValue(context, state);
|
||||
|
||||
tf_ssb_connection_set_attendant(connection, true, request_number);
|
||||
}
|
||||
|
||||
typedef struct _blobs_get_t
|
||||
{
|
||||
char id[k_blob_id_len];
|
||||
@ -506,11 +595,94 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback(tf_ssb_connection_
|
||||
js_free(context, ptab);
|
||||
}
|
||||
|
||||
static void _tf_ssb_rpc_connection_room_attendants_callback(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||
{
|
||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
JSValue type = JS_GetPropertyStr(context, args, "type");
|
||||
const char* type_string = JS_ToCString(context, type);
|
||||
if (!type_string)
|
||||
{
|
||||
tf_ssb_connection_rpc_send_error(connection, flags, -request_number, "Missing type.");
|
||||
}
|
||||
else if (strcmp(type_string, "state") == 0)
|
||||
{
|
||||
tf_ssb_connection_clear_room_attendants(connection);
|
||||
JSValue ids = JS_GetPropertyStr(context, args, "ids");
|
||||
int length = tf_util_get_length(context, ids);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
JSValue id = JS_GetPropertyUint32(context, ids, i);
|
||||
const char* id_string = JS_ToCString(context, id);
|
||||
if (id_string)
|
||||
{
|
||||
tf_ssb_connection_add_room_attendant(connection, id_string);
|
||||
}
|
||||
JS_FreeCString(context, id_string);
|
||||
JS_FreeValue(context, id);
|
||||
}
|
||||
JS_FreeValue(context, ids);
|
||||
}
|
||||
else if (strcmp(type_string, "joined") == 0)
|
||||
{
|
||||
JSValue id = JS_GetPropertyStr(context, args, "id");
|
||||
const char* id_string = JS_ToCString(context, id);
|
||||
if (id_string)
|
||||
{
|
||||
tf_ssb_connection_add_room_attendant(connection, id_string);
|
||||
}
|
||||
JS_FreeCString(context, id_string);
|
||||
JS_FreeValue(context, id);
|
||||
}
|
||||
else if (strcmp(type_string, "left") == 0)
|
||||
{
|
||||
JSValue id = JS_GetPropertyStr(context, args, "id");
|
||||
const char* id_string = JS_ToCString(context, id);
|
||||
if (id_string)
|
||||
{
|
||||
tf_ssb_connection_remove_room_attendant(connection, id_string);
|
||||
}
|
||||
JS_FreeCString(context, id_string);
|
||||
JS_FreeValue(context, id);
|
||||
}
|
||||
else
|
||||
{
|
||||
tf_ssb_connection_rpc_send_error(connection, flags, -request_number, "Unexpected room.attendants response type.");
|
||||
}
|
||||
JS_FreeCString(context, type_string);
|
||||
JS_FreeValue(context, type);
|
||||
}
|
||||
|
||||
static void _tf_ssb_rpc_connection_tunnel_isRoom_callback(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||
{
|
||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
if (JS_IsObject(args))
|
||||
{
|
||||
JSValue message = JS_NewObject(context);
|
||||
JSValue name = JS_NewArray(context);
|
||||
JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "room"));
|
||||
JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "attendants"));
|
||||
JS_SetPropertyStr(context, message, "name", name);
|
||||
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "source"));
|
||||
JS_SetPropertyStr(context, message, "args", JS_NewArray(context));
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connection,
|
||||
k_ssb_rpc_flag_stream,
|
||||
tf_ssb_connection_next_request_number(connection),
|
||||
message,
|
||||
_tf_ssb_rpc_connection_room_attendants_callback,
|
||||
NULL,
|
||||
NULL);
|
||||
JS_FreeValue(context, message);
|
||||
}
|
||||
}
|
||||
|
||||
static void _tf_ssb_rpc_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
||||
{
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
if (change == k_tf_ssb_change_connect)
|
||||
{
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
JSValue message = JS_NewObject(context);
|
||||
JSValue name = JS_NewArray(context);
|
||||
JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "blobs"));
|
||||
@ -518,27 +690,63 @@ static void _tf_ssb_rpc_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_chang
|
||||
JS_SetPropertyStr(context, message, "name", name);
|
||||
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "source"));
|
||||
JS_SetPropertyStr(context, message, "args", JS_NewArray(context));
|
||||
JSValue message_val = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
|
||||
size_t size;
|
||||
const char* message_str = JS_ToCStringLen(context, &size, message_val);
|
||||
|
||||
tf_ssb_connection_rpc_send(
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connection,
|
||||
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
|
||||
k_ssb_rpc_flag_stream,
|
||||
tf_ssb_connection_next_request_number(connection),
|
||||
(const uint8_t*)message_str,
|
||||
size,
|
||||
message,
|
||||
_tf_ssb_rpc_connection_blobs_createWants_callback,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
JS_FreeCString(context, message_str);
|
||||
JS_FreeValue(context, message_val);
|
||||
JS_FreeValue(context, message);
|
||||
|
||||
if (tf_ssb_connection_is_client(connection))
|
||||
{
|
||||
message = JS_NewObject(context);
|
||||
name = JS_NewArray(context);
|
||||
JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "tunnel"));
|
||||
JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "isRoom"));
|
||||
JS_SetPropertyStr(context, message, "name", name);
|
||||
JS_SetPropertyStr(context, message, "args", JS_NewArray(context));
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connection,
|
||||
0,
|
||||
tf_ssb_connection_next_request_number(connection),
|
||||
message,
|
||||
_tf_ssb_rpc_connection_tunnel_isRoom_callback,
|
||||
NULL,
|
||||
NULL);
|
||||
JS_FreeValue(context, message);
|
||||
}
|
||||
}
|
||||
else if (change == k_tf_ssb_change_remove)
|
||||
{
|
||||
tf_ssb_remove_blob_want_added_callback(ssb, _tf_ssb_rpc_blob_wants_added_callback, connection);
|
||||
|
||||
JSValue left = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, left, "type", JS_NewString(context, "left"));
|
||||
char id[k_id_base64_len] = "";
|
||||
if (tf_ssb_connection_get_id(connection, id, sizeof(id)))
|
||||
{
|
||||
JS_SetPropertyStr(context, left, "id", JS_NewString(context, id));
|
||||
}
|
||||
tf_ssb_connection_t* connections[1024];
|
||||
int count = tf_ssb_get_connections(ssb, connections, _countof(connections));
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (tf_ssb_connection_is_attendant(connections[i]))
|
||||
{
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connections[i],
|
||||
k_ssb_rpc_flag_stream,
|
||||
-tf_ssb_connection_get_attendant_request_number(connections[i]),
|
||||
left,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
JS_FreeValue(context, left);
|
||||
}
|
||||
}
|
||||
|
||||
@ -551,4 +759,5 @@ void tf_ssb_rpc_register(tf_ssb_t* ssb)
|
||||
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "createWants", NULL }, _tf_ssb_rpc_blobs_createWants, NULL, NULL);
|
||||
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "connect", NULL }, _tf_ssb_rpc_tunnel_connect, NULL, NULL);
|
||||
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "isRoom", NULL }, _tf_ssb_rpc_tunnel_is_room, NULL, NULL);
|
||||
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "room", "attendants", NULL }, _tf_ssb_rpc_room_attendants, NULL, NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user