Use the printf channel in our main example.
[cascardo/sgp.git] / src / main.c
index 9ce1fb7..a10aaa3 100644 (file)
  */
 
 #include <sgp/share.h>
+#include <sgp/channels/printf.h>
 
 int main(int argc, char **argv)
 {
        struct sgp_group *group;
        struct sgp_msg *msg;
        struct sgp_friend *friend;
+       int r;
        /* TODO: access a database here? */
-       group = sgp_group_new();
        friend = sgp_friend_new("Thadeu Cascardo");
+       if (!friend)
+               return 1;
+       sgp_friend_set_channel(friend, sgp_printf_channel());
+       group = sgp_group_new("Myself");
+       if (!group)
+               goto out_group;
        msg = sgp_msg_new("New message for you");
-       sgp_group_add_friend(group, friend);
-       sgp_share(group, msg);
-       return 0;
+       if (!msg)
+               goto out_msg;
+       r = sgp_group_add_friend(group, friend);
+       if (r)
+               goto out_add;
+       r = sgp_share(group, msg);
+       sgp_msg_destroy(msg);
+       sgp_group_destroy(group);
+       /* we must only destroy the friend after all group references
+        * are gone */
+       sgp_friend_destroy(friend);
+       return r;
+out_add:
+       sgp_msg_destroy(msg);
+out_msg:
+       sgp_group_destroy(group);
+out_group:
+       sgp_friend_destroy(friend);
+       return 1;
 }