add cmd line option for server vnc port
[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 /* Verbose messages are always compiled in, but have to
43  * be turned on using the --verbose command line switch.
44  */
45 extern gboolean verbose;
46
47 #define VERBOSE(fs,...)                                                 \
48   do {                                                                          \
49     if (verbose) {                                                      \
50       fprintf (stderr, "%s:%d: [thread %p] ", __FILE__, __LINE__,       \
51                g_thread_self ());                                       \
52       fprintf (stderr, (fs), ## __VA_ARGS__);                           \
53       fprintf (stderr, "\n");                                           \
54     }                                                                   \
55   } while (0)
56
57 /* String equality tests, suggested by Jim Meyering. */
58 #define STREQ(a,b) (strcmp((a),(b)) == 0)
59 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
60 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
61 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
62 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
63 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
64 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
65 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
66 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
67
68 extern const char *cainfo;
69 extern gboolean check_cert;
70
71 /* server we're connecting to */
72 extern const char* hostname;
73
74 /* port which to connect to the server via vnc */
75 extern int ovirt_server_vnc_port;
76
77 /* vm currently in focus */
78 extern struct vm* vm_in_focus;
79
80 /* Communications between the main thread and the WUI thread.  For
81  * an explanation of the threading model, please see the comment in
82  * main().
83  */
84
85 extern void start_wui_thread (void);
86 extern void stop_wui_thread (void);
87
88 extern void assert_is_main_thread (const char *, int);
89 extern void assert_is_wui_thread (const char *, int);
90
91 #define ASSERT_IS_MAIN_THREAD() assert_is_main_thread(__FILE__,__LINE__)
92 #define ASSERT_IS_WUI_THREAD() assert_is_wui_thread(__FILE__,__LINE__)
93
94 /* These are messages (instructions) which can be sent from the main
95  * thread to the WUI thread.
96  */
97
98 /* Start connecting to WUI, and set the base URI. */
99 extern void wui_thread_send_connect (const char *uri);
100
101 /* Disconnect, forget URI, credentials, VMs etc. */
102 extern void wui_thread_send_disconnect (void);
103
104 /* Set the username and password and tell the WUI to try to log in. */
105 extern void wui_thread_send_login (const char *username, const char *password);
106
107 /* Tell the WUI thread to refresh the VM list.  Note that the WUI
108  * thread does this automatically anyway after a successful login, and
109  * it also periodically updates the list.  This call just tells it to
110  * do so right away.
111  */
112 extern void wui_thread_send_refresh_vm_list (void);
113
114 /* Retrieve the list of VMs.
115  *
116  * wui_thread_get_vmlist returns TRUE if there was a valid list of
117  * VMs (even if it is empty), or FALSE if we don't have a valid list
118  * of VMs to return.
119  *
120  * NB: Caller must call free_vmlist once it is done with the list.
121  *
122  */
123 extern gboolean wui_thread_get_vmlist (GSList **ret);
124 extern void free_vmlist (GSList *vmlist);
125
126 struct vm {
127   char *description;
128   int hostid;
129   int id;
130   int vnc_port;
131   int forward_vnc_port;
132   char *uuid;                   /* Printable UUID. */
133   char *state;
134
135   /* Only the fields above this point are required.  The remainder may
136    * be NULL / -1 to indicate they were missing in the data we got
137    * back from the WUI.
138    */
139
140   long mem_allocated;           /* Kbytes */
141   long mem_used;                /* Kbytes */
142   int vcpus_allocated;
143   int vcpus_used;
144   char *mac_addr;               /* Printable MAC addr. */
145 };
146
147 /* Returns true if the WUI thread thinks it is connected to a remote
148  * WUI.  REST is connectionless so really this means that we
149  * successfully made an HTTP/HTTPS request "recently", and we haven't
150  * seen any errors above a certain threshold.
151  */
152 extern gboolean wui_thread_is_connected (void);
153
154 /* Returns true if we successfully logged in with the username
155  * and password supplied in a recent request, and we haven't
156  * received any authorization failures since.
157  */
158 extern gboolean wui_thread_is_logged_in (void);
159
160 /* Returns true if we have a valid list of VMs.  Note that because
161  * of race conditions, this doesn't guarantee that wui_thread_get_vmlist
162  * will work.
163  */
164 extern gboolean wui_thread_has_valid_vmlist (void);
165
166 /* Returns true if the WUI thread is busy performing a request
167  * at the moment.
168  */
169 extern gboolean wui_thread_is_busy (void);
170
171
172 /* Communications between the main thread and the tunnel thread.*/
173 extern void start_tunnel (void);
174 extern void stop_tunnel  (void);
175
176 /* port which local tunnel is listening on */
177 extern int tunnel_port;
178
179
180 /* Returns true if the main vm list contains a
181  * running vm w/ the same name as specified one
182  */
183 extern gboolean main_vmlist_has_running_vm(struct vm*);
184
185 /* Callbacks from the WUI thread to the main thread.  The WUI thread
186  * adds these to the Glib main loop using g_idle_add, which means they
187  * actually get executed in the context of the main thread.
188  */
189
190 /* The WUI thread has changed its state to connected. */
191 extern gboolean main_connected (gpointer);
192
193 /* The WUI thread has changed its state to disconnected. */
194 extern gboolean main_disconnected (gpointer);
195
196 /* The WUI thread has changed its state to logged in. */
197 extern gboolean main_logged_in (gpointer);
198
199 /* The WUI thread has changed its state to logged out. */
200 extern gboolean main_logged_out (gpointer);
201
202 /* The WUI thread has changed its state to busy. */
203 extern gboolean main_busy (gpointer);
204
205 /* The WUI thread has changed its state to idle. */
206 extern gboolean main_idle (gpointer);
207
208 /* The WUI thread had a connection problem. */
209 extern gboolean main_connection_error (gpointer str);
210
211 /* The WUI thread had a login problem. */
212 extern gboolean main_login_error (gpointer str);
213
214 /* The WUI thread reports a general status error. */
215 extern gboolean main_status_error (gpointer str);
216
217 /* The WUI thread has updated the vm list. */
218 extern gboolean main_vmlist_updated (gpointer);
219
220 #endif /* OVIRT_VIEWER_INTERNAL_H */