Ping a friend when we get it from the cache
[cascardo/f2fchat.git] / friend.c
index f749b4d..2d317e7 100644 (file)
--- a/friend.c
+++ b/friend.c
@@ -60,9 +60,22 @@ struct cache {
        GList *friends;
 };
 
+static struct cache *ucache;
+
+struct friend *friend_get_by_address(GInetAddress *address)
+{
+       GList *l;
+       for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
+               struct friend *friend = l->data;
+               if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address))
+                       return friend;
+       }
+       return NULL;
+}
+
 int create_cache(struct cache **cache)
 {
-       *cache = g_slice_new0(struct cache);
+       ucache = *cache = g_slice_new0(struct cache);
        (*cache)->friends = NULL;
        return 0;
 }
@@ -83,6 +96,11 @@ int destroy_cache(struct cache *cache)
        g_slice_free(struct cache, cache);
 }
 
+char * friend_get_name(struct friend *friend)
+{
+       return friend->name;
+}
+
 int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
 {
        struct friend *friend;
@@ -95,6 +113,7 @@ int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t po
        friend->saddr = G_INET_SOCKET_ADDRESS(g_inet_socket_address_new(addr, friend->port));
        g_object_unref(addr);
        cache->friends = g_list_append(cache->friends, friend);
+       ping(friend);
        return 0;
 }