I just decided. Braces on their own lines.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3668 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -22,14 +22,16 @@ typedef struct _tf_ssb_connections_t
|
||||
static void _tf_ssb_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
||||
{
|
||||
tf_ssb_connections_t* connections = user_data;
|
||||
switch (change) {
|
||||
switch (change)
|
||||
{
|
||||
case k_tf_ssb_change_create:
|
||||
{
|
||||
char key[ID_BASE64_LEN];
|
||||
if (tf_ssb_connection_get_host(connection) &&
|
||||
*tf_ssb_connection_get_host(connection) &&
|
||||
tf_ssb_connection_get_port(connection) &&
|
||||
tf_ssb_connection_get_id(connection, key, sizeof(key))) {
|
||||
tf_ssb_connection_get_id(connection, key, sizeof(key)))
|
||||
{
|
||||
tf_ssb_connections_store(connections, tf_ssb_connection_get_host(connection), tf_ssb_connection_get_port(connection), key);
|
||||
tf_ssb_connections_set_attempted(connections, tf_ssb_connection_get_host(connection), tf_ssb_connection_get_port(connection), key);
|
||||
}
|
||||
@ -38,7 +40,8 @@ static void _tf_ssb_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change_t
|
||||
case k_tf_ssb_change_connect:
|
||||
{
|
||||
char key[ID_BASE64_LEN];
|
||||
if (tf_ssb_connection_get_id(connection, key, sizeof(key))) {
|
||||
if (tf_ssb_connection_get_id(connection, key, sizeof(key)))
|
||||
{
|
||||
tf_ssb_connections_set_succeeded(connections, tf_ssb_connection_get_host(connection), tf_ssb_connection_get_port(connection), key);
|
||||
}
|
||||
}
|
||||
@ -52,16 +55,20 @@ static bool _tf_ssb_connections_get_next_connection(tf_ssb_connections_t* connec
|
||||
{
|
||||
bool result = false;
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(connections->db, "SELECT host, port, key FROM connections WHERE last_attempt IS NULL OR (strftime('%s', 'now') - last_attempt > $1) ORDER BY last_attempt LIMIT 1", -1, &statement, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_prepare(connections->db, "SELECT host, port, key FROM connections WHERE last_attempt IS NULL OR (strftime('%s', 'now') - last_attempt > $1) ORDER BY last_attempt LIMIT 1", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_bind_int(statement, 1, 60000) == SQLITE_OK &&
|
||||
sqlite3_step(statement) == SQLITE_ROW) {
|
||||
sqlite3_step(statement) == SQLITE_ROW)
|
||||
{
|
||||
snprintf(host, host_size, "%s", sqlite3_column_text(statement, 0));
|
||||
*port = sqlite3_column_int(statement, 1);
|
||||
snprintf(key, key_size, "%s", sqlite3_column_text(statement, 2));
|
||||
result = true;
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("prepare: %s\n", sqlite3_errmsg(connections->db));
|
||||
}
|
||||
return result;
|
||||
@ -72,13 +79,16 @@ static void _tf_ssb_connections_timer(uv_timer_t* timer)
|
||||
tf_ssb_connections_t* connections = timer->data;
|
||||
tf_ssb_connection_t* active[4];
|
||||
int count = tf_ssb_get_connections(connections->ssb, active, _countof(active));
|
||||
if (count < _countof(active)) {
|
||||
if (count < _countof(active))
|
||||
{
|
||||
char host[256];
|
||||
int port;
|
||||
char key[ID_BASE64_LEN];
|
||||
if (_tf_ssb_connections_get_next_connection(connections, host, sizeof(host), &port, key, sizeof(key))) {
|
||||
if (_tf_ssb_connections_get_next_connection(connections, host, sizeof(host), &port, key, sizeof(key)))
|
||||
{
|
||||
uint8_t key_bin[ID_BIN_LEN];
|
||||
if (tf_ssb_id_str_to_bin(key_bin, key)) {
|
||||
if (tf_ssb_id_str_to_bin(key_bin, key))
|
||||
{
|
||||
tf_ssb_connect(connections->ssb, host, port, key_bin);
|
||||
}
|
||||
}
|
||||
@ -118,12 +128,15 @@ void tf_ssb_connections_destroy(tf_ssb_connections_t* connections)
|
||||
void tf_ssb_connections_store(tf_ssb_connections_t* connections, const char* host, int port, const char* key)
|
||||
{
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(connections->db, "INSERT INTO connections (host, port, key) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_prepare(connections->db, "INSERT INTO connections (host, port, key) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_bind_text(statement, 1, host, -1, NULL) == SQLITE_OK &&
|
||||
sqlite3_bind_int(statement, 2, port) == SQLITE_OK &&
|
||||
sqlite3_bind_text(statement, 3, key, -1, NULL) == SQLITE_OK) {
|
||||
sqlite3_bind_text(statement, 3, key, -1, NULL) == SQLITE_OK)
|
||||
{
|
||||
int r = sqlite3_step(statement);
|
||||
if (r != SQLITE_DONE) {
|
||||
if (r != SQLITE_DONE)
|
||||
{
|
||||
printf("tf_ssb_connections_store: %d, %s.\n", r, sqlite3_errmsg(connections->db));
|
||||
}
|
||||
}
|
||||
@ -134,11 +147,14 @@ void tf_ssb_connections_store(tf_ssb_connections_t* connections, const char* hos
|
||||
void tf_ssb_connections_set_attempted(tf_ssb_connections_t* connections, const char* host, int port, const char* key)
|
||||
{
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(connections->db, "UPDATE connections SET last_attempt = strftime('%s', 'now') WHERE host = $1 AND port = $2 AND key = $3", -1, &statement, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_prepare(connections->db, "UPDATE connections SET last_attempt = strftime('%s', 'now') WHERE host = $1 AND port = $2 AND key = $3", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_bind_text(statement, 1, host, -1, NULL) == SQLITE_OK &&
|
||||
sqlite3_bind_int(statement, 2, port) == SQLITE_OK &&
|
||||
sqlite3_bind_text(statement, 3, key, -1, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
sqlite3_bind_text(statement, 3, key, -1, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_step(statement) != SQLITE_DONE)
|
||||
{
|
||||
printf("tf_ssb_connections_set_attempted: %s.\n", sqlite3_errmsg(connections->db));
|
||||
}
|
||||
}
|
||||
@ -149,11 +165,14 @@ void tf_ssb_connections_set_attempted(tf_ssb_connections_t* connections, const c
|
||||
void tf_ssb_connections_set_succeeded(tf_ssb_connections_t* connections, const char* host, int port, const char* key)
|
||||
{
|
||||
sqlite3_stmt* statement;
|
||||
if (sqlite3_prepare(connections->db, "UPDATE connections SET last_success = strftime('%s', 'now') WHERE host = $1 AND port = $2 AND key = $3", -1, &statement, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_prepare(connections->db, "UPDATE connections SET last_success = strftime('%s', 'now') WHERE host = $1 AND port = $2 AND key = $3", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_bind_text(statement, 1, host, -1, NULL) == SQLITE_OK &&
|
||||
sqlite3_bind_int(statement, 2, port) == SQLITE_OK &&
|
||||
sqlite3_bind_text(statement, 3, key, -1, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
sqlite3_bind_text(statement, 3, key, -1, NULL) == SQLITE_OK)
|
||||
{
|
||||
if (sqlite3_step(statement) != SQLITE_DONE)
|
||||
{
|
||||
printf("tf_ssb_connections_set_succeeded: %s.\n", sqlite3_errmsg(connections->db));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user