Commit 88bb7a8a authored by Julian Sparber's avatar Julian Sparber

[feat] create class for teleport peer and refactored avahi browser stuff

parent cf6a85f3
......@@ -4,7 +4,7 @@ CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0 avahi-client)
LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0 avahi-client)
GLIB_COMPILE_RESOURCES = $(shell $(PKGCONFIG) --variable=glib_compile_resources gio-2.0)
SRC = teleportapp.c teleportappwin.c main.c browser.c
SRC = teleportapp.c teleportappwin.c teleportpeer.c main.c browser.c
BUILT_SRC = resources.c
OBJS = $(BUILT_SRC:.c=.o) $(SRC:.c=.o)
......
......@@ -13,10 +13,13 @@
#include <avahi-common/error.h>
#include <avahi-common/thread-watch.h>
#include "teleportapp.h"
#include "teleportpeer.h"
#include "browser.h"
static AvahiSimplePoll *simple_poll = NULL;
static AvahiThreadedPoll *threaded_poll = NULL;
static AvahiClient *client = NULL;
static TeleportPeer *peerList = NULL;
static void resolve_callback(
AvahiServiceResolver *r,
......@@ -43,7 +46,8 @@ static void resolve_callback(
fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
avahi_address_snprint(a, sizeof(a), address);
t = avahi_string_list_to_string(txt);
teleport_app_add_peer(name, port, a);
teleport_peer_add_peer(peerList, g_strdup(name), g_strdup(a), port);
//teleport_app_add_peer(name, port, a);
/*fprintf(stderr,
"\t%s:%u (%s)\n"
"\tTXT=%s\n"
......@@ -92,12 +96,14 @@ static void browse_callback(
function we free it. If the server is terminated before
the callback function is called the server will free
the resolver for us. */
if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
//if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_INET, 0, resolve_callback, c)))
fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
break;
case AVAHI_BROWSER_REMOVE:
fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
teleport_app_remove_peer(name);
//teleport_app_remove_peer(name);
teleport_peer_remove_peer(peerList, g_strdup(name));
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
......@@ -158,7 +164,8 @@ fail:
}
*/
int run_avahi_service() {
int run_avahi_service(TeleportPeer* peers) {
peerList = peers;
/* Call this when the application starts up. */
int error;
......
#include <gtk/gtk.h>
#include "teleportapp.h"
#include "teleportpeer.h"
#include "teleportappwin.h"
#include "browser.h"
enum {
ADDPEER, REMOVEPEER
};
static TeleportAppWindow *win;
static TeleportApp *mainApp;
static gint signalIds [2];
struct _TeleportApp
{
GtkApplication parent;
};
typedef struct Peers {
char *name;
char *ip;
int port;
} Peer;
static Peer remote_peers[100];
G_DEFINE_TYPE(TeleportApp, teleport_app, GTK_TYPE_APPLICATION);
......@@ -50,7 +36,6 @@ void callback_remove_peer(GObject * instance, char * name, TeleportAppWindow * w
static void
teleport_app_init (TeleportApp *app)
{
mainApp = app;
}
......@@ -58,12 +43,19 @@ teleport_app_init (TeleportApp *app)
teleport_app_activate (GApplication *app)
{
//TeleportAppWindow *win;
TeleportPeer *peerList = g_object_new (TELEPORT_TYPE_PEER, NULL);
win = teleport_app_window_new (TELEPORT_APP (app));
gtk_window_present (GTK_WINDOW (win));
g_signal_connect (app, "addpeer", (GCallback)callback_add_peer, win);
g_signal_connect (app, "removepeer", (GCallback)callback_remove_peer, win);
run_avahi_service();
g_signal_connect (peerList, "addpeer", (GCallback)callback_add_peer, win);
g_signal_connect (peerList, "removepeer", (GCallback)callback_remove_peer, win);
/*teleport_peer_add_peer(peerList, "julian", "192.168.0.1", 3000);
g_print("Data: %s\n", teleport_peer_get_name(peerList, 0, NULL));
g_print("Data: %s\n", teleport_peer_get_ip(peerList, 0, NULL));
g_print("Data: %d\n", teleport_peer_get_port(peerList, 0, NULL));
*/
run_avahi_service(peerList);
}
static void
......@@ -92,28 +84,6 @@ teleport_app_class_init (TeleportAppClass *class)
{
G_APPLICATION_CLASS (class)->activate = teleport_app_activate;
G_APPLICATION_CLASS (class)->open = teleport_app_open;
//should acctualy be events form the avahi class
signalIds[ADDPEER] = g_signal_new ("addpeer",
G_TYPE_OBJECT,
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
0,
NULL /* accumulator */,
NULL /* accumulator data */,
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
signalIds[REMOVEPEER] = g_signal_new ("removepeer",
G_TYPE_OBJECT,
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
0,
NULL /* accumulator */,
NULL /* accumulator data */,
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
}
TeleportApp *
......@@ -124,14 +94,3 @@ teleport_app_new (void)
"flags", G_APPLICATION_HANDLES_OPEN,
NULL);
}
void teleport_app_add_peer (char *name, int port, char* addr) {
//g_print("\t%s:%u (%s)\n", name, port, addr);
g_signal_emit (mainApp, signalIds[ADDPEER], 0, name);
}
void teleport_app_remove_peer (char *name) {
g_signal_emit (mainApp, signalIds[REMOVEPEER], 0, name);
}
......@@ -9,8 +9,5 @@ G_DECLARE_FINAL_TYPE (TeleportApp, teleport_app, TELEPORT, APP, GtkApplication)
TeleportApp *teleport_app_new (void);
extern void teleport_app_add_peer();
extern void teleport_app_remove_peer();
#endif /* __TELEPORTAPP_H */
#include <gtk/gtk.h>
#include "teleportpeer.h"
enum {
ADD, REMOVE, N_SIGNALS
};
typedef struct Peers {
char *name;
char *ip;
gint port;
} Peer;
static gint signalIds [N_SIGNALS];
struct _TeleportPeer
{
GObject parent;
GArray *list;
/* instance members */
};
G_DEFINE_TYPE (TeleportPeer, teleport_peer, G_TYPE_OBJECT)
static void
teleport_peer_constructed (GObject *obj)
{
G_OBJECT_CLASS (teleport_peer_parent_class)->constructed (obj);
}
static void
teleport_peer_class_init (TeleportPeerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = teleport_peer_constructed;
signalIds[ADD] = g_signal_new ("addpeer",
G_TYPE_OBJECT,
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
0,
NULL /* accumulator */,
NULL /* accumulator data */,
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
signalIds[REMOVE] = g_signal_new ("removepeer",
G_TYPE_OBJECT,
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
0,
NULL /* accumulator */,
NULL /* accumulator data */,
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
}
static void
teleport_peer_init (TeleportPeer *self)
{
self->list = g_array_new (FALSE, FALSE, sizeof(Peer));
}
gchar * teleport_peer_get_name (TeleportPeer *self, gint index, GError **error)
{
//g_return_if_fail (TELEPORT_IS_PEER (self));
//g_return_if_fail (error == NULL || *error == NULL);
if (index > self->list->len-1)
return NULL;
Peer element = g_array_index(self->list, Peer, index);
return element.name;
}
gchar * teleport_peer_get_ip (TeleportPeer *self, gint index, GError **error)
{
//g_return_if_fail (TELEPORT_IS_PEER (self));
//g_return_if_fail (error == NULL || *error == NULL);
Peer element = g_array_index(self->list, Peer, index);
if (index > self->list->len-1)
return NULL;
return element.ip;
}
gint teleport_peer_get_port (TeleportPeer *self, gint index, GError **error)
{
Peer element = g_array_index(self->list, Peer, index);
if (index > self->list->len-1)
return 0;
return element.port;
}
void teleport_peer_add_peer (TeleportPeer *self, gchar * name, gchar * ip, gint port)
{
Peer new;
new.ip = ip;
new.port = port;
new.name = name;
g_array_append_val(self->list, new);
g_signal_emit (self, signalIds[ADD], 0, name);
}
void teleport_peer_remove_peer (TeleportPeer *self, gchar * name)
{
Peer element;
gboolean found = FALSE;
for (int i = 0; i < self->list->len && !found; i++) {
Peer element = g_array_index(self->list, Peer, i);
if (g_strcmp0(element.name, name) == 0) {
found = TRUE;
g_array_remove_index(self->list, i);
}
}
g_signal_emit (self, signalIds[REMOVE], 0, name);
}
#ifndef __TELEPORTPEER_H
#define __TELEPORTPEER_H
#include <gtk/gtk.h>
#define TELEPORT_TYPE_PEER teleport_peer_get_type ()
G_DECLARE_FINAL_TYPE (TeleportPeer, teleport_peer, TELEPORT, PEER, GObject)
gchar* teleport_peer_get_name (TeleportPeer *self, gint index, GError **error);
gchar* teleport_peer_get_ip (TeleportPeer *self, gint index, GError **error);
gint teleport_peer_get_port (TeleportPeer *self, gint index, GError **error);
void teleport_peer_add_peer (TeleportPeer *self, gchar * name, gchar * ip, gint port);
void teleport_peer_remove_peer (TeleportPeer *self, gchar * name);
#endif /* __TELEPORTPEER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment