~skiqqy/baal

6b8c1982fbff3085dcc2f04da66c9fb6e4d9159f — Stephen Cochrane 1 year, 11 months ago 8304b2d
Tidied up enums, refactored #define to libbaal
3 files changed, 14 insertions(+), 14 deletions(-)

M include/baal.h
M include/libbaal.h
M src/baalserver.c
M include/baal.h => include/baal.h +0 -4
@@ 78,10 78,6 @@ enum commmands {

#define SERVER_MAX_THREADS 8

// JSON Queries
#define REGISTER_QUERY  "register"
#define SEND_MAIL_QUERY "send"

// Commands
int server(int argc, char **argv); // Start a server
int client(int argc, char **argv); // Start a client

M include/libbaal.h => include/libbaal.h +4 -0
@@ 28,6 28,10 @@ typedef enum Codes {
	ERR_INVALID_QUERY,  // This query is invalid
} Code;

// JSON Queries
#define REGISTER_QUERY  "register"
#define SEND_MAIL_QUERY "send"

// Misc
char *getsha256(char *string); // Returns a pointer to the hashed string
void dprintJSON(cJSON *json);

M src/baalserver.c => src/baalserver.c +10 -10
@@ 10,9 10,9 @@

// IDs for locks
typedef enum Locks {
	FETCH_THREAD_LOCK,
	USERS_LOCK,        // Lock for server user list
	FLUSH_LOCK,        // Prevent concurrent flush_server calls.
	LOCK_FETCH_THREAD,
	LOCK_USERS,        // Lock for server user list
	LOCK_FLUSH,        // Prevent concurrent flush_server calls.
	LOCK_LEN           // This must be the last element of this enum.
} Lock;



@@ 35,7 35,7 @@ typedef struct WorkerArgs {
#define unlock(i) pthread_mutex_unlock(&locks[i]);

// Release i'th worker thread, is thread safe
#define rel_thread(i) lock(FETCH_THREAD_LOCK); thread_avail[i] = 1; unlock(FETCH_THREAD_LOCK);
#define rel_thread(i) lock(LOCK_FETCH_THREAD); thread_avail[i] = 1; unlock(LOCK_FETCH_THREAD);

extern char *baal_home;
Server serv;


@@ 74,7 74,7 @@ flush_server(int sig)
{
	char *str, buff[256];
	FILE *file;
	lock(FLUSH_LOCK); // This doesnt need to be unlocked, as this func terminates the programe
	lock(LOCK_FLUSH); // This doesnt need to be unlocked, as this func terminates the programe

	binfo("Caught sig %d", sig);
	if (!baal_home) goto exit;


@@ 141,7 141,7 @@ fetch_thread(int *id)
{
	int i, flag = 0;
	pthread_t *ret = NULL;
	lock(FETCH_THREAD_LOCK);
	lock(LOCK_FETCH_THREAD);
	// Critical Section
	for (i = 0; i < SERVER_MAX_THREADS; i++) {
		if (thread_avail[i]) {


@@ 156,7 156,7 @@ fetch_thread(int *id)
		ret = &workers[i];
	}
	// End Critical
	unlock(FETCH_THREAD_LOCK);
	unlock(LOCK_FETCH_THREAD);
	return ret;
}



@@ 346,18 346,18 @@ register_user_handler(WorkerArg *arg, cJSON *data)
	// Check that the user is unique
	// TODO. refresh on locks needed when only reading, techinically this is a read and write,
	// so ive added a lock to be safe, although it will be a performance decrease.
	lock(USERS_LOCK);
	lock(LOCK_USERS);
	cJSON_ArrayForEach(tmp, users) {
		if (!strcmp(cJSON_GetObjectItemCaseSensitive(tmp, "user")->valuestring, user->valuestring)) {
			bwarn("A user with the name %s already exists.", user->valuestring);
			resp_code = ERR_DUPLICATE_USER;
			unlock(USERS_LOCK); // Release the lock
			unlock(LOCK_USERS); // Release the lock
			goto fail;
		}
	}

	cJSON_AddItemToArray(users, data);
	unlock(USERS_LOCK);
	unlock(LOCK_USERS);
	binfo("Account '%s' successfully created!", user->valuestring);

fail: