From 81dd6b162351d93dae5d71a45df56ee1a74a36ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 7 Jul 2020 13:04:26 -0300 Subject: Split application into server and client --- jet.cpp | 142 +++++++++++++++++++++++++++------------------------------------- 1 file changed, 60 insertions(+), 82 deletions(-) (limited to 'jet.cpp') diff --git a/jet.cpp b/jet.cpp index b11744a..dd70f13 100644 --- a/jet.cpp +++ b/jet.cpp @@ -1,103 +1,81 @@ #include #include -#include -#include -#include -#define PAGE_SIZE 4096 +#include + +#include +#include +#include +#include +#include +#include + #include "page.cpp" #include "point.cpp" #include "buffer.cpp" +#include "client.cpp" -#define NORMAL_MODE 0 -#define INSERT_MODE 1 - -int main(int argc, char *argv[]) { - setlocale(LC_ALL, ""); - initscr(); - cbreak(); - noecho(); - intrflush(stdscr, FALSE); - keypad(stdscr, TRUE); +#define PORT 6969 +#define MAX_COMMAND_SIZE 128 +#define MAX_EVENTS 10 - Buffer buffer("test"); - Point window_start(buffer.start); +int create_listener() { + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)}; + int opt = 1; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)); + bind(s, (sockaddr *) &addr, sizeof(sockaddr_in)); + listen(s, MAX_EVENTS); + return s; +} - if (argc > 1) { - FILE *f = fopen(argv[1], "r"); - char c; - while ((c = fgetc(f)) != EOF) { - buffer.cursor.push(c); +void parse_command(char *command, Client *client) { + char *token = strtok(command, " \n"); + while (token) { + if (strcmp(token, "show") == 0) { + client->show(); + } else if (strcmp(token, "move") == 0) { + client->move(); + } else { + client->args.push(atoi(token)); } - buffer.cursor = buffer.start; - fclose(f); + token = strtok(0, " \n"); } +} - int window_height, window_width; - getmaxyx(stdscr, window_height, window_width); +int main() { + Buffer scratch("scratch"); + scratch.read("LICENSE"); - int mode = NORMAL_MODE; + int listener = create_listener(); + int epollfd = epoll_create1(0); + epoll_event events[MAX_EVENTS]; + epoll_event ev; + ev.events = EPOLLIN; + ev.data.fd = listener; + epoll_ctl(epollfd, EPOLL_CTL_ADD, listener, &ev); - int quit = 0; - while (!quit) { - clear(); + Client *clients[1024] = {}; - int x = -1, y = -1; - Point window_end(window_start); - while (!window_end.at_end() && getcury(stdscr) < window_height - 1) { - if (window_end == buffer.cursor) { - getyx(stdscr, y, x); - } - printw("%lc", window_end.element()); - window_end++; - } - printw("%d", buffer.cursor.index); - if (x > -1 && y > -1) { - move(y, x); - } + for (;;) { + int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); - int input = getch(); - if (byte_type(input) == 1) { - if (mode == NORMAL_MODE) { - switch (input) { - case '': - quit = 1; - break; - case 'i': - mode = INSERT_MODE; - break; - case 'k': - buffer.prev_line(window_width); - break; - case 'j': - buffer.next_line(window_width); - break; - case 'h': - buffer.cursor--; - break; - case 'l': - buffer.cursor++; - break; - } + for (int i = 0; i < nfds; i++) { + if (events[i].data.fd == listener) { + int clientfd = accept(listener, 0, 0); + ev.events = EPOLLIN | EPOLLET; + ev.data.fd = clientfd; + Client *c = new Client(scratch); + c->sockfd = clientfd; + clients[clientfd] = c; + epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev); } else { - switch (input) { - case '': - mode = NORMAL_MODE; - break; - case KEY_BACKSPACE: - buffer.cursor.pop(); - break; - default: - buffer.cursor.push(input); - } - } - } else { - buffer.cursor.push(input); - for (int i = 0; i < byte_type(input) - 1; i++) { - buffer.cursor.push(getch()); + char command[MAX_COMMAND_SIZE] = {}; + int clientfd = events[i].data.fd; + read(clientfd, command, MAX_COMMAND_SIZE - 1); + parse_command(command, clients[clientfd]); } } } - endwin(); - return 0; + close(listener); } -- cgit v1.2.3