-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinf-test-xmpp-server.c
192 lines (166 loc) · 4.19 KB
/
inf-test-xmpp-server.c
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
/* libinfinity - a GObject-based infinote implementation
* Copyright (C) 2007, 2008 Armin Burgmeier <[email protected]>
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <libinfinity/server/infd-xmpp-server.h>
#include <libinfinity/server/infd-xml-server.h>
#include <libinfinity/server/infd-tcp-server.h>
#include <libinfinity/common/inf-standalone-io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
static void
conn_notify_status_cb(InfXmlConnection* connection,
GParamSpec* spec,
gpointer user_data)
{
InfXmlConnectionStatus status;
g_object_get(
G_OBJECT(connection),
"status", &status,
NULL
);
switch(status)
{
case INF_XML_CONNECTION_CLOSED:
fprintf(stderr, "Connection closed\n");
break;
case INF_XML_CONNECTION_CLOSING:
fprintf(stderr, "Connection closing\n");
break;
case INF_XML_CONNECTION_OPENING:
fprintf(stderr, "Connection opening\n");
break;
case INF_XML_CONNECTION_OPEN:
fprintf(stderr, "Connection open\n");
break;
}
}
static void
conn_error_cb(InfXmlConnection* connection,
GError* error,
gpointer user_data)
{
fprintf(stderr, "Connection error occured: %s\n", error->message);
}
static void
new_connection_cb(InfdXmlServer* server,
InfXmlConnection* connection,
gpointer user_data)
{
fprintf(stderr, "New connection\n");
g_signal_connect(
G_OBJECT(connection),
"error",
G_CALLBACK(conn_error_cb),
NULL
);
g_signal_connect(
G_OBJECT(connection),
"notify::status",
G_CALLBACK(conn_notify_status_cb),
NULL
);
g_object_ref(G_OBJECT(connection));
}
static void
error_cb(InfdXmppServer* server,
GError* error,
gpointer user_data)
{
fprintf(stderr, "Server Error occured: %s\n", error->message);
}
static void
notify_status_cb(InfdXmlServer* server,
GParamSpec* pspec,
gpointer user_data)
{
InfdXmlServerStatus status;
g_object_get(
G_OBJECT(server),
"status", &status,
NULL
);
switch(status)
{
case INFD_XML_SERVER_CLOSED:
printf("Server closed\n");
inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data));
break;
case INFD_XML_SERVER_CLOSING:
printf("Server closing\n");
break;
case INFD_XML_SERVER_OPENING:
printf("Server opening\n");
break;
case INFD_XML_SERVER_OPEN:
printf("Server open\n");
break;
default:
g_assert_not_reached();
break;
}
}
int main(int argc, char* argv[])
{
InfStandaloneIo* io;
InfdTcpServer* server;
InfdXmppServer* xmpp;
GError* error;
gnutls_global_init();
g_type_init();
io = inf_standalone_io_new();
error = NULL;
server = g_object_new(
INFD_TYPE_TCP_SERVER,
"io", io,
"local-port", 5222,
NULL
);
if(infd_tcp_server_open(server, &error) == FALSE)
{
fprintf(stderr, "Could not open server: %s\n", error->message);
g_error_free(error);
}
else
{
xmpp = infd_xmpp_server_new(server, "localhost", NULL, NULL);
g_signal_connect(
G_OBJECT(xmpp),
"error",
G_CALLBACK(error_cb),
io
);
g_signal_connect(
G_OBJECT(xmpp),
"new-connection",
G_CALLBACK(new_connection_cb),
io
);
g_signal_connect(
G_OBJECT(xmpp),
"notify::status",
G_CALLBACK(notify_status_cb),
io
);
inf_standalone_io_loop(io);
}
g_object_unref(G_OBJECT(io));
g_object_unref(G_OBJECT(server));
return 0;
}
/* vim:set et sw=2 ts=2: */