diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dad0be0 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CXX = g++ +CXXFLAGS = -pthread -Wall -Wextra -O2 -std=c++17 +LDFLAGS = -lcurses +TARGET = ct +CXX_SRCS = main.cc chat.cc +CXX_OBJS = $(CXX_SRCS:.cc=.o) + +all: $(TARGET) + +$(TARGET): $(CXX_OBJS) + $(CXX) $(CXXFLAGS) $(CXX_OBJS) -o $(TARGET) $(LDFLAGS) + +%.o: %.cc + $(CXX) $(CXXFLAGS) -c $< -o $@ + +.PHONY: clean + +clean: + rm -f $(CXX_OBJS) $(TARGET) + diff --git a/chat.cc b/chat.cc new file mode 100644 index 0000000..345116f --- /dev/null +++ b/chat.cc @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "chat.h" + +void +clear_rows(int s, int e) +{ + /* preserve cursor location */ + int x_bef; + int y_bef; + + getyx(stdscr, y_bef, x_bef); + + for (int i = s; i < e; i++) { + move(i, 0); + clrtoeol(); + } + + move(y_bef, x_bef); +} + +int +file_disp(std::string p, int lnstart, int lncount) +{ + std::ifstream f(p); + + if (!f) + return 1; + + int i = 0; + int ln_no = 0; + std::string ln; + + clear_rows(0, lncount + 1); /* clear chat window */ + + while (getline(f, ln) && i <= ln_no + lnstart + 1) { + if (i >= lnstart) { + move(ln_no, 0); + printw("%s", ln.c_str()); + ln_no++; + } + i++; + } + + move(CURSOR_DEFAULT_POSITION_Y, CURSOR_DEFAULT_POSITION_X); + refresh(); + return 0; +} + +int +count_lines(std::string p) +{ + int c = 0; + + std::ifstream f(p); + if (!f) + return c; + + /* Apparently this is the most efficient way to count all lines in a file. How revolting! */ + c = std::count(std::istreambuf_iterator(f), std::istreambuf_iterator(), '\n'); + + return c; +} + +void * +await_client(void *a) +{ + wclient_args_t *args = static_cast(a); + + socket_descriptor_client = accept(socket_descriptor_server, (sockaddr *)&args->new_addr, &args->new_addr_siz); + + if (socket_descriptor_client >= 0) { + log_append(log_path, "client connected"); + + if (count_lines(log_path) > LOG_LENGTH) + line_position++; + + file_disp(log_path, line_position, LOG_LENGTH); + } + + delete args; + + return client_poll(); +} + +void * +client_poll(void) +{ + char msg[1024]; + for (;;) { + /* listen for msg from client */ + memset(&msg, 0, sizeof(msg)); /* clear buf */ + bytes_read += recv(socket_descriptor_client, (char *)&msg, sizeof(msg), 0); + log_append(log_path, msg); + + if (count_lines(log_path) > LOG_LENGTH) + line_position++; + + file_disp(log_path, line_position, LOG_LENGTH); + } + + return nullptr; +} + +void +server_start(int port) +{ + auto *a = new wclient_args_t{}; + sockaddr_in srv_addr; + sockaddr_in newsock; + socklen_t newsock_siz = sizeof(newsock); + int bstat; + int rc; + + /* set up socket and connection tools */ + bzero((char *)&srv_addr, sizeof(srv_addr)); + srv_addr.sin_family = AF_INET; + srv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + srv_addr.sin_port = htons(port); + + /* open stream-oriented socket w inet addr and track sock descriptor */ + socket_descriptor_server = socket(AF_INET, SOCK_STREAM, 0); + if (socket_descriptor_server < 0) + goto panic_if_no_server_sock; + + /* bind sock to its local addr */ + bstat = bind(socket_descriptor_server, (struct sockaddr *)&srv_addr, sizeof(srv_addr)); + if (bstat < 0) + goto panic_if_no_server_sock; + + log_append(log_path, "Waiting for a client to connect..."); + + /* listen for up to 5 simultaneous requests */ + listen(socket_descriptor_server, 5); + a->new_addr = newsock; + a->new_addr_siz = newsock_siz; + + rc = pthread_create(&client_wait_thread, nullptr, await_client, a); + + pthread_detach(client_wait_thread); + + log_append(log_path, "Server started successfully."); + + gettimeofday(&time_start, NULL); + + return; + +panic_if_no_server_sock: + endwin(); + fprintf(stderr, "Fatal: Could not establish server socket!\n"); + exit(1); +} + +void +server_stop(void) +{ + long e = time_end.tv_sec - time_start.tv_sec; + + gettimeofday(&time_end, NULL); + close(socket_descriptor_server); + + /* Don't just die silently */ + puts("********Session********"); + printf("Bytes written: %i\nBytes Read: %i\n", bytes_written, bytes_read); + printf("Elapsed time: %ld\n secs\n", e); + puts("Connection closed..."); +} + +void +client_stop(void) +{ + long e = time_end.tv_sec - time_start.tv_sec; + + gettimeofday(&time_end, NULL); + close(socket_descriptor_client); + puts("********Session********"); + printf("Bytes written: %i\nBytes read: %i\n", bytes_written, bytes_read); + printf("Elapsed time: %ld\n secs\n", e); + puts("Connection closed..."); +} + +void * +server_poll(void *args) +{ + wclient_args_t *a = static_cast(args); + int sock_desc = (int)a->aux; + char msg[1024]; + + for (;;) { + memset(&msg, 0, sizeof(msg)); + bytes_read += recv(sock_desc, (char*)&msg, sizeof(msg), 0); + + log_append(log_path, msg); + + if (count_lines(log_path) > LOG_LENGTH) + line_position++; + + file_disp(log_path, line_position, LOG_LENGTH); + } +} diff --git a/chat.h b/chat.h new file mode 100644 index 0000000..a8d3e4b --- /dev/null +++ b/chat.h @@ -0,0 +1,60 @@ +#ifndef CHAT_H +#define CHAT_H + +#ifndef _GLIBCXX_STRING +#include +#endif + +#ifndef _NETINET_IN_H +#include +#endif + +typedef enum { + NO_MODE = 0, + SERVER_MODE = 1, + CLIENT_MODE = 2 +} chatmode_t; + +typedef struct { + sockaddr_in new_addr; + socklen_t new_addr_siz; + int aux; +}wclient_args_t; + +const int CURSOR_DEFAULT_POSITION_X = 2; +const int CURSOR_DEFAULT_POSITION_Y = 12; + +const int LOG_LENGTH = 10; +int line_position = 0; + +chatmode_t mode = NO_MODE; + +struct timeval time_start; +struct timeval time_end; +int bytes_read; +int bytes_written; + +int listen_port = 9999; +std::string listen_ip_address = "127.0.0.1"; + +std::string log_path; + +int socket_descriptor_server; +int socket_descriptor_client; + +pthread_t client_wait_thread; + +void *await_client(void *a); +void clear_rows(int s, int e); +int count_lines(std::string p); +int file_disp(std::string p, int lnstart, int lncount = 10); +int log_append(std::string p, std::string ln); +int log_linect(std::string p); +void *client_poll(void); +void client_start(void); +void client_stop(void); +void *server_poll(void *a); +void server_start(int port); +void server_stop(void); + +#endif diff --git a/main.cc b/main.cc new file mode 100644 index 0000000..11434be --- /dev/null +++ b/main.cc @@ -0,0 +1,8 @@ +#include + +#include +#include +#include + +#include "chat.h" +