Skip to content
Snippets Groups Projects
Commit dd221945 authored by Michael Kmoch's avatar Michael Kmoch
Browse files

[fixed] some minor bugs, like multiadding of same address to gui

parent e173e819
No related branches found
No related tags found
1 merge request!1Kmoch lewash
...@@ -49,9 +49,8 @@ public class Wallet extends UntypedActor implements IWallet{ ...@@ -49,9 +49,8 @@ public class Wallet extends UntypedActor implements IWallet{
@Override @Override
public Vector<WalletPointer> join() { public Vector<WalletPointer> join() {
Vector<WalletPointer> allKnownNeighbors = new Vector<WalletPointer>(); Vector<WalletPointer> allKnownNeighbors = new Vector<WalletPointer>();
for(WalletPointer neighbors : allKnownNeighbors){ for(WalletPointer neighbors : this.allKnownNeighbors){
this.allKnownNeighbors.add(neighbors); allKnownNeighbors.add(neighbors);
gui.addKnownAddress(neighbors.address);
} }
return allKnownNeighbors; return allKnownNeighbors;
} }
...@@ -100,38 +99,23 @@ public class Wallet extends UntypedActor implements IWallet{ ...@@ -100,38 +99,23 @@ public class Wallet extends UntypedActor implements IWallet{
@Override @Override
public void onReceive(Object action) throws Exception { public void onReceive(Object action) throws Exception {
log(""+action); log(""+action);
boolean contains = false;
String senderName = actorToString(getSender()); String senderName = actorToString(getSender());
String selfName = actorToString(getSelf()); String selfName = actorToString(getSelf());
WalletPointer selfWP = null;
for(WalletPointer wp : this.allKnownNeighbors){
if(wp.address.equals(senderName)){
contains = true;
log("knows allready "+senderName);
}
if(wp.address.equals(selfName)){
selfWP = wp;
}
}
if(!contains){
this.allKnownNeighbors.add(new WalletPointer(senderName));
gui.addKnownAddress(senderName);
}
if(selfWP!=null){
this.allKnownNeighbors.remove(selfWP);
}
if(action instanceof JoinAction){ if(action instanceof JoinAction){
JoinActionRespond joinaction = new JoinActionRespond(); JoinAction joinAction = (JoinAction) action;
joinaction.setKnownNeighbors(join()); JoinActionRespond joinactionrespond = new JoinActionRespond();
joinactionrespond.setKnownNeighbors(join());
addKnownaddress(joinAction.getNewMember());
getSender().tell(joinaction, getSelf()); getSender().tell(joinactionrespond, getSelf());
}else if(action instanceof JoinActionRespond){ }else if(action instanceof JoinActionRespond){
JoinActionRespond joinaction = (JoinActionRespond) action; JoinActionRespond joinaction = (JoinActionRespond) action;
//allKnownNeighbors.addAll(joinaction.getAllKnownNeighbors()); //allKnownNeighbors.addAll(joinaction.getAllKnownNeighbors());
for(WalletPointer neighbor:joinaction.getAllKnownNeighbors()){ for(WalletPointer neighbor:joinaction.getAllKnownNeighbors()){
gui.addKnownAddress(neighbor.address); addKnownaddress(neighbor);
allKnownNeighbors.add(neighbor);
} }
log(actorToString(getSelf())+"ah there are new neighbours"+joinaction.getAllKnownNeighbors()); log(actorToString(getSelf())+"ah there are new neighbours"+joinaction.getAllKnownNeighbors());
log("now i know these "+allKnownNeighbors); log("now i know these "+allKnownNeighbors);
...@@ -162,15 +146,17 @@ public class Wallet extends UntypedActor implements IWallet{ ...@@ -162,15 +146,17 @@ public class Wallet extends UntypedActor implements IWallet{
getSender().tell(new FindWalletResponseAction(selfName,s.moneyAmount), getSelf()); getSender().tell(new FindWalletResponseAction(selfName,s.moneyAmount), getSelf());
} }
} }
}else if(action instanceof FindWalletResponseAction){ }else if(action instanceof FindWalletResponseAction){
FindWalletResponseAction findWalletResponseAction = (FindWalletResponseAction) action; FindWalletResponseAction findWalletResponseAction = (FindWalletResponseAction) action;
answers.add(new WalletPointer(findWalletResponseAction.getFoundneighbour())); answers.add(new WalletPointer(findWalletResponseAction.getFoundneighbour()));
gui.addKnownAddress(findWalletResponseAction.getFoundneighbour());
moneyAmount=findWalletResponseAction.getMoneyAmount(); moneyAmount=findWalletResponseAction.getMoneyAmount();
gui.setAmount(moneyAmount); gui.setAmount(moneyAmount);
}else if(action instanceof WaitForAnswersAction){ }else if(action instanceof WaitForAnswersAction){
log(""+answers); log(""+answers);
for(WalletPointer answer : answers){ for(WalletPointer answer : answers){
addKnownaddress(answer);
stringToActor(answer.address).tell(new InvalidateAction(actorToString(getSelf())),getSelf()); stringToActor(answer.address).tell(new InvalidateAction(actorToString(getSelf())),getSelf());
} }
}else if(action instanceof InvalidateAction){ }else if(action instanceof InvalidateAction){
...@@ -218,6 +204,16 @@ public class Wallet extends UntypedActor implements IWallet{ ...@@ -218,6 +204,16 @@ public class Wallet extends UntypedActor implements IWallet{
isAlive=true; isAlive=true;
} }
} }
private void addKnownaddress(WalletPointer senderPointer) {
if(!allKnownNeighbors.contains(senderPointer)
&&getSender().compareTo(getSelf())!=0){
this.allKnownNeighbors.add(senderPointer);
gui.addKnownAddress(senderPointer.address);
}
}
@Override @Override
public void preStart() throws Exception { public void preStart() throws Exception {
...@@ -228,9 +224,8 @@ public class Wallet extends UntypedActor implements IWallet{ ...@@ -228,9 +224,8 @@ public class Wallet extends UntypedActor implements IWallet{
addMoneyAmount(100); addMoneyAmount(100);
if(preknownNeighbour!=null){ if(preknownNeighbour!=null){
String p = actorToString(preknownNeighbour); String p = actorToString(preknownNeighbour);
allKnownNeighbors.add(new WalletPointer(p)); addKnownaddress(new WalletPointer(p));
gui.addKnownAddress(p); preknownNeighbour.tell(new JoinAction(new WalletPointer(address)), getSelf());
preknownNeighbour.tell(new JoinAction(), getSelf());
} }
} }
......
...@@ -14,6 +14,15 @@ public class WalletPointer { ...@@ -14,6 +14,15 @@ public class WalletPointer {
ActorRef actor = Wallet.stringToActor(address); ActorRef actor = Wallet.stringToActor(address);
return actor.path().toStringWithAddress(actor.path().address()); return actor.path().toStringWithAddress(actor.path().address());
} }
@Override
public boolean equals(Object obj) {
if(obj instanceof WalletPointer){
return ((WalletPointer)obj).address.equals(address);
}
return false;
}
......
package fucoin.actions; package fucoin.actions;
import fucoin.WalletPointer;
public class JoinAction { public class JoinAction {
private WalletPointer newMember;
public JoinAction(WalletPointer walletPointer) {
this.newMember=walletPointer;
}
public WalletPointer getNewMember() {
return newMember;
}
} }
...@@ -9,7 +9,7 @@ public class JoinActionRespond { ...@@ -9,7 +9,7 @@ public class JoinActionRespond {
@Override @Override
public String toString() { public String toString() {
return "join"; return "you joined successfully here are your new neighbours "+allKnownNeighbors;
} }
public void setKnownNeighbors(Vector<WalletPointer> allKnownNeighbors) { public void setKnownNeighbors(Vector<WalletPointer> allKnownNeighbors) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment