-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_command.c
More file actions
109 lines (107 loc) · 3.13 KB
/
client_command.c
File metadata and controls
109 lines (107 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "client_command.h"
#include "sds.h"
#include "socket_conf.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
static ssize_t read_large(int connfd, sds *msg);
static void do_nothing(client_t *client, char *line, char *cmd);
static void exit_command(client_t *client, char *line, char *cmd);
static void help_command(client_t *client, char *line, char *cmd);
static void list_command(client_t *client, char *line, char *cmd);
static void save_command(client_t *client, char *line, char *cmd);
static void find_command(client_t *client, char *line, char *cmd);
static void sort_command(client_t *client, char *line, char *cmd);
void setup_command_map(map *map) {
*map = map_create();
map_set(*map, "nothing", do_nothing);
map_set(*map, "exit", exit_command);
map_set(*map, "help", help_command);
map_set(*map, "list", list_command);
map_set(*map, "save", save_command);
map_set(*map, "find", find_command);
map_set(*map, "sort", sort_command);
}
void (*get_command(client_t *client, char *cmd))(client_t *, char *line,
char *cmd) {
void (*command)(client_t *, char *, char *);
map map = client->commands;
if (map_contains(map, cmd)) {
command = map_get(map, cmd);
} else {
command = map_get(map, "nothing");
}
return command;
}
static void do_nothing(client_t *client, char *line, char *cmd) {
printf("wrong command\n");
}
static void exit_command(client_t *client, char *line, char *cmd) {
int sockfd = client->sockfd;
client->running = false;
write(sockfd, "exit", 5);
}
static void help_command(client_t *client, char *line, char *cmd) {
int sockfd = client->sockfd;
write(sockfd, "help", 5);
sds msg;
read_large(sockfd, &msg);
printf("%s", msg);
sdsfree(msg);
}
static void list_command(client_t *client, char *line, char *cmd) {
int sockfd = client->sockfd;
write(sockfd, "list", 5);
sds msg;
read_large(sockfd, &msg);
printf("%s", msg);
sdsfree(msg);
};
static void save_command(client_t *client, char *line, char *cmd) {
int sockfd = client->sockfd;
write(sockfd, "save", 5);
char buff[MAX];
sprintf(buff, "%s", line);
write(sockfd, buff, MAX);
sds rsp;
read_large(sockfd, &rsp);
printf("%s", rsp);
};
static void find_command(client_t *client, char *line, char *cmd) {
int sockfd = client->sockfd;
write(sockfd, "find", 5);
char buff[MAX];
sprintf(buff, "%s", line);
write(sockfd, buff, MAX);
sds rsp;
read_large(sockfd, &rsp);
printf("%s", rsp);
};
static void sort_command(client_t *client, char *line, char *cmd) {
int sockfd = client->sockfd;
write(sockfd, "sort", 5);
char buff[MAX];
sprintf(buff, "%s", line);
write(sockfd, buff, MAX);
sds rsp;
read_large(sockfd, &rsp);
printf("%s", rsp);
}
static ssize_t read_large(int connfd, sds *msg) {
char buff[MAX];
bzero(buff, MAX);
read(connfd, buff, MAX);
char *remaining;
size_t length;
length = strtol(buff, &remaining, 10);
*msg = sdsempty();
ssize_t nread;
while (length > 0) {
bzero(buff, MAX);
nread = read(connfd, buff, MAX);
length -= nread;
sdscatlen(*msg, buff, nread);
}
return nread;
}