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