forked from scott/threaded_network_chat
91 lines
1.7 KiB
C++
91 lines
1.7 KiB
C++
#include <string>
|
|
|
|
#include <bits/types/struct_timeval.h>
|
|
#include <curses.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "chat.h"
|
|
|
|
chatmode_t mode = NO_MODE;
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
char *usr_in = new char[1024];
|
|
bool quit = false;
|
|
|
|
if (argc > 1 && (!strcmp(argv[1], "client")))
|
|
mode = CLIENT_MODE;
|
|
else
|
|
mode = SERVER_MODE;
|
|
|
|
initscr();
|
|
use_default_colors();
|
|
|
|
line_position = count_lines(log_path) - LOG_LENGTH + 1;
|
|
|
|
switch (mode) {
|
|
case CLIENT_MODE:
|
|
log_append(log_path, "CLIENT MODE");
|
|
client_start();
|
|
break;
|
|
case SERVER_MODE:
|
|
log_append(log_path, "SERVER MODE");
|
|
server_start(listen_port);
|
|
break;
|
|
default:
|
|
goto panic_no_mode;
|
|
break;
|
|
}
|
|
|
|
while (!quit) {
|
|
file_disp(log_path, line_position, LOG_LENGTH);
|
|
|
|
if (count_lines(log_path) > LOG_LENGTH)
|
|
line_position++;
|
|
|
|
/* prompt for input */
|
|
move(12, 0);
|
|
printw("> ");
|
|
|
|
/* await input */
|
|
getstr(usr_in);
|
|
log_append(log_path, usr_in);
|
|
send(socket_descriptor_client, (char *)usr_in, strlen(usr_in), 0);
|
|
}
|
|
|
|
/* Final exit point if everything goes OK */
|
|
|
|
endwin();
|
|
|
|
switch (mode) {
|
|
case CLIENT_MODE:
|
|
client_stop();
|
|
break;
|
|
case SERVER_MODE:
|
|
server_stop();
|
|
break;
|
|
default:
|
|
goto panic_no_mode;
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
|
|
panic_no_mode:
|
|
fprintf(stderr, "-----FATAL ERROR, THIS SHOULD NEVER HAPPEN-----\n");
|
|
fprintf(stderr, "You may ask yourself... how do I work this?\n");
|
|
fprintf(stderr, "And you may find yourself... past the point of return 0;!\n");
|
|
fprintf(stderr, "And you may ask yourself... well, how did I get here?\n");
|
|
|
|
if (mode == NO_MODE)
|
|
fprintf(stderr, "Fatal: Mode was not set!\n");
|
|
else
|
|
fprintf(stderr, "Fatal: Mode was set to invalid enum %i\n", mode);
|
|
|
|
return 1;
|
|
}
|