-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
224 lines (197 loc) · 5.23 KB
/
server.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include "sikv.h"
#define PORT 8007
size_t strlen0(char *buf)
{
size_t len = 0;
while (*buf != '\0')
{
buf++;
len++;
}
return len;
}
void free_input_buffer(char **input_buf)
{
for (size_t i = 0; i < 3; i++)
{
free(input_buf[i]);
}
free(input_buf);
}
static void sigint_handler(int sig)
{
if (write(STDERR_FILENO, "SIGINT\nCleaning up....\n", 23) == -1)
{
fprintf(stderr, "write error");
}
KV_destroy();
exit(EXIT_FAILURE);
}
char **parse_input(char *str, size_t len)
{
char **buf = malloc(sizeof(char *) * 3);
if (buf == NULL)
{
perror("failed malloc");
exit(EXIT_FAILURE);
}
memset(buf, 0, sizeof(char *) * 3);
size_t i = 0;
size_t off = 0;
size_t j = 0;
char *start = str;
while (*str == ' ')
{
i++;
str++;
}
while (i < len)
{
if (*str == ' ')
{
buf[j] = realloc(buf[j], (i - off) + 1);
if (buf[j] == NULL)
{
free(buf);
perror("failed realloc");
exit(EXIT_FAILURE);
}
memcpy(buf[j], (char *)&start[off], i - off);
buf[j][i - off] = '\0';
off = i;
j++;
}
else if (*str == '\n')
{
buf[j] = realloc(buf[j], (i - off) + 1);
if (buf[j] == NULL)
{
free(buf);
perror("failed realloc");
exit(EXIT_FAILURE);
}
memcpy(buf[j], (char *)&start[off], i - off);
buf[j][i - off] = '\0';
off = i;
j++;
}
i++;
str++;
}
return buf;
}
void serve(int argc, char *argv[])
{
if (argc < 3)
{
perror("hostname and port are required");
exit(EXIT_FAILURE);
}
if (MIN_ENTRY_NUM && CHECK_POWER_OF_2(MIN_ENTRY_NUM) != 0)
{
fprintf(stderr, "ERROR: Hmap size must be a power of two\n");
exit(EXIT_FAILURE);
}
char buf[BUFFSZ];
int server_fd, client_fd;
size_t nr_read;
int enable = 1;
socklen_t client_len;
struct protoent *proto;
struct sockaddr_in server_sock, client_sock;
unsigned short server_port = strtol(argv[2], NULL, 10);
proto = getprotobyname("tcp");
if (proto == NULL)
{
perror("getprotobyname");
exit(EXIT_FAILURE);
}
server_fd = socket(AF_INET, SOCK_STREAM, proto->p_proto);
if (server_fd == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0)
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
server_sock.sin_family = AF_INET;
server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
server_sock.sin_port = htons(server_port);
if (bind(server_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr_in)) == -1)
{
perror("bind");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) == -1)
{
perror("listen");
exit(EXIT_FAILURE);
}
if (signal(SIGINT, sigint_handler) == SIG_ERR)
{
exit(EXIT_FAILURE);
}
printf("SiKV InMemory Database Server\nListening for connections on port %d\n", PORT);
struct hash_map *hmap = KV_init(MIN_ENTRY_NUM, KV_hash_function, KV_STRING, false);
while (1)
{
client_len = sizeof(struct sockaddr_in);
client_fd = accept(server_fd, (struct sockaddr *)&client_sock, &client_len);
while ((nr_read = read(client_fd, buf, BUFFSZ)) > 0)
{
if (buf[nr_read - 1] == '\n')
{
char **input_buf = parse_input(buf, nr_read);
char *ret = process_cmd(hmap, sizeof(input_buf), input_buf);
if (ret == NULL)
{
ret = "GET Not found\n";
printf("%s\n", ret);
if (write(client_fd, ret, strlen(ret)) == -1)
{
fprintf(stderr, "write error");
}
}
else if (ret == SUCCESS)
{
ret = "Ok\n";
printf("%s\n", ret);
if (write(client_fd, ret, strlen(ret)) == -1)
{
fprintf(stderr, "write error");
}
}
else
{
size_t n = strlen(ret);
char *temp = malloc(n + 1);
if (temp == NULL)
{
perror("malloc");
}
memcpy(temp, ret, n);
temp[n] = '\n';
if (write(client_fd, temp, n + 1) == -1)
{
fprintf(stderr, "write error");
}
free(temp);
}
free_input_buffer(input_buf);
}
}
close(client_fd);
}
KV_destroy();
}