-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKVClient.cpp
More file actions
259 lines (220 loc) · 8.12 KB
/
KVClient.cpp
File metadata and controls
259 lines (220 loc) · 8.12 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "KVClientLibrary.hpp"
// Name of the server configuration file
#define SERVER_CONFIG_FILE "server.config"
// Port number on which server is listening
int SERVER_LISTENING_PORT = -1;
// Socket-related variables
struct sockaddr_in addr = {0};
int sockFD;
int main(int argc, char *argv[])
{
//struct timespec start,finish;
//double elapsed;
// Keeps track of the number of total requests processed processed through the file (if any)
int NUMBER_OF_REQUESTS_PROCESSED = 0;
// Reading the port number from the server configuration file
FILE *serverConfigFile = fopen(SERVER_CONFIG_FILE, "r");
char *temp = NULL;
size_t len = 0;
ssize_t numberOfLinesRead;
//clock_gettime(CLOCK_MONOTONIC, &start);
while ((numberOfLinesRead = getline(&temp, &len, serverConfigFile)) != -1)
{
char *configParameter = strtok(temp, "=");
char *parameterValue = strtok(NULL, "=");
if (strcmp(configParameter, "PORT_NUMBER") == 0)
{
SERVER_LISTENING_PORT = atoi(parameterValue);
break;
}
}
char message[256];
// Setting up socket structures and connecting to the server
sockFD = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVER_LISTENING_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
connect(sockFD, (struct sockaddr *) &addr, sizeof(addr));
// Opening the testcase file
FILE *testCaseFile = NULL;
char *temp_test = NULL;
size_t len_test = 0;
ssize_t numberOfLinesRead_test;
if (argc > 1)
{
testCaseFile = fopen(argv[1], "r");
}
while (1)
{
char requestType[4];
char keyInput[300];
char valueInput[300];
// Asking the client what type of operation it wants to perform
if (testCaseFile != NULL)
{
numberOfLinesRead = getline(&temp_test, &len_test, testCaseFile);
if (numberOfLinesRead == -1)
{
printf("All testcases are over!\n");
break;
}
else
{
NUMBER_OF_REQUESTS_PROCESSED += 1;
char *requestTypeFromFile = strtok(temp_test, "[ \n]");
for (int i = 0; i < 3; i++)
{
requestType[i] = requestTypeFromFile[i];
}
requestType[3] = '\0';
}
}
else
{
printf("Enter request type: ");
scanf("%s", requestType);
}
if (strlen(requestType) == 3)
{
/* Checking whether the user has made one of the valid requests (i.e. GET, PUT or DEL)
* and handling each of these requests */
if (strcmp(requestType, "GET") == 0)
{
if (testCaseFile != NULL)
{
char *keyFromFile = strtok(NULL, "[ \n]");
for (int i = 0; i < strlen(keyFromFile); i++)
{
keyInput[i] = keyFromFile[i];
}
keyInput[strlen(keyFromFile)] = '\0';
// printf("Performing GET for Key: %s\n", keyInput);
}
else
{
printf("Enter the key: ");
scanf("%s", keyInput);
}
// If the user enters a key longer than 256 characters
if (strlen(keyInput) > KEY_VALUE_MAX_LENGTH)
{
// Displaying the appropriate error message
printf("Key should be less than or equal to 256 characters.\n");
}
else
{
// Using the GET function of KVClientLibrary to request the corresponding value
char *serverResponse = GET(keyInput);
printf("\n%s\n", serverResponse);
free(serverResponse);
}
}
else if (strcmp(requestType, "PUT") == 0)
{
if (testCaseFile != NULL)
{
char *keyFromFile = strtok(NULL, "[ \n]");
for (int i = 0; i < strlen(keyFromFile); i++)
{
keyInput[i] = keyFromFile[i];
}
keyInput[strlen(keyFromFile)] = '\0';
// printf("Performing PUT for Key: %s\n", keyInput);
}
else
{
printf("Enter the key: ");
scanf("%s", keyInput);
}
// If the user enters a key longer than 256 characters
if (strlen(keyInput) > KEY_VALUE_MAX_LENGTH)
{
// Displaying the appropriate error message
printf("Key should be less than or equal to 256 characters.\n");
}
else
{
if (testCaseFile != NULL)
{
char *valueFromFile = strtok(NULL, "[ \n]");
for (int i = 0; i < strlen(valueFromFile); i++)
{
valueInput[i] = valueFromFile[i];
}
valueInput[strlen(valueFromFile)] = '\0';
}
else
{
printf("Enter the value: ");
scanf("%s", valueInput);
}
// If the user enters a value longer than 256 characters
if (strlen(valueInput) > KEY_VALUE_MAX_LENGTH)
{
// Displaying the appropriate error message
printf("Value should be less than or equal to 256 characters.\n");
}
else
{
// Using the PUT function of KVClientLibrary to insert/update the key-value pair
PUT(keyInput, valueInput);
}
}
}
else if (strcmp(requestType, "DEL") == 0)
{
if (testCaseFile != NULL)
{
char *keyFromFile = strtok(NULL, "[ \n]");
for (int i = 0; i < strlen(keyFromFile); i++)
{
keyInput[i] = keyFromFile[i];
}
keyInput[strlen(keyFromFile)] = '\0';
// printf("Performing DEL for Key: %s\n", keyInput);
}
else
{
printf("Enter the key: ");
scanf("%s", keyInput);
}
// If the user enters a key longer than 256 characters
if (strlen(keyInput) > KEY_VALUE_MAX_LENGTH)
{
// Displaying the appropriate error message
printf("Key should be less than or equal to 256 characters.\n");
}
else
{
// Using the DEL function of KVClientLibrary to remove the key-value pair
DEL(keyInput);
}
}
else
{
// Invalid request type
printf("Invalid request type!\n");
}
}
else
{
// Invalid request type
printf("Invalid request type!\n");
}
}
/*
clock_gettime(CLOCK_MONOTONIC, &finish);
// Calculating the total time and throughput
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec)/1000000000.0;
double throughput = (double) (60000/elapsed);
printf("\n\nTotal time to process %d requests is %f and throughput is %f\n", NUMBER_OF_REQUESTS_PROCESSED, elapsed, throughput);
*/
// If the requests were processed through a file, close that file
if (argc > 1)
{
fclose(testCaseFile);
}
// Closing the socket
close(sockFD);
}