summaryrefslogtreecommitdiffstats
path: root/03_exercise/cli/client.c
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-06-06 17:19:55 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-06-06 17:19:55 +0200
commit2821f125c91c192cdd28997e78f366f2db2d39c4 (patch)
tree7f6f4b6bbfdb3a2b65f87ed03b7b55d26a43fa1a /03_exercise/cli/client.c
parent178cbf7c04639cd24b7a1eb70d532fb64b9f908d (diff)
downloadbetriebssysteme-2821f125c91c192cdd28997e78f366f2db2d39c4.tar.gz
betriebssysteme-2821f125c91c192cdd28997e78f366f2db2d39c4.zip
stuff works
Diffstat (limited to '03_exercise/cli/client.c')
-rw-r--r--03_exercise/cli/client.c154
1 files changed, 89 insertions, 65 deletions
diff --git a/03_exercise/cli/client.c b/03_exercise/cli/client.c
index d64dd62..c1bc5d1 100644
--- a/03_exercise/cli/client.c
+++ b/03_exercise/cli/client.c
@@ -8,20 +8,21 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <signal.h>
#define PORT 9000
#define HOST "127.0.0.1"
#define BUF_SIZE 2048
-int cfd;
+int sock;
/* Signal Handler for SIGINT */
void sigintHandler(int sig_num) {
errno = 0;
- if (fcntl(cfd, F_GETFD) != -1 || errno != EBADF) {
- write(cfd, "exit\n", 6);
- close(cfd);
+ if (fcntl(sock, F_GETFD) != -1 || errno != EBADF) {
+ write(sock, "exit\n", 6);
+ close(sock);
}
printf("Terminating client\n");
fflush(stdout);
@@ -81,86 +82,109 @@ void send_file(int server_fd, char *path) {
close(file_fd);
}
+#define BREAK 1
+#define CONTINUE 2
+
+int parse(int server_sock, char *buffer, ssize_t length) {
+ char cmd[4];
+ char path[BUF_SIZE - 3];
+ memcpy(cmd, buffer, 3);
+ cmd[3] = 0;
+ memcpy(path, buffer + 4, BUF_SIZE - 4);
+ path[strlen(path) - 1] = 0;
+ if (strcmp(cmd, "put") == 0) {
+ if (strlen(path) == 0) {
+ fprintf(stderr, "path missing\n");
+ } else {
+ printf("got put with %s\n", path);
+ send_file(sock, path);
+ }
+ return CONTINUE;
+ }
+
+ buffer[length] = '\0';
+
+ if (strspn(buffer, " \n\t") == strlen(buffer)) {
+ // skip empty lines - empty being just spaces or tabs
+ return CONTINUE;
+ }
+
+ if (write(server_sock, buffer, strlen(buffer)) < 0)
+ die("Could not send message");
+
+ if (strcmp(buffer, "exit\n") == 0) {
+ return BREAK;
+ }
+
+ return 0;
+}
+
+void read_stdin(int *done);
+
+void read_server_sock(int *done);
+
int main() {
+ setvbuf(stdout, NULL, _IONBF, 0);
+
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = inet_addr(HOST)
};
- char buf[BUF_SIZE];
- if ((cfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die("Could not open socket");
- if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+ if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
die("Could not connect to socket");
- char *line = NULL;
- size_t cap = 0;
- __ssize_t length;
+ signal(SIGINT, &sigintHandler);
- if (read(cfd, buf, BUF_SIZE) < 0)
- die("Could not receive message");
+ pid_t pid = fork();
- printf("%s\n", buf);
- memset(buf, 0, BUF_SIZE);
-
- while (1) {
- printf("$> ");
- //printf("[C] awaiting command\n");
- if ((length = getline(&line, &cap, stdin)) < 0) {
- fprintf(stderr, "Failed to read from STDIN");
- fflush(stderr);
- exit(-1);
- }
+ int done = 0;
+ if (pid == 0) {
+ read_stdin(&done);
+ } else {
+ read_server_sock(&done);
+ }
- if (strspn(line, " \n\t") == strlen(line)) {
- // skip empty lines - empty being just spaces or tabs
- continue;
- }
+ close(sock);
+ return 0;
+}
- char cmd[4];
- char path[BUF_SIZE - 3];
- memcpy(cmd, line, 3);
- cmd[3] = 0;
- memcpy(path, line + 4, BUF_SIZE - 4);
- path[strlen(path) - 1] = 0;
- printf("cmd: \"%s\"\n", cmd);
- printf("path: \"%s\"\n", path);
- if (strcmp(cmd, "put") == 0) {
- if (strlen(path) == 0) {
- fprintf(stderr, "path missing\n");
- } else {
- printf("got put with %s\n", path);
- send_file(cfd, path);
+void read_server_sock(int *done) {
+ char server_buffer[BUF_SIZE];
+ ssize_t length;
+
+ while (!*done) {
+ if ((length = read(sock, server_buffer, BUF_SIZE)) > 0) {
+ server_buffer[length] = '\0';
+ printf("%s", server_buffer);
+ if (server_buffer[length - 1] == '\n') {
+ printf("$> ");
+ fflush(stdout);
}
- free(line);
- line = NULL;
- continue;
}
+ }
+}
- strncpy(buf, line, strlen(line) + 1);
-
- //printf("[C] pre send: \"%s\"\n", buf);
-
- if (write(cfd, buf, strlen(line)) < 0)
- die("Could not send message");
- if (strcmp(line, "exit\n") == 0) {
- free(line);
- line = NULL;
- break;
+void read_stdin(int *done) {
+ char stdinp_buffer[BUF_SIZE];
+ ssize_t length;
+
+ while (!*done) {
+ if ((length = read(STDIN_FILENO, stdinp_buffer, BUF_SIZE)) > 0) {
+ switch (parse(sock, stdinp_buffer, length)) {
+ case CONTINUE:
+ continue;
+ case BREAK:
+ *done = 1;
+ break;
+ default:
+ break;
+ }
}
-
- if (read(cfd, buf, BUF_SIZE) < 0)
- die("Could not receive message");
-
- printf("%s", buf);
- memset(buf, 0, BUF_SIZE);
- free(line);
- line = NULL;
}
-
- close(cfd);
- return 0;
}