Restructure main program and separate thread for WUI interaction.
[ovirt-viewer.git] / internal.h
1 /* ovirt viewer console application
2  * Copyright (C) 2008 Red Hat Inc.
3  * Written by Richard W.M. Jones <rjones@redhat.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #ifndef OVIRT_VIEWER_INTERNAL_H
21 #define OVIRT_VIEWER_INTERNAL_H
22
23 #ifndef G_THREADS_ENABLED
24 #error "This program requires GLib threads, and cannot be compiled without."
25 #endif
26
27 /* Debugging messages are always compiled in, but have to
28  * be turned on using the --debug command line switch.
29  */
30 extern gboolean debug;
31
32 #define DEBUG(fs,...)                                                   \
33   do {                                                                  \
34     if (debug) {                                                        \
35       fprintf (stderr, "%s:%d: [thread %p] ", __FILE__, __LINE__,       \
36                g_thread_self ());                                       \
37       fprintf (stderr, (fs), ## __VA_ARGS__);                           \
38       fprintf (stderr, "\n");                                           \
39     }                                                                   \
40   } while (0)
41
42 /* String equality tests, suggested by Jim Meyering. */
43 #define STREQ(a,b) (strcmp((a),(b)) == 0)
44 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
45 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
46 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
47 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
48 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
49 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
50 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
51 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
52
53 /* Communications between the main thread and the WUI thread.  For
54  * an explanation of the threading model, please see the comment in
55  * main().
56  */
57
58 extern void start_wui_thread (void);
59 extern void stop_wui_thread (void);
60
61 /* These are messages (instructions) which can be sent from the main
62  * thread to the WUI thread.
63  */
64
65 /* Start connecting to WUI, and set the base URI. */
66 extern void wui_thread_send_connect (const char *uri);
67
68 /* Disconnect, forget URI, credentials, VMs etc. */
69 extern void wui_thread_send_disconnect (void);
70
71 /* Set the username and password and tell the WUI to try to log in. */
72 extern void wui_thread_send_login (const char *username, const char *password);
73
74 /* Tell the WUI thread to refresh the VM list.  Note that the WUI
75  * thread does this automatically anyway after a successful login, and
76  * it also periodically updates the list.  This call just tells it to
77  * do so right away.
78  */
79 extern void wui_thread_send_refresh_vm_list (void);
80
81 /* Retrieve the list of VMs.
82  *
83  * NB: Caller must call free_vmlist once it is done with the list.
84  *
85  * This can return NULL if the WUI thread doesn't have a valid
86  * list of VMs to return to the caller.  In the case of a valid,
87  * empty list, you will get a non-NULL GSList pointer to an empty
88  * list.
89  */
90 extern GSList *wui_thread_get_vmlist (void);
91 extern void free_vmlist (GSList *vmlist);
92
93 /* Returns true if the WUI thread thinks it is connected to a remote
94  * WUI.  REST is connectionless so really this means that we
95  * successfully made an HTTP/HTTPS request "recently", and we haven't
96  * seen any errors above a certain threshold.
97  */
98 extern gboolean wui_thread_is_connected (void);
99
100 /* Returns true if we successfully logged in with the username
101  * and password supplied in a recent request, and we haven't
102  * received any authorization failures since.
103  */
104 extern gboolean wui_thread_is_logged_in (void);
105
106 /* Returns true if we have a valid list of VMs.  Note that because
107  * of race conditions, this doesn't guarantee that wui_thread_get_vmlist
108  * will work.
109  */
110 extern gboolean wui_thread_has_valid_vmlist (void);
111
112 /* Returns true if the WUI thread is busy performing a request
113  * at the moment.
114  */
115 extern gboolean wui_thread_is_busy (void);
116
117 #endif /* OVIRT_VIEWER_INTERNAL_H */