Basic chat session states
This commit is contained in:
@@ -31,7 +31,6 @@ static ChatSession _chat_session_new(char *recipient);
|
|||||||
static void _chat_session_free(ChatSession session);
|
static void _chat_session_free(ChatSession session);
|
||||||
|
|
||||||
struct chat_session_t {
|
struct chat_session_t {
|
||||||
GTimer *last_composing_sent;
|
|
||||||
char *recipient;
|
char *recipient;
|
||||||
chat_state_t state;
|
chat_state_t state;
|
||||||
};
|
};
|
||||||
@@ -45,43 +44,41 @@ chat_session_init(void)
|
|||||||
(GDestroyNotify)_chat_session_free);
|
(GDestroyNotify)_chat_session_free);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatSession
|
void
|
||||||
chat_session_new(char *recipient)
|
chat_session_start(char *recipient)
|
||||||
{
|
{
|
||||||
ChatSession session = _chat_session_new(recipient);
|
ChatSession session = _chat_session_new(recipient);
|
||||||
g_hash_table_insert(sessions, recipient, session);
|
g_hash_table_insert(sessions, recipient, session);
|
||||||
return session;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
chat_session_size(void)
|
chat_session_end(char *recipient)
|
||||||
{
|
{
|
||||||
return g_hash_table_size(sessions);
|
g_hash_table_remove(sessions, recipient);
|
||||||
}
|
|
||||||
|
|
||||||
ChatSession
|
|
||||||
chat_session_get(char *recipient)
|
|
||||||
{
|
|
||||||
return (ChatSession) g_hash_table_lookup(sessions, recipient);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chat_state_t
|
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 *
|
void
|
||||||
chat_session_get_recipient(ChatSession session)
|
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
|
static ChatSession
|
||||||
_chat_session_new(char *recipient)
|
_chat_session_new(char *recipient)
|
||||||
{
|
{
|
||||||
ChatSession new_session = malloc(sizeof(struct chat_session_t));
|
ChatSession new_session = malloc(sizeof(struct chat_session_t));
|
||||||
new_session->last_composing_sent = NULL;
|
|
||||||
new_session->recipient = malloc(strlen(recipient) + 1);
|
new_session->recipient = malloc(strlen(recipient) + 1);
|
||||||
strcpy(new_session->recipient, recipient);
|
strcpy(new_session->recipient, recipient);
|
||||||
new_session->state = ACTIVE;
|
new_session->state = ACTIVE;
|
||||||
@@ -92,10 +89,8 @@ _chat_session_new(char *recipient)
|
|||||||
static void
|
static void
|
||||||
_chat_session_free(ChatSession session)
|
_chat_session_free(ChatSession session)
|
||||||
{
|
{
|
||||||
if (session->last_composing_sent != NULL) {
|
if (session != NULL) {
|
||||||
g_timer_destroy(session->last_composing_sent);
|
g_free(session->recipient);
|
||||||
session->last_composing_sent = NULL;
|
g_free(session);
|
||||||
}
|
}
|
||||||
g_free(session->recipient);
|
|
||||||
g_free(session);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,15 +27,17 @@ typedef struct chat_session_t *ChatSession;
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ACTIVE,
|
ACTIVE,
|
||||||
|
INACTIVE,
|
||||||
|
GONE,
|
||||||
COMPOSING,
|
COMPOSING,
|
||||||
PAUSED
|
PAUSED,
|
||||||
|
SESSION_ERR
|
||||||
} chat_state_t;
|
} chat_state_t;
|
||||||
|
|
||||||
void chat_session_init(void);
|
void chat_session_init(void);
|
||||||
int chat_session_size(void);
|
void chat_session_start(char *recipient);
|
||||||
ChatSession chat_session_new(char *recipient);
|
void chat_session_end(char *recipient);
|
||||||
ChatSession chat_session_get(char *recipient);
|
chat_state_t chat_session_get_state(char *recipient);
|
||||||
chat_state_t chat_session_get_state(ChatSession session);
|
void chat_session_set_state(char *recipient, chat_state_t state);
|
||||||
char * chat_session_get_recipient(ChatSession session);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,24 +2,72 @@
|
|||||||
#include <head-unit.h>
|
#include <head-unit.h>
|
||||||
#include "chat_session.h"
|
#include "chat_session.h"
|
||||||
|
|
||||||
void can_init(void)
|
void setup(void)
|
||||||
{
|
{
|
||||||
chat_session_init();
|
chat_session_init();
|
||||||
assert_true(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void adding_new_sets_state_to_active(void)
|
void adding_new_sets_state_to_active(void)
|
||||||
{
|
{
|
||||||
chat_session_init();
|
chat_session_start("prof1@panesar");
|
||||||
chat_session_new("prof1@panesar");
|
chat_state_t state = chat_session_get_state("prof1@panesar");
|
||||||
ChatSession session = chat_session_get("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)
|
void register_chat_session_tests(void)
|
||||||
{
|
{
|
||||||
TEST_MODULE("chat_session_tests");
|
TEST_MODULE("chat_session_tests");
|
||||||
TEST(can_init);
|
SETUP(setup);
|
||||||
TEST(adding_new_sets_state_to_active);
|
TEST(adding_new_sets_state_to_active);
|
||||||
|
TEST(set_inactive);
|
||||||
|
TEST(set_gone);
|
||||||
|
TEST(set_composing);
|
||||||
|
TEST(set_paused);
|
||||||
|
TEST(end_session);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user