#include <stdio.h>
#include "message.h"
+enum {
+ STATE_OFFLINE,
+ STATE_PINGED,
+ STATE_ONLINE,
+};
+
struct friend {
char *name;
char *address;
uint16_t port;
GInetSocketAddress *saddr;
+ int state;
};
static GSocket *usock;
return 0;
}
+void friend_timeout(struct friend *friend)
+{
+ if (friend->state == STATE_PINGED) {
+ friend->state = STATE_OFFLINE;
+ }
+}
+
+void friend_got_message(struct friend *friend, char *buffer, size_t len)
+{
+ if (len >= 4 && !strncmp(buffer, "PING", 4)) {
+ pong(friend);
+ } else if (len >= 4 && !strncmp(buffer, "PONG", 4)) {
+ friend->state = STATE_ONLINE;
+ }
+}
+
struct cache {
GList *friends;
};
g_object_unref(addr);
cache->friends = g_list_append(cache->friends, friend);
ping(friend);
+ friend->state = STATE_PINGED;
return 0;
}