-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKVClientLibrary.cpp
More file actions
158 lines (120 loc) · 4.9 KB
/
KVClientLibrary.cpp
File metadata and controls
158 lines (120 loc) · 4.9 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
// This contains the implementation of all functions declared in KVClientLibrary
#include "KVClientLibrary.hpp"
char *GET(char *key)
{
char requestMessage[MESSAGE_LENGTH];
char responseMessage[MESSAGE_LENGTH];
// This is done to make the padding process easy
for (int i = 0; i < MESSAGE_LENGTH; i++)
{
requestMessage[i] = '\0';
}
// The first byte of the request message will be status code
requestMessage[0] = GET_STATUS_CODE;
// This will point to the location where the 'key' will be inserted in our request message
char *keyInRequestMessage = &requestMessage[1];
// Copying the 'key' to the request message
strcpy(keyInRequestMessage, key);
// Sending the GET request to the server
int n = write(sockFD, requestMessage, MESSAGE_LENGTH);
// Receiving the response from the server
int m = read(sockFD, responseMessage, MESSAGE_LENGTH);
// Decoding the response by first checking the status code
int statusCode = responseMessage[0] & 255;
// If the Key is not available in the storage, then response includes the error status code (240)
if (statusCode == ERROR_STATUS_CODE)
{
char errorMessage[256] = "GET Request Error: Key not availabe on storage!";
char *requestResult = (char *) malloc(sizeof(errorMessage));
strcpy(requestResult, errorMessage);
return requestResult;
}
/* If the Key is available in the storage, then response message include the success status code
* (200) as well as the corresponding value present in the storage */
else if (statusCode == SUCCESS_STATUS_CODE)
{
char receivedValue[KEY_VALUE_MAX_LENGTH + 1];
char *valueInResponseMessage = &responseMessage[KEY_VALUE_MAX_LENGTH + 1];
for (int i = 0; i < KEY_VALUE_MAX_LENGTH + 1; i++)
{
receivedValue[i] = '\0';
}
strcpy(receivedValue, valueInResponseMessage);
char *requestResult = (char *) malloc(sizeof(receivedValue));
strcpy(requestResult, receivedValue);
return requestResult;
}
else
{
return NULL;
}
return NULL;
}
void PUT(char *key, char *value)
{
char requestMessage[MESSAGE_LENGTH];
char responseMessage[MESSAGE_LENGTH];
// This is done to make the padding process easy
for (int i = 0; i < MESSAGE_LENGTH; i++)
{
requestMessage[i] = '\0';
}
// The first byte of the request message will be status code
requestMessage[0] = PUT_STATUS_CODE;
// This will point to the location where the 'key' will be inserted in our request message
char *keyInRequestMessage = &requestMessage[1];
// Copying the 'key' to the request message
strcpy(keyInRequestMessage, key);
// This will point to the location where the 'value' will be inserted in our request message
char *valueInRequestMessage = &requestMessage[257];
// Copying the 'value' to the request message
strcpy(valueInRequestMessage, value);
// Sending the PUT request to the server
int n = write(sockFD, requestMessage, MESSAGE_LENGTH);
// Receiving the response from the server
int m = read(sockFD, responseMessage, MESSAGE_LENGTH);
// Decoding the response by first checking the status code
int statusCode = responseMessage[0] & 255;
// The server returns success status code (200) on successful insertion
if (statusCode == SUCCESS_STATUS_CODE)
{
printf("Successfully inserted the key-value pair!\n");
}
else
{
// If insertion fails, the server returns error status code (240)
printf("Unexpected error occurred during insertion!\n");
}
}
void DEL(char *key)
{
char requestMessage[MESSAGE_LENGTH];
char responseMessage[MESSAGE_LENGTH];
// This is done to make the padding process easy
for (int i = 0; i < MESSAGE_LENGTH; i++)
{
requestMessage[i] = '\0';
}
// The first byte of the request message will be status code
requestMessage[0] = DEL_STATUS_CODE;
// This will point to the location where the 'key' will be inserted in our request message
char *keyInRequestMessage = &requestMessage[1];
// Copying the 'key' to the request message
strcpy(keyInRequestMessage, key);
// Sending the DEL request to the server
int n = write(sockFD, requestMessage, MESSAGE_LENGTH);
// Receiving the response from the server
int m = read(sockFD, responseMessage, MESSAGE_LENGTH);
// Decoding the response by first checking the status code
int statusCode = responseMessage[0] & 255;
// The server returns success status code (200) on successful deletion
if (statusCode == SUCCESS_STATUS_CODE)
{
printf("Successfully deleted the key-value pair!\n");
}
else
{
// The server returns error status code (240) if the key was not present in the storage
printf("DEL Error: Key was not present in the storage!\n");
}
}