summaryrefslogtreecommitdiff
path: root/examples/client/stream-tubes/accepter.c
blob: ca0649dd384cedcb1eeba71b21c35301f0b99e1a (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
#include "config.h"

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

static GMainLoop *loop = NULL;

static void
tube_conn_closed_cb (TpStreamTubeConnection *conn,
    const GError *error,
    gpointer user_data)
{
  g_message ("Tube connection has been closed: %s", error->message);
}

static void
_tube_accepted (GObject *tube,
    GAsyncResult *res,
    gpointer user_data)
{
  TpHandleChannelContext *context = user_data;
  TpStreamTubeConnection *tube_conn;
  GSocketConnection *conn;
  GInputStream *in;
  GOutputStream *out;
  char buf[128] = { 0, };
  GError *error = NULL;

  tube_conn = tp_stream_tube_channel_accept_finish (
      TP_STREAM_TUBE_CHANNEL (tube), res, &error);

  g_signal_connect (tube_conn, "closed",
      G_CALLBACK (tube_conn_closed_cb), NULL);

  if (error != NULL)
    {
      g_message ("Can't accept the tube: %s", error->message);
      tp_handle_channel_context_fail (context, error);
      g_error_free (error);
      return;
    }

  tp_handle_channel_context_accept (context);
  g_object_unref (context);

  g_message ("Tube open, have IOStream");

  conn = tp_stream_tube_connection_get_socket_connection (tube_conn);

  in = g_io_stream_get_input_stream (G_IO_STREAM (conn));
  out = g_io_stream_get_output_stream (G_IO_STREAM (conn));

  /* this bit is not a good example */
  g_message ("Sending: Ping");
  g_output_stream_write (out, "Ping\n", 5, NULL, &error);
  g_assert_no_error (error);

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

  g_message ("Received: %s", buf);

  g_object_unref (tube_conn);
}

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

static void
_handle_channel (TpSimpleHandler *handler,
    TpAccount *account,
    TpConnection *conn,
    TpChannel *channel,
    GList *requests,
    gint64 action_time,
    TpHandleChannelContext *context,
    gpointer user_data)
{
  g_message ("Handling channel; accepting tube");

  g_signal_connect (channel, "invalidated",
      G_CALLBACK (tube_invalidated_cb), NULL);

  tp_stream_tube_channel_accept_async (TP_STREAM_TUBE_CHANNEL (channel),
      _tube_accepted, context);

  g_message ("Delaying channel acceptance");

  tp_handle_channel_context_delay (context);
  g_object_ref (context);
}


int
main (int argc,
    const char **argv)
{
  TpAccountManager *manager;
  TpBaseClient *handler;
  GError *error = NULL;

  manager = tp_account_manager_dup ();
  handler = tp_simple_handler_new_with_am (manager, FALSE, FALSE,
      "ExampleServiceHandler", FALSE, _handle_channel, NULL, NULL);

  tp_base_client_take_handler_filter (handler, tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE,
        G_TYPE_STRING,
        TP_IFACE_CHANNEL_TYPE_STREAM_TUBE1,

        TP_PROP_CHANNEL_TARGET_ENTITY_TYPE,
        G_TYPE_UINT,
        TP_ENTITY_TYPE_CONTACT,

        TP_PROP_CHANNEL_TYPE_STREAM_TUBE1_SERVICE,
        G_TYPE_STRING,
        "ExampleService",

        NULL));

  tp_base_client_register (handler, &error);
  g_assert_no_error (error);

  g_message ("Waiting for tube offer");

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

  g_main_loop_unref (loop);
  g_object_unref (handler);
  g_object_unref (manager);

  return 0;
}