diff options
author | Will Thompson <will.thompson@collabora.co.uk> | 2009-11-19 15:25:32 +0000 |
---|---|---|
committer | Will Thompson <will.thompson@collabora.co.uk> | 2009-11-20 18:42:48 +0000 |
commit | 2ee360dce6b0ea69d0f4d1fdea4e1148291a13d0 (patch) | |
tree | daa98d15f235caefbbd266e905c082d7fbee0b0c /plugins | |
parent | 3562de5e5ba3a00f4e9969a69d3771786105edf1 (diff) |
Test ensuring a sidecar which pings a server first
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/test.c | 207 | ||||
-rw-r--r-- | plugins/test.h | 34 |
2 files changed, 241 insertions, 0 deletions
diff --git a/plugins/test.c b/plugins/test.c index 3be25dae..ecc1af55 100644 --- a/plugins/test.c +++ b/plugins/test.c @@ -24,11 +24,13 @@ static void plugin_iface_init ( #define IFACE_TEST "org.freedesktop.Telepathy.Gabble.Plugin.Test" #define IFACE_TEST_PROPS IFACE_TEST ".Props" #define IFACE_TEST_BUGGY IFACE_TEST ".Buggy" +#define IFACE_TEST_IQ IFACE_TEST ".IQ" static const gchar * const sidecar_interfaces[] = { IFACE_TEST, IFACE_TEST_PROPS, IFACE_TEST_BUGGY, + IFACE_TEST_IQ, NULL }; @@ -48,6 +50,33 @@ test_plugin_class_init (TestPluginClass *klass) } static void +sidecar_iq_created_cb ( + GObject *source, + GAsyncResult *new_result, + gpointer user_data) +{ + GSimpleAsyncResult *result = user_data; + GabbleSidecar *sidecar = GABBLE_SIDECAR (source); + GError *error = NULL; + + if (g_async_initable_init_finish (G_ASYNC_INITABLE (source), + new_result, &error)) + { + g_simple_async_result_set_op_res_gpointer (result, sidecar, + g_object_unref); + } + else + { + g_simple_async_result_set_from_error (result, error); + g_clear_error (&error); + g_object_unref (sidecar); + } + + g_simple_async_result_complete (result); + g_object_unref (result); +} + +static void test_plugin_create_sidecar ( GabblePlugin *plugin, const gchar *sidecar_interface, @@ -72,6 +101,12 @@ test_plugin_create_sidecar ( { sidecar = g_object_new (TEST_TYPE_SIDECAR_PROPS, NULL); } + else if (!tp_strdiff (sidecar_interface, IFACE_TEST_IQ)) + { + g_async_initable_new_async (TEST_TYPE_SIDECAR_IQ, G_PRIORITY_DEFAULT, + NULL, sidecar_iq_created_cb, result, "session", session, NULL); + return; + } else { /* This deliberately doesn't check for IFACE_TEST_BUGGY, to test Gabble's @@ -202,5 +237,177 @@ static void sidecar_props_iface_init ( iface->get_immutable_properties = sidecar_props_get_immutable_properties; } +/******************************** + * TestSidecarIQ implementation * + ********************************/ + +static void sidecar_iq_iface_init ( + gpointer g_iface, + gpointer data); +static void async_initable_iface_init ( + gpointer g_iface, + gpointer data); + +G_DEFINE_TYPE_WITH_CODE (TestSidecarIQ, test_sidecar_iq, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GABBLE_TYPE_SIDECAR, sidecar_iq_iface_init); + G_IMPLEMENT_INTERFACE (GABBLE_TYPE_SVC_GABBLE_PLUGIN_TEST, NULL); + G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init); + ) + +enum { + PROP_SESSION = 1 +}; + +static void +test_sidecar_iq_init (TestSidecarIQ *object) +{ + DEBUG ("%p", object); +} + +static void +test_sidecar_iq_set_property ( + GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + TestSidecarIQ *self = TEST_SIDECAR_IQ (object); + + switch (property_id) + { + case PROP_SESSION: + self->session = g_value_dup_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + } +} + +static void +test_sidecar_iq_dispose (GObject *object) +{ + TestSidecarIQ *self = TEST_SIDECAR_IQ (object); + + DEBUG ("called for %p", object); + + if (self->session != NULL) + { + g_object_unref (self->session); + self->session = NULL; + } +} + +static void +test_sidecar_iq_class_init (TestSidecarIQClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->set_property = test_sidecar_iq_set_property; + object_class->dispose = test_sidecar_iq_dispose; + + g_object_class_install_property (object_class, PROP_SESSION, + g_param_spec_object ("session", "SESSION", + "THIS IS A WOCKY SESSION YOU CAN TELL BY THE TYPE", + WOCKY_TYPE_SESSION, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); +} + +static void +sidecar_iq_iface_init ( + gpointer g_iface, + gpointer data) +{ + GabbleSidecarInterface *iface = g_iface; + + iface->interface = IFACE_TEST_IQ; + iface->get_immutable_properties = NULL; +} + +static void +iq_cb ( + GObject *source, + GAsyncResult *nested_result, + gpointer user_data) +{ + GSimpleAsyncResult *result = user_data; + GError *error = NULL; + WockyXmppStanza *reply; + + reply = wocky_porter_send_iq_finish (WOCKY_PORTER (source), + nested_result, &error); + + if (reply == NULL) + { + g_simple_async_result_set_from_error (result, error); + g_clear_error (&error); + } + else + { + WockyStanzaSubType t; + wocky_xmpp_stanza_get_type_info (reply, NULL, &t); + if (t == WOCKY_STANZA_SUB_TYPE_RESULT) + g_simple_async_result_set_op_res_gboolean (result, TRUE); + else + g_simple_async_result_set_error (result, TP_ERRORS, + TP_ERROR_NOT_AVAILABLE, "server said no!"); + + g_object_unref (reply); + } + + g_simple_async_result_complete (result); +} + +static void +sidecar_iq_init_async ( + GAsyncInitable *initable, + int io_priority, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + TestSidecarIQ *self = TEST_SIDECAR_IQ (initable); + GSimpleAsyncResult *result = g_simple_async_result_new (G_OBJECT (self), + callback, user_data, sidecar_iq_init_async); + WockyPorter *porter = wocky_session_get_porter (self->session); + WockyXmppStanza *iq; + + iq = wocky_xmpp_stanza_build (WOCKY_STANZA_TYPE_IQ, + WOCKY_STANZA_SUB_TYPE_GET, NULL, "sidecar.example.com", + WOCKY_NODE, "query", + WOCKY_NODE_XMLNS, "http://example.com/sidecar", + WOCKY_NODE, "oh-hai", + WOCKY_NODE_END, + WOCKY_NODE_END, + WOCKY_STANZA_END); + wocky_porter_send_iq_async (porter, iq, cancellable, iq_cb, result); +} + +static gboolean +sidecar_iq_init_finish ( + GAsyncInitable *initable, + GAsyncResult *result, + GError **error) +{ + GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result); + + if (g_simple_async_result_propagate_error (simple, error)) + return FALSE; + + g_return_val_if_fail (g_simple_async_result_is_valid (result, + G_OBJECT (initable), sidecar_iq_init_async), FALSE); + + return g_simple_async_result_get_op_res_gboolean (simple); +} + +static void +async_initable_iface_init ( + gpointer g_iface, + gpointer data) +{ + GAsyncInitableIface *iface = g_iface; + + iface->init_async = sidecar_iq_init_async; + iface->init_finish = sidecar_iq_init_finish; +} diff --git a/plugins/test.h b/plugins/test.h index d71c4841..8d4d25e8 100644 --- a/plugins/test.h +++ b/plugins/test.h @@ -1,5 +1,8 @@ #include <glib-object.h> +#include <gio/gio.h> +#include <wocky/wocky-session.h> + /* Plugin */ typedef struct _TestPluginClass TestPluginClass; typedef struct _TestPlugin TestPlugin; @@ -87,3 +90,34 @@ GType test_sidecar_props_get_type (void); #define TEST_SIDECAR_PROPS_GET_CLASS(obj) \ (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_SIDECAR_PROPS, \ TestSidecarPropsClass)) + +/* Sidecar which sends an IQ and waits for a reply before being ready. */ +typedef struct _TestSidecarIQClass TestSidecarIQClass; +typedef struct _TestSidecarIQ TestSidecarIQ; + +struct _TestSidecarIQClass { + GObjectClass parent; +}; + +struct _TestSidecarIQ { + GObject parent; + GSimpleAsyncResult *result; + WockySession *session; +}; + +GType test_sidecar_iq_get_type (void); + +#define TEST_TYPE_SIDECAR_IQ \ + (test_sidecar_iq_get_type ()) +#define TEST_SIDECAR_IQ(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), TEST_TYPE_SIDECAR_IQ, TestSidecarIQ)) +#define TEST_SIDECAR_IQ_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), TEST_TYPE_SIDECAR_IQ, \ + TestSidecarIQClass)) +#define TEST_IS_SIDECAR_IQ(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), TEST_TYPE_SIDECAR_IQ)) +#define TEST_IS_SIDECAR_IQ_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), TEST_TYPE_SIDECAR_IQ)) +#define TEST_SIDECAR_IQ_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_SIDECAR_IQ, \ + TestSidecarIQClass)) |