-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverB.cpp
More file actions
202 lines (176 loc) · 5.66 KB
/
serverB.cpp
File metadata and controls
202 lines (176 loc) · 5.66 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.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 <signal.h>
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#define BUDPPORT "31527"
#define AWSUDPPORT "33527"
#define MAXDATASIZE 1000
int searchmap(std::string &mapbuff,char mapid, int srcVtxid, int dstVtxid);
int getUDPsockandbind(struct addrinfo *servinfo);
int main(void)
{
int selffd;
struct addrinfo hints, *servinfo;
int rv;
char buf[MAXDATASIZE];int numbytes;
char mapid; int srcVtxid,dstVtxid;
std::string mapbuff; int foundmap=0;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo("127.0.0.1", BUDPPORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
selffd = getUDPsockandbind(servinfo);
freeaddrinfo(servinfo);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo("127.0.0.1", AWSUDPPORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
std::cout << "The server B is up and running using UDP on port " << BUDPPORT << "\n" << std::endl;
while(1)
{
if ((numbytes = recvfrom(selffd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *) NULL, NULL)) == -1)
{
perror("recvfrom");
exit(1);
}
mapid = buf[0];
memcpy(&srcVtxid,buf+1,sizeof(int));
memcpy(&dstVtxid,buf+1+sizeof(int),sizeof(int));
std::cout << "The server B has received input for finding graph of map " << mapid << "\n" << std::endl;
foundmap = searchmap(mapbuff, mapid, srcVtxid, dstVtxid);
if(!foundmap)
std::cout << "The server B does not have the required graph id " << mapid << "\n" << std::endl;
if ((numbytes = sendto(selffd, mapbuff.data(), mapbuff.length(), 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1)
{
perror("talker: sendto");
exit(1);
}
if(foundmap)
std::cout << "The server B has sent Graph to AWS" << "\n" << std::endl;
else
std::cout << "The server B has sent \"Graph not found\" to AWS" << "\n" << std::endl;
}
close(selffd);
freeaddrinfo(servinfo);
return 0;
}
int searchmap(std::string &mapbuff,char mapid, int srcVtxid, int dstVtxid)
{
std::string line;
int foundmap = 0, foundsrc = 0, founddst = 0;
int lspacepos, rspacepos, fstdgt, secdgt;
std::ifstream map2;
map2.open("./map2.txt");
if (map2.is_open())
{
while ( getline (map2,line) )
{
if (foundmap)
{
if (line.length() == 1 && isalpha(line.c_str()[0])) // next map begins
break;
line.append("\n");
mapbuff.append(line);
lspacepos = -1; rspacepos = -1;
lspacepos = line.find(" ");
rspacepos = line.find(" ",lspacepos+1);
if(lspacepos != -1 && rspacepos != -1)
{
fstdgt = atoi(line.substr(0,lspacepos).c_str());
secdgt = atoi(line.substr(lspacepos+1,rspacepos-lspacepos-1).c_str());
if (fstdgt == srcVtxid)
foundsrc = 1;
else if (fstdgt == dstVtxid)
founddst = 1;
if (secdgt == srcVtxid)
foundsrc = 1;
else if (secdgt == dstVtxid)
founddst = 1;
}
}
else if (line.compare(&mapid) == 0) // found the required map
{
foundmap = 1;
mapbuff.clear();
mapbuff.append("serverB\n");
}
}
map2.close();
if(foundmap == 1 && foundsrc == 1 && founddst ==1)
return 1;
else
{
mapbuff.clear();
if (!foundmap)
mapbuff.append("serverB!m");
else if (!foundsrc && founddst)
mapbuff.append("serverB!s");
else if (foundsrc && !founddst)
mapbuff.append("serverB!d");
else if (!foundsrc && !founddst)
mapbuff.append("serverB!b");
if (foundmap)
return 1;
else
return 0;
}
}
else
{
std::cout << "Unable to open file" << std::endl;
exit(1);
}
}
int getUDPsockandbind(struct addrinfo *servinfo)
{
int sockfd;
struct addrinfo *p;
int reuseaddr=1;
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
perror("listener: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("listener: bind");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "listener: failed to bind socket\n");
exit (2);
}
return sockfd;
}