From fe89c93bc1ce9ceda746e3282b71e0fbec2eeb0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=B6nnecke?= <simonkoennecke@gmail.com> Date: Mon, 13 Jun 2016 18:08:29 +0200 Subject: [PATCH] create Wallet package, use AbstractWallet instead WalletImpl, rename classes, apply lint --- pom.xml | 3 +- src/fucoin/AbstractNode.java | 58 ---------- src/fucoin/AbstractWallet.java | 31 ------ src/fucoin/IWallet.java | 13 --- src/fucoin/Main.java | 62 ++--------- src/fucoin/MainRemote.java | 8 +- src/fucoin/WalletCreator.java | 34 ------ src/fucoin/actions/Action.java | 2 +- src/fucoin/actions/ClientAction.java | 6 +- src/fucoin/actions/join/ActionJoin.java | 20 ++-- src/fucoin/actions/join/ActionJoinAnswer.java | 33 +++--- src/fucoin/actions/join/GeneralAction.java | 2 +- src/fucoin/actions/join/Join.java | 6 +- src/fucoin/actions/join/ServerActionJoin.java | 32 +++--- .../actions/persist/ActionInvalidate.java | 23 ++-- .../actions/persist/ActionInvokeLeave.java | 32 +++--- .../actions/persist/ActionInvokeRevive.java | 16 +-- .../actions/persist/ActionInvokeUpdate.java | 16 +-- .../actions/persist/ActionSearchMyWallet.java | 31 +++--- .../persist/ActionSearchMyWalletAnswer.java | 23 ++-- .../actions/persist/ActionStoreOrUpdate.java | 17 +-- .../search/ActionSearchWalletReference.java | 68 ++++++------ .../ActionSearchWalletReferenceAnswer.java | 41 +++---- ...tionCommitDistributedCommitedTransfer.java | 8 +- .../actions/transaction/ActionGetAmount.java | 14 +-- .../transaction/ActionGetAmountAnswer.java | 5 +- ...tionInvokeDistributedCommitedTransfer.java | 9 +- .../transaction/ActionInvokeSentMoney.java | 63 +++++------ ...ionPrepareDistributedCommitedTransfer.java | 34 ------ ...onPrepareDistributedCommittedTransfer.java | 38 +++++++ ...reDistributedCommittedTransferAnswer.java} | 10 +- .../transaction/ActionReceiveTransaction.java | 22 ++-- .../transaction/CoordinatorTransaction.java | 13 +-- .../actions/transaction/SuperVisorAction.java | 7 +- .../actions/transaction/Transaction.java | 4 +- src/fucoin/gui/IWalletControle.java | 7 -- src/fucoin/gui/IWalletGuiControle.java | 13 --- src/fucoin/gui/LogMessage.java | 4 +- src/fucoin/gui/WalletControle.java | 24 ----- src/fucoin/gui/WalletController.java | 7 ++ src/fucoin/gui/WalletControllerImpl.java | 24 +++++ src/fucoin/gui/WalletCoreGui.java | 72 ++++++------- src/fucoin/gui/WalletGuiController.java | 14 +++ ...tGui.java => WalletGuiControllerImpl.java} | 32 ++---- src/fucoin/supervisor/ActionUpdateQueue.java | 3 +- .../DistributedCommitedTransferRequest.java | 100 +++++++++--------- src/fucoin/supervisor/SuperVisorCreator.java | 56 ---------- src/fucoin/supervisor/SuperVisorFactory.java | 49 +++++++++ .../{SuperVisor.java => SuperVisorImpl.java} | 31 ++---- src/fucoin/wallet/AbstractNode.java | 71 +++++++++++++ src/fucoin/wallet/AbstractWallet.java | 68 ++++++++++++ src/fucoin/wallet/Wallet.java | 14 +++ src/fucoin/wallet/WalletFactory.java | 31 ++++++ .../{Wallet.java => wallet/WalletImpl.java} | 27 +++-- 54 files changed, 714 insertions(+), 737 deletions(-) delete mode 100644 src/fucoin/AbstractNode.java delete mode 100644 src/fucoin/AbstractWallet.java delete mode 100644 src/fucoin/IWallet.java delete mode 100644 src/fucoin/WalletCreator.java delete mode 100644 src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransfer.java create mode 100644 src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransfer.java rename src/fucoin/actions/transaction/{ActionPrepareDistributedCommitedTransferAnswer.java => ActionPrepareDistributedCommittedTransferAnswer.java} (80%) delete mode 100644 src/fucoin/gui/IWalletControle.java delete mode 100644 src/fucoin/gui/IWalletGuiControle.java delete mode 100644 src/fucoin/gui/WalletControle.java create mode 100644 src/fucoin/gui/WalletController.java create mode 100644 src/fucoin/gui/WalletControllerImpl.java create mode 100644 src/fucoin/gui/WalletGuiController.java rename src/fucoin/gui/{WalletGui.java => WalletGuiControllerImpl.java} (87%) delete mode 100644 src/fucoin/supervisor/SuperVisorCreator.java create mode 100644 src/fucoin/supervisor/SuperVisorFactory.java rename src/fucoin/supervisor/{SuperVisor.java => SuperVisorImpl.java} (82%) create mode 100644 src/fucoin/wallet/AbstractNode.java create mode 100644 src/fucoin/wallet/AbstractWallet.java create mode 100644 src/fucoin/wallet/Wallet.java create mode 100644 src/fucoin/wallet/WalletFactory.java rename src/fucoin/{Wallet.java => wallet/WalletImpl.java} (86%) diff --git a/pom.xml b/pom.xml index ccdd40b..41563e6 100644 --- a/pom.xml +++ b/pom.xml @@ -16,11 +16,12 @@ </plugin> </plugins> </build> + <dependencies> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor_2.11</artifactId> - <version>2.4-M1</version> + <version>2.4.7</version> </dependency> </dependencies> </project> \ No newline at end of file diff --git a/src/fucoin/AbstractNode.java b/src/fucoin/AbstractNode.java deleted file mode 100644 index 912b0a2..0000000 --- a/src/fucoin/AbstractNode.java +++ /dev/null @@ -1,58 +0,0 @@ -package fucoin; - -import java.io.Serializable; -import java.util.HashMap; - -import akka.actor.ActorRef; -import akka.actor.UntypedActor; - -public abstract class AbstractNode extends UntypedActor implements Serializable { - - // Returns the akka-style address as String, which - // could be converted to an ActorRef object later - public String getAddress() { - return getAddress(getSelf()); - } - - public String getAddress(ActorRef self) { - return self.path().toSerializationFormatWithAddress(self.path().address()); - } - - // The which receives Action objects - public abstract void onReceive(Object message); - - // Holds references to neighbors that were in - // contact with this wallet during runtime; - // The key corresponds to the Wallet's name - private transient HashMap<String, ActorRef> knownNeighbors = new HashMap<String, ActorRef>(); - - // Holds references to neighbors this wallet - // synchronizes itself to (the Wallet object); - // The key corresponds to the Wallet's name - public transient HashMap<String, ActorRef> localNeighbors = new HashMap<String, ActorRef>(); - - // Holds all Wallets from network participants - // which synchronize their state (Wallet object) - // with us; - // The key corresponds to the Wallet's name - public transient HashMap<String, AbstractWallet> backedUpNeighbors = new HashMap<String, AbstractWallet>(); - - public transient HashMap<ActorRef, Integer> amounts = new HashMap<ActorRef, Integer>(); - - public boolean addKnownNeighbor(String key, ActorRef value) { - if(!knownNeighbors.containsKey(key)){ - knownNeighbors.put(key,value); - return true; - } - return false; - } - - public HashMap<String, ActorRef> getKnownNeighbors() { - return knownNeighbors; - } - - public void log(String string) { - System.out.println(getSelf().path().name()+": "+string); - } - -} \ No newline at end of file diff --git a/src/fucoin/AbstractWallet.java b/src/fucoin/AbstractWallet.java deleted file mode 100644 index 076a8f2..0000000 --- a/src/fucoin/AbstractWallet.java +++ /dev/null @@ -1,31 +0,0 @@ -package fucoin; - -public abstract class AbstractWallet extends AbstractNode{ - - // Constructor - public AbstractWallet(String name) { - this.name = name; - } - - // Returns the name of this wallet, e.g. "Lieschen Müller" - public String getName() { - return this.name; - } - - // Performs housekeeping operations, e.g. pushes - // backedUpNeighbor-entries to other neighbors - public abstract void leave(); - - // The amount this wallet currently holds - protected int amount; - - // The name of this wallet (does never change, no - // duplicates in network assumed) - public final String name; - - public int getAmount(){ - return amount; - } - - -} diff --git a/src/fucoin/IWallet.java b/src/fucoin/IWallet.java deleted file mode 100644 index 54ec9b3..0000000 --- a/src/fucoin/IWallet.java +++ /dev/null @@ -1,13 +0,0 @@ -package fucoin; -import java.util.Vector; - -import fucoin.gui.IWalletControle; - - -public interface IWallet extends IWalletControle{ - //Vector<WalletPointer> join(); - void storeOrUpdate(Wallet w); - void invalidateWallet(Wallet w); - void receiveTransaction(int amount); - //Vector<WalletPointer> searchWallet(String adress); -} diff --git a/src/fucoin/Main.java b/src/fucoin/Main.java index b911900..cf53035 100644 --- a/src/fucoin/Main.java +++ b/src/fucoin/Main.java @@ -10,69 +10,27 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import fucoin.actions.join.ServerActionJoin; -import fucoin.actions.transaction.ActionInvokeSentMoney; -import fucoin.supervisor.SuperVisor; - +import fucoin.supervisor.SuperVisorImpl; +import fucoin.wallet.WalletImpl; public class Main { 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(SuperVisor.props(),"SuperVisor"); + ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(),"SuperVisorImpl"); List<ActorRef> activeActors = new ArrayList<>(); - ActorRef a1 = system.actorOf(Wallet.props(null,"","Main",superVisorActor),"Main"); - ActorRef a2 = system.actorOf(Wallet.props(a1,"Main","Main2",superVisorActor),"Main2"); + 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); - - //a2.tell(new ActionInvokeSentMoney("Main", 200), a2); - //activeActors.add(a1); - /* - int maxrounds = 100; - int maxactors = 10; - for(int actor=0; actor<maxactors;actor++){ - activeActors.add(system.actorOf(Wallet.props(a1,"Main","Main"+actor,superVisorActor),"Main"+actor)); - } - List<List<ActorRef>> offline = new ArrayList<List<ActorRef>>(); - - for(int listnr=0; listnr<maxrounds;listnr++){ - offline.add(new ArrayList<ActorRef>()); - } - - for(int timestep=0; timestep<maxrounds;timestep++){ - System.out.println("timestamp:"+timestep); - List<ActorRef> removedActors = new ArrayList<ActorRef>(); - for(ActorRef actor:activeActors){ - if(Math.random()<0.6){ - actor.tell(new ActionInvokeSentMoney("Main"+(int)Math.floor(Math.random()*maxactors), (int) (Math.round(Math.random()*100))), actor); - } - if(Math.random()<0.2){ - removedActors.add(actor); - int offtime = timestep+(int)(Math.random()*6)+2; - - offline.get(Math.min(offtime, maxrounds-1)).add(actor); - } - } - activeActors.removeAll(removedActors); - for(ActorRef actorName:offline.get(timestep)){ - actorName.tell(new ActionInvokeRevive(), actorName); - activeActors.add(actorName); - } - for(ActorRef removedActor : removedActors){ - removedActor.tell(new ActionInvokeLeave(), removedActor); - } - - Thread.sleep(1000); - System.out.println("timestamp end:"+timestep); - System.out.println("activeActors:"+activeActors); - System.out.println("revived"+offline.get(timestep)); - - } - superVisorActor.tell(new ActionInvokeUpdate(), superVisorActor); - */ + } + + private static void startSupervisor() { + } } diff --git a/src/fucoin/MainRemote.java b/src/fucoin/MainRemote.java index 309bae2..3568dd0 100644 --- a/src/fucoin/MainRemote.java +++ b/src/fucoin/MainRemote.java @@ -2,13 +2,13 @@ package fucoin; import java.io.File; -import akka.actor.ActorPath; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Address; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import fucoin.wallet.WalletImpl; public class MainRemote { public static ActorRef remoteSuperVisorActor; @@ -23,10 +23,10 @@ public class MainRemote { Address address = new Address("akka.tcp", "Core", "127.0.0.1", 1234); System.out.println(address); String path = "akka.tcp://Core@127.0.0.1:1234/user/Main"; - System.out.println(system.actorSelection(ActorPath.fromString(path))); + //System.out.println(system.actorSelection(ActorPath.fromString(path))); - System.out.println(ActorPath.isValidPathElement(""+address+"/user/Main")); - ActorRef a1 = system.actorOf(Wallet.props(null,"","Main2",remoteSuperVisorActor),"Main2"); + //System.out.println(ActorPath.isValidPathElement(""+address+"/user/Main")); + ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Main2",remoteSuperVisorActor),"Main2"); } } diff --git a/src/fucoin/WalletCreator.java b/src/fucoin/WalletCreator.java deleted file mode 100644 index 3918b11..0000000 --- a/src/fucoin/WalletCreator.java +++ /dev/null @@ -1,34 +0,0 @@ -package fucoin; - -import akka.actor.ActorRef; -import akka.japi.Creator; -import fucoin.gui.IWalletGuiControle; -import fucoin.gui.WalletGui; - -public class WalletCreator implements Creator<Wallet> { - - 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 Wallet create() throws Exception { - Wallet wallet = new Wallet(preknownNeighbour,preknownNeighbourName, walletName,remoteSuperVisorActor); - - IWalletGuiControle gui = new WalletGui(wallet); - wallet.setGui(gui); - return wallet; - } - - - -} diff --git a/src/fucoin/actions/Action.java b/src/fucoin/actions/Action.java index 926c6b2..2c99aed 100644 --- a/src/fucoin/actions/Action.java +++ b/src/fucoin/actions/Action.java @@ -1,6 +1,6 @@ package fucoin.actions; -import fucoin.AbstractNode; +import fucoin.wallet.AbstractNode; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; diff --git a/src/fucoin/actions/ClientAction.java b/src/fucoin/actions/ClientAction.java index e4f5119..5a38b97 100644 --- a/src/fucoin/actions/ClientAction.java +++ b/src/fucoin/actions/ClientAction.java @@ -2,11 +2,11 @@ package fucoin.actions; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; -public abstract class ClientAction extends Action<Wallet>{ +public abstract class ClientAction extends Action<AbstractWallet>{ @Override protected abstract void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet abstractNode); + UntypedActorContext context, AbstractWallet abstractNode); } diff --git a/src/fucoin/actions/join/ActionJoin.java b/src/fucoin/actions/join/ActionJoin.java index 93b690d..e5bb08f 100644 --- a/src/fucoin/actions/join/ActionJoin.java +++ b/src/fucoin/actions/join/ActionJoin.java @@ -2,17 +2,17 @@ package fucoin.actions.join; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractNode; +import fucoin.wallet.AbstractNode; -//Used to join the network (a pre known participant/Wallet must be known) -public class ActionJoin extends GeneralAction{ +//Used to join the network (a pre known participant/WalletImpl must be known) +public class ActionJoin extends GeneralAction { - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, AbstractNode node) { - ActionJoinAnswer aja = new ActionJoinAnswer(); - aja.someNeighbors.putAll(node.getKnownNeighbors()); - sender.tell(aja, self); - } + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractNode node) { + ActionJoinAnswer aja = new ActionJoinAnswer(); + aja.someNeighbors.putAll(node.getKnownNeighbors()); + sender.tell(aja, self); + } } diff --git a/src/fucoin/actions/join/ActionJoinAnswer.java b/src/fucoin/actions/join/ActionJoinAnswer.java index 0750e85..4a86c83 100644 --- a/src/fucoin/actions/join/ActionJoinAnswer.java +++ b/src/fucoin/actions/join/ActionJoinAnswer.java @@ -1,27 +1,28 @@ package fucoin.actions.join; -import java.util.HashMap; -import java.util.Map.Entry; - import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; import fucoin.actions.ClientAction; import fucoin.actions.persist.ActionSearchMyWallet; +import fucoin.wallet.AbstractWallet; + +import java.util.HashMap; +import java.util.Map.Entry; // Returns some neighbors that might be used as known // and/or local neighbors -public class ActionJoinAnswer extends ClientAction{ - public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - log("Addressed to "+self.path().name()+" from "+sender.path().name()+": someNeighbors:"+someNeighbors); - for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){ - wallet.addKnownNeighbor(neighbor.getKey(),neighbor.getValue()); - } - for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){ - neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self); - } - } +public class ActionJoinAnswer extends ClientAction { + public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); + + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + log("Addressed to " + self.path().name() + " from " + sender.path().name() + ": someNeighbors:" + someNeighbors); + for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { + wallet.addKnownNeighbor(neighbor.getKey(), neighbor.getValue()); + } + for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) { + neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self); + } + } } diff --git a/src/fucoin/actions/join/GeneralAction.java b/src/fucoin/actions/join/GeneralAction.java index 8ddb517..fd23197 100644 --- a/src/fucoin/actions/join/GeneralAction.java +++ b/src/fucoin/actions/join/GeneralAction.java @@ -1,7 +1,7 @@ package fucoin.actions.join; -import fucoin.AbstractNode; import fucoin.actions.Action; +import fucoin.wallet.AbstractNode; public abstract class GeneralAction extends Action<AbstractNode> { diff --git a/src/fucoin/actions/join/Join.java b/src/fucoin/actions/join/Join.java index eccba13..616dbf9 100644 --- a/src/fucoin/actions/join/Join.java +++ b/src/fucoin/actions/join/Join.java @@ -1,10 +1,6 @@ package fucoin.actions.join; -import akka.actor.ActorRef; -import akka.actor.UntypedActorContext; -import fucoin.AbstractNode; -import fucoin.Wallet; import fucoin.actions.ClientAction; -public abstract class Join extends ClientAction{ +public abstract class Join extends ClientAction { } diff --git a/src/fucoin/actions/join/ServerActionJoin.java b/src/fucoin/actions/join/ServerActionJoin.java index 9dfc08a..f44019d 100644 --- a/src/fucoin/actions/join/ServerActionJoin.java +++ b/src/fucoin/actions/join/ServerActionJoin.java @@ -4,24 +4,24 @@ import akka.actor.ActorRef; import akka.actor.UntypedActorContext; import fucoin.actions.transaction.ActionInvokeDistributedCommitedTransfer; import fucoin.actions.transaction.SuperVisorAction; -import fucoin.supervisor.SuperVisor; +import fucoin.supervisor.SuperVisorImpl; public class ServerActionJoin extends SuperVisorAction { - private String name; + private String name; - public ServerActionJoin(String name) { - this.name = name; - } + public ServerActionJoin(String name) { + this.name = name; + } - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisor node) { - ActionJoinAnswer aja = new ActionJoinAnswer(); - aja.someNeighbors.putAll(node.getKnownNeighbors()); - sender.tell(aja, self); - node.addKnownNeighbor(name, sender); - self.tell( - new ActionInvokeDistributedCommitedTransfer(self, sender, 100), - sender); - } + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, SuperVisorImpl node) { + ActionJoinAnswer aja = new ActionJoinAnswer(); + aja.someNeighbors.putAll(node.getKnownNeighbors()); + sender.tell(aja, self); + node.addKnownNeighbor(name, sender); + self.tell( + new ActionInvokeDistributedCommitedTransfer(self, sender, 100), + sender); + } } diff --git a/src/fucoin/actions/persist/ActionInvalidate.java b/src/fucoin/actions/persist/ActionInvalidate.java index f5068d3..71c3774 100644 --- a/src/fucoin/actions/persist/ActionInvalidate.java +++ b/src/fucoin/actions/persist/ActionInvalidate.java @@ -2,18 +2,21 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractNode; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; + +/** + * May be used to delete a stored WalletImpl on another participant + */ +public class ActionInvalidate extends Persist { + public final String name; -// May be used to delete a stored Wallet on another participant -public class ActionInvalidate extends Persist{ - public final String name; public ActionInvalidate(String name) { this.name = name; } - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - wallet.backedUpNeighbors.remove(name); - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + wallet.backedUpNeighbors.remove(name); + } } diff --git a/src/fucoin/actions/persist/ActionInvokeLeave.java b/src/fucoin/actions/persist/ActionInvokeLeave.java index e8d0102..201c090 100644 --- a/src/fucoin/actions/persist/ActionInvokeLeave.java +++ b/src/fucoin/actions/persist/ActionInvokeLeave.java @@ -2,23 +2,23 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; -public class ActionInvokeLeave extends Persist{ +public class ActionInvokeLeave extends Persist { - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - for(ActorRef neighbor : wallet.getKnownNeighbors().values()){ - if(self.compareTo(neighbor)!=0){ - neighbor.tell(new ActionStoreOrUpdate(wallet), self); - } - } - - - wallet.setActive(false); - wallet.backedUpNeighbors.clear(); - wallet.getKnownNeighbors().clear(); - } + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + for (ActorRef neighbor : wallet.getKnownNeighbors().values()) { + if (self.compareTo(neighbor) != 0) { + neighbor.tell(new ActionStoreOrUpdate(wallet), self); + } + } + + + wallet.setActive(false); + wallet.backedUpNeighbors.clear(); + wallet.getKnownNeighbors().clear(); + } } diff --git a/src/fucoin/actions/persist/ActionInvokeRevive.java b/src/fucoin/actions/persist/ActionInvokeRevive.java index d1bf101..622b917 100644 --- a/src/fucoin/actions/persist/ActionInvokeRevive.java +++ b/src/fucoin/actions/persist/ActionInvokeRevive.java @@ -2,16 +2,16 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; import fucoin.actions.join.ActionJoin; +import fucoin.wallet.AbstractWallet; -public class ActionInvokeRevive extends Persist{ +public class ActionInvokeRevive extends Persist { - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - wallet.setActive(true); - wallet.getPreknownNeighbour().tell(new ActionJoin(), self); - } + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + wallet.setActive(true); + wallet.getPreknownNeighbour().tell(new ActionJoin(), self); + } } diff --git a/src/fucoin/actions/persist/ActionInvokeUpdate.java b/src/fucoin/actions/persist/ActionInvokeUpdate.java index e71f052..78149b5 100644 --- a/src/fucoin/actions/persist/ActionInvokeUpdate.java +++ b/src/fucoin/actions/persist/ActionInvokeUpdate.java @@ -2,15 +2,15 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; -public class ActionInvokeUpdate extends Persist{ +public class ActionInvokeUpdate extends Persist { - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - // TODO Auto-generated method stub - - } + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + // TODO Auto-generated method stub + + } } diff --git a/src/fucoin/actions/persist/ActionSearchMyWallet.java b/src/fucoin/actions/persist/ActionSearchMyWallet.java index 885cbd6..5d4078e 100644 --- a/src/fucoin/actions/persist/ActionSearchMyWallet.java +++ b/src/fucoin/actions/persist/ActionSearchMyWallet.java @@ -2,27 +2,28 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractWallet; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; -// Used to search a Wallet by name, i.e. the own wallet if we just -// joined the network; If a receiving participant holds the stored Wallet, +// Used to search a WalletImpl by name, i.e. the own wallet if we just +// joined the network; If a receiving participant holds the stored WalletImpl, // he returns it, otherwise, he might use gossiping methods to go on // with the search; // Note: You should also forward the sender (the participant who actually -// searches for this Wallet, so that it can be returnd the direct way) -public class ActionSearchMyWallet extends Persist{ +// searches for this WalletImpl, so that it can be returnd the direct way) +public class ActionSearchMyWallet extends Persist { public final String name; + public ActionSearchMyWallet(String name) { this.name = name; } - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - wallet.addKnownNeighbor(name, sender); - AbstractWallet storedWallet =wallet.backedUpNeighbors.get(name); - if(storedWallet!=null){ - sender.tell(new ActionSearchMyWalletAnswer(storedWallet), self); - } - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + wallet.addKnownNeighbor(name, sender); + AbstractWallet storedWallet = wallet.backedUpNeighbors.get(name); + if (storedWallet != null) { + sender.tell(new ActionSearchMyWalletAnswer(storedWallet), self); + } + } } diff --git a/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java b/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java index 0635d72..3f602d9 100644 --- a/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java +++ b/src/fucoin/actions/persist/ActionSearchMyWalletAnswer.java @@ -2,19 +2,22 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractWallet; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; -// Used to return a searched Wallet +/** + * Used to return a searched WalletImpl + */ public class ActionSearchMyWalletAnswer extends Persist { - public final AbstractWallet w; + public final AbstractWallet w; + public ActionSearchMyWalletAnswer(AbstractWallet w) { this.w = w; } - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - wallet.setAmount(w.getAmount()); - sender.tell(new ActionInvalidate(wallet.name), self); - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + wallet.setAmount(w.getAmount()); + sender.tell(new ActionInvalidate(wallet.name), self); + } } diff --git a/src/fucoin/actions/persist/ActionStoreOrUpdate.java b/src/fucoin/actions/persist/ActionStoreOrUpdate.java index 977c199..50b08f6 100644 --- a/src/fucoin/actions/persist/ActionStoreOrUpdate.java +++ b/src/fucoin/actions/persist/ActionStoreOrUpdate.java @@ -2,19 +2,20 @@ package fucoin.actions.persist; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractNode; -import fucoin.AbstractWallet; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; +import fucoin.wallet.WalletImpl; //Used to push the state of my/a wallet to another participant public class ActionStoreOrUpdate extends Persist { public final AbstractWallet w; + public ActionStoreOrUpdate(AbstractWallet w) { this.w = w; } - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - wallet.backedUpNeighbors.put(w.name, w); - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + wallet.backedUpNeighbors.put(w.name, w); + } } diff --git a/src/fucoin/actions/search/ActionSearchWalletReference.java b/src/fucoin/actions/search/ActionSearchWalletReference.java index 05385ad..0a92194 100644 --- a/src/fucoin/actions/search/ActionSearchWalletReference.java +++ b/src/fucoin/actions/search/ActionSearchWalletReference.java @@ -1,46 +1,46 @@ package fucoin.actions.search; -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; - import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; -//Used to return a Wallet reference (akka-style string which can +import java.util.ArrayList; +import java.util.List; + +//Used to return a WalletImpl reference (akka-style string which can // be transformed to an ActorRef) -public class ActionSearchWalletReference extends Search{ +public class ActionSearchWalletReference extends Search { + + public final String name; + public final List<ActorRef> ttl = new ArrayList<ActorRef>(); - public final String name; - public final List<ActorRef> ttl = new ArrayList<ActorRef>(); public ActionSearchWalletReference(String name) { this.name = name; } - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - log(wallet.getKnownNeighbors()+"contains "+name+"?"); - ttl.add(self); - ActionSearchWalletReferenceAnswer answer = null; - if(this.name.equals(wallet.getName())){ - answer = new ActionSearchWalletReferenceAnswer(name,wallet.getAddress(),ttl); - }else if(wallet.backedUpNeighbors.containsKey(name)){ - answer = new ActionSearchWalletReferenceAnswer(name,wallet.backedUpNeighbors.get(name).getAddress(),ttl); - } else if(wallet.getKnownNeighbors().containsKey(name)){ - answer = new ActionSearchWalletReferenceAnswer(name,wallet.getAddress(wallet.getKnownNeighbors().get(name)),ttl); - } else if (ttl.size()<5){ - for(ActorRef neighbor : wallet.getKnownNeighbors().values()){ - if(!ttl.contains(neighbor)){ - neighbor.tell(this, self); - } - } - } - //User unknown by this Wallet - if(answer!=null&&ttl.size()>0){ - ttl.get(ttl.size()-1).tell(answer, self); - } - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + log(wallet.getKnownNeighbors() + "contains " + name + "?"); + ttl.add(self); + ActionSearchWalletReferenceAnswer answer = null; + if (this.name.equals(wallet.getName())) { + answer = new ActionSearchWalletReferenceAnswer(name, wallet.getAddress(), ttl); + } else if (wallet.backedUpNeighbors.containsKey(name)) { + answer = new ActionSearchWalletReferenceAnswer(name, wallet.backedUpNeighbors.get(name).getAddress(), ttl); + } else if (wallet.getKnownNeighbors().containsKey(name)) { + answer = new ActionSearchWalletReferenceAnswer(name, wallet.getAddress(wallet.getKnownNeighbors().get(name)), ttl); + } else if (ttl.size() < 5) { + for (ActorRef neighbor : wallet.getKnownNeighbors().values()) { + if (!ttl.contains(neighbor)) { + neighbor.tell(this, self); + } + } + } + //User unknown by this WalletImpl + if (answer != null && ttl.size() > 0) { + ttl.get(ttl.size() - 1).tell(answer, self); + } + } } diff --git a/src/fucoin/actions/search/ActionSearchWalletReferenceAnswer.java b/src/fucoin/actions/search/ActionSearchWalletReferenceAnswer.java index 6cc2f40..d1bbdd0 100644 --- a/src/fucoin/actions/search/ActionSearchWalletReferenceAnswer.java +++ b/src/fucoin/actions/search/ActionSearchWalletReferenceAnswer.java @@ -1,30 +1,31 @@ package fucoin.actions.search; -import java.util.List; - import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; + +import java.util.List; public class ActionSearchWalletReferenceAnswer extends Search { - public final String address; - public final String name; - public final List<ActorRef> pathToSearchedWallet; - public ActionSearchWalletReferenceAnswer(String name,String address, List<ActorRef> pathToSearchedWallet) { + public final String address; + public final String name; + public final List<ActorRef> pathToSearchedWallet; + + public ActionSearchWalletReferenceAnswer(String name, String address, List<ActorRef> pathToSearchedWallet) { this.address = address; - this.name=name; - this.pathToSearchedWallet=pathToSearchedWallet; + this.name = name; + this.pathToSearchedWallet = pathToSearchedWallet; + } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + ActorRef target = context.actorSelection(address).anchor(); + wallet.addKnownNeighbor(name, target); + int pos = pathToSearchedWallet.indexOf(self); + if (pos > 0) { + pathToSearchedWallet.get(pos - 1).tell(this, self); + } } - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - ActorRef target = context.actorSelection(address).anchor(); - wallet.addKnownNeighbor(name,target); - int pos = pathToSearchedWallet.indexOf(self); - if(pos>0){ - pathToSearchedWallet.get(pos-1).tell(this, self); - } - } } diff --git a/src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java b/src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java index 5236bf4..ef8428e 100644 --- a/src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java +++ b/src/fucoin/actions/transaction/ActionCommitDistributedCommitedTransfer.java @@ -2,9 +2,9 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; import fucoin.actions.ClientAction; import fucoin.supervisor.DistributedCommitedTransferRequest; +import fucoin.wallet.AbstractWallet; public class ActionCommitDistributedCommitedTransfer extends ClientAction { @@ -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 ActionCommitDistributedCommitedTransfer(ActorRef source, ActorRef target, + int amount, boolean granted, long timestamp, long id) { this.source = source; this.target = target; this.amount = amount; @@ -38,7 +38,7 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { @Override protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { + UntypedActorContext context, AbstractWallet wallet) { System.out.println(self.path().name() + ": ActionCommitDistributedCommitedTransfer is granted?" + granted); if (granted) { diff --git a/src/fucoin/actions/transaction/ActionGetAmount.java b/src/fucoin/actions/transaction/ActionGetAmount.java index f14f8ae..6cbcbfd 100644 --- a/src/fucoin/actions/transaction/ActionGetAmount.java +++ b/src/fucoin/actions/transaction/ActionGetAmount.java @@ -2,15 +2,15 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; public class ActionGetAmount extends Transaction { - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(wallet.getAddress(),wallet.getName(),wallet.getAmount()); - sender.tell(agaa, self); - } + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(wallet.getAddress(), wallet.getName(), wallet.getAmount()); + sender.tell(agaa, self); + } } diff --git a/src/fucoin/actions/transaction/ActionGetAmountAnswer.java b/src/fucoin/actions/transaction/ActionGetAmountAnswer.java index 4a85da1..f8334fb 100644 --- a/src/fucoin/actions/transaction/ActionGetAmountAnswer.java +++ b/src/fucoin/actions/transaction/ActionGetAmountAnswer.java @@ -2,7 +2,8 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; +import fucoin.wallet.AbstractWallet; +import fucoin.wallet.WalletImpl; public class ActionGetAmountAnswer extends Transaction { @@ -18,7 +19,7 @@ public class ActionGetAmountAnswer extends Transaction { @Override protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { + UntypedActorContext context, AbstractWallet wallet) { } diff --git a/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java b/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java index 655aa8d..ff2e44f 100644 --- a/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java +++ b/src/fucoin/actions/transaction/ActionInvokeDistributedCommitedTransfer.java @@ -1,12 +1,9 @@ package fucoin.actions.transaction; -import java.util.HashMap; - import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; import fucoin.supervisor.DistributedCommitedTransferRequest; -import fucoin.supervisor.SuperVisor; +import fucoin.supervisor.SuperVisorImpl; public class ActionInvokeDistributedCommitedTransfer extends CoordinatorTransaction{ @@ -23,12 +20,12 @@ public class ActionInvokeDistributedCommitedTransfer extends CoordinatorTransact @Override protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisor superVisor) { + 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); - ActionPrepareDistributedCommitedTransfer apdct = new ActionPrepareDistributedCommitedTransfer(source,target,amount,timeout,ds.getId()); + 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 7a2ee7e..e406972 100644 --- a/src/fucoin/actions/transaction/ActionInvokeSentMoney.java +++ b/src/fucoin/actions/transaction/ActionInvokeSentMoney.java @@ -3,39 +3,42 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; import fucoin.actions.search.ActionSearchWalletReference; +import fucoin.wallet.AbstractWallet; + +public class ActionInvokeSentMoney extends Transaction { + public final String name; + public final int amount; -public class ActionInvokeSentMoney extends Transaction{ - public final String name; - public final int amount; public ActionInvokeSentMoney(String name, int amount) { - this.name=name; + this.name = name; this.amount = amount; } - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - log(wallet.getKnownNeighbors()+""); - if(wallet.getKnownNeighbors().containsKey(name)){ - wallet.getRemoteSuperVisorActor().tell( - new ActionInvokeDistributedCommitedTransfer(self,wallet.getKnownNeighbors().get(name),amount), sender); - }else{ - ActionSearchWalletReference aswr = new ActionSearchWalletReference(name); - for(ActorRef neighbor : wallet.getKnownNeighbors().values()){ - neighbor.tell(aswr, self); - } - sleep(self, context, 200); - 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(); - } - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + log(wallet.getKnownNeighbors() + ""); + if (wallet.getKnownNeighbors().containsKey(name)) { + wallet.getRemoteSuperVisorActor().tell( + new ActionInvokeDistributedCommitedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender); + } else { + ActionSearchWalletReference aswr = new ActionSearchWalletReference(name); + for (ActorRef neighbor : wallet.getKnownNeighbors().values()) { + neighbor.tell(aswr, self); + } + sleep(self, context, 200); + self.tell(this, self); + } + } + + private void sleep(ActorRef self, UntypedActorContext context, int timeOfIdle) { + try { + context.unwatch(self); + Thread.sleep(timeOfIdle); + context.watch(self); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } diff --git a/src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransfer.java b/src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransfer.java deleted file mode 100644 index 26eb49e..0000000 --- a/src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransfer.java +++ /dev/null @@ -1,34 +0,0 @@ -package fucoin.actions.transaction; - -import akka.actor.ActorRef; -import akka.actor.UntypedActorContext; -import fucoin.Wallet; - -public class ActionPrepareDistributedCommitedTransfer extends Transaction { - - private ActorRef source; - private ActorRef target; - private int amount; - private long timestamp; - private long id; - - public ActionPrepareDistributedCommitedTransfer(ActorRef source, - ActorRef target, int amount, long timestamp, long id) { - this.source = source; - this.target = target; - this.amount = amount; - this.timestamp = timestamp; - this.id = id; - } - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - boolean granted = amount > 0 && - (sender.compareTo(source) == 0 //sender is supervisor(bank) has always money - || (wallet.amounts.containsKey(source) //sender is unknown, might be valid - && wallet.amounts.getOrDefault(source, 0) >= amount)); //sender have enough money - sender.tell(new ActionPrepareDistributedCommitedTransferAnswer(source, target, amount, timestamp, granted, id), self); - } - -} diff --git a/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransfer.java b/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransfer.java new file mode 100644 index 0000000..7e7de88 --- /dev/null +++ b/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransfer.java @@ -0,0 +1,38 @@ +package fucoin.actions.transaction; + +import akka.actor.ActorRef; +import akka.actor.UntypedActorContext; +import fucoin.wallet.AbstractWallet; + +public class ActionPrepareDistributedCommittedTransfer extends Transaction { + + private ActorRef source; + private ActorRef target; + private int amount; + private long timestamp; + private long id; + + public ActionPrepareDistributedCommittedTransfer(ActorRef source, ActorRef target, + int amount, long timestamp, long id) { + this.source = source; + this.target = target; + this.amount = amount; + this.timestamp = timestamp; + this.id = id; + } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + boolean granted = amount > 0 && + //sender is supervisor(bank) has always money + (sender.compareTo(source) == 0 + //sender is unknown, might be valid + || (wallet.amounts.containsKey(source) + //sender have enough money + && wallet.amounts.getOrDefault(source, 0) >= amount)); + + sender.tell(new ActionPrepareDistributedCommittedTransferAnswer(source, target, amount, timestamp, granted, id), self); + } + +} diff --git a/src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransferAnswer.java b/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java similarity index 80% rename from src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransferAnswer.java rename to src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java index 8a01b2e..4b6393d 100644 --- a/src/fucoin/actions/transaction/ActionPrepareDistributedCommitedTransferAnswer.java +++ b/src/fucoin/actions/transaction/ActionPrepareDistributedCommittedTransferAnswer.java @@ -1,11 +1,11 @@ package fucoin.actions.transaction; import fucoin.supervisor.DistributedCommitedTransferRequest; -import fucoin.supervisor.SuperVisor; +import fucoin.supervisor.SuperVisorImpl; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -public class ActionPrepareDistributedCommitedTransferAnswer extends CoordinatorTransaction { +public class ActionPrepareDistributedCommittedTransferAnswer extends CoordinatorTransaction { private ActorRef source; private ActorRef target; @@ -14,8 +14,8 @@ public class ActionPrepareDistributedCommitedTransferAnswer extends CoordinatorT private long timestamp; private long id; - public ActionPrepareDistributedCommitedTransferAnswer(ActorRef source, - ActorRef target, int amount, long timestamp, boolean granted, long id) { + public ActionPrepareDistributedCommittedTransferAnswer(ActorRef source, + ActorRef target, int amount, long timestamp, boolean granted, long id) { this.source=source; this.target=target; this.amount=amount; @@ -26,7 +26,7 @@ public class ActionPrepareDistributedCommitedTransferAnswer extends CoordinatorT @Override protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisor superVisor) { + UntypedActorContext context, SuperVisorImpl superVisor) { log(""+superVisor.getKnownNeighbors()); log("granted?"+granted); DistributedCommitedTransferRequest request = superVisor.getRequest(id); diff --git a/src/fucoin/actions/transaction/ActionReceiveTransaction.java b/src/fucoin/actions/transaction/ActionReceiveTransaction.java index da9aa55..cb018aa 100644 --- a/src/fucoin/actions/transaction/ActionReceiveTransaction.java +++ b/src/fucoin/actions/transaction/ActionReceiveTransaction.java @@ -2,17 +2,21 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.Wallet; -//Used to send (positive amount) or retreive money (negative amount) +import fucoin.wallet.AbstractWallet; + +/** + * Used to send (positive amount) or retreive money (negative amount) + */ public class ActionReceiveTransaction extends Transaction { - final public int amount; + final public int amount; + public ActionReceiveTransaction(int amount) { this.amount = amount; } - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - wallet.addAmount(wallet.getAmount()); - } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + wallet.addAmount(wallet.getAmount()); + } } diff --git a/src/fucoin/actions/transaction/CoordinatorTransaction.java b/src/fucoin/actions/transaction/CoordinatorTransaction.java index 7c95795..b28dc56 100644 --- a/src/fucoin/actions/transaction/CoordinatorTransaction.java +++ b/src/fucoin/actions/transaction/CoordinatorTransaction.java @@ -2,15 +2,12 @@ package fucoin.actions.transaction; import akka.actor.ActorRef; import akka.actor.UntypedActorContext; -import fucoin.AbstractNode; -import fucoin.Wallet; -import fucoin.actions.ClientAction; -import fucoin.supervisor.SuperVisor; +import fucoin.supervisor.SuperVisorImpl; -public abstract class CoordinatorTransaction extends SuperVisorAction{ +public abstract class CoordinatorTransaction extends SuperVisorAction { -@Override -protected abstract void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisor abstractNode); + @Override + protected abstract void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, SuperVisorImpl abstractNode); } diff --git a/src/fucoin/actions/transaction/SuperVisorAction.java b/src/fucoin/actions/transaction/SuperVisorAction.java index a6bce1e..0548603 100644 --- a/src/fucoin/actions/transaction/SuperVisorAction.java +++ b/src/fucoin/actions/transaction/SuperVisorAction.java @@ -1,11 +1,8 @@ package fucoin.actions.transaction; -import akka.actor.ActorRef; -import akka.actor.UntypedActorContext; import fucoin.actions.Action; -import fucoin.actions.ClientAction; -import fucoin.supervisor.SuperVisor; +import fucoin.supervisor.SuperVisorImpl; -public abstract class SuperVisorAction extends Action<SuperVisor>{ +public abstract class SuperVisorAction extends Action<SuperVisorImpl>{ } diff --git a/src/fucoin/actions/transaction/Transaction.java b/src/fucoin/actions/transaction/Transaction.java index d764afc..fd418f9 100644 --- a/src/fucoin/actions/transaction/Transaction.java +++ b/src/fucoin/actions/transaction/Transaction.java @@ -2,6 +2,6 @@ package fucoin.actions.transaction; import fucoin.actions.ClientAction; -public abstract class Transaction extends ClientAction{ - +public abstract class Transaction extends ClientAction { + } diff --git a/src/fucoin/gui/IWalletControle.java b/src/fucoin/gui/IWalletControle.java deleted file mode 100644 index f56fbde..0000000 --- a/src/fucoin/gui/IWalletControle.java +++ /dev/null @@ -1,7 +0,0 @@ -package fucoin.gui; - - -public interface IWalletControle { - public void leave(); - public void send(String address, int amount); -} diff --git a/src/fucoin/gui/IWalletGuiControle.java b/src/fucoin/gui/IWalletGuiControle.java deleted file mode 100644 index dcfa621..0000000 --- a/src/fucoin/gui/IWalletGuiControle.java +++ /dev/null @@ -1,13 +0,0 @@ -package fucoin.gui; - - -public interface IWalletGuiControle { - public void setAddress(String address); - public void setAmount(int amount); - public void addKnownAddress(String address); - public void addLogMsg(String msg); - public void addTransactionLogMessage(String message); - - - -} diff --git a/src/fucoin/gui/LogMessage.java b/src/fucoin/gui/LogMessage.java index c2928d0..8c93133 100644 --- a/src/fucoin/gui/LogMessage.java +++ b/src/fucoin/gui/LogMessage.java @@ -6,7 +6,7 @@ public class LogMessage { private String message; private boolean transactionRelated; - public LogMessage(String message, boolean transactionRelated){ + public LogMessage(String message, boolean transactionRelated) { this.message = message; this.transactionRelated = transactionRelated; } @@ -20,7 +20,7 @@ public class LogMessage { } @Override - public String toString(){ + public String toString() { return getMessage(); } } diff --git a/src/fucoin/gui/WalletControle.java b/src/fucoin/gui/WalletControle.java deleted file mode 100644 index 9bb8b77..0000000 --- a/src/fucoin/gui/WalletControle.java +++ /dev/null @@ -1,24 +0,0 @@ -package fucoin.gui; - -import fucoin.Wallet; - - -public class WalletControle implements IWalletControle{ - - private Wallet wallet; - - public WalletControle(Wallet wallet) { - this.wallet=wallet; - } - - @Override - public void leave() { - wallet.leave(); - } - - @Override - public void send(String name, int amount) { - wallet.send(name, amount); - } - -} diff --git a/src/fucoin/gui/WalletController.java b/src/fucoin/gui/WalletController.java new file mode 100644 index 0000000..c32fd53 --- /dev/null +++ b/src/fucoin/gui/WalletController.java @@ -0,0 +1,7 @@ +package fucoin.gui; + +public interface WalletController { + void leave(); + + void send(String address, int amount); +} diff --git a/src/fucoin/gui/WalletControllerImpl.java b/src/fucoin/gui/WalletControllerImpl.java new file mode 100644 index 0000000..954bf86 --- /dev/null +++ b/src/fucoin/gui/WalletControllerImpl.java @@ -0,0 +1,24 @@ +package fucoin.gui; + +import fucoin.wallet.WalletImpl; + + +public class WalletControllerImpl implements WalletController { + + private WalletImpl wallet; + + public WalletControllerImpl(WalletImpl wallet) { + this.wallet = wallet; + } + + @Override + public void leave() { + wallet.leave(); + } + + @Override + public void send(String name, int amount) { + wallet.send(name, amount); + } + +} diff --git a/src/fucoin/gui/WalletCoreGui.java b/src/fucoin/gui/WalletCoreGui.java index 6e2e4c5..1f728d3 100644 --- a/src/fucoin/gui/WalletCoreGui.java +++ b/src/fucoin/gui/WalletCoreGui.java @@ -1,51 +1,47 @@ package fucoin.gui; -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JTextField; - import akka.actor.Address; import akka.actor.AddressFromURIString; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + public class WalletCoreGui { - public WalletCoreGui() { - JFrame frame = new JFrame("Manager"); - frame.setLayout(new GridLayout(3,2)); - frame.add(new JLabel("Connect to:")); - JTextField input = new JTextField("akka://MySystem/user/main"); - frame.add(input); - frame.add(new JLabel("Name:")); - JTextField name = new JTextField("<Name>"); - frame.add(name); - JButton button = new JButton("connect"); - button.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - /*MessageDispatcherConfigurator mc = null; + public WalletCoreGui() { + JFrame frame = new JFrame("Manager"); + frame.setLayout(new GridLayout(3, 2)); + frame.add(new JLabel("Connect to:")); + JTextField input = new JTextField("akka://MySystem/user/main"); + frame.add(input); + frame.add(new JLabel("Name:")); + JTextField name = new JTextField("<Name>"); + frame.add(name); + JButton button = new JButton("connect"); + button.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + /*MessageDispatcherConfigurator mc = null; String id = input.getText()+"-dispatched"; int hroughput = 1; Duration d = Duration.ofSeconds(2); Dispatcher d = new Dispatcher(mc,id, 1, d, Executors.newSingleThreadExecutor(),1000);*/ - String path = "akka.tcp://Test@127.0.0.1:1234/user/main"; - Address addr = AddressFromURIString.parse(path); - //RemoteScope remoteScope = new RemoteScope(addr); - //Deploy deploy = new Deploy(remoteScope); - //Props remoteWallet = Props.apply(deploy, Wallet.class, null); - - - } - }); - - frame.add(button); - frame.setSize(300, 300); - frame.setVisible(true); - } + String path = "akka.tcp://Test@127.0.0.1:1234/user/main"; + Address addr = AddressFromURIString.parse(path); + //RemoteScope remoteScope = new RemoteScope(addr); + //Deploy deploy = new Deploy(remoteScope); + //Props remoteWallet = Props.apply(deploy, WalletImpl.class, null); + + + } + }); + + frame.add(button); + frame.setSize(300, 300); + frame.setVisible(true); + } } diff --git a/src/fucoin/gui/WalletGuiController.java b/src/fucoin/gui/WalletGuiController.java new file mode 100644 index 0000000..3e1be4c --- /dev/null +++ b/src/fucoin/gui/WalletGuiController.java @@ -0,0 +1,14 @@ +package fucoin.gui; + + +public interface WalletGuiController { + void setAddress(String address); + + void setAmount(int amount); + + void addKnownAddress(String address); + + void addLogMsg(String msg); + + void addTransactionLogMessage(String message); +} diff --git a/src/fucoin/gui/WalletGui.java b/src/fucoin/gui/WalletGuiControllerImpl.java similarity index 87% rename from src/fucoin/gui/WalletGui.java rename to src/fucoin/gui/WalletGuiControllerImpl.java index 564eaea..59f87b1 100644 --- a/src/fucoin/gui/WalletGui.java +++ b/src/fucoin/gui/WalletGuiControllerImpl.java @@ -1,14 +1,11 @@ package fucoin.gui; -import com.sun.tools.javac.comp.Flow; - +import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Enumeration; -import javax.swing.*; - -public class WalletGui implements IWalletGuiControle { +public class WalletGuiControllerImpl implements WalletGuiController { private DefaultListModel<LogMessage> log = new DefaultListModel<>(); @@ -33,7 +30,7 @@ public class WalletGui implements IWalletGuiControle { private JScrollPane logPane = new JScrollPane(txtLog); private JCheckBox showDebug; - public WalletGui(IWalletControle walletControle) { + public WalletGuiControllerImpl(WalletController walletControle) { window.setSize(400, 600); window.setLayout(new GridLayout(3, 1)); @@ -104,29 +101,16 @@ public class WalletGui implements IWalletGuiControle { window.add(bottomPanel); window.setVisible(true); - btnSend.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - walletControle.send(txtSendTo.getSelectedItem().toString(), Integer.parseInt(txtSendAmount.getText())); - } + btnSend.addActionListener(e -> { + walletControle.send(txtSendTo.getSelectedItem().toString(), + Integer.parseInt(txtSendAmount.getText())); }); - btnStore.addActionListener(new ActionListener() { + btnStore.addActionListener(e -> { - @Override - public void actionPerformed(ActionEvent e) { - - } }); - btnExit.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - window.dispose(); - } - }); + btnExit.addActionListener(e -> window.dispose()); window.addWindowListener(new WindowAdapter() { diff --git a/src/fucoin/supervisor/ActionUpdateQueue.java b/src/fucoin/supervisor/ActionUpdateQueue.java index 82cc40c..073d6e6 100644 --- a/src/fucoin/supervisor/ActionUpdateQueue.java +++ b/src/fucoin/supervisor/ActionUpdateQueue.java @@ -1,6 +1,5 @@ package fucoin.supervisor; -import java.util.ArrayList; import java.util.List; import akka.actor.ActorRef; @@ -12,7 +11,7 @@ public class ActionUpdateQueue extends SuperVisorAction{ @Override protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, SuperVisor superVisor) { + UntypedActorContext context, SuperVisorImpl superVisor) { List<DistributedCommitedTransferRequest> deletes = superVisor.updateList(); diff --git a/src/fucoin/supervisor/DistributedCommitedTransferRequest.java b/src/fucoin/supervisor/DistributedCommitedTransferRequest.java index a27f873..e0b60fe 100644 --- a/src/fucoin/supervisor/DistributedCommitedTransferRequest.java +++ b/src/fucoin/supervisor/DistributedCommitedTransferRequest.java @@ -1,58 +1,58 @@ package fucoin.supervisor; +import akka.actor.ActorRef; +import akka.actor.UntypedActorContext; +import fucoin.actions.transaction.Transaction; +import fucoin.wallet.AbstractWallet; + import java.util.LinkedList; import java.util.List; import java.util.Random; -import fucoin.Wallet; -import fucoin.actions.transaction.Transaction; -import akka.actor.ActorRef; -import akka.actor.UntypedActorContext; - public class DistributedCommitedTransferRequest 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>(); - - public DistributedCommitedTransferRequest(ActorRef source, ActorRef target, - long timeout) { - this.source=source; - this.target=target; - this.timeout=timeout; - this.id=random.nextLong(); - } - - @Override - protected void onAction(ActorRef sender, ActorRef self, - UntypedActorContext context, Wallet wallet) { - - } - - public ActorRef getSource() { - return source; - } - - public ActorRef getTarget() { - return target; - } - - public long getTimeout() { - return timeout; - } - - public int addPositiveAnswer(ActorRef sender) { - answers.add(sender); - return answers.size(); - } - - public List<ActorRef> getAnswers() { - return answers; - } - - public long getId() { - return id; - } + 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>(); + + public DistributedCommitedTransferRequest(ActorRef source, ActorRef target, + long timeout) { + this.source = source; + this.target = target; + this.timeout = timeout; + this.id = random.nextLong(); + } + + @Override + protected void onAction(ActorRef sender, ActorRef self, + UntypedActorContext context, AbstractWallet wallet) { + + } + + public ActorRef getSource() { + return source; + } + + public ActorRef getTarget() { + return target; + } + + public long getTimeout() { + return timeout; + } + + public int addPositiveAnswer(ActorRef sender) { + answers.add(sender); + return answers.size(); + } + + public List<ActorRef> getAnswers() { + return answers; + } + + public long getId() { + return id; + } } diff --git a/src/fucoin/supervisor/SuperVisorCreator.java b/src/fucoin/supervisor/SuperVisorCreator.java deleted file mode 100644 index a67730d..0000000 --- a/src/fucoin/supervisor/SuperVisorCreator.java +++ /dev/null @@ -1,56 +0,0 @@ -package fucoin.supervisor; - -import java.awt.GridLayout; -import java.awt.Label; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JScrollPane; -import javax.swing.JTable; - -import akka.japi.Creator; - -public class SuperVisorCreator implements Creator<SuperVisor>{ - - @Override - public SuperVisor create() throws Exception { - - JFrame frame = new JFrame("Server"); - frame.setLayout(new GridLayout(3, 2)); - frame.add(new Label("All Amounts:")); - AmountTableModel amountTableModel = new AmountTableModel(); - JTable amountListView = new JTable(amountTableModel); - frame.add(new JScrollPane(amountListView)); - frame.add(new Label("Average Amounts:")); - Label averageamountLbl = new Label("Average Amounts:"); - frame.add(averageamountLbl); - JButton updateBtn = new JButton("Update"); - JButton exitBtn = new JButton("exit"); - frame.add(updateBtn); - frame.add(exitBtn); - - SuperVisor sv = new SuperVisor(amountTableModel,averageamountLbl); - updateBtn.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - sv.updateValues(); - } - }); - exitBtn.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - sv.exit(); - frame.setVisible(false); - } - }); - frame.setSize(200, 400); - frame.setVisible(true); - - return sv; - } - -} diff --git a/src/fucoin/supervisor/SuperVisorFactory.java b/src/fucoin/supervisor/SuperVisorFactory.java new file mode 100644 index 0000000..166c957 --- /dev/null +++ b/src/fucoin/supervisor/SuperVisorFactory.java @@ -0,0 +1,49 @@ +package fucoin.supervisor; + +import akka.japi.Creator; + +import javax.swing.*; +import java.awt.*; + +/** + * Create SuperVisor with a AWT Window. + * The window displays the information from the supervisor. + */ +public class SuperVisorFactory implements Creator<SuperVisorImpl> { + + @Override + public SuperVisorImpl create() throws Exception { + //Show AWT window for runtime information + JFrame frame = new JFrame("Server"); + frame.setLayout(new GridLayout(3, 2)); + frame.add(new Label("All Amounts:")); + + //Init Amount Table and SuperVisorImpl + AmountTableModel amountTableModel = new AmountTableModel(); + SuperVisorImpl sv = new SuperVisorImpl(amountTableModel); + + JTable amountListView = new JTable(amountTableModel); + Label averageAmountLbl = new Label("Average Amounts:"); + frame.add(new JScrollPane(amountListView)); + frame.add(new Label("Average Amounts:")); + frame.add(averageAmountLbl); + + //Call update on supervisor + JButton updateBtn = new JButton("Update"); + updateBtn.addActionListener(e -> sv.updateValues()); + frame.add(updateBtn); + + //Exit Button and shutdown supervisor + JButton exitBtn = new JButton("exit"); + exitBtn.addActionListener(e -> { + sv.exit(); + frame.setVisible(false); + }); + frame.add(exitBtn); + frame.setSize(200, 400); + frame.setVisible(true); + + return sv; + } + +} diff --git a/src/fucoin/supervisor/SuperVisor.java b/src/fucoin/supervisor/SuperVisorImpl.java similarity index 82% rename from src/fucoin/supervisor/SuperVisor.java rename to src/fucoin/supervisor/SuperVisorImpl.java index 807af32..9e19e90 100644 --- a/src/fucoin/supervisor/SuperVisor.java +++ b/src/fucoin/supervisor/SuperVisorImpl.java @@ -1,33 +1,23 @@ package fucoin.supervisor; -import java.awt.Label; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Queue; -import java.util.concurrent.Semaphore; - -import akka.actor.ActorRef; import akka.actor.Props; -import fucoin.AbstractNode; +import fucoin.wallet.AbstractNode; import fucoin.actions.Action; -import fucoin.actions.ClientAction; import fucoin.actions.persist.ActionInvokeUpdate; import fucoin.actions.transaction.ActionGetAmountAnswer; import fucoin.actions.transaction.SuperVisorAction; -public class SuperVisor extends AbstractNode { - +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +public class SuperVisorImpl extends AbstractNode { private AmountTableModel amountTableModel; - private Label averageamountLbl; - public SuperVisor(AmountTableModel amountTableModel, Label averageamountLbl) { + public SuperVisorImpl(AmountTableModel amountTableModel) { this.amountTableModel = amountTableModel; - this.averageamountLbl = averageamountLbl; } @Override @@ -43,11 +33,10 @@ public class SuperVisor extends AbstractNode { } } - Semaphore mutex = new Semaphore(1); private Map<Long, DistributedCommitedTransferRequest> requestQueue; public static Props props() { - return Props.create(SuperVisor.class, new SuperVisorCreator()); + return Props.create(SuperVisorImpl.class, new SuperVisorFactory()); } public void updateValues() { @@ -65,7 +54,7 @@ public class SuperVisor extends AbstractNode { public void addDistributedCommitedTransferRequest( DistributedCommitedTransferRequest request) { - System.out.println("Füge Request in queue ein: " + request.getId()); + System.out.println("Add request to queue: " + request.getId()); requestQueue.put(request.getId(), request); } diff --git a/src/fucoin/wallet/AbstractNode.java b/src/fucoin/wallet/AbstractNode.java new file mode 100644 index 0000000..2a6d51e --- /dev/null +++ b/src/fucoin/wallet/AbstractNode.java @@ -0,0 +1,71 @@ +package fucoin.wallet; + +import akka.actor.ActorRef; +import akka.actor.UntypedActor; + +import java.io.Serializable; +import java.util.HashMap; + +public abstract class AbstractNode extends UntypedActor implements Serializable { + + /** + * Returns the akka-style address as String, + * which could be converted to an ActorRef object later + * + * @return + */ + public String getAddress() { + return getAddress(getSelf()); + } + + public String getAddress(ActorRef self) { + return self.path().toSerializationFormatWithAddress(self.path().address()); + } + + /** + * The which receives Action objects + * + * @param message + */ + public abstract void onReceive(Object message); + + /** + * Holds references to neighbors that were in contact with this wallet during runtime; + * The key corresponds to the WalletImpl's name + */ + private transient HashMap<String, ActorRef> knownNeighbors = new HashMap<String, ActorRef>(); + + /** + * Holds references to neighbors + * this wallet synchronizes itself to (the WalletImpl object); + * The key corresponds to the WalletImpl's name + */ + public transient HashMap<String, ActorRef> localNeighbors = new HashMap<String, ActorRef>(); + + /** + * Holds all Wallets from network participants + * which synchronize their state (WalletImpl object) with us; + * The key corresponds to the WalletImpl's name + */ + public transient HashMap<String, AbstractWallet> backedUpNeighbors = + new HashMap<String, AbstractWallet>(); + + public transient HashMap<ActorRef, Integer> amounts = new HashMap<ActorRef, Integer>(); + + public boolean addKnownNeighbor(String key, ActorRef value) { + if (!knownNeighbors.containsKey(key)) { + knownNeighbors.put(key, value); + return true; + } + return false; + } + + public HashMap<String, ActorRef> getKnownNeighbors() { + return knownNeighbors; + } + + public void log(String string) { + System.out.println(getSelf().path().name() + ": " + string); + } + +} \ No newline at end of file diff --git a/src/fucoin/wallet/AbstractWallet.java b/src/fucoin/wallet/AbstractWallet.java new file mode 100644 index 0000000..2e0da3b --- /dev/null +++ b/src/fucoin/wallet/AbstractWallet.java @@ -0,0 +1,68 @@ +package fucoin.wallet; + +import akka.actor.ActorRef; + +public abstract class AbstractWallet extends AbstractNode { + /** + * The amount this wallet currently holds + */ + protected int amount; + + /** + * The name of this wallet (does never change, no duplicates in network assumed) + */ + public final String name; + + /** + * Init. a wallet with a name. + * + * @param name Name of the Wallet + */ + public AbstractWallet(String name) { + this.name = name; + } + + /** + * Returns the name of the wallet + * + * @return Name of the wallet. + */ + public String getName() { + return this.name; + } + + /** + * Performs housekeeping operations, e.g. pushes + * backedUpNeighbor-entries to other neighbors + */ + public abstract void leave(); + + /** + * Returns the current amount of the wallet + * + * @return amount of the wallet + */ + public int getAmount() { + return amount; + } + + /** + * Set new Amount of the wallet. + * @param amount New amount of the wallet + */ + public abstract void setAmount(int amount); + + /** + * Add amount to current amount. + * @param amount value to add to current account. + */ + public abstract void addAmount(int amount); + + public abstract void setActive(boolean isActive); + + public abstract ActorRef getPreknownNeighbour(); + + public abstract ActorRef getRemoteSuperVisorActor(); + + public abstract void logTransaction(String msg); +} diff --git a/src/fucoin/wallet/Wallet.java b/src/fucoin/wallet/Wallet.java new file mode 100644 index 0000000..78d1870 --- /dev/null +++ b/src/fucoin/wallet/Wallet.java @@ -0,0 +1,14 @@ +package fucoin.wallet; + +import fucoin.gui.WalletController; + + +public interface Wallet extends WalletController { + //Vector<WalletPointer> join(); + void storeOrUpdate(WalletImpl w); + + void invalidateWallet(WalletImpl w); + + void receiveTransaction(int amount); + //Vector<WalletPointer> searchWallet(String adress); +} diff --git a/src/fucoin/wallet/WalletFactory.java b/src/fucoin/wallet/WalletFactory.java new file mode 100644 index 0000000..d635272 --- /dev/null +++ b/src/fucoin/wallet/WalletFactory.java @@ -0,0 +1,31 @@ +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.java b/src/fucoin/wallet/WalletImpl.java similarity index 86% rename from src/fucoin/Wallet.java rename to src/fucoin/wallet/WalletImpl.java index 9aa7c79..30d80ad 100644 --- a/src/fucoin/Wallet.java +++ b/src/fucoin/wallet/WalletImpl.java @@ -1,27 +1,26 @@ -package fucoin; +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.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; +import fucoin.gui.WalletController; +import fucoin.gui.WalletGuiController; -public class Wallet extends AbstractWallet implements IWalletControle { +public class WalletImpl extends AbstractWallet implements WalletController { private ActorRef preknownNeighbour; private ActorRef remoteSuperVisorActor; - private IWalletGuiControle gui; + private WalletGuiController gui; private String preknownNeighbourName; private boolean isActive; - public Wallet(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { + public WalletImpl(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { super(walletName); this.preknownNeighbourName = preknownNeighbourName; this.preknownNeighbour = preknownNeighbour; @@ -88,19 +87,19 @@ public class Wallet extends AbstractWallet implements IWalletControle { public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { - return Props.create(Wallet.class, new WalletCreator(preknownNeighbour, preknownNeighbourName, walletName, remoteSuperVisorActor)); + return Props.create(WalletImpl.class, new WalletFactory(preknownNeighbour, preknownNeighbourName, walletName, remoteSuperVisorActor)); } @Override public boolean equals(Object obj) { - if (obj instanceof Wallet) { - Wallet wobj = (Wallet) obj; + if (obj instanceof WalletImpl) { + WalletImpl wobj = (WalletImpl) obj; return amount == wobj.amount && name.equals(wobj.name); } return false; } - public void setGui(IWalletGuiControle gui) { + public void setGui(WalletGuiController gui) { this.gui = gui; } @@ -126,7 +125,7 @@ public class Wallet extends AbstractWallet implements IWalletControle { return remoteSuperVisorActor; } - public IWalletGuiControle getGui() { + public WalletGuiController getGui() { return gui; } @@ -140,7 +139,7 @@ public class Wallet extends AbstractWallet implements IWalletControle { @Override public boolean addKnownNeighbor(String key, ActorRef value) { - System.out.println(key + " is newNeighbor of "+name+"?" + !getKnownNeighbors().containsKey(key)); + System.out.println(key + " is newNeighbor of " + name + "?" + !getKnownNeighbors().containsKey(key)); if (getKnownNeighbors().containsKey(key) || key.equals(name)) { return false; } @@ -173,7 +172,7 @@ public class Wallet extends AbstractWallet implements IWalletControle { } } - public void logTransaction(String msg){ + public void logTransaction(String msg) { if (gui != null) { gui.addTransactionLogMessage(msg); } else { -- GitLab