/
reduceFunction.cpp
184 lines (165 loc) · 4.63 KB
/
reduceFunction.cpp
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
#include "reduceFunction.h"
#include <iostream>
#include <stdio.h>
//using namespace std;
reduceFunction::reduceFunction(){
strcpy(dataSystemIp,"127.0.0.1");
dataSystemPort=8090;
}
////////////////////////////////////////////////////////
void reduceFunction::getMapInfo(){
for(std::map<int,mapNode>::iterator it=mapAddress.begin(); it!=mapAddress.end(); ++it){
int fd;
//int cound=0;
while(true){
fd=open_clientfd(it->second.mapIp, it->second.mapPort);
if(fd<0){
/*if(cound==3){
NodeInfo newNode("8080", "10.0.2.15");
changeInfo(1, newNode);
}*/
printf("Node is missing.\n");
sleep(1);
//cound++;
continue;
}
else{
//break;
std::cout<<"geting map info"<<std::endl;
char buf_s[80];
sprintf(buf_s,"GETMAP %d\n",it->first);
Rio_writep(fd, buf_s, strlen(buf_s));
char buf_r[80];
char * saveptr=NULL;
int numBytes;
rio_t t;
Rio_readinitb( &t,fd);
bool finished=false;
std::map<std::string, int> tempMap;
while(numBytes = Rio_readlineb(&t, buf_r, MAXLINE)>0){
if(numBytes<=0){
std::cout<<"close fd"<<std::endl;
break;
}
printf("numBytes is %d\n", numBytes);
char* r1 = strtok_r(buf_r, "*",&saveptr);
char r_name[80];
strcpy(r_name,r1);
std::string s_name(r_name);
if(s_name=="FINISH"){
finished=true;
break;
}
char* r2 = strtok_r(NULL, "*",&saveptr);
int r_num=atoi(r2);
tempMap[s_name]+=r_num;
//std::cout<<r_num<<std::endl;
}
if(finished){
for(std::map<std::string,int>::iterator tempit=tempMap.begin(); tempit!=tempMap.end(); ++tempit){
myMap[tempit->first]=tempit->second;
}
break;
}
continue;
}
}
}
}
/////////////////////////////////////////////////////////////
void reduceFunction::sendInfo(){
printf("print result:\n");
int fd=Open_clientfd(dataSystemIp, dataSystemPort);
printf("%d\n",fd);
if(fd<0){
printf("data system is missing.\n");
return;
}
char buf[80];
printf("print result:\n");
sprintf(buf,"ADD\n");
Rio_writep(fd, buf, strlen(buf));
for (std::map<std::string,int>::iterator it=myMap.begin(); it!=myMap.end(); ++it){
//printf("%s: %d\n",it->first.data(),it->second);
sprintf(buf,"%s#%d\n",it->first.data(),it->second);
Rio_writep(fd, buf, strlen(buf));
}
printf("aaa: %s", buf);
close(fd);
/*printf("print node addresses:\n");
for (std::map<int, mapNode>::iterator nit=mapAddress.begin(); nit!=mapAddress.end(); ++nit){
printf("%d: %s %d\n",nit->first, nit->second.mapIp, nit->second.mapPort);
//sprintf(buf,"%s*%d\n",it->first.data(),it->second);
//Rio_writep(fd, buf, strlen(buf));
}*/
}
////////////////////////for test///////////////////////
void reduceFunction::sendInfo(char* tip, int tport){
/*int fd=Open_clientfd(tip, tport);
if(fd<0){
printf("data system is missing.\n");
return;
}
char buf[80];*/
//std::cout<<"sendInfo"<<std::endl;
for (std::map<std::string,int>::iterator it=myMap.begin(); it!=myMap.end(); ++it){
std::cout<<it->first.data()<<" "<<it->second<<std::endl;
}
}
/////////////////////////////////////////////////////
void reduceFunction::getInfo(int fd, rio_t client, int id){
char buf1[MAXLINE];
int numBytes;
char * saveptr=NULL;
while(true){
numBytes = Rio_readlineb(&client, buf1, MAXLINE);
if(numBytes==0){
break;
}
printf("buf1 is %s\n", buf1);
char* d1 = strtok_r(buf1, "*",&saveptr);
char ip[20];
strcpy(ip,d1);
char* d2 = strtok_r(NULL, "*",&saveptr);
int port=atoi(d2);
char* d3 = strtok_r(NULL, "* \r\n",&saveptr);
int num=atoi(d3);
mapNode mapWorker;
strcpy(mapWorker.mapIp,ip);
mapWorker.mapPort=port;
mapAddress[num]=mapWorker;
}
getMapInfo();
sendInfo();
}
///////////////////////////////////////
void reduceFunction::changeInfo(int jobID, NodeInfo newNode){
mapNode a;
strcpy(a.mapIp,newNode.IP);
a.mapPort=atoi(newNode.port);
mapAddress[jobID]=a;
//std::cout<<a.mapIp<<" "<<a.mapPort<<std::endl;
}
////////////////////////////////////////
void reduceFunction::clearReduce(){
mapAddress.clear();
myMap.clear();
}
///////////////for test//////////////////
void reduceFunction::setIPNode(int tid, char* tip, int tport){
mapNode a;
strcpy(a.mapIp,tip);
a.mapPort=tport;
mapAddress[tid]=a;
}
///////////////////////////////////////////
void reduceFunction::printReduce(){
printf("print result:\n");
for (std::map<std::string,int>::iterator it=myMap.begin(); it!=myMap.end(); ++it){
printf("%s: %d\n",it->first.data(),it->second);
}
printf("print node addresses:\n");
for (std::map<int, mapNode>::iterator nit=mapAddress.begin(); nit!=mapAddress.end(); ++nit){
printf("%d: %s %d\n",nit->first, nit->second.mapIp, nit->second.mapPort);
}
}