File: inf-test-chat.c

package info (click to toggle)
libinfinity 0.4.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 12,332 kB
  • ctags: 7,384
  • sloc: ansic: 65,073; xml: 36,770; sh: 10,347; makefile: 728
file content (398 lines) | stat: -rw-r--r-- 10,182 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* libinfinity - a GObject-based infinote implementation
 * Copyright (C) 2007, 2008, 2009 Armin Burgmeier <armin@arbur.net>
 *
 * 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 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 <libinfinity/client/infc-browser.h>
#include <libinfinity/communication/inf-communication-manager.h>
#include <libinfinity/common/inf-chat-buffer.h>
#include <libinfinity/common/inf-chat-session.h>
#include <libinfinity/common/inf-xmpp-connection.h>
#include <libinfinity/common/inf-tcp-connection.h>
#include <libinfinity/common/inf-ip-address.h>
#include <libinfinity/common/inf-standalone-io.h>
#include <libinfinity/common/inf-io.h>

#include <string.h>

typedef struct _InfTestChat InfTestChat;
struct _InfTestChat {
  InfStandaloneIo* io;
  InfXmppConnection* conn;
  InfcBrowser* browser;
  int input_fd;

  InfChatBuffer* buffer;
  InfUser* self;
};

static void
inf_test_chat_input_cb(InfNativeSocket* fd,
                       InfIoEvent io,
                       gpointer user_data)
{
  InfTestChat* test;
  char buffer[1024];

  test = (InfTestChat*)user_data;

  if(io & INF_IO_ERROR)
  {
  }

  if(io & INF_IO_INCOMING)
  {
    fgets(buffer, sizeof(buffer), stdin);
    if(strlen(buffer) != sizeof(buffer) || buffer[sizeof(buffer)-2] == '\n')
    {
      buffer[strlen(buffer)-1] = '\0';

      if(test->buffer != NULL && test->self != NULL)
      {
        inf_chat_buffer_add_message(
          test->buffer,
          test->self,
          buffer,
          strlen(buffer),
          time(NULL)
        );
      }
    }
  }
}

static void
inf_chat_test_buffer_receive_message_cb(InfChatSession* session,
                                        InfChatBufferMessage* message,
                                        gpointer user_data)
{
  switch(message->type)
  {
  case INF_CHAT_BUFFER_MESSAGE_NORMAL:
    printf("<%s> %s\n", inf_user_get_name(message->user), message->text);
    break;
  case INF_CHAT_BUFFER_MESSAGE_EMOTE:
    printf(" * %s %s\n", inf_user_get_name(message->user), message->text);
    break;
  case INF_CHAT_BUFFER_MESSAGE_USERJOIN:
    printf(" --> %s has joined\n", inf_user_get_name(message->user));
    break;
  case INF_CHAT_BUFFER_MESSAGE_USERPART:
    printf(" <-- %s has left\n", inf_user_get_name(message->user));
    break;
  }
}

static void
inf_test_chat_userjoin_finished_cb(InfcRequest* request,
                                   InfUser* user,
                                   gpointer user_data)
{
  InfTestChat* test;
  test = (InfTestChat*)user_data;

  printf("User join complete. Start chatting!\n");

  inf_io_watch(
    INF_IO(test->io),
    &test->input_fd,
    INF_IO_INCOMING | INF_IO_ERROR,
    inf_test_chat_input_cb,
    test,
    NULL
  );

  test->self = user;
}

static void
inf_test_chat_userjoin_failed_cb(InfcRequest* request,
                                 const GError* error,
                                 gpointer user_data)
{
  fprintf(stderr, "User join failed: %s\n", error->message);
  fprintf(stderr, "Chat will be read-only\n");
}

static void
inf_chat_test_session_synchronization_complete_cb(InfSession* session,
                                                  InfXmlConnection* connection,
                                                  gpointer user_data)
{
  InfTestChat* test;
  InfcSessionProxy* proxy;
  InfcUserRequest* request;
  GParameter params[1] = { { "name", { 0 } } };
  GError* error;

  printf("Synchronization complete, joining user...\n");

  test = (InfTestChat*)user_data;
  proxy = infc_browser_get_chat_session(test->browser);

  g_value_init(&params[0].value, G_TYPE_STRING);
  g_value_set_string(&params[0].value, g_get_user_name());

  error = NULL;
  request = infc_session_proxy_join_user(
    proxy,
    params,
    G_N_ELEMENTS(params),
    &error
  );

  g_value_unset(&params[0].value);

  if(!request)
  {
    fprintf(stderr, "User join failed: %s\n", error->message);
    g_error_free(error);

    inf_standalone_io_loop_quit(test->io);
  }
  else
  {
    g_signal_connect_after(
      G_OBJECT(request),
      "failed",
      G_CALLBACK(inf_test_chat_userjoin_failed_cb),
      test
    );

    g_signal_connect_after(
      G_OBJECT(request),
      "finished",
      G_CALLBACK(inf_test_chat_userjoin_finished_cb),
      test
    );
  }
}

static void
inf_chat_test_session_synchronization_failed_cb(InfSession* session,
                                                InfXmlConnection* connection,
                                                const GError* error,
                                                gpointer user_data)
{
  InfTestChat* test;
  test = (InfTestChat*)user_data;

  fprintf(stderr, "Synchronization failed: %s\n", error->message);
  inf_standalone_io_loop_quit(test->io);
}

static void
inf_chat_test_session_close_cb(InfSession* session,
                               gpointer user_data)
{
  InfTestChat* test;
  test = (InfTestChat*)user_data;

  printf("The server closed the chat session\n");
  inf_standalone_io_loop_quit(test->io);
}

static void
inf_chat_test_subscribe_finished_cb(InfcNodeRequest* request,
                                    const InfcBrowserIter* iter,
                                    gpointer user_data)
{
  InfTestChat* test;
  InfSession* session;
  test = (InfTestChat*)user_data;

  printf("Subscription successful, waiting for synchronization...\n");

  session = infc_session_proxy_get_session(
    infc_browser_get_chat_session(test->browser));

  test->buffer = INF_CHAT_BUFFER(inf_session_get_buffer(session));

  /* TODO: Show backlog after during/after synchronization */

  g_signal_connect_after(
    G_OBJECT(session),
    "receive-message",
    G_CALLBACK(inf_chat_test_buffer_receive_message_cb),
    test
  );

  g_signal_connect_after(
    G_OBJECT(session),
    "synchronization-complete",
    G_CALLBACK(inf_chat_test_session_synchronization_complete_cb),
    test
  );

  g_signal_connect_after(
    G_OBJECT(session),
    "synchronization-failed",
    G_CALLBACK(inf_chat_test_session_synchronization_failed_cb),
    test
  );

  /* This can happen when the server disables the chat without being
   * shutdown. */
  g_signal_connect_after(
    G_OBJECT(session),
    "close",
    G_CALLBACK(inf_chat_test_session_close_cb),
    test
  );
}

static void
inf_chat_test_subscribe_failed_cb(InfcRequest* request,
                                  const GError* error,
                                  gpointer user_data)
{
  InfTestChat* test;
  test = (InfTestChat*)user_data;

  fprintf(stderr, "Subscription failed: %s\n", error->message);
  inf_standalone_io_loop_quit(test->io);
}

static void
inf_test_chat_notify_status_cb(GObject* object,
                               GParamSpec* pspec,
                               gpointer user_data)
{
  InfTestChat* test;
  InfXmlConnectionStatus status;
  InfcNodeRequest* request;

  test = (InfTestChat*)user_data;
  g_object_get(G_OBJECT(test->conn), "status", &status, NULL);

  if(status == INF_XML_CONNECTION_OPEN)
  {
    printf("Connection established, subscribing to chat...\n");

    /* Subscribe to chat */
    request = infc_browser_subscribe_chat(test->browser);

    g_signal_connect_after(
      G_OBJECT(request),
      "failed",
      G_CALLBACK(inf_chat_test_subscribe_failed_cb),
      test
    );

    g_signal_connect_after(
      G_OBJECT(request),
      "finished",
      G_CALLBACK(inf_chat_test_subscribe_finished_cb),
      test
    );
  }

  if(status == INF_XML_CONNECTION_CLOSING ||
     status == INF_XML_CONNECTION_CLOSED)
  {
    printf("Connection closed\n");
    inf_standalone_io_loop_quit(test->io);
  }
}

static void
inf_test_chat_error_cb(InfXmppConnection* xmpp,
                          GError* error,
                          gpointer user_data)
{
  /* status notify will close conn: */
  fprintf(stderr, "Connection error: %s\n", error->message);
}

int
main(int argc, char* argv[])
{
  InfTestChat test;
  InfIpAddress* address;
  InfCommunicationManager* manager;
  InfTcpConnection* tcp_conn;
  GError* error;

  gnutls_global_init();
  g_type_init();

  test.io = inf_standalone_io_new();
  test.input_fd = STDIN_FILENO;
  test.buffer = NULL;

  address = inf_ip_address_new_loopback4();

  error = NULL;
  tcp_conn =
    inf_tcp_connection_new_and_open(INF_IO(test.io), address, 6523, &error);

  inf_ip_address_free(address);

  if(tcp_conn == NULL)
  {
    fprintf(stderr, "Could not open TCP connection: %s\n", error->message);
    g_error_free(error);
  }
  else
  {
    test.conn = inf_xmpp_connection_new(
      tcp_conn,
      INF_XMPP_CONNECTION_CLIENT,
      NULL,
      "localhost",
      INF_XMPP_CONNECTION_SECURITY_BOTH_PREFER_TLS,
      NULL,
      NULL,
      NULL
    );

    g_signal_connect_after(
      G_OBJECT(test.conn),
      "notify::status",
      G_CALLBACK(inf_test_chat_notify_status_cb),
      &test
    );

    g_signal_connect(
      G_OBJECT(test.conn),
      "error",
      G_CALLBACK(inf_test_chat_error_cb),
      &test
    );

    g_object_unref(G_OBJECT(tcp_conn));

    manager = inf_communication_manager_new();
    test.browser = infc_browser_new(
      INF_IO(test.io),
      manager,
      INF_XML_CONNECTION(test.conn)
    );

    inf_standalone_io_loop(test.io);
    g_object_unref(G_OBJECT(manager));
    g_object_unref(G_OBJECT(test.browser));

    /* TODO: Wait until the XMPP connection is in status closed */
    g_object_unref(G_OBJECT(test.conn));
  }

  g_object_unref(G_OBJECT(test.io));
  return 0;
}

/* vim:set et sw=2 ts=2: */