summaryrefslogtreecommitdiff
path: root/examples/client/stream-tubes/offerer.c
blob: af31a22e80501bcda0c628de15c7cf2254bede2b (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
#include <telepathy-glib/telepathy-glib.h>

static GMainLoop *loop = NULL;


static void
_incoming_iostream (TpStreamTubeChannel *tube,
    TpContact *contact,
    GIOStream *iostream,
    gpointer user_data)
{
  GInputStream *in;
  GOutputStream *out;
  char buf[128] = { 0, };
  GError *error = NULL;

  g_debug ("Got IOStream from %s",
      tp_contact_get_identifier (contact));

  in = g_io_stream_get_input_stream (iostream);
  out = g_io_stream_get_output_stream (iostream);

  /* this bit is not a good example */
  g_output_stream_write (out, "Pong", 4, NULL, &error);
  g_assert_no_error (error);

  g_input_stream_read (in, &buf, sizeof (buf), NULL, &error);
  g_assert_no_error (error);

  g_debug ("Send Pong got: %s", buf);

  // FIXME: close the channel

  g_object_unref (tube);

  g_main_loop_quit (loop);
}


static void
_tube_offered (GObject *tube,
    GAsyncResult *res,
    gpointer user_data)
{
  GError *error = NULL;

  if (!tp_stream_tube_channel_offer_finish (TP_STREAM_TUBE_CHANNEL (tube), res, &error))
    {
      g_debug ("Failed to offer tube: %s", error->message);

      g_error_free (error);
      return;
    }

  g_debug ("Tube offered");
}

static void
tube_invalidated_cb (TpStreamTubeChannel *tube,
    guint domain,
    gint code,
    gchar *message,
    gpointer user_data)
{
  g_debug ("Tube has been invalidated: %s", message);
}

static void
_channel_created (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  TpChannel *channel;
  GError *error = NULL;
  TpStreamTubeChannel *tube;

  channel = tp_account_channel_request_create_and_handle_channel_finish (
      TP_ACCOUNT_CHANNEL_REQUEST (source), result, NULL, &error);
  if (channel == NULL)
    {
      g_debug ("Failed to create channel: %s", error->message);

      g_error_free (error);
      return;
    }

  g_debug ("Channel created: %s", tp_proxy_get_object_path (channel));

  tube = tp_stream_tube_channel_new (tp_channel_borrow_connection (channel),
      tp_proxy_get_object_path (channel),
      tp_channel_borrow_immutable_properties (channel),
      &error);
  g_assert_no_error (error);

  g_object_unref (channel);

  g_signal_connect (tube, "incoming",
      G_CALLBACK (_incoming_iostream), NULL);
  g_signal_connect (tube, "invalidated",
      G_CALLBACK (tube_invalidated_cb), NULL);

  tp_stream_tube_channel_offer_async (tube, NULL, _tube_offered, NULL);
}

int
main (int argc,
    const char **argv)
{
  TpDBusDaemon *dbus;
  TpAccount *account;
  char *account_path;
  GError *error = NULL;
  TpAccountChannelRequest *req;
  GHashTable *request;

  g_assert (argc == 3);

  g_type_init ();

  dbus = tp_dbus_daemon_dup (&error);
  g_assert_no_error (error);

  account_path = g_strconcat (TP_ACCOUNT_OBJECT_PATH_BASE, argv[1], NULL);
  account = tp_account_new (dbus, account_path, &error);
  g_assert_no_error (error);
  g_free (account_path);

  request = tp_asv_new (
      TP_PROP_CHANNEL_CHANNEL_TYPE,
      G_TYPE_STRING,
      TP_IFACE_CHANNEL_TYPE_STREAM_TUBE,

      TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
      G_TYPE_UINT,
      TP_HANDLE_TYPE_CONTACT,

      TP_PROP_CHANNEL_TARGET_ID,
      G_TYPE_STRING,
      argv[2],

      TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE,
      G_TYPE_STRING,
      "ExampleService",

      NULL);

  g_debug ("Offer channel to %s", argv[2]);

  req = tp_account_channel_request_new (account, request,
      TP_USER_ACTION_TIME_CURRENT_TIME);

  tp_account_channel_request_create_and_handle_channel_async (req, NULL,
      _channel_created, NULL);

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

  g_object_unref (account);
  g_object_unref (req);
  g_hash_table_unref (request);
  g_main_loop_unref (loop);

  return 0;
}