30
30
import java .util .Collection ;
31
31
import java .util .concurrent .CompletableFuture ;
32
32
33
- public class Client {
33
+ public class Client {
34
34
35
35
private static CopycatClient client = null ;
36
36
@@ -101,18 +101,20 @@ public static void putMessage1(Message message) {
101
101
//System.out.println("Block: consensus success");
102
102
103
103
int i = 1 ;
104
- boolean f = true ;
105
- while ( f ) {
104
+ final boolean [] f = { true } ;
105
+ while ( f [ 0 ]) {
106
106
String block_key = "block" + i ;
107
107
Object block = client .submit (new GetQuery (block_key )).join ();
108
108
try {
109
109
if (!(block == null )) {
110
- f =true ;
111
- i = i +1 ;
112
- }else {
113
- client .submit (new PutCommand (block_key , message .getMessage ()));
110
+ System .out .println ("1234" );
111
+ f [0 ] = true ;
112
+ i = i + 1 ;
113
+ } else {
114
+ client .submit (new PutCommand (block_key , message
115
+ .getMessage ()));
114
116
System .out .println ("Block: consensus success" );
115
- f = false ;
117
+ f [ 0 ] = false ;
116
118
}
117
119
} catch (NullPointerException e ) {
118
120
e .printStackTrace ();
@@ -122,27 +124,30 @@ public static void putMessage1(Message message) {
122
124
}
123
125
}
124
126
125
- public static void getMessage (Peer peer ,String key ) {
127
+ public static void getMessage (Peer peer , String key ) {
126
128
final String [] preMessage = {null };
127
129
final String [] preTime = {null };
128
130
if (key .equals ("transaction" )) {
129
131
Thread thread = new Thread (() -> {
130
- while (true ){
132
+ while (true ) {
131
133
Object time = client .submit (new GetQuery ("time" )).join ();
132
- if (!time .toString ().equals (preTime [0 ])) {
134
+ if (!time .toString ().equals (preTime [0 ])) {
133
135
client .submit (new GetQuery (key )).thenAccept (transaction
134
136
-> {
135
- //System.out.println("Consensus " + key + " is: " + result);
136
- //System.out.println("type: " + result.getClass().getSimpleName());
137
- peer .addReceiveTransaction (String .valueOf (transaction ));
137
+ //System.out.println("Consensus " + key + " is: "
138
+ // + result);
139
+ //System.out.println("type: " + result.getClass()
140
+ // .getSimpleName());
141
+ peer .addReceiveTransaction (String .valueOf
142
+ (transaction ));
138
143
});
139
144
preTime [0 ] = time .toString ();
140
- }else {
145
+ } else {
141
146
preTime [0 ] = preTime [0 ];
142
147
}
143
148
try {
144
149
Thread .sleep (3000 );
145
- }catch (Exception e ){
150
+ } catch (Exception e ) {
146
151
e .printStackTrace ();
147
152
}
148
153
}
@@ -152,34 +157,36 @@ public static void getMessage(Peer peer,String key) {
152
157
153
158
if (key .equals ("block" )) {
154
159
Thread thread = new Thread (() -> {
155
- while (true ){
160
+ while (true ) {
156
161
int i = 1 ;
157
- boolean f = true ;
162
+ final boolean [] f = { true } ;
158
163
String block_key ;
159
- while ( f ) {
164
+ while ( f [ 0 ]) {
160
165
block_key = "block" + i ;
161
- Object block = client .submit (new GetQuery (block_key )).join ();
166
+ Object block = client .submit (new GetQuery (block_key ))
167
+ .join ();
162
168
try {
163
169
if (!(block == null )) {
164
- f = true ;
165
- i = i + 1 ;
166
- }else {
167
- f = false ;
170
+ f [ 0 ] = true ;
171
+ i = i + 1 ;
172
+ } else {
173
+ f [ 0 ] = false ;
168
174
}
169
175
} catch (NullPointerException e ) {
170
176
e .printStackTrace ();
171
177
}
172
178
}
173
179
174
- i = i - 1 ;
180
+ i = i - 1 ;
175
181
String finalBlock_key = "block" + i ;
176
- client .submit (new GetQuery (finalBlock_key )).thenAccept (block -> {
182
+ client .submit (new GetQuery (finalBlock_key )).thenAccept
183
+ (block -> {
177
184
/*System.out.println("Consensus " + key + " is: " +
178
185
block);*/
179
186
if (!String .valueOf (block ).equals (preMessage [0 ])) {
180
187
peer .addReceiveBlock (String .valueOf (block ));
181
188
preMessage [0 ] = String .valueOf (block );
182
- }else {
189
+ } else {
183
190
preMessage [0 ] = preMessage [0 ];
184
191
}
185
192
});
@@ -193,17 +200,19 @@ public static void getMessage(Peer peer,String key) {
193
200
thread .start ();
194
201
}
195
202
}
203
+
196
204
public static void loadBlock (Peer peer ) {
197
205
int i = 2 ;
198
206
final boolean [] f = {true };
199
207
while (f [0 ]) {
200
208
String block_key = "block" + i ;
201
- client .submit (new GetQuery (block_key )).thenAccept ((Object block ) -> {
209
+ client .submit (new GetQuery (block_key )).thenAccept ((Object block )
210
+ -> {
202
211
if (!(block == null )) {
203
212
peer .addReceiveBlock (String .valueOf
204
213
(block ));
205
214
f [0 ] = true ;
206
- } else {
215
+ } else {
207
216
f [0 ] = false ;
208
217
}
209
218
});
0 commit comments