1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* test-vfs.c - Test program for the GNOME Virtual File System. |
---|
3 | |
---|
4 | Copyright (C) 1999 Free Software Foundation |
---|
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: Ettore Perazzoli <ettore@comm2000.it> |
---|
22 | */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | |
---|
26 | #include <glib/gmain.h> |
---|
27 | #include <libgnomevfs/gnome-vfs-async-ops.h> |
---|
28 | #include <libgnomevfs/gnome-vfs-init.h> |
---|
29 | #include <stdio.h> |
---|
30 | |
---|
31 | #define QUEUE_LENGTH 4000 |
---|
32 | |
---|
33 | static GMainLoop *main_loop; |
---|
34 | |
---|
35 | /* Callbacks. */ |
---|
36 | static void |
---|
37 | close_callback (GnomeVFSAsyncHandle *handle, |
---|
38 | GnomeVFSResult result, |
---|
39 | gpointer callback_data) |
---|
40 | { |
---|
41 | fprintf (stderr, "Close: %s.\n", gnome_vfs_result_to_string (result)); |
---|
42 | g_main_loop_quit (main_loop); |
---|
43 | } |
---|
44 | |
---|
45 | static void |
---|
46 | file_control_callback (GnomeVFSAsyncHandle *handle, |
---|
47 | GnomeVFSResult result, |
---|
48 | gpointer operation_data, |
---|
49 | gpointer callback_data) |
---|
50 | { |
---|
51 | if (result != GNOME_VFS_OK) { |
---|
52 | fprintf (stderr, "file_control failed: %s\n", |
---|
53 | gnome_vfs_result_to_string (result)); |
---|
54 | } else { |
---|
55 | printf ("file_control result: %s\n", *(char **)operation_data); |
---|
56 | } |
---|
57 | |
---|
58 | fprintf (stderr, "Now closing the file.\n"); |
---|
59 | gnome_vfs_async_close (handle, close_callback, "close"); |
---|
60 | } |
---|
61 | |
---|
62 | static void |
---|
63 | read_callback (GnomeVFSAsyncHandle *handle, |
---|
64 | GnomeVFSResult result, |
---|
65 | gpointer buffer, |
---|
66 | GnomeVFSFileSize bytes_requested, |
---|
67 | GnomeVFSFileSize bytes_read, |
---|
68 | gpointer callback_data) |
---|
69 | { |
---|
70 | char **op_data; |
---|
71 | |
---|
72 | if (result != GNOME_VFS_OK) { |
---|
73 | fprintf (stderr, "Read failed: %s\n", |
---|
74 | gnome_vfs_result_to_string (result)); |
---|
75 | } else { |
---|
76 | printf ("%"GNOME_VFS_SIZE_FORMAT_STR"/" |
---|
77 | "%"GNOME_VFS_SIZE_FORMAT_STR" " |
---|
78 | "byte(s) read, callback data `%s'\n", |
---|
79 | bytes_read, bytes_requested, |
---|
80 | (gchar *) callback_data); |
---|
81 | *((gchar *) buffer + bytes_read) = 0; |
---|
82 | fprintf (stderr, "%s", (char *) buffer); |
---|
83 | } |
---|
84 | |
---|
85 | fprintf (stderr, "Now testing file_control.\n"); |
---|
86 | op_data = g_new (char *, 1); |
---|
87 | gnome_vfs_async_file_control (handle, "file:test", op_data, g_free, file_control_callback, "file_control"); |
---|
88 | } |
---|
89 | |
---|
90 | static void |
---|
91 | open_callback (GnomeVFSAsyncHandle *handle, |
---|
92 | GnomeVFSResult result, |
---|
93 | gpointer callback_data) |
---|
94 | { |
---|
95 | if (result != GNOME_VFS_OK) { |
---|
96 | fprintf (stderr, "Open failed: %s.\n", |
---|
97 | gnome_vfs_result_to_string (result)); |
---|
98 | g_main_loop_quit (main_loop); |
---|
99 | } else { |
---|
100 | gchar *buffer; |
---|
101 | const gulong buffer_size = 1024; |
---|
102 | |
---|
103 | fprintf (stderr, "File opened correctly, data `%s'.\n", |
---|
104 | (gchar *) callback_data); |
---|
105 | |
---|
106 | buffer = g_malloc (buffer_size); |
---|
107 | gnome_vfs_async_read (handle, |
---|
108 | buffer, |
---|
109 | buffer_size - 1, |
---|
110 | read_callback, |
---|
111 | "read_callback"); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | dummy_close_callback (GnomeVFSAsyncHandle *handle, |
---|
117 | GnomeVFSResult result, |
---|
118 | gpointer callback_data) |
---|
119 | { |
---|
120 | } |
---|
121 | |
---|
122 | static void |
---|
123 | async_queue_callback (GnomeVFSAsyncHandle *handle, |
---|
124 | GnomeVFSResult result, |
---|
125 | gpointer callback_data) |
---|
126 | { |
---|
127 | int *completed = callback_data; |
---|
128 | |
---|
129 | (*completed)++; |
---|
130 | |
---|
131 | if (result == GNOME_VFS_OK) |
---|
132 | gnome_vfs_async_close (handle, dummy_close_callback, NULL); |
---|
133 | } |
---|
134 | |
---|
135 | int |
---|
136 | main (int argc, char **argv) |
---|
137 | { |
---|
138 | int completed, i; |
---|
139 | GnomeVFSAsyncHandle *handle; |
---|
140 | |
---|
141 | if (argc < 2) { |
---|
142 | fprintf (stderr, "Usage: %s <uri of text file>\n", argv[0]); |
---|
143 | return 1; |
---|
144 | } |
---|
145 | |
---|
146 | fprintf (stderr, "Initializing gnome-vfs...\n"); |
---|
147 | gnome_vfs_init (); |
---|
148 | |
---|
149 | fprintf (stderr, "Creating async context...\n"); |
---|
150 | |
---|
151 | fprintf (stderr, "Starting open for `%s'...\n", argv[1]); |
---|
152 | gnome_vfs_async_open (&handle, argv[1], GNOME_VFS_OPEN_READ, |
---|
153 | GNOME_VFS_PRIORITY_MIN, |
---|
154 | open_callback, "open_callback"); |
---|
155 | |
---|
156 | fprintf (stderr, "Main loop running.\n"); |
---|
157 | main_loop = g_main_loop_new (NULL, TRUE); |
---|
158 | g_main_loop_run (main_loop); |
---|
159 | |
---|
160 | fprintf (stderr, "Main loop finished.\n"); |
---|
161 | |
---|
162 | fprintf (stderr, "Test async queue efficiency ..."); |
---|
163 | |
---|
164 | for (completed = i = 0; i < QUEUE_LENGTH; i++) { |
---|
165 | gnome_vfs_async_open (&handle, argv [1], GNOME_VFS_OPEN_READ, 0, |
---|
166 | async_queue_callback, &completed); |
---|
167 | } |
---|
168 | |
---|
169 | while (completed < QUEUE_LENGTH) |
---|
170 | g_main_context_iteration (NULL, TRUE); |
---|
171 | |
---|
172 | fprintf (stderr, "Passed\n"); |
---|
173 | |
---|
174 | g_main_loop_unref (main_loop); |
---|
175 | |
---|
176 | fprintf (stderr, "All done\n"); |
---|
177 | |
---|
178 | gnome_vfs_shutdown (); |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|