added flag for enabling debug messages, without debug messages only connection events are displayed

This commit is contained in:
Igor Socec 2014-03-20 12:13:52 +01:00
parent 1001f3ad9c
commit 91becdad1c
3 changed files with 21 additions and 14 deletions

View file

@ -35,7 +35,7 @@ int client_read(struct client_t *client) {
} }
//TODO let's print received bytes during development phase... //TODO let's print received bytes during development phase...
{ if (debug_messages) {
int i; int i;
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
fprintf(stderr, "client %s <- %u '%c'\n", 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); //telnet_handle_client_write(databuf, &datalen);
//TODO let's print received bytes during development phase... //TODO let's print received bytes during development phase...
{ if (debug_messages) {
int i; int i;
for(i = 0; i < datalen; i++) { for(i = 0; i < datalen; i++) {
fprintf(stderr, "client %s -> %u '%c'\n", fprintf(stderr, "client %s -> %u '%c'\n",

View file

@ -15,7 +15,8 @@
/* Prints help message. */ /* Prints help message. */
static void usage() { static void usage() {
//TODO maybe some styling should be done //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"); fprintf(stderr, "\n");
} }
@ -70,7 +71,8 @@ int main(int argc, char *argv[]) {
return -1; return -1;
} }
/* grab arguments */ /* 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) { switch (ret) {
/* get server port number */ /* get server port number */
case 'p': case 'p':
@ -88,6 +90,10 @@ int main(int argc, char *argv[]) {
strcpy(tty_dev.path, optarg); strcpy(tty_dev.path, optarg);
} }
break; break;
/* enable debug messages */
case 'd':
debug_messages = 1;
break;
/* print help and exit */ /* print help and exit */
case 'h': case 'h':
usage(); usage();
@ -102,9 +108,6 @@ int main(int argc, char *argv[]) {
/* introduction message */ /* introduction message */
fprintf(stderr, "[%s] === MoxaNix ===\n", NAME); 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 */ /* initialize */
server_setup(&server, tcp_port); server_setup(&server, tcp_port);
client.socket = -1; client.socket = -1;
@ -143,12 +146,12 @@ int main(int argc, char *argv[]) {
/* check server status */ /* check server status */
if (FD_ISSET(server.socket, &read_fds)) { if (FD_ISSET(server.socket, &read_fds)) {
fprintf(stderr, "[%s] received client connection request\n", NAME); 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) { if (client.socket == -1) {
ret = server_accept(&server, &client); ret = server_accept(&server, &client);
if ( ret != 0) { if ( ret != 0) {
/* print error but continue waiting for connection request */ /* 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); fprintf(stderr, "[%s] problem accepting client\n", NAME);
continue; continue;
} }
@ -185,21 +188,24 @@ int main(int argc, char *argv[]) {
if (client.socket != -1) { if (client.socket != -1) {
//TODO we could drop client if inactive for some time //TODO we could drop client if inactive for some time
time_t current_time = time(NULL); time_t current_time = time(NULL);
if (debug_messages) {
fprintf(stderr, "[%s] client last active %u seconds ago\n", NAME, fprintf(stderr, "[%s] client last active %u seconds ago\n", NAME,
(unsigned int) (current_time - client.last_active)); (unsigned int) (current_time - client.last_active));
} }
}
/* do something while listening for client connections */ /* do something while listening for client connections */
else { else {
if (debug_messages) {
fprintf(stderr, "[%s] listening for client connection\n", NAME); fprintf(stderr, "[%s] listening for client connection\n", NAME);
} }
} }
}
} /* END while() loop */ } /* END while() loop */
/* unexpected break from while() loop */ /* unexpected break from while() loop */
fprintf(stderr, "[%s] unexpected condition\n", NAME); fprintf(stderr, "[%s] unexpected condition\n", NAME);
/* cleanup and exit with -1 */ /* 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); cleanup(-1);
return -1; return -1;

View file

@ -41,6 +41,7 @@ struct tty_t {
/* Global variables used throughout the application. */ /* Global variables used throughout the application. */
int debug_messages; /* if > 0 debug messages will be printed */
struct server_t server; /* main server structure */ 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 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 */ struct tty_t tty_dev; /* connected tty device */