1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* test-dns-ds.c - Test program for the GNOME Virtual File System. |
---|
3 | |
---|
4 | Copyright (C) 2004 Red Hat, Inc |
---|
5 | |
---|
6 | The Gnome Library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of the |
---|
9 | License, or (at your option) any later version. |
---|
10 | |
---|
11 | The Gnome Library is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public |
---|
17 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | |
---|
21 | Author: Alexander Larsson <alexl@redhat.com> |
---|
22 | */ |
---|
23 | |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | #include <libgnomevfs/gnome-vfs-init.h> |
---|
27 | #include <libgnomevfs/gnome-vfs-dns-sd.h> |
---|
28 | |
---|
29 | static GMainLoop *main_loop; |
---|
30 | |
---|
31 | static void |
---|
32 | print_browse_result (GnomeVFSDNSSDBrowseHandle *handle, |
---|
33 | GnomeVFSDNSSDServiceStatus status, |
---|
34 | const GnomeVFSDNSSDService *service, |
---|
35 | gpointer callback_data) |
---|
36 | { |
---|
37 | g_print ("status: %s, service: '%s', type: %s, domain: %s\n", |
---|
38 | status == GNOME_VFS_DNS_SD_SERVICE_ADDED? "added" : "removed", |
---|
39 | service->name, service->type, service->domain); |
---|
40 | } |
---|
41 | |
---|
42 | static void |
---|
43 | print_resolve_result (GnomeVFSDNSSDResolveHandle *handle, |
---|
44 | GnomeVFSResult result, |
---|
45 | const GnomeVFSDNSSDService *service, |
---|
46 | const char *host, |
---|
47 | int port, |
---|
48 | const GHashTable *text, |
---|
49 | int text_raw_len, |
---|
50 | const char *text_raw, |
---|
51 | gpointer callback_data) |
---|
52 | { |
---|
53 | if (result == GNOME_VFS_OK) |
---|
54 | g_print ("service: '%s', type: %s, domain: %s, host: %s, port: %d\n", |
---|
55 | service->name, service->type, service->domain, |
---|
56 | host, port); |
---|
57 | else |
---|
58 | g_print ("error: %d\n", result); |
---|
59 | |
---|
60 | g_main_loop_quit (main_loop); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | int |
---|
65 | main (int argc, char **argv) |
---|
66 | { |
---|
67 | int i; |
---|
68 | GnomeVFSResult res; |
---|
69 | int n_services; |
---|
70 | GnomeVFSDNSSDService *services; |
---|
71 | GnomeVFSDNSSDBrowseHandle *browse_handle; |
---|
72 | GnomeVFSDNSSDResolveHandle *resolve_handle; |
---|
73 | char *domain, *type; |
---|
74 | int port, text_len; |
---|
75 | char *host; |
---|
76 | char *text; |
---|
77 | GList *list, *l, *domains; |
---|
78 | GHashTable *text_hash; |
---|
79 | |
---|
80 | if (argc < 3) { |
---|
81 | domain = "dns-sd.org"; |
---|
82 | type = "_ftp._tcp"; |
---|
83 | } else { |
---|
84 | domain = argv[1]; |
---|
85 | type = argv[2]; |
---|
86 | } |
---|
87 | |
---|
88 | fprintf (stderr, "Initializing gnome-vfs...\n"); |
---|
89 | gnome_vfs_init (); |
---|
90 | |
---|
91 | fprintf (stderr, "Getting default browse domains:"); |
---|
92 | domains = gnome_vfs_get_default_browse_domains (); |
---|
93 | for (l = domains; l != NULL; l = l->next) { |
---|
94 | g_print ("%s,", (char *)l->data); |
---|
95 | g_free (l->data); |
---|
96 | } |
---|
97 | g_print ("\n"); |
---|
98 | g_list_free (domains); |
---|
99 | |
---|
100 | |
---|
101 | fprintf (stderr, "Trying sync list\n"); |
---|
102 | res = gnome_vfs_dns_sd_list_browse_domains_sync ("dns-sd.org", |
---|
103 | 2000, |
---|
104 | &list); |
---|
105 | if (res == GNOME_VFS_OK) { |
---|
106 | for (l = list; l != NULL; l = l->next) { |
---|
107 | g_print ("search domain: %s\n", (char *)l->data); |
---|
108 | g_free (l->data); |
---|
109 | } |
---|
110 | g_list_free (list); |
---|
111 | } else { |
---|
112 | fprintf (stderr, "list error %d\n", res); |
---|
113 | } |
---|
114 | |
---|
115 | fprintf (stderr, "Trying sync resolve\n"); |
---|
116 | res = gnome_vfs_dns_sd_resolve_sync ( |
---|
117 | "Apple QuickTime Files", "_ftp._tcp", "dns-sd.org", |
---|
118 | 4000, |
---|
119 | &host, &port, |
---|
120 | &text_hash, |
---|
121 | &text_len, &text); |
---|
122 | if (res == GNOME_VFS_OK) { |
---|
123 | g_print ("host: %s, port: %d, text: %.*s\n", |
---|
124 | host, port, text_len, text); |
---|
125 | } else { |
---|
126 | fprintf (stderr, "resolve error %d\n", res); |
---|
127 | } |
---|
128 | |
---|
129 | fprintf (stderr, "Trying local resolve\n"); |
---|
130 | res = gnome_vfs_dns_sd_resolve_sync ("Alex other test", |
---|
131 | "_ftp._tcp", |
---|
132 | "local", |
---|
133 | 4000, |
---|
134 | &host, &port, |
---|
135 | NULL, |
---|
136 | &text_len, &text); |
---|
137 | if (res == GNOME_VFS_OK) { |
---|
138 | g_print ("host: %s, port: %d, text: %.*s\n", |
---|
139 | host, port, text_len, text); |
---|
140 | } else { |
---|
141 | fprintf (stderr, "resolve error %d\n", res); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | fprintf (stderr, "Trying sync browsing\n"); |
---|
146 | res = gnome_vfs_dns_sd_browse_sync (domain, type, |
---|
147 | 4000, |
---|
148 | &n_services, |
---|
149 | &services); |
---|
150 | if (res == GNOME_VFS_OK) { |
---|
151 | fprintf (stderr, "Number of hits: %d\n", n_services); |
---|
152 | for (i = 0; i < n_services; i++) { |
---|
153 | fprintf (stderr, "service name: '%s' type: %s, domain: %s\n", |
---|
154 | services[i].name, services[i].type, services[i].domain); |
---|
155 | } |
---|
156 | } else { |
---|
157 | fprintf (stderr, "error %d\n", res); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | main_loop = g_main_loop_new (NULL, TRUE); |
---|
162 | |
---|
163 | fprintf (stderr, "Trying async resolve\n"); |
---|
164 | |
---|
165 | res = gnome_vfs_dns_sd_resolve (&resolve_handle, |
---|
166 | "Apple QuickTime Files", "_ftp._tcp", "dns-sd.org", |
---|
167 | 4000, |
---|
168 | print_resolve_result, |
---|
169 | NULL, NULL); |
---|
170 | |
---|
171 | fprintf (stderr, "Main loop running.\n"); |
---|
172 | g_main_loop_run (main_loop); |
---|
173 | |
---|
174 | fprintf (stderr, "Trying local async resolve\n"); |
---|
175 | |
---|
176 | res = gnome_vfs_dns_sd_resolve (&resolve_handle, |
---|
177 | "Alex other test", |
---|
178 | "_ftp._tcp", |
---|
179 | "local", |
---|
180 | 4000, |
---|
181 | print_resolve_result, |
---|
182 | NULL, NULL); |
---|
183 | |
---|
184 | fprintf (stderr, "Main loop running.\n"); |
---|
185 | g_main_loop_run (main_loop); |
---|
186 | |
---|
187 | |
---|
188 | fprintf (stderr, "Trying async browsing (won't terminate)\n"); |
---|
189 | |
---|
190 | res = gnome_vfs_dns_sd_browse (&browse_handle, |
---|
191 | domain, type, |
---|
192 | print_browse_result, |
---|
193 | NULL, NULL); |
---|
194 | |
---|
195 | fprintf (stderr, "Main loop running.\n"); |
---|
196 | g_main_loop_run (main_loop); |
---|
197 | |
---|
198 | fprintf (stderr, "Main loop finished.\n"); |
---|
199 | |
---|
200 | g_main_loop_unref (main_loop); |
---|
201 | |
---|
202 | fprintf (stderr, "All done\n"); |
---|
203 | |
---|
204 | gnome_vfs_shutdown (); |
---|
205 | |
---|
206 | return 0; |
---|
207 | } |
---|