Respond pings with pongs.
[cascardo/f2fchat.git] / friend.c
index 68deccd..03d306d 100644 (file)
--- a/friend.c
+++ b/friend.c
 #include <stdio.h>
 #include "message.h"
 
 #include <stdio.h>
 #include "message.h"
 
+enum {
+       STATE_OFFLINE,
+       STATE_PINGED,
+       STATE_ONLINE,
+};
+
 struct friend {
        char *name;
        char *address;
        uint16_t port;
        GInetSocketAddress *saddr;
 struct friend {
        char *name;
        char *address;
        uint16_t port;
        GInetSocketAddress *saddr;
+       int state;
 };
 
 static GSocket *usock;
 };
 
 static GSocket *usock;
@@ -64,6 +71,22 @@ int friend_send_message(struct friend *friend, char *buffer, size_t len)
        return 0;
 }
 
        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;
 };
 struct cache {
        GList *friends;
 };
@@ -123,6 +146,7 @@ int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t po
        g_object_unref(addr);
        cache->friends = g_list_append(cache->friends, friend);
        ping(friend);
        g_object_unref(addr);
        cache->friends = g_list_append(cache->friends, friend);
        ping(friend);
+       friend->state = STATE_PINGED;
        return 0;
 }
 
        return 0;
 }