Newer
Older
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
package fucoin.supervisor;
import java.awt.Label;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Semaphore;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import fucoin.AbstractWallet.ActionJoin;
import fucoin.AbstractWallet.ActionStoreOrUpdate;
import fucoin.actions.ActionGetAmount;
import fucoin.actions.ActionGetAmountAnswer;
import fucoin.actions.ActionInvokeUpdate;
public class SuperVisor extends UntypedActor {
private List<ActorRef> knownClients = new ArrayList<ActorRef>();
private Map<String, Map<String, Integer>> amounts = new HashMap<String, Map<String, Integer>>();
private AmountTableModel amountTableModel;
private Label averageamountLbl;
public SuperVisor(AmountTableModel amountTableModel, Label averageamountLbl) {
this.amountTableModel = amountTableModel;
this.averageamountLbl = averageamountLbl;
}
@Override
public void onReceive(Object msg) throws Exception {
if (msg instanceof ActionJoin) {
if (!knownClients.contains(getSender())) {
knownClients.add(getSender());
}
} else if (msg instanceof ActionInvokeUpdate) {
log("" + knownClients);
for (ActorRef neighbor : knownClients) {
neighbor.tell(new ActionGetAmount(), getSelf());
}
} else if (msg instanceof ActionGetAmountAnswer) {
ActionGetAmountAnswer agaa = (ActionGetAmountAnswer) msg;
try {
update(agaa.address, agaa.name, agaa.amount);
} catch (Exception ignoreException) {
}
} else if (msg instanceof ActionStoreOrUpdate) {
ActionStoreOrUpdate asou = (ActionStoreOrUpdate) msg;
try {
update(asou.w.getAddress(), asou.w.name, asou.w.amount);
} catch (Exception ignoreException) {
}
knownClients.remove(asou.w.getAddress());
}
}
private void log(String msg) {
System.out.println(getSelf() + ": " + msg);
}
Semaphore mutex = new Semaphore(1);
private void update(String address, String name, int amount)
throws InterruptedException {
//log(address + ", " + name + ", " + amount);
if (!amounts.containsKey(address)) {
amounts.put(address, new HashMap<String, Integer>());
}
amounts.get(address).put(name, amount);
amountTableModel.clear();
int user = 0;
double avgAmount = 0;
for (Entry<String, Map<String, Integer>> process : amounts.entrySet()) {
for (Entry<String, Integer> account : process.getValue().entrySet()) {
// amountTableModel.addRow(new Object[] { process.getKey(),
// account.getKey(), account.getValue() });
user++;
avgAmount += account.getValue();
}
}
if (user > 0) {
avgAmount /= user;
}
avgAmount = ((int) (avgAmount * 100) / 100.0);
this.averageamountLbl.setText("" + avgAmount);
}
public static Props props() {
return Props.create(SuperVisor.class, new SuperVisorCreator());
}
public void updateValues() {
getSelf().tell(new ActionInvokeUpdate(), getSelf());
}
public void exit() {
getContext().stop(getSelf());
}
@Override
public void postStop() throws Exception {
int user = 0;
double avgAmount = 0;
//System.out.println(amounts);
for (Entry<String, Map<String, Integer>> process : amounts.entrySet()) {
for (Entry<String, Integer> account : process.getValue().entrySet()) {
amountTableModel.addRow(new Object[] { process.getKey(),
account.getKey(), account.getValue() });
user++;
avgAmount += account.getValue();
}
}
if (user > 0) {
avgAmount /= user;
}
}
}