summaryrefslogtreecommitdiff
path: root/telepathy-ytstenut-glib/tests/server-client-pong.c
blob: 56d3be68d64a170b280382c4ca32b96cfff57b9d (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
/*
 * client-pong.c - return a message when an incoming channel appears
 *
 * Copyright (C) 2011 Intel Corp.
 *
 * 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
 */

#include "config.h"

#include <telepathy-ytstenut-glib/telepathy-ytstenut-glib.h>

#include <telepathy-glib/account.h>

static GMainLoop *loop = NULL;

static void
getoutofhere (void)
{
  g_main_loop_quit (loop);
}

static void
reply_cb (GObject *source_object,
    GAsyncResult *result,
    gpointer user_data)
{
  TpYtsChannel *channel = TP_YTS_CHANNEL (source_object);
  GError *error = NULL;

  /* Done */

  if (!tp_yts_channel_reply_finish (channel, result, &error))
    g_printerr ("Failed to reply: %s\n", error->message);
  else
    g_print ("Successfully replied\n");

  g_clear_error (&error);
  getoutofhere ();
}

static void
received_channels (TpYtsClient *client,
    gpointer user_data)
{
  TpYtsChannel *channel;

  /* ::received-channels was emitted, so let's reply to each channel
   * which has popped up. */

  g_print ("received channels\n");

  while ((channel = tp_yts_client_accept_channel (client)) != NULL)
    {
      /* We'll create some rubbish to put in the request attribute
       * argument of Reply */
      GHashTable *tmp = g_hash_table_new (g_str_hash, g_str_equal);
      g_hash_table_insert (tmp, "nyan", "cat");

      g_print ("request type: %u\n",
          tp_yts_channel_get_request_type (channel));
      g_print ("request attributes: %p\n",
          tp_yts_channel_get_request_attributes (channel));
      g_print ("request body: %s\n",
          tp_yts_channel_get_request_body (channel));
      g_print ("target service: %s\n",
          tp_yts_channel_get_target_service (channel));
      g_print ("initiator service: %s\n",
          tp_yts_channel_get_initiator_service (channel));

      /* Call Reply() */
      tp_yts_channel_reply_async (channel, tmp, NULL,
          NULL, reply_cb, NULL);
    }
}

static void
got_account (TpAccount *account,
    gpointer user_data)
{
  TpYtsClient *client;

  /* Now we can create the Ytstenut channel handler with service
   * name specified on the command line. */
  client = tp_yts_client_new ((const gchar *) user_data, account);
  g_signal_connect (client, "received-channels",
      G_CALLBACK (received_channels), NULL);

  tp_yts_client_register (client, NULL);

  g_print ("listening...\n");
}

static void
account_prepared_cb (GObject *source_object,
    GAsyncResult *result,
    gpointer user_data)
{
  TpAccount *account = TP_ACCOUNT (source_object);
  GError *error = NULL;

  if (!tp_account_prepare_finish (account, result, &error))
    {
      g_print ("failed to prepare account: %s\n", error->message);
      g_clear_error (&error);
      getoutofhere ();
      return;
    }

  got_account (account, user_data);
}

int
main (int argc,
    char **argv)
{
  TpDBusDaemon *dbus;
  TpAccount *account;
  gchar *path;

  if (argv[1] == NULL || argv[2] == NULL
      || !tp_dbus_check_valid_interface_name (argv[2], NULL))
    {
      g_print ("usage: %s [account] [service name]\n", argv[0]);
      return 1;
    }

  g_type_init ();

  dbus = tp_dbus_daemon_dup (NULL);

  path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE, argv[1]);
  account = tp_account_new (dbus, path, NULL);
  g_free (path);

  tp_account_prepare_async (account, NULL, account_prepared_cb, argv[2]);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  g_main_loop_unref (loop);

  g_object_unref (account);
  g_object_unref (dbus);

  return 0;
}