Additional logging, moderate cleanup.
[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 #include <time.h>
40
41 #include <guacamole/socket.h>
42 #include <guacamole/client.h>
43 #include <guacamole/error.h>
44
45 #include "client.h"
46 #include "thread.h"
47
48 /**
49  * Sleep for the given number of milliseconds.
50  *
51  * @param millis The number of milliseconds to sleep.
52  */
53 void __guacd_sleep(int millis) {
54
55     struct timespec sleep_period;
56
57     sleep_period.tv_sec =   millis / 1000;
58     sleep_period.tv_nsec = (millis % 1000) * 1000000L;
59
60     nanosleep(&sleep_period, NULL);
61
62 }
63
64 void guac_client_stop(guac_client* client) {
65     client->state = STOPPING;
66 }
67
68 void* __guac_client_output_thread(void* data) {
69
70     guac_client* client = (guac_client*) data;
71     guac_socket* socket = client->socket;
72
73     guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
74
75     /* Guacamole client output loop */
76     while (client->state == RUNNING) {
77
78         /* Occasionally ping client with repeat of last sync */
79         guac_timestamp timestamp = guac_protocol_get_timestamp();
80         if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
81
82             /* Record time of last synnc */
83             last_ping_timestamp = timestamp;
84
85             /* Send sync */
86             if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
87                 guac_client_log_error(client,
88                         "Error sending \"sync\" instruction: %s",
89                         guac_status_string(guac_error));
90                 guac_client_stop(client);
91                 return NULL;
92             }
93
94             /* Flush */
95             if (guac_socket_flush(socket)) {
96                 guac_client_log_error(client,
97                         "Error flushing output: %s",
98                         guac_status_string(guac_error));
99                 guac_client_stop(client);
100                 return NULL;
101             }
102
103         }
104
105         /* Handle server messages */
106         if (client->handle_messages) {
107
108             /* Only handle messages if synced within threshold */
109             if (client->last_sent_timestamp - client->last_received_timestamp
110                     < GUAC_SYNC_THRESHOLD) {
111
112                 int retval = client->handle_messages(client);
113                 if (retval) {
114                     guac_client_log_error(client,
115                             "Error handling server messages: %s",
116                             guac_status_string(guac_error));
117                     guac_client_stop(client);
118                     return NULL;
119                 }
120
121                 /* Send sync instruction */
122                 client->last_sent_timestamp = guac_protocol_get_timestamp();
123                 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
124                     guac_client_log_error(client, 
125                             "Error sending \"sync\" instruction: %s",
126                             guac_status_string(guac_error));
127                     guac_client_stop(client);
128                     return NULL;
129                 }
130
131                 /* Flush */
132                 if (guac_socket_flush(socket)) {
133                     guac_client_log_error(client,
134                             "Error flushing output: %s",
135                             guac_status_string(guac_error));
136                     guac_client_stop(client);
137                     return NULL;
138                 }
139
140             }
141
142             /* Sleep before handling more messages */
143             __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
144
145         }
146
147         /* If no message handler, just sleep until next sync ping */
148         else
149             __guacd_sleep(GUAC_SYNC_FREQUENCY);
150
151     } /* End of output loop */
152
153     guac_client_stop(client);
154     return NULL;
155
156 }
157
158 void* __guac_client_input_thread(void* data) {
159
160     guac_client* client = (guac_client*) data;
161     guac_socket* socket = client->socket;
162
163     /* Guacamole client input loop */
164     while (client->state == RUNNING) {
165
166         /* Read instruction */
167         guac_instruction* instruction =
168             guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT);
169
170         /* Stop on error */
171         if (instruction == NULL) {
172             guac_client_log_error(client,
173                     "Error reading instruction: %s",
174                     guac_status_string(guac_error));
175             guac_client_stop(client);
176             return NULL;
177         }
178
179         /* Call handler, stop on error */
180         if (guac_client_handle_instruction(client, instruction) < 0) {
181             guac_client_log_error(client,
182                     "Error in client \"%s\" instruction handler: %s",
183                     instruction->opcode, guac_status_string(guac_error));
184             guac_instruction_free(instruction);
185             guac_client_stop(client);
186             return NULL;
187         }
188
189         /* Free allocated instruction */
190         guac_instruction_free(instruction);
191
192     }
193
194     return NULL;
195
196 }
197
198 int guac_start_client(guac_client* client) {
199
200     guac_thread input_thread, output_thread;
201
202     if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
203         guac_client_log_error(client, "Unable to start output thread");
204         return -1;
205     }
206
207     if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
208         guac_client_log_error(client, "Unable to start input thread");
209         guac_client_stop(client);
210         guac_thread_join(output_thread);
211         return -1;
212     }
213
214     /* Wait for I/O threads */
215     guac_thread_join(input_thread);
216     guac_thread_join(output_thread);
217
218     /* Done */
219     return 0;
220
221 }
222