From 9c09dff0bdeb34b10ec4878a2446c31b9753f2d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=B6nnecke?= <simonkoennecke@gmail.com> Date: Tue, 14 Jun 2016 00:58:09 +0200 Subject: [PATCH] rename Factory to Creator, typos, code formatting --- src/fucoin/Main.java | 37 ++++---- src/fucoin/actions/Action.java | 26 +++--- src/fucoin/actions/ClientAction.java | 8 +- src/fucoin/actions/join/ServerActionJoin.java | 4 +- .../actions/persist/ActionInvokeRevive.java | 2 +- .../persist/ActionSearchMyWalletAnswer.java | 2 +- .../actions/persist/ActionStoreOrUpdate.java | 2 +- .../search/ActionSearchWalletReference.java | 8 +- src/fucoin/actions/search/Search.java | 4 +- ...onCommitDistributedCommittedTransfer.java} | 14 +-- ...tionInvokeDistributedCommitedTransfer.java | 34 ------- ...ionInvokeDistributedCommittedTransfer.java | 38 ++++++++ .../transaction/ActionInvokeSentMoney.java | 2 +- ...areDistributedCommittedTransferAnswer.java | 10 +-- .../transaction/ActionReceiveTransaction.java | 2 +- .../actions/transaction/SuperVisorAction.java | 6 +- src/fucoin/supervisor/ActionUpdateQueue.java | 60 ++++++------- src/fucoin/supervisor/AmountTableModel.java | 2 +- ... DistributedCommittedTransferRequest.java} | 8 +- ...sorFactory.java => SuperVisorCreator.java} | 2 +- src/fucoin/supervisor/SuperVisorImpl.java | 24 ++--- src/fucoin/wallet/AbstractWallet.java | 6 +- src/fucoin/wallet/WalletCreator.java | 32 +++++++ src/fucoin/wallet/WalletFactory.java | 31 ------- src/fucoin/wallet/WalletImpl.java | 89 ++++++++++--------- 25 files changed, 234 insertions(+), 219 deletions(-) rename src/fucoin/actions/transaction/{ActionCommitDistributedCommitedTransfer.java => ActionCommitDistributedCommittedTransfer.java} (78%) delete mode 100644 src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java create mode 100644 src/fucoin/actions/transaction/ActionInvokeDistributedCommittedTransfer.java rename src/fucoin/supervisor/{DistributedCommitedTransferRequest.java => DistributedCommittedTransferRequest.java} (81%) rename src/fucoin/supervisor/{SuperVisorFactory.java => SuperVisorCreator.java} (95%) create mode 100644 src/fucoin/wallet/WalletCreator.java delete mode 100644 src/fucoin/wallet/WalletFactory.java diff --git a/src/fucoin/Main.java b/src/fucoin/Main.java index cf53035..1441a4f 100644 --- a/src/fucoin/Main.java +++ b/src/fucoin/Main.java @@ -1,36 +1,35 @@ package fucoin; -import java.io.File; -import java.util.ArrayList; -import java.util.List; import akka.actor.ActorRef; import akka.actor.ActorSystem; - import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; - import fucoin.actions.join.ServerActionJoin; import fucoin.supervisor.SuperVisorImpl; import fucoin.wallet.WalletImpl; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + public class Main { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws InterruptedException { - File file = new File("application.conf"); - System.out.println("config found? " + file.exists()); - Config config = ConfigFactory.parseFile(file); - ActorSystem system = ActorSystem.create("Core", config); - ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(),"SuperVisorImpl"); - List<ActorRef> activeActors = new ArrayList<>(); - ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Main",superVisorActor),"Main"); - ActorRef a2 = system.actorOf(WalletImpl.props(a1,"Main","Main2",superVisorActor),"Main2"); - superVisorActor.tell(new ServerActionJoin("Main"), a1); - superVisorActor.tell(new ServerActionJoin("Main2"), a2); - } + File file = new File("application.conf"); + System.out.println("config found? " + file.exists()); + Config config = ConfigFactory.parseFile(file); + ActorSystem system = ActorSystem.create("Core", config); + ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(), "SuperVisorImpl"); + List<ActorRef> activeActors = new ArrayList<>(); + ActorRef a1 = system.actorOf(WalletImpl.props(null, "", "Main", superVisorActor), "Main"); + ActorRef a2 = system.actorOf(WalletImpl.props(a1, "Main", "Main2", superVisorActor), "Main2"); + superVisorActor.tell(new ServerActionJoin("Main"), a1); + superVisorActor.tell(new ServerActionJoin("Main2"), a2); + } - private static void startSupervisor() { + private static void startSupervisor() { - } + } } diff --git a/src/fucoin/actions/Action.java b/src/fucoin/actions/Action.java index 2c99aed..bacffdf 100644 --- a/src/fucoin/actions/Action.java +++ b/src/fucoin/actions/Action.java @@ -1,21 +1,23 @@ package fucoin.actions; -import fucoin.wallet.AbstractNode; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; +import fucoin.wallet.AbstractNode; public abstract class Action<T extends AbstractNode> { - private ActorRef self; + private ActorRef self; + + public final void doAction(T abstractNode) { + this.self = abstractNode.getSelf(); + onAction(abstractNode.getSender(), abstractNode.getSelf(), + abstractNode.getContext(), abstractNode); + } + + protected abstract void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, T abstractNode); - public final void doAction(T abstractNode){ - this.self=abstractNode.getSelf(); - onAction(abstractNode.getSender(),abstractNode.getSelf(),abstractNode.getContext(),abstractNode); - } + public void log(String string) { + System.out.println(self.path().name() + ": " + string); + } - protected abstract void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, T abstractNode); - - public void log(String string) { - System.out.println(self.path().name()+": "+string); - } - } diff --git a/src/fucoin/actions/ClientAction.java b/src/fucoin/actions/ClientAction.java index 5a38b97..3be489c 100644 --- a/src/fucoin/actions/ClientAction.java +++ b/src/fucoin/actions/ClientAction.java @@ -4,9 +4,9 @@ import akka.actor.ActorRef; import akka.actor.UntypedActorContext; import fucoin.wallet.AbstractWallet; -public abstract class ClientAction extends Action<AbstractWallet>{ - @Override - protected abstract void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, AbstractWallet abstractNode); +public abstract class ClientAction extends Action<AbstractWallet> { + @Override + protected abstract void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet abstractNode); } diff --git a/src/fucoin/actions/join/ServerActionJoin.java b/src/fucoin/actions/join/ServerActionJoin.java index f44019d..d7b78ff 100644 --- a/src/fucoin/actions/join/ServerActionJoin.java +++ b/src/fucoin/actions/join/ServerActionJoin.java @@ -2,7 +2,7 @@ package fucoin.actions.join; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.actions.transaction.ActionInvokeDistributedCommitedTransfer; +import fucoin.actions.transaction.ActionInvokeDistributedCommittedTransfer; import fucoin.actions.transaction.SuperVisorAction; import fucoin.supervisor.SuperVisorImpl; @@ -21,7 +21,7 @@ public class ServerActionJoin extends SuperVisorAction { sender.tell(aja, self); node.addKnownNeighbor(name, sender); self.tell( - new ActionInvokeDistributedCommitedTransfer(self, sender, 100), + new ActionInvokeDistributedCommittedTransfer(self, sender, 100), sender); } } diff --git a/src/fucoin/actions/persist/ActionInvokeRevive.java b/src/fucoin/actions/persist/ActionInvokeRevive.java index 622b917..6bfdb84 100644 --- a/src/fucoin/actions/persist/ActionInvokeRevive.java +++ b/src/fucoin/actions/persist/ActionInvokeRevive.java @@ -11,7 +11,7 @@ public class ActionInvokeRevive extends Persist { protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet wallet) { wallet.setActive(true); - wallet.getPreknownNeighbour().tell(new ActionJoin(), self); + wallet.getPreKnownNeighbour().tell(new ActionJoin(), self); } } diff --git a/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java b/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java index 3f602d9..4f3f29b 100644 --- a/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java +++ b/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java @@ -18,6 +18,6 @@ public class ActionSearchMyWalletAnswer extends Persist { protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet wallet) { wallet.setAmount(w.getAmount()); - sender.tell(new ActionInvalidate(wallet.name), self); + sender.tell(new ActionInvalidate(wallet.getName()), self); } } diff --git a/src/fucoin/actions/persist/ActionStoreOrUpdate.java b/src/fucoin/actions/persist/ActionStoreOrUpdate.java index 50b08f6..83b9f73 100644 --- a/src/fucoin/actions/persist/ActionStoreOrUpdate.java +++ b/src/fucoin/actions/persist/ActionStoreOrUpdate.java @@ -16,6 +16,6 @@ public class ActionStoreOrUpdate extends Persist { @Override protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet wallet) { - wallet.backedUpNeighbors.put(w.name, w); + wallet.backedUpNeighbors.put(w.getName(), w); } } diff --git a/src/fucoin/actions/search/ActionSearchWalletReference.java b/src/fucoin/actions/search/ActionSearchWalletReference.java index 0a92194..950b7fd 100644 --- a/src/fucoin/actions/search/ActionSearchWalletReference.java +++ b/src/fucoin/actions/search/ActionSearchWalletReference.java @@ -7,12 +7,14 @@ import fucoin.wallet.AbstractWallet; import java.util.ArrayList; import java.util.List; -//Used to return a WalletImpl reference (akka-style string which can -// be transformed to an ActorRef) +/** + * Used to return a WalletImpl reference (akka-style string which can + * be transformed to an ActorRef) + */ public class ActionSearchWalletReference extends Search { public final String name; - public final List<ActorRef> ttl = new ArrayList<ActorRef>(); + public final List<ActorRef> ttl = new ArrayList<>(); public ActionSearchWalletReference(String name) { this.name = name; diff --git a/src/fucoin/actions/search/Search.java b/src/fucoin/actions/search/Search.java index 505f915..56dc7d2 100644 --- a/src/fucoin/actions/search/Search.java +++ b/src/fucoin/actions/search/Search.java @@ -2,6 +2,6 @@ package fucoin.actions.search; import fucoin.actions.ClientAction; -public abstract class Search extends ClientAction{ - +public abstract class Search extends ClientAction { + } diff --git a/src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java b/src/fucoin/actions/transaction/ActionCommitDistributedCommittedTransfer.java similarity index 78% rename from src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java rename to src/fucoin/actions/transaction/ActionCommitDistributedCommittedTransfer.java index ef8428e..0c3a622 100644 --- a/src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java +++ b/src/fucoin/actions/transaction/ActionCommitDistributedCommittedTransfer.java @@ -3,10 +3,10 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; import fucoin.actions.ClientAction; -import fucoin.supervisor.DistributedCommitedTransferRequest; +import fucoin.supervisor.DistributedCommittedTransferRequest; import fucoin.wallet.AbstractWallet; -public class ActionCommitDistributedCommitedTransfer extends ClientAction { +public class ActionCommitDistributedCommittedTransfer extends ClientAction { private ActorRef source; private ActorRef target; @@ -16,8 +16,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { private long id; - public ActionCommitDistributedCommitedTransfer(ActorRef source, ActorRef target, - int amount, boolean granted, long timestamp, long id) { + public ActionCommitDistributedCommittedTransfer(ActorRef source, ActorRef target, + int amount, boolean granted, long timestamp, long id) { this.source = source; this.target = target; this.amount = amount; @@ -26,8 +26,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { this.id = id; } - public ActionCommitDistributedCommitedTransfer( - DistributedCommitedTransferRequest outdatedRequest) { + public ActionCommitDistributedCommittedTransfer( + DistributedCommittedTransferRequest outdatedRequest) { this.source = outdatedRequest.getSource(); this.target = outdatedRequest.getTarget(); this.amount = 0; @@ -39,7 +39,7 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { @Override protected void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, AbstractWallet wallet) { - System.out.println(self.path().name() + ": ActionCommitDistributedCommitedTransfer is granted?" + granted); + System.out.println(self.path().name() + ": ActionCommitDistributedCommittedTransfer is granted?" + granted); if (granted) { Integer sourceAmount = wallet.amounts.getOrDefault(source, 0); diff --git a/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java b/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java deleted file mode 100644 index ff2e44f..0000000 --- a/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java +++ /dev/null @@ -1,34 +0,0 @@ -package fucoin.actions.transaction; - -import akka.actor.ActorRef; -import akka.actor.UntypedActorContext; -import fucoin.supervisor.DistributedCommitedTransferRequest; -import fucoin.supervisor.SuperVisorImpl; - -public class ActionInvokeDistributedCommitedTransfer extends CoordinatorTransaction{ - - private ActorRef source; - private ActorRef target; - private int amount; - - public ActionInvokeDistributedCommitedTransfer(ActorRef source, - ActorRef target, int amount) { - this.source=source; - this.target=target; - this.amount=amount; - } - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisorImpl superVisor) { - log("invoke transaction "+source.path().name()+" sends "+amount+" to "+target.path().name()); - long timeout = System.currentTimeMillis()+500; - DistributedCommitedTransferRequest ds = new DistributedCommitedTransferRequest(source,target,timeout); - superVisor.addDistributedCommitedTransferRequest(ds); - ActionPrepareDistributedCommittedTransfer apdct = new ActionPrepareDistributedCommittedTransfer(source,target,amount,timeout,ds.getId()); - for(ActorRef neighbor : superVisor.getKnownNeighbors().values()){ - neighbor.tell(apdct, self); - } - } - -} diff --git a/src/fucoin/actions/transaction/ActionInvokeDistributedCommittedTransfer.java b/src/fucoin/actions/transaction/ActionInvokeDistributedCommittedTransfer.java new file mode 100644 index 0000000..8279580 --- /dev/null +++ b/src/fucoin/actions/transaction/ActionInvokeDistributedCommittedTransfer.java @@ -0,0 +1,38 @@ +package fucoin.actions.transaction; + +import akka.actor.ActorRef; +import akka.actor.UntypedActorContext; +import fucoin.supervisor.DistributedCommittedTransferRequest; +import fucoin.supervisor.SuperVisorImpl; + +public class ActionInvokeDistributedCommittedTransfer extends CoordinatorTransaction { + + private ActorRef source; + private ActorRef target; + private int amount; + + public ActionInvokeDistributedCommittedTransfer(ActorRef source, + ActorRef target, int amount) { + this.source = source; + this.target = target; + this.amount = amount; + } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, SuperVisorImpl superVisor) { + log("invoke transaction " + source.path().name() + + " sends " + amount + + " to " + target.path().name()); + + long timeout = System.currentTimeMillis() + 500; + + DistributedCommittedTransferRequest ds = new DistributedCommittedTransferRequest(source, target, timeout); + superVisor.addDistributedCommitedTransferRequest(ds); + ActionPrepareDistributedCommittedTransfer apdct = new ActionPrepareDistributedCommittedTransfer(source, target, amount, timeout, ds.getId()); + for (ActorRef neighbor : superVisor.getKnownNeighbors().values()) { + neighbor.tell(apdct, self); + } + } + +} diff --git a/src/fucoin/actions/transaction/ActionInvokeSentMoney.java b/src/fucoin/actions/transaction/ActionInvokeSentMoney.java index e406972..688b3f7 100644 --- a/src/fucoin/actions/transaction/ActionInvokeSentMoney.java +++ b/src/fucoin/actions/transaction/ActionInvokeSentMoney.java @@ -21,7 +21,7 @@ public class ActionInvokeSentMoney extends Transaction { log(wallet.getKnownNeighbors() + ""); if (wallet.getKnownNeighbors().containsKey(name)) { wallet.getRemoteSuperVisorActor().tell( - new ActionInvokeDistributedCommitedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender); + new ActionInvokeDistributedCommittedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender); } else { ActionSearchWalletReference aswr = new ActionSearchWalletReference(name); for (ActorRef neighbor : wallet.getKnownNeighbors().values()) { diff --git a/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java b/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java index 4b6393d..6c0c473 100644 --- a/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java +++ b/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java @@ -1,6 +1,6 @@ package fucoin.actions.transaction; -import fucoin.supervisor.DistributedCommitedTransferRequest; +import fucoin.supervisor.DistributedCommittedTransferRequest; import fucoin.supervisor.SuperVisorImpl; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; @@ -29,14 +29,14 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator UntypedActorContext context, SuperVisorImpl superVisor) { log(""+superVisor.getKnownNeighbors()); log("granted?"+granted); - DistributedCommitedTransferRequest request = superVisor.getRequest(id); + DistributedCommittedTransferRequest request = superVisor.getRequest(id); if(granted){ - if(request==null)//unknown DistributedCommitedTransferRequest ignore + if(request==null)//unknown DistributedCommittedTransferRequest ignore return; int newCount = request.addPositiveAnswer(sender); System.out.println(newCount+" have agreed on request"+id); if(newCount == superVisor.getKnownNeighbors().size()){ - ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,true,timestamp,id); + ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source,target,amount,true,timestamp,id); for(ActorRef neighbor : request.getAnswers()){ neighbor.tell(acdct, self); } @@ -45,7 +45,7 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator }else{ //A client wants to rollback if(request!=null){ - ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,false,timestamp,id); + ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source,target,amount,false,timestamp,id); for(ActorRef neighbor : request.getAnswers()){ neighbor.tell(acdct, self); } diff --git a/src/fucoin/actions/transaction/ActionReceiveTransaction.java b/src/fucoin/actions/transaction/ActionReceiveTransaction.java index cb018aa..4250b8e 100644 --- a/src/fucoin/actions/transaction/ActionReceiveTransaction.java +++ b/src/fucoin/actions/transaction/ActionReceiveTransaction.java @@ -5,7 +5,7 @@ import akka.actor.UntypedActorContext; import fucoin.wallet.AbstractWallet; /** - * Used to send (positive amount) or retreive money (negative amount) + * Used to send (positive amount) or retrieve money (negative amount) */ public class ActionReceiveTransaction extends Transaction { final public int amount; diff --git a/src/fucoin/actions/transaction/SuperVisorAction.java b/src/fucoin/actions/transaction/SuperVisorAction.java index 0548603..d65efc0 100644 --- a/src/fucoin/actions/transaction/SuperVisorAction.java +++ b/src/fucoin/actions/transaction/SuperVisorAction.java @@ -1,8 +1,10 @@ package fucoin.actions.transaction; +import akka.actor.ActorRef; +import akka.actor.UntypedActorContext; import fucoin.actions.Action; import fucoin.supervisor.SuperVisorImpl; -public abstract class SuperVisorAction extends Action<SuperVisorImpl>{ - +public abstract class SuperVisorAction extends Action<SuperVisorImpl> { + } diff --git a/src/fucoin/supervisor/ActionUpdateQueue.java b/src/fucoin/supervisor/ActionUpdateQueue.java index 073d6e6..e15ce4b 100644 --- a/src/fucoin/supervisor/ActionUpdateQueue.java +++ b/src/fucoin/supervisor/ActionUpdateQueue.java @@ -1,38 +1,38 @@ package fucoin.supervisor; -import java.util.List; - import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.actions.transaction.ActionCommitDistributedCommitedTransfer; +import fucoin.actions.transaction.ActionCommitDistributedCommittedTransfer; import fucoin.actions.transaction.SuperVisorAction; -public class ActionUpdateQueue extends SuperVisorAction{ - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisorImpl superVisor) { - - List<DistributedCommitedTransferRequest> deletes = superVisor.updateList(); - - for(DistributedCommitedTransferRequest outdatedRequest : deletes){ - ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(outdatedRequest); - for(ActorRef neighbor : superVisor.getKnownNeighbors().values()){ - neighbor.tell(acdct, self); - } - } - sleep(self,context,1000); - self.tell(this, self); - } - - private void sleep(ActorRef self, UntypedActorContext context, int sleeptime) { - try { - context.unwatch(self); - Thread.sleep(sleeptime); - context.watch(self); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } +import java.util.List; + +public class ActionUpdateQueue extends SuperVisorAction { + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, SuperVisorImpl superVisor) { + + List<DistributedCommittedTransferRequest> deletes = superVisor.updateList(); + + for (DistributedCommittedTransferRequest outdatedRequest : deletes) { + ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(outdatedRequest); + for (ActorRef neighbor : superVisor.getKnownNeighbors().values()) { + neighbor.tell(acdct, self); + } + } + sleep(self, context, 1000); + self.tell(this, self); + } + + private void sleep(ActorRef self, UntypedActorContext context, int sleeptime) { + try { + context.unwatch(self); + Thread.sleep(sleeptime); + context.watch(self); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } diff --git a/src/fucoin/supervisor/AmountTableModel.java b/src/fucoin/supervisor/AmountTableModel.java index a8a6a8a..6c1d118 100644 --- a/src/fucoin/supervisor/AmountTableModel.java +++ b/src/fucoin/supervisor/AmountTableModel.java @@ -17,7 +17,7 @@ public class AmountTableModel extends DefaultTableModel { public void updateTable(String address, String name, int amount) { - Vector<Object> rows = this.getDataVector(); + Vector rows = this.getDataVector(); for (int i = 0; i < rows.size(); i++) { if (rows.get(i) instanceof Vector){ Vector<Object> row = (Vector<Object>) rows.get(i); diff --git a/src/fucoin/supervisor/DistributedCommitedTransferRequest.java b/src/fucoin/supervisor/DistributedCommittedTransferRequest.java similarity index 81% rename from src/fucoin/supervisor/DistributedCommitedTransferRequest.java rename to src/fucoin/supervisor/DistributedCommittedTransferRequest.java index e0b60fe..920db36 100644 --- a/src/fucoin/supervisor/DistributedCommitedTransferRequest.java +++ b/src/fucoin/supervisor/DistributedCommittedTransferRequest.java @@ -9,16 +9,16 @@ import java.util.LinkedList; import java.util.List; import java.util.Random; -public class DistributedCommitedTransferRequest extends Transaction { +public class DistributedCommittedTransferRequest extends Transaction { private final static Random random = new Random(System.currentTimeMillis() + System.nanoTime()); private ActorRef source; private ActorRef target; private long timeout; private long id; - private List<ActorRef> answers = new LinkedList<ActorRef>(); + private List<ActorRef> answers = new LinkedList<>(); - public DistributedCommitedTransferRequest(ActorRef source, ActorRef target, - long timeout) { + public DistributedCommittedTransferRequest(ActorRef source, ActorRef target, + long timeout) { this.source = source; this.target = target; this.timeout = timeout; diff --git a/src/fucoin/supervisor/SuperVisorFactory.java b/src/fucoin/supervisor/SuperVisorCreator.java similarity index 95% rename from src/fucoin/supervisor/SuperVisorFactory.java rename to src/fucoin/supervisor/SuperVisorCreator.java index 166c957..70ea296 100644 --- a/src/fucoin/supervisor/SuperVisorFactory.java +++ b/src/fucoin/supervisor/SuperVisorCreator.java @@ -9,7 +9,7 @@ import java.awt.*; * Create SuperVisor with a AWT Window. * The window displays the information from the supervisor. */ -public class SuperVisorFactory implements Creator<SuperVisorImpl> { +public class SuperVisorCreator implements Creator<SuperVisorImpl> { @Override public SuperVisorImpl create() throws Exception { diff --git a/src/fucoin/supervisor/SuperVisorImpl.java b/src/fucoin/supervisor/SuperVisorImpl.java index 9e19e90..224efaf 100644 --- a/src/fucoin/supervisor/SuperVisorImpl.java +++ b/src/fucoin/supervisor/SuperVisorImpl.java @@ -28,15 +28,16 @@ public class SuperVisorImpl extends AbstractNode { if (msg instanceof ActionGetAmountAnswer) { ActionGetAmountAnswer answer = (ActionGetAmountAnswer) msg; amountTableModel.updateTable(answer.address, answer.name, answer.amount); - } else if (msg instanceof SuperVisorAction) { + } /* TODO: Whats happened here?? Why we can invoke doAction of abstract class? */ + else if (msg instanceof SuperVisorAction) { ((Action) msg).doAction(this); } } - private Map<Long, DistributedCommitedTransferRequest> requestQueue; + private Map<Long, DistributedCommittedTransferRequest> requestQueue; public static Props props() { - return Props.create(SuperVisorImpl.class, new SuperVisorFactory()); + return Props.create(new SuperVisorCreator()); } public void updateValues() { @@ -53,7 +54,7 @@ public class SuperVisorImpl extends AbstractNode { } public void addDistributedCommitedTransferRequest( - DistributedCommitedTransferRequest request) { + DistributedCommittedTransferRequest request) { System.out.println("Add request to queue: " + request.getId()); requestQueue.put(request.getId(), request); } @@ -70,10 +71,10 @@ public class SuperVisorImpl extends AbstractNode { * * @return deleted outdated request */ - public List<DistributedCommitedTransferRequest> updateList() { - List<Long> deletesIds = new ArrayList<Long>(); - List<DistributedCommitedTransferRequest> deletes = new ArrayList<DistributedCommitedTransferRequest>(); - for (Entry<Long, DistributedCommitedTransferRequest> outdatedRequest : requestQueue.entrySet()) { + public List<DistributedCommittedTransferRequest> updateList() { + List<Long> deletesIds = new ArrayList<>(); + List<DistributedCommittedTransferRequest> deletes = new ArrayList<>(); + for (Entry<Long, DistributedCommittedTransferRequest> outdatedRequest : requestQueue.entrySet()) { if (outdatedRequest.getValue().getTimeout() < System.currentTimeMillis()) { deletesIds.add(outdatedRequest.getKey()); deletes.add(outdatedRequest.getValue()); @@ -86,12 +87,11 @@ public class SuperVisorImpl extends AbstractNode { return deletes; } - public DistributedCommitedTransferRequest getRequest(Long id) { - DistributedCommitedTransferRequest searchedrequest = requestQueue.get(id); - return searchedrequest; + public DistributedCommittedTransferRequest getRequest(Long id) { + return requestQueue.get(id); } - public void deleteRequest(DistributedCommitedTransferRequest request) { + public void deleteRequest(DistributedCommittedTransferRequest request) { requestQueue.remove(request.getId()); } } diff --git a/src/fucoin/wallet/AbstractWallet.java b/src/fucoin/wallet/AbstractWallet.java index 2e0da3b..e12bb4a 100644 --- a/src/fucoin/wallet/AbstractWallet.java +++ b/src/fucoin/wallet/AbstractWallet.java @@ -4,14 +4,14 @@ import akka.actor.ActorRef; public abstract class AbstractWallet extends AbstractNode { /** - * The amount this wallet currently holds + * Currently amount of this wallet */ protected int amount; /** * The name of this wallet (does never change, no duplicates in network assumed) */ - public final String name; + protected final String name; /** * Init. a wallet with a name. @@ -60,7 +60,7 @@ public abstract class AbstractWallet extends AbstractNode { public abstract void setActive(boolean isActive); - public abstract ActorRef getPreknownNeighbour(); + public abstract ActorRef getPreKnownNeighbour(); public abstract ActorRef getRemoteSuperVisorActor(); diff --git a/src/fucoin/wallet/WalletCreator.java b/src/fucoin/wallet/WalletCreator.java new file mode 100644 index 0000000..d35360d --- /dev/null +++ b/src/fucoin/wallet/WalletCreator.java @@ -0,0 +1,32 @@ +package fucoin.wallet; + +import akka.actor.ActorRef; +import akka.japi.Creator; +import fucoin.gui.WalletGuiController; +import fucoin.gui.WalletGuiControllerImpl; + +public class WalletCreator implements Creator<AbstractWallet> { + private ActorRef preKnownNeighbour; + private String walletName; + private ActorRef remoteSuperVisorActor; + private String preKnownNeighbourName; + + public WalletCreator(ActorRef preKnownNeighbour, String preKnownNeighbourName, + String walletName, ActorRef remoteSuperVisorActor) { + this.preKnownNeighbour = preKnownNeighbour; + this.preKnownNeighbourName = preKnownNeighbourName; + this.walletName = walletName; + this.remoteSuperVisorActor = remoteSuperVisorActor; + + } + + @Override + public WalletImpl create() throws Exception { + WalletImpl wallet = new WalletImpl(preKnownNeighbour, preKnownNeighbourName, + walletName, remoteSuperVisorActor); + + WalletGuiControllerImpl gui = new WalletGuiControllerImpl(wallet); + wallet.setGui(gui); + return wallet; + } +} diff --git a/src/fucoin/wallet/WalletFactory.java b/src/fucoin/wallet/WalletFactory.java deleted file mode 100644 index d635272..0000000 --- a/src/fucoin/wallet/WalletFactory.java +++ /dev/null @@ -1,31 +0,0 @@ -package fucoin.wallet; - -import akka.actor.ActorRef; -import akka.japi.Creator; -import fucoin.gui.WalletGuiController; -import fucoin.gui.WalletGuiControllerImpl; - -public class WalletFactory implements Creator<AbstractWallet> { - - private ActorRef preknownNeighbour; - private String walletName; - private ActorRef remoteSuperVisorActor; - private String preknownNeighbourName; - - public WalletFactory(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { - this.preknownNeighbour = preknownNeighbour; - this.preknownNeighbourName = preknownNeighbourName; - this.walletName = walletName; - this.remoteSuperVisorActor = remoteSuperVisorActor; - - } - - @Override - public WalletImpl create() throws Exception { - WalletImpl wallet = new WalletImpl(preknownNeighbour, preknownNeighbourName, walletName, remoteSuperVisorActor); - - WalletGuiController gui = new WalletGuiControllerImpl(wallet); - wallet.setGui(gui); - return wallet; - } -} diff --git a/src/fucoin/wallet/WalletImpl.java b/src/fucoin/wallet/WalletImpl.java index 30d80ad..83ee800 100644 --- a/src/fucoin/wallet/WalletImpl.java +++ b/src/fucoin/wallet/WalletImpl.java @@ -14,22 +14,32 @@ import fucoin.gui.WalletGuiController; public class WalletImpl extends AbstractWallet implements WalletController { - private ActorRef preknownNeighbour; + private ActorRef preKnownNeighbour; private ActorRef remoteSuperVisorActor; private WalletGuiController gui; - private String preknownNeighbourName; + private String preKnownNeighbourName; private boolean isActive; - public WalletImpl(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { + public WalletImpl(String name) { + super(name); + } + + public WalletImpl(ActorRef preKnownNeighbour, String preKnownNeighbourName, + String walletName, ActorRef remoteSuperVisorActor) { super(walletName); - this.preknownNeighbourName = preknownNeighbourName; - this.preknownNeighbour = preknownNeighbour; + 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)); + } + public void addAmount(int amount) { - setAmount(this.amount + amount); - log(" My amount is now " + this.amount); + setAmount(this.getAmount() + amount); + log(" My amount is now " + this.getAmount()); } @Override @@ -61,17 +71,17 @@ public class WalletImpl extends AbstractWallet implements WalletController { 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); + //log(""+preKnownNeighbour); //knownNeighbors.put(getName(),getSelf()); //System.out.println(knownNeighbors); - if (preknownNeighbour != null) { - addKnownNeighbor(preknownNeighbourName, preknownNeighbour); - preknownNeighbour.tell(new ActionJoin(), getSelf()); + 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()); + preKnownNeighbour.tell(aja, getSelf()); } //setAmount(100); @@ -85,22 +95,17 @@ public class WalletImpl extends AbstractWallet implements WalletController { } - - public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { - return Props.create(WalletImpl.class, new WalletFactory(preknownNeighbour, preknownNeighbourName, walletName, remoteSuperVisorActor)); - } - @Override public boolean equals(Object obj) { if (obj instanceof WalletImpl) { WalletImpl wobj = (WalletImpl) obj; - return amount == wobj.amount && name.equals(wobj.name); + return amount == wobj.getAmount() && name.equals(wobj.getName()); } return false; } - public void setGui(WalletGuiController gui) { - this.gui = gui; + public int getAmount() { + return amount; } public void setAmount(int amount) { @@ -113,30 +118,46 @@ public class WalletImpl extends AbstractWallet implements WalletController { } } - public int getAmount() { - return amount; + public ActorRef getPreKnownNeighbour() { + return preKnownNeighbour; } - 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; + } + public WalletGuiController getGui() { return gui; } - public String getPreknownNeighbourName() { - return preknownNeighbourName; + 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) { System.out.println(key + " is newNeighbor of " + name + "?" + !getKnownNeighbors().containsKey(key)); @@ -151,18 +172,6 @@ public class WalletImpl extends AbstractWallet implements WalletController { 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) { @@ -180,10 +189,6 @@ public class WalletImpl extends AbstractWallet implements WalletController { } } - public void setActive(boolean isActive) { - this.isActive = isActive; - } - @Override public void send(String address, int amount) { getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf()); -- GitLab