-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBasicProcessWithCallBack.java
More file actions
429 lines (364 loc) · 12.1 KB
/
Copy pathBasicProcessWithCallBack.java
File metadata and controls
429 lines (364 loc) · 12.1 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.shimmerresearch.driver;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import com.shimmerresearch.driverUtilities.UtilShimmer;
public abstract class BasicProcessWithCallBack {
private class EndThread{};
protected Callable mThread = null;
//protected BlockingQueue<ShimmerMSG> mQueue = new ArrayBlockingQueue<ShimmerMSG>(1024);
protected LinkedBlockingDeque<ShimmerMsg> mQueue = new LinkedBlockingDeque<ShimmerMsg>(1024);
protected ConsumerThread mGUIConsumerThread = null;
private WaitForData mWaitForData = null;
//private List<Callable> mListOfThreads = new ArrayList<Callable>();
private List<Callable> mListOfConsumers = Collections.synchronizedList(new ArrayList<Callable>());
private List<BasicProcessWithCallBack> mListOfMsgProducers = new ArrayList<BasicProcessWithCallBack>();
// private List<WaitForData> mListWaitForData = new ArrayList<WaitForData>();
private List<WaitForData> mListWaitForData = Collections.synchronizedList(new ArrayList<WaitForData>());
private String threadName = "";
private boolean mIsDebug = false;
/**This is a seperate thread running on the callback msgs from lower layer
* @param shimmerMSG
*/
protected abstract void processMsgFromCallback(ShimmerMsg shimmerMSG);
public BasicProcessWithCallBack(){
}
public BasicProcessWithCallBack(BasicProcessWithCallBack b){
mWaitForData = new WaitForData(b);
}
public void queueMethod(ShimmerMsg smsg){
try {
mQueue.put(smsg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void queueMethod(int i,Object ojc){
try {
mQueue.put(new ShimmerMsg(i,ojc));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getQueueSize(){
return mQueue.size();
}
public void startConsumerThreadIfNull(){
if (mGUIConsumerThread==null || mGUIConsumerThread.stop){
mGUIConsumerThread = new ConsumerThread();
if(!threadName.isEmpty()){
mGUIConsumerThread.setName(threadName);
}
mGUIConsumerThread.start();
}
}
/** This is used by the msg producer to remove the child consumer from its list of threads
* @param b is the msg consumer
*/
public void removeConsumer(BasicProcessWithCallBack b){
synchronized (mListOfConsumers) {
Iterator <Callable> entries = mListOfConsumers.iterator();
while (entries.hasNext()) {
Callable c = entries.next();
if (c instanceof WaitForData){
// UtilShimmer.consolePrintCurrentStackTrace();
for (WaitForData w: b.mListWaitForData){
if (c.equals(w)){
entries.remove();
}
}
if (c.equals(b.mWaitForData)){
entries.remove();
}
}
}
}
}
/** This stops the consumer thread, and removes all callbacks from producers
*
*/
public void stopConsumerThread(){
if (mGUIConsumerThread!=null){
mGUIConsumerThread.stop = true;
ShimmerMsg smsg = new ShimmerMsg(0,new EndThread());
try {
mQueue.put(smsg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ConsumerThread extends Thread {
public boolean stop = false;
public void run() {
while (!stop) {
try {
ShimmerMsg shimmerMSG = mQueue.take();
if (shimmerMSG.mB instanceof EndThread){
stop=true;
} else {
processMsgFromCallback(shimmerMSG);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.print("QUE BLOCKED");
e.printStackTrace();
}
}
//as a safety precaution, remove all its callbacks
removeSetWaitForDataAll();
};
}
/** This allows the class to receive msgs from the class passed in as an argument
* @param b the msg producer
*/
public void setWaitForData(BasicProcessWithCallBack b){
startConsumerThreadIfNull();
mListOfMsgProducers.add(b);
if (mWaitForData!=null){
synchronized (mListWaitForData){
mListWaitForData.add(new WaitForData(b));
}
} else {
mWaitForData = new WaitForData(b);
}
if(mIsDebug){
printListOfThreads();
}
};
/** TODO needs work to include all cases in "setWaitForData" */
/** This removes a producer
* @param b b is the producer
*/
public void removeSetWaitForData(BasicProcessWithCallBack b){
// consolePrintLn(this.getClass().getSimpleName() + " -> Trying to remove -> " + b.hashCode());
StringBuilder builder = null;
if(mIsDebug){
builder = new StringBuilder();
}
if(mWaitForData!=null){
BasicProcessWithCallBack bpwc = mWaitForData.returnBasicProcessWithCallBack();
if(bpwc==b || bpwc.equals(b)){
if(builder!=null) {
builder.append("\tRemoving thread\tSimpleName: " + b.threadName + "\tHashCode: " + mWaitForData.hashCode());
}
mWaitForData = null;
}
}
synchronized (mListWaitForData) {
Iterator<WaitForData> entries = mListWaitForData.iterator();
while (entries.hasNext()) {
WaitForData wFD = entries.next();
BasicProcessWithCallBack bpwc = wFD.returnBasicProcessWithCallBack();
if(bpwc==b || bpwc.equals(b)){
b.removeConsumer(this);
if(builder!=null) {
builder.append("\n\tRemoving thread\tSimpleName: " + b.threadName + "\tHashCode: " + wFD.hashCode());
}
entries.remove();
}
}
}
synchronized (mListOfMsgProducers) {
Iterator<BasicProcessWithCallBack> entries = mListOfMsgProducers.iterator();
while (entries.hasNext()) {
BasicProcessWithCallBack bpwc = entries.next();
if(bpwc==b || bpwc.equals(b)){
//TODO don't think removeConsumer needs to be called here as that method does not handle the mListOfMsgProducers
b.removeConsumer(this);
if(builder!=null) {
builder.append("\n\tRemoving thread\tSimpleName: " + b.threadName + "\tHashCode: " + bpwc.hashCode());
}
entries.remove();
}
}
}
if(builder!=null && builder.length()>0) {
consolePrintLn("");
consolePrintLn(this.getClass().getSimpleName() + " -> BasicProcessWithCallBack");
consolePrintLn(builder.toString());
}
if(mWaitForData==null && mListWaitForData.isEmpty() && mListOfMsgProducers.isEmpty()){
stopConsumerThread();
}
if(mIsDebug){
printListOfThreads();
}
}
/** Removes all producers registered with this class
*
*/
public void removeSetWaitForDataAll(){
for (BasicProcessWithCallBack bpwc:mListOfMsgProducers){
bpwc.removeConsumer(this);
}
mWaitForData = null;
mListWaitForData.clear();
}
public void setWaitForDataWithSingleInstanceCheck(BasicProcessWithCallBack b){
startConsumerThreadIfNull();
if (mWaitForData!=null){
boolean found = false;
if (mWaitForData.returnBasicProcessWithCallBack().equals(b)){
found = true;
}
if (!found){
synchronized (mListWaitForData) {
Iterator<WaitForData> entries = mListWaitForData.iterator();
while (entries.hasNext()) {
WaitForData wfd = entries.next();
if(wfd != null && wfd.returnBasicProcessWithCallBack() != null){
if (wfd.returnBasicProcessWithCallBack().equals(b)){
found = true;
}
}
}
}
}
if(!found){
synchronized (mListWaitForData){
mListOfMsgProducers.add(b);
mListWaitForData.add(new WaitForData(b));
}
}
} else {
mWaitForData = new WaitForData(b);
}
};
public void passCallback(Callable c) {
if (mThread!=null){
synchronized (mListOfConsumers) {
mListOfConsumers.add(c);
}
}
else {
mThread = c;
}
}
public void sendCallBackMsg(ShimmerMsg s){
if (mThread!=null){
mThread.callBackMethod(s);
}
// April 2017: RM changed for loop to iterator and added synchronisation block as concurrentmodification with for loop from time to time (this solution may not resolve concurrentmodification, monitor over time)
synchronized (mListOfConsumers) {
Iterator <Callable> entries = mListOfConsumers.iterator();
while (entries.hasNext()) {
Callable c = entries.next();
c.callBackMethod(s);
}
}
// for (Callable c: mListOfThreads){
// c.callBackMethod(s);
// }
}
public void sendCallBackMsg(int i, Object ojc){
if (mThread!=null){
mThread.callBackMethod( i, ojc);
}
// May 2017: RM changed for loop to iterator and added synchronisation block as concurrentmodification with for loop from time to time (this solution may not resolve concurrentmodification, monitor over time)
synchronized (mListOfConsumers) {
Iterator <Callable> entries = mListOfConsumers.iterator();
while (entries.hasNext()) {
Callable c = entries.next();
c.callBackMethod(i,ojc);
}
}
// for (Callable c:mListOfThreads){
// c.callBackMethod(i,ojc);
// }
}
//this is for the upper layer
public class WaitForData implements com.shimmerresearch.driver.Callable
{
BasicProcessWithCallBack track;
public BasicProcessWithCallBack returnBasicProcessWithCallBack(){
return track;
}
public WaitForData(BasicProcessWithCallBack bpwcb) {
track = bpwcb;
bpwcb.passCallback(this);
}
@Override
public void callBackMethod(ShimmerMsg s) {
try {
mQueue.put(s);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
@Override
public void callBackMethod(int i, Object ojc) {
try {
mQueue.put(new ShimmerMsg(i,ojc));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
public void setThreadName(String name){
threadName = name;
if(mGUIConsumerThread!=null){
mGUIConsumerThread.setName(name);
}
}
public void processMsgFromCallbackFromUpperLevel(ShimmerMsg shimmerMSG){
processMsgFromCallback(shimmerMSG);
}
public void printListOfThreads() {
consolePrintLn("");
consolePrintLn(this.getClass().getSimpleName() + " -> BasicProcessWithCallBack" + ": Printing List of Threads");
StringBuilder builder = new StringBuilder();
builder.append("\tmWaitForData:");
if (mWaitForData!=null){
builder.append("\n\t\tSimple Name: " + mWaitForData.getClass().getSimpleName() + "\t" + mWaitForData.returnBasicProcessWithCallBack().getClass().getSimpleName() + "\tHashCode: " + mWaitForData.hashCode());
} else {
builder.append("\n\t\tnull");
}
builder.append("\n\tmListOfConsumers:");
synchronized (mListOfConsumers) {
if(mListOfConsumers.size()>0) {
Iterator <Callable> entries = mListOfConsumers.iterator();
while (entries.hasNext()) {
Callable c = entries.next();
builder.append("\n\t\tSimple Name: " + c.getClass().getSimpleName() + "\tHashCode: " + c.hashCode());
}
} else {
builder.append("\n\t\tempty");
}
}
builder.append("\n\tmListWaitForData:");
synchronized (mListWaitForData) {
if(mListWaitForData.size()>0) {
Iterator<WaitForData> entries = mListWaitForData.iterator();
while (entries.hasNext()) {
WaitForData c = entries.next();
builder.append("\n\t\tSimple Name: " + c.getClass().getSimpleName() + "\t" + c.returnBasicProcessWithCallBack().getClass().getSimpleName() + "\tHashCode: " + c.hashCode());
}
} else {
builder.append("\n\t\tempty");
}
}
builder.append("\n\tmListOfMsgProducers:");
synchronized (mListOfMsgProducers) {
if(mListOfMsgProducers.size()>0) {
Iterator<BasicProcessWithCallBack> entries = mListOfMsgProducers.iterator();
while (entries.hasNext()) {
BasicProcessWithCallBack c = entries.next();
builder.append("\n\t\tSimple Name: " + c.getClass().getSimpleName() + "\t" + "\tHashCode: " + c.hashCode());
}
} else {
builder.append("\n\t\tempty");
}
}
consolePrintLn(builder.toString());
consolePrintLn("");
}
private void consolePrintLn(String toPrint){
System.out.println(toPrint);
}
}