-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_and.cpp
More file actions
248 lines (227 loc) · 4.62 KB
/
Copy pathserver_and.cpp
File metadata and controls
248 lines (227 loc) · 4.62 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
#include <iostream>
#include<fstream>
#include<string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include<vector>
#include<math.h>
using namespace std;
char* dispose(char*,int);
/**
server_and processes for and operation.
receiving the information from edge server, parse the data and send back the result.
**/
int main()
{
// record the information of the addresses that will be used
struct in_addr addr;
ulong l;
l=inet_addr("127.0.0.1");
memcpy(&addr,&l,4);
struct sockaddr_in remote;
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_port=htons(22749);
local.sin_addr=addr;
remote.sin_family=AF_INET;
remote.sin_port=htons(24749);
remote.sin_addr=addr;
memset(&(local.sin_zero), 0, 8);
int count=0;
//create new UDP socket;
int sock=socket(PF_INET,SOCK_DGRAM,0);
if(bind(sock,(struct sockaddr*)&local,sizeof(struct sockaddr))==-1)
{
cout<<"bad binding"<<endl;
return 0;
}
cout<<"The Server AND is up and running using UDP on port 22749."<<endl;
cout<<"The Server AND start receiving lines from the edge server for AND computation. The computation results are:"<<endl;
//loop for data transfer between backend server and edge server
while(1)
{
socklen_t clientSize=sizeof(struct sockaddr_in);
vector<char*> result_list;
char buffer[50];
char message[50];
char success[10]="@";
int len;
//receving from the edge server one by one.
while(1)
{
int len=recvfrom(sock,buffer,50,0,(struct sockaddr *)&remote,&clientSize);
if((len)==-1)
{
cout<<"recv error"<<endl;
return 0;
}
if(len>0)
{
//if get the end message, stop receiving and run to the process process
if(buffer[0]=='@')
{
cout<<"The Server AND has successfully received ";
cout<<count;
cout<<" lines from the edge server and finished all AND computations"<<endl;
count=0;
break;
}
char* res=dispose(buffer,len); //do and operation for the row
result_list.push_back(res); //push the result to list
count++;
}
}
//send the result together to edge server.
for(int i=0;i<result_list.size();i++)
{
char* res=result_list[i];
int length=0;
while(res[length]!='$')
{
length++;
}
if(len=sendto(sock,res,length+1,0,(struct sockaddr*)&remote,sizeof(struct sockaddr))==-1)
{
cout<<"sending error"<<endl;
return 0;
}
}
//send the end message
if(len=sendto(sock,success,10,0,(struct sockaddr*)&remote,sizeof(struct sockaddr))==-1)
{
cout<<"sending error"<<endl;
return 0;
}
cout<<"The Server AND has successfully finished sending all computation results to the edge server."<<endl;
//release cache
for(int i=0;i<result_list.size();i++)
{
delete result_list[i];
}
}
close(sock);
}
/**
function for doing the operation
**/
char* dispose(char* source,int length)
{
char * test=new char[50];
//get information from buffer
for(int i=0;i<length;i++)
{
test[i]=source[i];
}
int lena=0,lenb=0,len=0,start=0;
char num[2];
bool digiFlag=false;
//get the position of two operands a and b
for(int i=0;test[i]!='#';i++)
{
len=i;
if(test[i]==',')
{
if(start!=0)
{
lena=i-start-1;
}
start=i;
}
}
num[0]=test[len+2];
if(test[len+3]!='$'&&test[len+4]=='$')
{
digiFlag=true;
num[1]=test[len+3];
}
lenb=len-start;
len=max(lena,lenb);
//start to get two operands.
char a[len],b[len];
start=0;
int count=0;
for(int i=0;i<length;i++)
{
if(test[i]==',')
{
start++;
if(start==1)
{
for(int j=0;j<len-lena;j++)
{
a[j]='0';
}
count=len-lena;
}
if(start==2)
{
for(int j=0;j<len-lenb;j++)
{
b[j]='0';
}
count=len-lenb;
}
}
else
{
if(start==1)
{
a[count++]=test[i];
}
if(start==2)
{
b[count++]=test[i];
}
}
}
delete test;
//calculate and output the result.
char* res=new char[50];
bool flag=false;
count=0;
for(int i=len-lena;i<len;i++)
{
res[count++]=a[i];
}
res[count++]=' ';
res[count++]='a';
res[count++]='n';
res[count++]='d';
res[count++]=' ';
for(int i=len-lenb;i<len;i++)
{
res[count++]=b[i];
}
res[count++]=' ';
res[count++]='=';
res[count++]=' ';
for(int i=0;i<len;i++)
{
int tmp=(a[i]-'0')&(b[i]-'0');
if(flag||tmp==1)
{
flag=true;
res[count++]=(char)(tmp+'0');
}
}
if(!flag)
{
res[count++]='0';
}
res[count++]='#';
res[count++]=num[0];
if(digiFlag)
{
res[count++]=num[1];
}
res[count]='$';
for(int i=0;res[i]!='#';i++)
{
cout<<res[i];
}
cout<<endl;
return res;
}