Newer
Older
package fucoin;
import akka.actor.ActorRef;
import akka.actor.Props;
import fucoin.actions.ClientAction;
import fucoin.actions.join.ActionJoin;
import fucoin.actions.join.ActionJoinAnswer;
import fucoin.actions.join.ServerActionJoin;
import fucoin.actions.persist.ActionInvokeLeave;
import fucoin.actions.persist.ActionInvokeRevive;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.gui.IWalletControle;
import fucoin.gui.IWalletGuiControle;
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
public class Wallet extends AbstractWallet implements IWalletControle {
private ActorRef preknownNeighbour;
private ActorRef remoteSuperVisorActor;
private IWalletGuiControle gui;
private String preknownNeighbourName;
private boolean isActive;
public Wallet(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
super(walletName);
this.preknownNeighbourName = preknownNeighbourName;
this.preknownNeighbour = preknownNeighbour;
this.remoteSuperVisorActor = remoteSuperVisorActor;
}
public void addAmount(int amount) {
setAmount(this.amount + amount);
log(" My amount is now " + this.amount);
}
@Override
public void leave() {
getSelf().tell(new ActionInvokeLeave(), getSelf());
}
@Override
public void onReceive(Object message) {
//log(getSender().path().name() + " invokes " + getSelf().path().name() + " to do " + message.getClass().getSimpleName());
if (message instanceof ActionInvokeRevive) {
((ActionInvokeRevive) message).doAction(this);
}
if (!isActive && !(message instanceof ActionInvokeRevive)) return;
//System.out.println(message);
if (message instanceof ClientAction) {
((ClientAction) message).doAction(this);
}
}
@Override
public void preStart() throws Exception {
isActive = true;
if (gui != null) {
gui.setAddress(getAddress());
}
String path = "akka.tcp://Core@127.0.0.1:1234/user/Main";
//System.out.println(getContext().provider().getExternalAddressFor(getSelf().path().address()));
//log("my address should be "+getAddress());
//log(""+preknownNeighbour);
//knownNeighbors.put(getName(),getSelf());
//System.out.println(knownNeighbors);
if (preknownNeighbour != null) {
addKnownNeighbor(preknownNeighbourName, preknownNeighbour);
preknownNeighbour.tell(new ActionJoin(), getSelf());
ActionJoinAnswer aja = new ActionJoinAnswer();
aja.someNeighbors.putAll(getKnownNeighbors());
aja.someNeighbors.put(name, getSelf());
preknownNeighbour.tell(aja, getSelf());
}
//setAmount(100);
//remoteSuperVisorActor.tell(new ServerActionJoin(name), getSelf());
}
@Override
public void postStop() throws Exception {
leave();
super.postStop();
}
public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
return Props.create(Wallet.class, new WalletCreator(preknownNeighbour, preknownNeighbourName, walletName, remoteSuperVisorActor));
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Wallet) {
Wallet wobj = (Wallet) obj;
return amount == wobj.amount && name.equals(wobj.name);
}
return false;
}
public void setGui(IWalletGuiControle gui) {
this.gui = gui;
}
public void setAmount(int amount) {
this.amount = amount;
if (remoteSuperVisorActor != null) {
remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf());
}
if (gui != null) {
gui.setAmount(this.amount);
}
}
public int getAmount() {
return amount;
}
public ActorRef getPreknownNeighbour() {
return preknownNeighbour;
}
public ActorRef getRemoteSuperVisorActor() {
return remoteSuperVisorActor;
}
public IWalletGuiControle getGui() {
return gui;
}
public String getPreknownNeighbourName() {
return preknownNeighbourName;
}
public boolean isActive() {
return isActive;
}
@Override
public boolean addKnownNeighbor(String key, ActorRef value) {
System.out.println(key + " is newNeighbor of "+name+"?" + !getKnownNeighbors().containsKey(key));
if (getKnownNeighbors().containsKey(key) || key.equals(name)) {
return false;
}
boolean newNeighbor = super.addKnownNeighbor(key, value);
if (gui != null && newNeighbor) {
gui.addKnownAddress(key);
}
return newNeighbor;
}
public void setPreknownNeighbour(ActorRef preknownNeighbour) {
this.preknownNeighbour = preknownNeighbour;
}
public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) {
this.remoteSuperVisorActor = remoteSuperVisorActor;
}
public void setPreknownNeighbourName(String preknownNeighbourName) {
this.preknownNeighbourName = preknownNeighbourName;
}
@Override
public void log(String msg) {
if (gui != null) {
gui.addLogMsg(msg);
} else {
System.out.println(msg);
}
}
public void logTransaction(String msg){
if (gui != null) {
gui.addTransactionLogMessage(msg);
} else {
System.out.println(msg);
}
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@Override
public void send(String address, int amount) {
getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
}