1
0
Fork 0

Basic chat session states

This commit is contained in:
James Booth 2012-10-04 00:34:03 +01:00
parent f8de28232e
commit 5b8599272a
3 changed files with 82 additions and 37 deletions

View File

@ -31,7 +31,6 @@ static ChatSession _chat_session_new(char *recipient);
static void _chat_session_free(ChatSession session);
struct chat_session_t {
GTimer *last_composing_sent;
char *recipient;
chat_state_t state;
};
@ -45,43 +44,41 @@ chat_session_init(void)
(GDestroyNotify)_chat_session_free);
}
ChatSession
chat_session_new(char *recipient)
void
chat_session_start(char *recipient)
{
ChatSession session = _chat_session_new(recipient);
g_hash_table_insert(sessions, recipient, session);
return session;
}
int
chat_session_size(void)
void
chat_session_end(char *recipient)
{
return g_hash_table_size(sessions);
}
ChatSession
chat_session_get(char *recipient)
{
return (ChatSession) g_hash_table_lookup(sessions, recipient);
g_hash_table_remove(sessions, recipient);
}
chat_state_t
chat_session_get_state(ChatSession session)
chat_session_get_state(char *recipient)
{
return session->state;
ChatSession session = g_hash_table_lookup(sessions, recipient);
if (session == NULL) {
return SESSION_ERR;
} else {
return session->state;
}
}
char *
chat_session_get_recipient(ChatSession session)
void
chat_session_set_state(char *recipient, chat_state_t state)
{
return session->recipient;
ChatSession session = g_hash_table_lookup(sessions, recipient);
session->state = state;
}
static ChatSession
_chat_session_new(char *recipient)
{
ChatSession new_session = malloc(sizeof(struct chat_session_t));
new_session->last_composing_sent = NULL;
new_session->recipient = malloc(strlen(recipient) + 1);
strcpy(new_session->recipient, recipient);
new_session->state = ACTIVE;
@ -92,10 +89,8 @@ _chat_session_new(char *recipient)
static void
_chat_session_free(ChatSession session)
{
if (session->last_composing_sent != NULL) {
g_timer_destroy(session->last_composing_sent);
session->last_composing_sent = NULL;
if (session != NULL) {
g_free(session->recipient);
g_free(session);
}
g_free(session->recipient);
g_free(session);
}

View File

@ -27,15 +27,17 @@ typedef struct chat_session_t *ChatSession;
typedef enum {
ACTIVE,
INACTIVE,
GONE,
COMPOSING,
PAUSED
PAUSED,
SESSION_ERR
} chat_state_t;
void chat_session_init(void);
int chat_session_size(void);
ChatSession chat_session_new(char *recipient);
ChatSession chat_session_get(char *recipient);
chat_state_t chat_session_get_state(ChatSession session);
char * chat_session_get_recipient(ChatSession session);
void chat_session_start(char *recipient);
void chat_session_end(char *recipient);
chat_state_t chat_session_get_state(char *recipient);
void chat_session_set_state(char *recipient, chat_state_t state);
#endif

View File

@ -2,24 +2,72 @@
#include <head-unit.h>
#include "chat_session.h"
void can_init(void)
void setup(void)
{
chat_session_init();
assert_true(1);
}
void adding_new_sets_state_to_active(void)
{
chat_session_init();
chat_session_new("prof1@panesar");
ChatSession session = chat_session_get("prof1@panesar");
chat_session_start("prof1@panesar");
chat_state_t state = chat_session_get_state("prof1@panesar");
assert_int_equals(ACTIVE, chat_session_get_state(session));
assert_int_equals(ACTIVE, state);
}
void set_inactive(void)
{
chat_session_start("prof2@panesar");
chat_session_set_state("prof2@panesar", INACTIVE);
chat_state_t state = chat_session_get_state("prof2@panesar");
assert_int_equals(INACTIVE, state);
}
void set_gone(void)
{
chat_session_start("prof3@panesar");
chat_session_set_state("prof3@panesar", GONE);
chat_state_t state = chat_session_get_state("prof3@panesar");
assert_int_equals(GONE, state);
}
void set_composing(void)
{
chat_session_start("prof4@panesar");
chat_session_set_state("prof4@panesar", COMPOSING);
chat_state_t state = chat_session_get_state("prof4@panesar");
assert_int_equals(COMPOSING, state);
}
void set_paused(void)
{
chat_session_start("prof5@panesar");
chat_session_set_state("prof5@panesar", PAUSED);
chat_state_t state = chat_session_get_state("prof5@panesar");
assert_int_equals(PAUSED, state);
}
void end_session(void)
{
chat_session_start(strdup("prof6@panesar"));
chat_session_end("prof6@panesar");
chat_state_t state = chat_session_get_state("prof5@panesat");
assert_int_equals(SESSION_ERR, state);
}
void register_chat_session_tests(void)
{
TEST_MODULE("chat_session_tests");
TEST(can_init);
SETUP(setup);
TEST(adding_new_sets_state_to_active);
TEST(set_inactive);
TEST(set_gone);
TEST(set_composing);
TEST(set_paused);
TEST(end_session);
}