Newer
Older
Simon Könnecke
committed
package fucoin.wallet;
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.persist.ActionInvokeLeave;
import fucoin.actions.persist.ActionInvokeRevive;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionInvokeSentMoney;
Simon Könnecke
committed
import fucoin.gui.WalletController;
import fucoin.gui.WalletGuiController;
Simon Könnecke
committed
public class WalletImpl extends AbstractWallet implements WalletController {
private ActorRef preKnownNeighbour;
Simon Könnecke
committed
private WalletGuiController gui;
private String preKnownNeighbourName;
public WalletImpl(String name) {
super(name);
}
public WalletImpl(ActorRef preKnownNeighbour, String preKnownNeighbourName,
String walletName, ActorRef remoteSuperVisorActor) {
this.preKnownNeighbourName = preKnownNeighbourName;
this.preKnownNeighbour = preKnownNeighbour;
this.remoteSuperVisorActor = remoteSuperVisorActor;
}
public static Props props(ActorRef preKnownNeighbour, String preKnownNeighbourName,
String walletName, ActorRef remoteSuperVisorActor) {
return Props.create(new WalletCreator(preKnownNeighbour, preKnownNeighbourName, walletName, remoteSuperVisorActor));
}
setAmount(this.getAmount() + amount);
log(" My amount is now " + this.getAmount());
}
@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();
}
@Override
public boolean equals(Object obj) {
Simon Könnecke
committed
if (obj instanceof WalletImpl) {
WalletImpl wobj = (WalletImpl) obj;
return amount == wobj.getAmount() && name.equals(wobj.getName());
public int getAmount() {
return amount;
}
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 ActorRef getPreKnownNeighbour() {
return preKnownNeighbour;
public void setPreKnownNeighbour(ActorRef preKnownNeighbour) {
this.preKnownNeighbour = preKnownNeighbour;
}
public ActorRef getRemoteSuperVisorActor() {
return remoteSuperVisorActor;
}
public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) {
this.remoteSuperVisorActor = remoteSuperVisorActor;
}
Simon Könnecke
committed
public WalletGuiController getGui() {
public void setGui(WalletGuiController gui) {
this.gui = gui;
}
public String getPreKnownNeighbourName() {
return preKnownNeighbourName;
}
public void setPreKnownNeighbourName(String preKnownNeighbourName) {
this.preKnownNeighbourName = preKnownNeighbourName;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@Override
public boolean addKnownNeighbor(String key, ActorRef value) {
Simon Könnecke
committed
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;
}
@Override
public void log(String msg) {
if (gui != null) {
gui.addLogMsg(msg);
} else {
System.out.println(msg);
}
}
Simon Könnecke
committed
public void logTransaction(String msg) {
if (gui != null) {
gui.addTransactionLogMessage(msg);
} else {
System.out.println(msg);
}
}
@Override
public void send(String address, int amount) {
getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
}