@@ -25,7 +25,7 @@ class LatteAggrement{
25
25
vector<Message> Tobroadcast; // first index stands for iter;
26
26
vector<vector<Message>> Tosend; // first index stands for to-Pi;
27
27
set<long unsigned > proposed_value;
28
- set<long unsigned > accepted_value ;
28
+ vector< set<long unsigned >> accepted_value_vec ;
29
29
30
30
31
31
@@ -39,7 +39,6 @@ class LatteAggrement{
39
39
this ->myid =myid;
40
40
this ->socket_fd =socket_fd;
41
41
this ->host_dicts =host_dicts;
42
- this ->active =true ;
43
42
struct sockaddr_in myaddr=host_dicts[(myid-1 )];
44
43
45
44
this ->host_num =host_dicts.size ();
@@ -55,6 +54,7 @@ class LatteAggrement{
55
54
// Trigger the beb of the first proposed_value;
56
55
57
56
Tosend=vector<vector<Message>>(128 );
57
+ accepted_value_vec.push_back (set<long unsigned >());
58
58
59
59
60
60
// Add the forst Proposal
@@ -70,14 +70,9 @@ class LatteAggrement{
70
70
while (1 ){
71
71
// BroadCast
72
72
// cout<<"Broadcast"<<endl;
73
- unsigned int k=0 ;
74
73
mut_broadcast.lock ();
75
74
for (long unsigned i=0 ;i<Tobroadcast.size ();i++){
76
75
sendrange (Tobroadcast[i]);
77
- // if(Tobroadcast[i].proposal_num==1){
78
- // k=k+1;
79
- // cout<<k<<endl;
80
- // }
81
76
}
82
77
mut_broadcast.unlock ();
83
78
// Send
@@ -99,16 +94,13 @@ class LatteAggrement{
99
94
// cout<<m.m_type<< " "<<m.value_set[0]<<endl;
100
95
for (long unsigned i=0 ;i<this ->host_num ;i++){
101
96
this ->pl_link .send (m,host_dicts[i]);
102
- if (m.proposal_num ==1 &&i==2 ){
103
- // cout<<"To 3"<<endl;
104
- }
105
97
}
106
98
107
99
}
108
100
109
101
void upon_change_ack_nack (Message & m){
110
102
if (nack_count>0 && (ack_count+nack_count>=host_num/2 +host_num%2 ) &&
111
- active){
103
+ m. iter ==current_iter){ // Modify the active bool
112
104
active_proposal_num++;
113
105
ack_count=0 ;
114
106
nack_count=0 ;
@@ -119,12 +111,28 @@ class LatteAggrement{
119
111
current_iter,myid,active_proposal_num,proposed_value));// WARN: Modify Here
120
112
mut_broadcast.unlock ();
121
113
}
122
- else if (ack_count>=(host_num/2 +host_num%2 ) && active ) {
114
+ else if (ack_count>=(host_num/2 +host_num%2 ) && m. iter ==current_iter ) {
123
115
// decide
124
116
Todecide.push_back (proposed_value);
125
117
cout<<" Decide" <<endl;
126
- active=false ;
127
- // this->current_iter++; // Enter the next proposal stage(iter);
118
+ // Clean the previous things;
119
+ // Enter the next proposal stage(iter);
120
+ this ->ack_count =0 ;
121
+ this ->nack_count =0 ;
122
+ this ->active_proposal_num =0 ;
123
+ this ->accepted_value_vec .push_back (set<long unsigned >());
124
+ this ->proposed_value .clear ();
125
+
126
+ // Start next round
127
+ mut_broadcast.lock ();
128
+ this ->current_iter ++;
129
+ for (auto it=vec_proposals[current_iter-1 ].begin ();
130
+ it!=vec_proposals[current_iter-1 ].end ();it++){
131
+ proposed_value.insert (*it);
132
+ }
133
+ this ->Tobroadcast .push_back (Message (0 ,
134
+ this ->current_iter ,myid,active_proposal_num,proposed_value));
135
+ mut_broadcast.unlock ();
128
136
}
129
137
130
138
}
@@ -164,6 +172,7 @@ class LatteAggrement{
164
172
void deliver (){
165
173
Message m;
166
174
while (1 ){
175
+
167
176
// Pay Attention: I would force perfect link only
168
177
// deliver message belonging to the same 'iter'
169
178
// and same 'propose_num'
@@ -172,15 +181,15 @@ class LatteAggrement{
172
181
if (m.original_id !=0 ){
173
182
// ACK
174
183
175
- if ((m.m_type )==1 &&m.proposal_num ==active_proposal_num){
184
+ if ((m.m_type )==1 &&m.proposal_num ==active_proposal_num &&m. iter ==current_iter ){
176
185
ack_count++;
177
186
cout<<" Receive ACK Oid:" <<m.original_id <<" Iter:" <<m.iter <<" Type:" <<m.m_type <<" Value:" ;
178
187
cout<<" activeround:" <<m.proposal_num <<" myround:" <<active_proposal_num <<endl;
179
188
upon_change_ack_nack (m);
180
189
// cout<<"hello man"<<endl;
181
190
}
182
191
// NACK
183
- if (m.m_type ==2 &&m.proposal_num ==active_proposal_num){
192
+ if (m.m_type ==2 &&m.proposal_num ==active_proposal_num&&m. iter ==current_iter ){
184
193
nack_count++;
185
194
for (long unsigned k=0 ;k<m.value_set_num ;k++){
186
195
proposed_value.insert (m.value_set [k]);
@@ -189,37 +198,41 @@ class LatteAggrement{
189
198
for (long unsigned i=0 ;i<m.value_set_num ;i++){
190
199
cout<<m.value_set [i]<<" " ;
191
200
}
192
- cout<<" activeround:" <<m.proposal_num <<" myround:" <<active_proposal_num <<endl;
201
+ cout<<" activeround:" <<m.proposal_num <<" myround:" <<active_proposal_num <<endl;
193
202
upon_change_ack_nack (m);
194
203
195
204
}
196
205
// PROPOSAL
197
- bool tag=mergeset (accepted_value,m);
198
- if (m.m_type ==0 && tag){
199
- // ACK
200
- mut_send.lock ();
201
- Tosend[m.original_id -1 ].push_back (Message (1 ,
202
- current_iter,myid,m.proposal_num ));
203
- mut_send.unlock ();
204
-
205
- cout<<" Send Ack Oid:" <<myid<<" To " <<m.original_id <<" Type:" <<m.m_type ;
206
- cout<<" activeround:" <<m.proposal_num <<" myround:" <<active_proposal_num <<endl;
207
206
208
- }
209
- else if (m.m_type ==0 && !tag){
210
- // NACK
211
- mut_send.lock ();
212
- Tosend[m.original_id -1 ].push_back (Message (2 ,
213
- current_iter,myid,m.proposal_num ,accepted_value));
214
- mut_send.unlock ();
215
-
216
- cout<<" Send NACK Oid:" <<myid<<" To " <<m.original_id <<" Type:" <<m.m_type <<" Value:" ;
217
- for (auto it= accepted_value.begin ();it!=accepted_value.end ();it++){
218
- cout<<*it<<" " ;
219
- }
207
+ // Modify
208
+ if (m.m_type ==0 && m.iter <=current_iter){
209
+ bool tag=mergeset (accepted_value_vec[m.iter -1 ],m);
210
+ if (m.m_type ==0 && tag){
211
+ // ACK
212
+ mut_send.lock ();
213
+ Tosend[m.original_id -1 ].push_back (Message (1 ,
214
+ m.iter ,myid,m.proposal_num )); // Modify the current_iter to m.tier
215
+ mut_send.unlock ();
216
+
217
+ cout<<" Send Ack Oid:" <<myid<<" Iter " <<m.iter <<" To " <<m.original_id <<" Type:" <<m.m_type ;
220
218
cout<<" activeround:" <<m.proposal_num <<" myround:" <<active_proposal_num <<endl;
221
- }
222
219
220
+ }
221
+ else if (m.m_type ==0 && !tag){
222
+ // NACK
223
+ mut_send.lock ();
224
+ Tosend[m.original_id -1 ].push_back (Message (2 ,
225
+ m.iter ,myid,m.proposal_num ,accepted_value_vec[m.iter -1 ]));
226
+ mut_send.unlock ();
227
+
228
+ cout<<" Send NACK Oid:" <<myid<<" To " <<m.original_id <<" Type:" <<m.m_type <<" Value:" ;
229
+ for (auto it= accepted_value_vec[m.iter -1 ].begin ();
230
+ it!=accepted_value_vec[m.iter -1 ].end ();it++){
231
+ cout<<*it<<" " ;
232
+ }
233
+ cout<<" activeround:" <<m.proposal_num <<" myround:" <<active_proposal_num <<endl;
234
+ }
235
+ }
223
236
224
237
}
225
238
0 commit comments