Added logging to client threads
[guacd.git] / src / client.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is guacd.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2010
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdlib.h>
39
40 #include <guacamole/guacio.h>
41 #include <guacamole/client.h>
42 #include <guacamole/log.h>
43 #include <guacamole/error.h>
44
45 #include "client.h"
46 #include "thread.h"
47
48 void guac_client_stop(guac_client* client) {
49     client->state = STOPPING;
50 }
51
52 void* __guac_client_output_thread(void* data) {
53
54     guac_client* client = (guac_client*) data;
55     GUACIO* io = client->io;
56
57     guac_timestamp last_ping_timestamp = guac_current_timestamp();
58
59     /* Guacamole client output loop */
60     while (client->state == RUNNING) {
61
62         /* Occasionally ping client with repeat of last sync */
63         guac_timestamp timestamp = guac_current_timestamp();
64         if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
65
66             /* Record time of last synnc */
67             last_ping_timestamp = timestamp;
68
69             /* Send sync */
70             if (guac_send_sync(io, client->last_sent_timestamp)) {
71                 guac_log_error("Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
72                 guac_client_stop(client);
73                 return NULL;
74             }
75
76             /* Flush */
77             if (guac_flush(io)) {
78                 guac_log_error("Error flushing output: %s", guac_status_string(guac_error));
79                 guac_client_stop(client);
80                 return NULL;
81             }
82
83         }
84
85         /* Handle server messages */
86         if (client->handle_messages) {
87
88             /* Only handle messages if synced within threshold */
89             if (client->last_sent_timestamp - client->last_received_timestamp
90                     < GUAC_SYNC_THRESHOLD) {
91
92                 int retval = client->handle_messages(client);
93                 if (retval) {
94                     guac_log_error("Error handling server messages: %s", guac_status_string(guac_error));
95                     guac_client_stop(client);
96                     return NULL;
97                 }
98
99                 /* Sleep as necessary */
100                 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
101
102                 /* Send sync instruction */
103                 client->last_sent_timestamp = guac_current_timestamp();
104                 if (guac_send_sync(io, client->last_sent_timestamp)) {
105                     guac_log_error("Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
106                     guac_client_stop(client);
107                     return NULL;
108                 }
109
110                 if (guac_flush(io)) {
111                     guac_log_error("Error flushing output: %s", guac_status_string(guac_error));
112                     guac_client_stop(client);
113                     return NULL;
114                 }
115
116             }
117
118             /* If sync threshold exceeded, don't spin waiting for resync */
119             else
120                 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
121
122         }
123
124         /* If no message handler, just sleep until next sync ping */
125         else
126             guac_sleep(GUAC_SYNC_FREQUENCY);
127
128     } /* End of output loop */
129
130     guac_client_stop(client);
131     return NULL;
132
133 }
134
135 void* __guac_client_input_thread(void* data) {
136
137     guac_client* client = (guac_client*) data;
138     GUACIO* io = client->io;
139
140     /* Guacamole client input loop */
141     while (client->state == RUNNING) {
142
143         /* Read instruction */
144         guac_instruction* instruction =
145             guac_read_instruction(io, GUAC_USEC_TIMEOUT);
146
147         /* Stop on error */
148         if (instruction == NULL) {
149             guac_log_error("Error reading instruction: %s", guac_status_string(guac_error));
150             guac_client_stop(client);
151             return NULL;
152         }
153
154         /* Call handler, stop on error */
155         if (guac_client_handle_instruction(client, instruction) < 0) {
156             guac_log_error("Error in client \"%s\" instruction handler: %s", instruction->opcode, guac_status_string(guac_error));
157             guac_free_instruction(instruction);
158             guac_client_stop(client);
159             return NULL;
160         }
161
162         /* Free allocated instruction */
163         guac_free_instruction(instruction);
164
165     }
166
167     return NULL;
168
169 }
170
171 int guac_start_client(guac_client* client) {
172
173     guac_thread input_thread, output_thread;
174
175     if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
176         return -1;
177     }
178
179     if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
180         guac_client_stop(client);
181         guac_thread_join(output_thread);
182         return -1;
183     }
184
185     /* Wait for I/O threads */
186     guac_thread_join(input_thread);
187     guac_thread_join(output_thread);
188
189     /* Done */
190     return 0;
191
192 }
193
194