-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverB.cpp
More file actions
228 lines (205 loc) · 5.83 KB
/
serverB.cpp
File metadata and controls
228 lines (205 loc) · 5.83 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <cstring>
#define BPORT "22982"
#define UDPPORT "24982"
#define MAXDATASIZE 2048
using namespace std;
map<string, string> dictionary;
map<string, string>::iterator it;
string search(string str){
string results = "", def1 = "", sim = "", def2 = "", d = " :: ";
int num = 0, cnt = 0;
for(it=dictionary.begin(); it!=dictionary.end(); ++it){
string word = it->first;
int c = 0;
if(str.length() != word.length()) continue;
for(unsigned int i = 0; i < str.length(); ++i){
if(tolower(str[i]) != tolower(word[i])) ++c;
}
if(c == 0){
num = 1;
def1 = it->second;
} else if(c == 1){
++cnt;
sim = word;
def2 = it->second;
}
}
cout << "The ServerB has found < " << num <<
" > match and < " << cnt << " > similar words" << endl;
results += to_string(num) + d + def1 + d + to_string(cnt) + d + sim + d + def2;
return results;
}
string prefix(string str){
string results = "", words = "", d = " :: ";
int cnt = 0;
for(it=dictionary.begin(); it!=dictionary.end(); ++it){
string word = it->first;
int c = 0;
if(str.length() > word.length()) continue;
for(unsigned int i = 0; i < str.length(); ++i){
if(tolower(str[i]) != tolower(word[i])) ++c;
}
if(c == 0){
words += d + word;
++cnt;
}
}
cout << "The ServerB has found < " << cnt << " > matches" << endl;
results += to_string(cnt) + words;
return results;
}
string suffix(string str){
string results = "", words = "", d = " :: ";
int cnt = 0;
for(it=dictionary.begin(); it!=dictionary.end(); ++it){
string word = it->first;
int c = 0;
if(str.length() > word.length()) continue;
for(unsigned int i = 0; i < str.length(); ++i){
if(tolower(str[str.length()-i-1]) != tolower(word[word.length()-i-1])) ++c;
}
if(c == 0){
words += d + word;
++cnt;
}
}
cout << "The ServerB has found < " << cnt << " > matches" << endl;
results += to_string(cnt) + words;
return results;
}
string func(string function, string input){
string results;
if(function == "search"){
results = function + " :: " + search(input);
} else if(function == "prefix"){
results = function + " :: " + prefix(input);
} else if(function == "suffix"){
results = function + " :: " + suffix(input);
} else {
results = "";
}
return results;
}
bool parseFile(const string &filename){
// Opens file and prepares for transfer
ifstream infile(filename.c_str());
if(!infile) return false;
string line, word, definition;
string delimiter = " :: ";
// Reads from file into map
while(getline(infile, line)){
size_t pos = line.find(delimiter);
word = line.substr(0, pos);
line.erase(0, pos+delimiter.length());
definition = line;
dictionary.insert(pair<string, string>(word, definition));
}
infile.close();
return true;
}
int main(void)
{
int sockfd, sockfdU, byteNum, error;
char buffer[MAXDATASIZE];
struct addrinfo serv_info, *servinfo, *process;
struct addrinfo serv_infoU, *servinfoU, *processU;
struct sockaddr_storage curr_addr;
socklen_t addr_len;
// Parse dictionary
if(!parseFile("backendB.txt")){
exit(0);
perror("file failed");
}
// Sets server classification
memset(&serv_info, 0, sizeof serv_info);
serv_info.ai_family = AF_INET;
serv_info.ai_socktype = SOCK_DGRAM;
serv_info.ai_flags = AI_PASSIVE;
memset(&serv_infoU, 0, sizeof serv_infoU);
serv_infoU.ai_family = AF_INET;
serv_infoU.ai_socktype = SOCK_DGRAM;
serv_infoU.ai_flags = AI_PASSIVE;
// Gets server information
if(( error = getaddrinfo("localhost", BPORT, &serv_info, &servinfo) ) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
exit(1);
}
if(( error = getaddrinfo("localhost", UDPPORT, &serv_infoU, &servinfoU) ) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
exit(1);
}
// Find usable process for ports
for(process = servinfo; process != NULL; process = process->ai_next){
if((sockfd = socket(process->ai_family, process->ai_socktype, process->ai_protocol)) == -1){
perror("socket failed");
continue;
}
if(bind(sockfd, process->ai_addr, process->ai_addrlen) == -1){
close(sockfd);
perror("bind failed");
continue;
}
break;
}
for(processU = servinfoU; processU != NULL; processU = processU->ai_next){
if((sockfdU = socket(processU->ai_family, processU->ai_socktype, processU->ai_protocol)) == -1){
perror("socket failed");
continue;
}
break;
}
// Check if valid connection
if((process == NULL) || (processU == NULL)){
fprintf(stderr, "failed bind\n");
exit(1);
}
freeaddrinfo(servinfo);
freeaddrinfo(servinfoU);
cout << "\nThe ServerB is up and running using UDP on port " << BPORT << "." << endl;
// Loop for receiving datagrams
while(1){
// Blocks until datagram received
addr_len = sizeof curr_addr;
bzero(buffer, MAXDATASIZE);
if((byteNum = recvfrom(sockfd, buffer, MAXDATASIZE-1, 0,
(struct sockaddr*)&curr_addr, &addr_len)) == -1){
perror("receive failed");
exit(1);
}
// Decode initial message
string msg = string(buffer);
string delimiter = " :: ";
size_t pos = msg.find(delimiter);
string function = msg.substr(0, pos);
msg.erase(0, pos+delimiter.length());
string input = msg;
cout << "The ServerB received input < " << input << " > and operation < "
<< function << " >" << endl;
// Get results in a string
string temp = "B :: " + func(function, input);
// Send back to aws
const char* msg2 = temp.c_str();
if ((byteNum = sendto(sockfdU, msg2, strlen(msg2), 0, processU->ai_addr, processU->ai_addrlen)) == -1) {
perror("send failed");
exit(1);
}
cout << "The ServerB finished sending the output to AWS\n" << endl;
}
close(sockfd);
close(sockfdU);
return 0;
}