diff --git a/client.c b/client.c index 40c058d..cca23df 100644 --- a/client.c +++ b/client.c @@ -35,7 +35,7 @@ int client_read(struct client_t *client) { } //TODO let's print received bytes during development phase... - { + if (debug_messages) { int i; for(i = 0; i < len; i++) { fprintf(stderr, "client %s <- %u '%c'\n", @@ -63,7 +63,7 @@ int client_write(struct client_t *client, char *databuf, int datalen) { //telnet_handle_client_write(databuf, &datalen); //TODO let's print received bytes during development phase... - { + if (debug_messages) { int i; for(i = 0; i < datalen; i++) { fprintf(stderr, "client %s -> %u '%c'\n", diff --git a/moxerver.c b/moxerver.c index 71c1aa4..411fc60 100644 --- a/moxerver.c +++ b/moxerver.c @@ -15,7 +15,8 @@ /* Prints help message. */ static void usage() { //TODO maybe some styling should be done - fprintf(stderr, "Usage: %s -p tcp_port -t tty_path [-h]\n", NAME); + fprintf(stderr, "Usage: %s -p tcp_port -t tty_path [-d] [-h]\n", NAME); + fprintf(stderr, "\t-d\tturns on debug messages\n"); fprintf(stderr, "\n"); } @@ -70,7 +71,8 @@ int main(int argc, char *argv[]) { return -1; } /* grab arguments */ - while ((ret = getopt(argc, argv, ":p:t:h")) != -1) { + debug_messages = 0; + while ((ret = getopt(argc, argv, ":p:t:dh")) != -1) { switch (ret) { /* get server port number */ case 'p': @@ -88,6 +90,10 @@ int main(int argc, char *argv[]) { strcpy(tty_dev.path, optarg); } break; + /* enable debug messages */ + case 'd': + debug_messages = 1; + break; /* print help and exit */ case 'h': usage(); @@ -102,9 +108,6 @@ int main(int argc, char *argv[]) { /* introduction message */ fprintf(stderr, "[%s] === MoxaNix ===\n", NAME); - //TODO remove the following line after development phase - fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path); - /* initialize */ server_setup(&server, tcp_port); client.socket = -1; @@ -119,7 +122,7 @@ int main(int argc, char *argv[]) { /* start thread that handles tty device */ ret = pthread_create(&tty_thread, NULL, tty_thread_func, &tty_dev); //TODO check return value? - /* loop with timeouts waiting for client connection and data*/ + /* loop with timeouts waiting for client connection and data */ while (1) { /* setup parameters for select() */ @@ -143,12 +146,12 @@ int main(int argc, char *argv[]) { /* check server status */ if (FD_ISSET(server.socket, &read_fds)) { fprintf(stderr, "[%s] received client connection request\n", NAME); - /* accept connection request if there is no client connected yet */ + /* accept connection request if no client is connected */ if (client.socket == -1) { ret = server_accept(&server, &client); if ( ret != 0) { /* print error but continue waiting for connection request */ - //TODO maybe we should break here to avoid endless loop + //TODO maybe we should break here to avoid endless loop, what are possible causes of this failure? fprintf(stderr, "[%s] problem accepting client\n", NAME); continue; } @@ -185,12 +188,16 @@ int main(int argc, char *argv[]) { if (client.socket != -1) { //TODO we could drop client if inactive for some time time_t current_time = time(NULL); - fprintf(stderr, "[%s] client last active %u seconds ago\n", NAME, - (unsigned int) (current_time - client.last_active)); + if (debug_messages) { + fprintf(stderr, "[%s] client last active %u seconds ago\n", NAME, + (unsigned int) (current_time - client.last_active)); + } } /* do something while listening for client connections */ else { - fprintf(stderr, "[%s] listening for client connection\n", NAME); + if (debug_messages) { + fprintf(stderr, "[%s] listening for client connection\n", NAME); + } } } @@ -199,7 +206,6 @@ int main(int argc, char *argv[]) { /* unexpected break from while() loop */ fprintf(stderr, "[%s] unexpected condition\n", NAME); /* cleanup and exit with -1 */ - pthread_join(tty_thread, NULL); //TODO maybe we should be able to kill this thread now, what if we wait forever? cleanup(-1); return -1; diff --git a/moxerver.h b/moxerver.h index d663ee2..440ad44 100644 --- a/moxerver.h +++ b/moxerver.h @@ -41,6 +41,7 @@ struct tty_t { /* Global variables used throughout the application. */ +int debug_messages; /* if > 0 debug messages will be printed */ struct server_t server; /* main server structure */ struct client_t client; /* connected client structure */ //TODO working with only 1 client, this can be expanded into a list struct tty_t tty_dev; /* connected tty device */