-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
171 lines (133 loc) · 3.78 KB
/
Copy pathclient.cpp
File metadata and controls
171 lines (133 loc) · 3.78 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
//standard import
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
//import for to grab socket / ip includes necessaryq
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <vector>
/*
This program is fully developed and created by: Marcus Ruth
https://www.github.com/polter-dev/miniredis_cpp
Program Synopsis: client that connects and communicates to the server to simulate a redis like system
*/
//grab socket and error handles
int grabSocket(){
int fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd == -1){
printf("Socket could not be grabbed!\n");
exit(errno);
}
return fd;
}
void initSockAddr(struct sockaddr_in *s1){
s1->sin_family = AF_INET;
s1->sin_port = htons(1234);
s1->sin_addr.s_addr = inet_addr("127.0.0.1"); //local address
}
void connectSock(int fd, struct sockaddr_in *s){
int connectNo = connect(fd, (struct sockaddr *)s, sizeof(*s));
if (connectNo){
printf("Connect could not be made\n");
exit(errno);
}
}
int sendText(const char *word, int fd){
int val = send(fd, word, strlen(word), 0);
if (val == -1){
printf("There was an issue sending!\n");
exit(errno);
}
return val;
}
char *setWord(char input[1024]){
int len = strlen(input);
char *ret = (char*)malloc(sizeof(char) * (len+1));
strcpy(ret, input);
return ret;
}
string buildRESPEncoding(std::vector<string> &arr, int count){
string start = "*" + std::to_string(count) + "\r\n";
for (int i = 0; i < count; i++){
string temp = "$" + std::to_string(arr[i].length()) + "\r\n" + arr[i] + "\r\n";
start = start.append(temp);
}
return start;
}
int encodeSendCmd(int fd, char *word){
if (!word) return 0;
std::vector<string> totalWords;
int count = 0;
char *poach = strtok(word, " ");
while (poach){
totalWords.push_back(poach);
count++;
poach = strtok(NULL, " ");
}
string build = buildRESPEncoding(totalWords, count);
return sendText(build.c_str(), fd);
}
void extractSimpleString(char *response){
char *finder = strchr(response, '\r');
string word(response + 1, finder);
cout << word << endl;
}
void extractBulkString(char *response){
char *finder = strchr(response, '\r');
string num(response + 1, finder);
int size = stoi(num);
std::string word(finder + 2, size);
cout << word << endl;
}
void decodeResponse(char* response){
char type = response[0];
if (type == '+'){
extractSimpleString(response);
} else if (type == '$'){
extractBulkString(response);
} else if (type == '_'){
cout << "(nil)" << endl;
} else if (type == '-'){
extractSimpleString(response);
}
}
int clientRun(int fd){
char userBuffer[1024];
fgets(userBuffer, 1024, stdin);
userBuffer[strcspn(userBuffer, "\n")] = '\0';
char *temp = setWord(userBuffer);
int sent = encodeSendCmd(fd, temp);
if (!strncmp(temp, "QUIT", 4)){
free(temp);
return 0;
}
free(temp);
char *buffer = (char*)malloc(sizeof(char) * 1024 + 1);
int received = recv(fd, buffer, 1024, 0);
if (received == -1){
printf("Issue receiving size of text in bytes\n");
exit(-1);
} else if (received == 0){
printf("TCP connection was closed on server side before processing \n");
exit(0);
}
decodeResponse(buffer);
free(buffer);
return 1;
}
int main(){
int fd = grabSocket();
struct sockaddr_in clientSock1;
initSockAddr(&clientSock1);
connectSock(fd, &clientSock1);
int x = 1;
cout << "Input commands\n" << endl;
while(x){
x = clientRun(fd);
}
close(fd);
return 0;
}