summaryrefslogtreecommitdiffstats
path: root/avahi-gobject/ga-client.c
blob: 1874bb1102a86598fd5ea75f6060fd0cf5302f67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * ga-client.c - Source for GaClient
 * Copyright (C) 2006-2007 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include "ga-client.h"

#include "ga-client-enumtypes.h"
#include "ga-error.h"

/* FIXME what to do about glib-malloc ? */
#include <avahi-glib/glib-watch.h>
#include <avahi-glib/glib-malloc.h>
#include <avahi-common/error.h>
#include <avahi-common/timeval.h>

G_DEFINE_TYPE(GaClient, ga_client, G_TYPE_OBJECT)

/* signal enum */
enum {
    STATE_CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

/* properties */
enum {
    PROP_STATE = 1,
    PROP_FLAGS
};

/* private structure */
typedef struct _GaClientPrivate GaClientPrivate;

struct _GaClientPrivate {
    AvahiGLibPoll *poll;
    GaClientFlags flags;
    GaClientState state;
    gboolean dispose_has_run;
};

#define GA_CLIENT_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), GA_TYPE_CLIENT, GaClientPrivate))

static void ga_client_init(GaClient * self) {
    GaClientPrivate *priv = GA_CLIENT_GET_PRIVATE(self);
    /* allocate any data required by the object here */
    self->avahi_client = NULL;
    priv->state = GA_CLIENT_STATE_NOT_STARTED;
    priv->flags = GA_CLIENT_FLAG_NO_FLAGS;
}

static void ga_client_dispose(GObject * object);
static void ga_client_finalize(GObject * object);

static void ga_client_set_property(GObject * object,
                       guint property_id,
                       const GValue * value, GParamSpec * pspec) {
    GaClient *client = GA_CLIENT(object);
    GaClientPrivate *priv = GA_CLIENT_GET_PRIVATE(client);

    switch (property_id) {
        case PROP_FLAGS:
            g_assert(client->avahi_client == NULL);
            priv->flags = g_value_get_enum(value);
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
            break;
    }
}

static void ga_client_get_property(GObject * object,
                       guint property_id,
                       GValue * value, GParamSpec * pspec) {
    GaClient *client = GA_CLIENT(object);
    GaClientPrivate *priv = GA_CLIENT_GET_PRIVATE(client);

    switch (property_id) {
        case PROP_STATE:
            g_value_set_enum(value, priv->state);
            break;
        case PROP_FLAGS:
            g_value_set_enum(value, priv->flags);
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
            break;
    }
}

static void ga_client_class_init(GaClientClass * ga_client_class) {
    GObjectClass *object_class = G_OBJECT_CLASS(ga_client_class);
    GParamSpec *param_spec;

    g_type_class_add_private(ga_client_class, sizeof (GaClientPrivate));


    object_class->dispose = ga_client_dispose;
    object_class->finalize = ga_client_finalize;

    object_class->set_property = ga_client_set_property;
    object_class->get_property = ga_client_get_property;

    param_spec = g_param_spec_enum("state", "Client state",
                                   "The state of the Avahi client",
                                   GA_TYPE_CLIENT_STATE,
                                   GA_CLIENT_STATE_NOT_STARTED,
                                   G_PARAM_READABLE |
                                   G_PARAM_STATIC_NAME |
                                   G_PARAM_STATIC_BLURB);
    g_object_class_install_property(object_class, PROP_STATE, param_spec);

    param_spec = g_param_spec_enum("flags", "Client flags",
                                   "The flags the Avahi client is started with",
                                   GA_TYPE_CLIENT_FLAGS,
                                   GA_CLIENT_FLAG_NO_FLAGS,
                                   G_PARAM_READWRITE |
                                   G_PARAM_CONSTRUCT_ONLY |
                                   G_PARAM_STATIC_NAME |
                                   G_PARAM_STATIC_BLURB);
    g_object_class_install_property(object_class, PROP_FLAGS, param_spec);

    signals[STATE_CHANGED] =
            g_signal_new("state-changed",
                         G_OBJECT_CLASS_TYPE(ga_client_class),
                         G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                         0,
                         NULL, NULL,
                         g_cclosure_marshal_VOID__ENUM,
                         G_TYPE_NONE, 1, GA_TYPE_CLIENT_STATE);

}

void ga_client_dispose(GObject * object) {
    GaClient *self = GA_CLIENT(object);
    GaClientPrivate *priv = GA_CLIENT_GET_PRIVATE(self);

    if (priv->dispose_has_run)
        return;

    priv->dispose_has_run = TRUE;

    if (self->avahi_client) {
        avahi_client_free(self->avahi_client);
        self->avahi_client = NULL;
    }
    if (priv->poll) {
        avahi_glib_poll_free(priv->poll);
        priv->poll = NULL;
    }

    /* release any references held by the object here */
    if (G_OBJECT_CLASS(ga_client_parent_class)->dispose)
        G_OBJECT_CLASS(ga_client_parent_class)->dispose(object);
}

void ga_client_finalize(GObject * object) {

    /* free any data held directly by the object here */
    G_OBJECT_CLASS(ga_client_parent_class)->finalize(object);
}

GaClient *ga_client_new(GaClientFlags flags) {
    return g_object_new(GA_TYPE_CLIENT, "flags", flags, NULL);
}

static GQuark detail_for_state(AvahiClientState state) {
    static struct {
        AvahiClientState state;
        const gchar *name;
        GQuark quark;
    } states[] = {
        { AVAHI_CLIENT_S_REGISTERING, "registering", 0},
        { AVAHI_CLIENT_S_RUNNING, "running", 0},
        { AVAHI_CLIENT_S_COLLISION, "collistion", 0},
        { AVAHI_CLIENT_FAILURE, "failure", 0},
        { AVAHI_CLIENT_CONNECTING, "connecting", 0},
        { 0, NULL, 0}
    };
    int i;

    for (i = 0; states[i].name != NULL; i++) {
        if (state != states[i].state)
            continue;

        if (!states[i].quark)
            states[i].quark = g_quark_from_static_string(states[i].name);
/*         printf("Detail: %s\n", states[i].name); */
        return states[i].quark;
    }
    g_assert_not_reached();
}

static void _avahi_client_cb(AvahiClient * c, AvahiClientState state, void *data) {
    GaClient *self = GA_CLIENT(data);
    GaClientPrivate *priv = GA_CLIENT_GET_PRIVATE(self);

/*     printf("CLIENT CB: %d\n", state); */

    /* Avahi can call the callback before return from _client_new */
    if (self->avahi_client == NULL)
        self->avahi_client = c;

    g_assert(c == self->avahi_client);
    priv->state = state;
    g_signal_emit(self, signals[STATE_CHANGED],
                  detail_for_state(state), state);
}

gboolean ga_client_start(GaClient * client, GError ** error) {
    GaClientPrivate *priv = GA_CLIENT_GET_PRIVATE(client);
    AvahiClient *aclient;
    int aerror;

    g_assert(client->avahi_client == NULL);
    g_assert(priv->poll == NULL);

    avahi_set_allocator(avahi_glib_allocator());

    priv->poll = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);

    aclient = avahi_client_new(avahi_glib_poll_get(priv->poll),
                               priv->flags,
                               _avahi_client_cb, client, &aerror);
    if (aclient == NULL) {
        if (error != NULL) {
            *error = g_error_new(GA_ERROR, aerror,
                                 "Failed to create avahi client: %s",
                                 avahi_strerror(aerror));
        }
        return FALSE;
    }
    client->avahi_client = aclient;
    return TRUE;
}