diff --git a/JavaAkkaFuCoin/src/akkateststuff/Main.java b/JavaAkkaFuCoin/src/akkateststuff/Main.java deleted file mode 100644 index a07272a1098f4c092ec0a69d5b8382684d8585ea..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/akkateststuff/Main.java +++ /dev/null @@ -1,11 +0,0 @@ -package akkateststuff; -import akka.actor.*; - -public class Main { -public static void main(String[] args) { - final ActorSystem system = ActorSystem.create("MySystem"); - final ActorRef myActor42 = system.actorOf(Node.props(42),"42"); - final ActorRef myActor43 = system.actorOf(Node.props(43),"43"); - myActor42.tell("test", myActor43); -} -} diff --git a/JavaAkkaFuCoin/src/akkateststuff/MyNodeCreator.java b/JavaAkkaFuCoin/src/akkateststuff/MyNodeCreator.java deleted file mode 100644 index c20b97fdb1dd0c17add6a241f8634e1b39c0fe41..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/akkateststuff/MyNodeCreator.java +++ /dev/null @@ -1,18 +0,0 @@ -package akkateststuff; -import akka.japi.Creator; - - -public class MyNodeCreator implements Creator<Node>{ - private static final long serialVersionUID = 1L; - private int magicNumber; - - public MyNodeCreator(int magicNumber) { - this.magicNumber=magicNumber; - } - - @Override - public Node create() throws Exception { - return new Node(magicNumber); - } - -} diff --git a/JavaAkkaFuCoin/src/akkateststuff/Node.java b/JavaAkkaFuCoin/src/akkateststuff/Node.java deleted file mode 100644 index 730d92b28fd2bb3a17cf65540615999eb8d78f2b..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/akkateststuff/Node.java +++ /dev/null @@ -1,23 +0,0 @@ -package akkateststuff; -import akka.actor.*; -import akka.japi.Creator; - -public class Node extends UntypedActor { - - private int magicnumber; - - public Node(int magicnumber) { - this.magicnumber = magicnumber; - } - - @Override - public void onReceive(Object arg0) throws Exception { - System.out.println(magicnumber+" said \""+arg0+"\" to me"); - } - public static int magicNumber=0; - - public static Props props(final int magicNumber) { - Node.magicNumber=magicNumber; - return Props.create(new MyNodeCreator(magicNumber)); - } -} diff --git a/JavaAkkaFuCoin/src/fucoin/AbstractWallet.java b/JavaAkkaFuCoin/src/fucoin/AbstractWallet.java new file mode 100644 index 0000000000000000000000000000000000000000..c7bd8d83bcc53e58bc1dad8bd363b170f6f24bd7 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/AbstractWallet.java @@ -0,0 +1,131 @@ +package fucoin; + +import java.io.Serializable; +import java.util.HashMap; + +import akka.actor.ActorRef; +import akka.actor.UntypedActor; + +public abstract class AbstractWallet extends UntypedActor implements Serializable { + + // Used to join the network (a pre known participant/Wallet must be known) + public static class ActionJoin implements Serializable {} + + // Returns some neighbors that might be used as known + // and/or local neighbors + public class ActionJoinAnswer implements Serializable { + public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); + } + + // Used to push the state of my/a wallet to another participant + public static class ActionStoreOrUpdate implements Serializable { + public final AbstractWallet w; + public ActionStoreOrUpdate(AbstractWallet w) { + this.w = w; + } + } + + // May be used to delete a stored Wallet on another participant + public static class ActionInvalidate implements Serializable { + final String name; + public ActionInvalidate(String name) { + this.name = name; + } + } + + // Used to send (positive amount) or retreive money (negative amount) + public static class ActionReceiveTransaction implements Serializable { + final public int amount; + public ActionReceiveTransaction(int amount) { + this.amount = amount; + } + } + + // Used to search a Wallet by name, i.e. when we want to + // perform a transaction on it + public static class ActionSearchWalletReference implements Serializable { + final String name; + final long ttl; + public ActionSearchWalletReference(String name, long ttl) { + this.name = name; + this.ttl=ttl; + } + } + + // Used to return a Wallet reference (akka-style string which can + // be transformed to an ActorRef) + public static class ActionSearchWalletReferenceAnswer implements Serializable { + final String address; + final String name; + public ActionSearchWalletReferenceAnswer(String name,String address) { + this.address = address; + this.name=name; + } + } + + // 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, + // 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 static class ActionSearchMyWallet implements Serializable { + final String name; + public ActionSearchMyWallet(String name) { + this.name = name; + } + } + + // Used to return a searched Wallet + public static class ActionSearchMyWalletAnswer implements Serializable { + final AbstractWallet w; + public ActionSearchMyWalletAnswer(AbstractWallet w) { + this.w = w; + } + } + + // 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; + } + + // Returns the akka-style address as String, which + // could be converted to an ActorRef object later + public abstract String getAddress(); + + // Performs housekeeping operations, e.g. pushes + // backedUpNeighbor-entries to other neighbors + public abstract void leave(); + + // 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 + public 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>(); + + // The name of this wallet (does never change, no + // duplicates in network assumed) + public final String name; + + // The amount this wallet currently holds + public int amount; + +} \ No newline at end of file diff --git a/JavaAkkaFuCoin/src/fucoin/IWallet.java b/JavaAkkaFuCoin/src/fucoin/IWallet.java index f1d96a10c113b8a1de4bc27dbc0da472de3ca246..54ec9b3291fdd832ffdd6c67411d10e2bfb80562 100644 --- a/JavaAkkaFuCoin/src/fucoin/IWallet.java +++ b/JavaAkkaFuCoin/src/fucoin/IWallet.java @@ -5,9 +5,9 @@ import fucoin.gui.IWalletControle; public interface IWallet extends IWalletControle{ - Vector<WalletPointer> join(); + //Vector<WalletPointer> join(); void storeOrUpdate(Wallet w); void invalidateWallet(Wallet w); void receiveTransaction(int amount); - Vector<WalletPointer> searchWallet(String adress); + //Vector<WalletPointer> searchWallet(String adress); } diff --git a/JavaAkkaFuCoin/src/fucoin/Main.java b/JavaAkkaFuCoin/src/fucoin/Main.java index 7ac9d2621f7a41ac0aa3e70f1bf7e47348576bfb..c131f6c8f5cb4f717799833d6b62c2df4043c034 100644 --- a/JavaAkkaFuCoin/src/fucoin/Main.java +++ b/JavaAkkaFuCoin/src/fucoin/Main.java @@ -1,79 +1,76 @@ package fucoin; -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.time.Duration; -import java.util.concurrent.Executors; +import java.io.File; +import java.util.ArrayList; +import java.util.List; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JTextField; - -import fucoin.actions.JoinActionRespond; import akka.actor.ActorRef; import akka.actor.ActorSystem; -import akka.dispatch.Dispatcher; -import akka.dispatch.ExecutorServiceFactoryProvider; -import akka.dispatch.MessageDispatcherConfigurator; -import akkateststuff.Node; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; + +import fucoin.actions.ActionInvokeLeave; +import fucoin.actions.ActionInvokeRevive; +import fucoin.actions.ActionInvokeSentMoney; +import fucoin.actions.ActionInvokeUpdate; +import fucoin.supervisor.SuperVisor; + public class Main { - private static Main singleton; - public static Main getSingleton() { - return singleton; - } - - public final ActorSystem system; - - public ActorSystem getSystem() { - return system; - } - - public Main() { - this.singleton=this; - system = ActorSystem.create("MySystem"); - system.actorOf(Wallet.props(null,"Main"),"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"); + List<ActorRef> activeActors = new ArrayList<ActorRef>(); + ActorRef a1 = system.actorOf(Wallet.props(null,"","Main",superVisorActor),"Main"); + //ActorRef a2 = system.actorOf(Wallet.props(a1,"Main","Main2",superVisorActor),"Main2"); - } - public void start(){ - //43 tells 42 join - } - static int walletCounter = 0; - public static void main(String[] args) { - Main main = new Main(); - main.start(); - 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(""+walletCounter); - 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);*/ - getSingleton().getSystem().actorOf(Wallet.props(input.getText(),name.getText()),name.getText()); - } - }); + //activeActors.add(a1); + int maxrounds = 1000; + int maxactors = 100; + 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>>(); - frame.add(button); - frame.setSize(300, 300); - frame.setVisible(true); + 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()*10), (int) (Math.round(Math.random()*100)-50)), actor); + } + if(Math.random()<0.2){ + removedActors.add(actor); + int offtime = timestep+(int)(Math.random()*6)+2; + + offline.get(Math.min(offtime, maxrounds)).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); } - } diff --git a/JavaAkkaFuCoin/src/fucoin/MainRemote.java b/JavaAkkaFuCoin/src/fucoin/MainRemote.java new file mode 100644 index 0000000000000000000000000000000000000000..309bae2951caf6bd4b66df846f58ea8eec7bb4e2 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/MainRemote.java @@ -0,0 +1,32 @@ +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; + +public class MainRemote { + public static ActorRef remoteSuperVisorActor; + + 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("Test", config); + + 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(ActorPath.isValidPathElement(""+address+"/user/Main")); + ActorRef a1 = system.actorOf(Wallet.props(null,"","Main2",remoteSuperVisorActor),"Main2"); + + } +} diff --git a/JavaAkkaFuCoin/src/fucoin/Wallet.java b/JavaAkkaFuCoin/src/fucoin/Wallet.java index a29e482e13603c6c9544dd61d46abf6996d5a0a7..0d558765d4cbf57fc8f084f739add26dcdd07638 100644 --- a/JavaAkkaFuCoin/src/fucoin/Wallet.java +++ b/JavaAkkaFuCoin/src/fucoin/Wallet.java @@ -1,282 +1,262 @@ package fucoin; -import java.util.Collections; -import java.util.Vector; -import javax.swing.DefaultListModel; +import java.util.Map.Entry; +import java.util.concurrent.Semaphore; -import fucoin.actions.FindWalletAction; -import fucoin.actions.FindWalletResponseAction; -import fucoin.actions.InvalidateAction; -import fucoin.actions.InvokeExitAction; -import fucoin.actions.InvokeFindWalletAction; -import fucoin.actions.InvokePerformTransactionAction; -import fucoin.actions.PerformTransactionAction; -import fucoin.actions.StoreOrUpdateAction; -import fucoin.actions.JoinAction; -import fucoin.actions.JoinActionRespond; -import fucoin.actions.InvokeStoreOrUpdateAction; -import fucoin.actions.TransactionPerformedAction; -import fucoin.actions.WaitForAnswersAction; -import fucoin.actions.WaitForPerformTransactionAction; -import fucoin.gui.IWalletGuiControle; -import akka.actor.ActorPath; import akka.actor.ActorRef; -import akka.actor.ActorSelection; -import akka.actor.ActorSystem; -import akka.actor.ActorSystemImpl; import akka.actor.Props; -import akka.actor.UntypedActor; -import akkateststuff.MyNodeCreator; -import akkateststuff.Node; - - -public class Wallet extends UntypedActor implements IWallet{ +import fucoin.actions.ActionGetAmount; +import fucoin.actions.ActionGetAmountAnswer; +import fucoin.actions.ActionInvokeLeave; +import fucoin.actions.ActionInvokeRevive; +import fucoin.actions.ActionInvokeSentMoney; +import fucoin.actions.ActionInvokeSentMoney2; +import fucoin.gui.IWalletControle; +import fucoin.gui.IWalletGuiControle; - private ActorRef preknownNeighbour; - private String address ; - private int moneyAmount ; - private Vector<WalletPointer> allKnownNeighbors = new Vector<WalletPointer>(); - private Vector<Wallet> synchronizedNeighbors = new Vector<Wallet>(); +public class Wallet extends AbstractWallet implements IWalletControle{ + private ActorRef preknownNeighbour; + private ActorRef remoteSuperVisorActor; + private IWalletGuiControle gui; + private String preknownNeighbourName; + private boolean isActive; - public Wallet(String preknownNeighbour) { - - if(preknownNeighbour!=null){ - this.preknownNeighbour=stringToActor(preknownNeighbour); - } + public Wallet(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { + super(walletName); + this.preknownNeighbourName=preknownNeighbourName; + this.preknownNeighbour=preknownNeighbour; + this.remoteSuperVisorActor=remoteSuperVisorActor; } - + @Override - public Vector<WalletPointer> join() { - Vector<WalletPointer> allKnownNeighbors = new Vector<WalletPointer>(); - for(WalletPointer neighbors : this.allKnownNeighbors){ - allKnownNeighbors.add(neighbors); - } - return allKnownNeighbors; + public String getAddress() { + return getAddress(getSelf()); } - @Override - public void storeOrUpdate(Wallet w) { - if(!synchronizedNeighbors.contains(w)){ - synchronizedNeighbors.add(w); - }; + private String getAddress(ActorRef self) { + return self.path().toSerializationFormatWithAddress(self.path().address()); } - @Override - public void invalidateWallet(Wallet w) { - synchronizedNeighbors.remove(w); + public void send(String name, int amount){ + //System.out.println("search wallet"+name+" in "+knownNeighbors.keySet()); + if(knownNeighbors.containsKey(name)){ + addAmount(-amount); + knownNeighbors.get(name).tell(new ActionReceiveTransaction(amount), getSelf()); + }else{ + for(ActorRef neighbor : knownNeighbors.values()){ + neighbor.tell(new ActionSearchWalletReference(name,System.currentTimeMillis()+10), getSelf()); + } + + try { + getContext().unwatch(getSelf()); + Thread.sleep(200); + getContext().watch(getSelf()); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + //getContext().unwatch(getSelf()); + getSelf().tell(new ActionInvokeSentMoney(name, amount), getSelf()); + + } + } + - @Override - public void receiveTransaction(int moneyAmount) { - addMoneyAmount(moneyAmount); + private void addAmount(int amount) { + setAmount(this.amount+amount); + log(" My amount is now "+this.amount); + if(gui!=null){ + gui.setAmount(this.amount); + } } - private Vector<WalletPointer> answers = new Vector<WalletPointer>(); + @Override - public Vector<WalletPointer> searchWallet(String adress) { - for(WalletPointer neighbor : allKnownNeighbors){ - if(!neighbor.address.equals(adress)){ - ActorRef actor = stringToActor(neighbor.address); - actor.tell(new FindWalletAction(adress), getSelf()); - + public void leave() { + for(ActorRef neighbor : knownNeighbors.values()){ + if(getSelf().compareTo(neighbor)!=0){ + neighbor.tell(new ActionStoreOrUpdate(this), getSelf()); } } - //unschoener workaround - getSelf().tell(new WaitForAnswersAction(), getSelf()); - return answers; + remoteSuperVisorActor.tell(new ActionStoreOrUpdate(this), getSelf()); + isActive=false; + backedUpNeighbors.clear(); + knownNeighbors.clear(); + knownNeighbors.put(preknownNeighbourName,preknownNeighbour); } - - private boolean isAlive = false; - private IWalletGuiControle gui; - void performTransaction ( WalletPointer w, int amount ){ - - ActorRef destActor = stringToActor(w.address); - destActor.tell(new PerformTransactionAction(w,amount), getSelf()); - getSelf().tell(new WaitForPerformTransactionAction(w,amount), getSelf()); - //.tell(new Perform<transaction>, sender); - } - + @Override - public void onReceive(Object action) throws Exception { - log(""+action); - String senderName = actorToString(getSender()); - String selfName = actorToString(getSelf()); - - if(action instanceof JoinAction){ - JoinAction joinAction = (JoinAction) action; - JoinActionRespond joinactionrespond = new JoinActionRespond(); - joinactionrespond.setKnownNeighbors(join()); - addKnownaddress(joinAction.getNewMember()); - + public void onReceive(Object message) { + log(getSender().path().name()+" invokes "+getSelf().path().name()+" to do "+message.getClass().getSimpleName()); + if(message instanceof ActionInvokeRevive){ + isActive=true; - getSender().tell(joinactionrespond, getSelf()); - }else if(action instanceof JoinActionRespond){ - JoinActionRespond joinaction = (JoinActionRespond) action; + preknownNeighbour.tell(new ActionJoin(), getSelf()); - //allKnownNeighbors.addAll(joinaction.getAllKnownNeighbors()); - for(WalletPointer neighbor:joinaction.getAllKnownNeighbors()){ - addKnownaddress(neighbor); - } - log(actorToString(getSelf())+"ah there are new neighbours"+joinaction.getAllKnownNeighbors()); - log("now i know these "+allKnownNeighbors); - getSelf().tell(new InvokeFindWalletAction(), getSelf()); - }else if(action instanceof StoreOrUpdateAction){ - StoreOrUpdateAction storeOrUpdateAction = (StoreOrUpdateAction) action; - storeOrUpdate(storeOrUpdateAction.getWallet()); - }else if(action instanceof InvokeStoreOrUpdateAction){ - StoreOrUpdateAction storeOrUpdateAction = new StoreOrUpdateAction(); - storeOrUpdateAction.setWallet(this); - for(WalletPointer neighbor : allKnownNeighbors){ - stringToActor(neighbor.address).tell(storeOrUpdateAction, getSelf()); + } + if(!isActive)return; + //System.out.println(message); + if(message instanceof ActionJoin){ + ActionJoinAnswer aja = new ActionJoinAnswer(); + aja.someNeighbors.putAll(knownNeighbors); + getSender().tell(aja, getSelf()); + }else if(message instanceof ActionJoinAnswer){ + ActionJoinAnswer aja = (ActionJoinAnswer) message; + for(Entry<String, ActorRef> neighbor : knownNeighbors.entrySet()){ + addKnownNeighbor(neighbor.getKey(),neighbor.getValue()); + neighbor.getValue().tell(new ActionSearchMyWallet(name), getSelf()); } - }else if(action instanceof InvokeExitAction){ - getContext().stop(getSelf()); - }else if(action instanceof InvokeFindWalletAction){ - InvokeFindWalletAction invokeFindWalletAction = (InvokeFindWalletAction) action; - Vector<WalletPointer> wallets = searchWallet(selfName); - - }else if(action instanceof FindWalletAction){ - FindWalletAction findWalletAction = (FindWalletAction) action; - for(Wallet s : synchronizedNeighbors){ - log(findWalletAction.getSearchedName()); - log(s.address); - if(s.address.equals(findWalletAction.getSearchedName())){ - log(""+s.moneyAmount); - getSender().tell(new FindWalletResponseAction(selfName,s.moneyAmount), getSelf()); - } - } - }else if(action instanceof FindWalletResponseAction){ - FindWalletResponseAction findWalletResponseAction = (FindWalletResponseAction) action; - answers.add(new WalletPointer(findWalletResponseAction.getFoundneighbour())); + }else if(message instanceof ActionSearchMyWallet){ + ActionSearchMyWallet asmw = (ActionSearchMyWallet) message; + //If I know that somebody is searching himself, + //I can store him under the searched wallet name + addKnownNeighbor(asmw.name, getSender()); - moneyAmount=findWalletResponseAction.getMoneyAmount(); - gui.setAmount(moneyAmount); - }else if(action instanceof WaitForAnswersAction){ - log(""+answers); - for(WalletPointer answer : answers){ - addKnownaddress(answer); - stringToActor(answer.address).tell(new InvalidateAction(actorToString(getSelf())),getSelf()); + AbstractWallet storedWallet = backedUpNeighbors.get(asmw.name); + log(" "+knownNeighbors); + if(storedWallet!=null){ + getSender().tell(new ActionSearchMyWalletAnswer(storedWallet), getSelf()); } - }else if(action instanceof InvalidateAction){ - InvalidateAction invalidateAction = (InvalidateAction) action; - - Wallet delWallet = null; - - for(Wallet w : synchronizedNeighbors){ - if(w.address.equals(invalidateAction.getInvalidWallet())){ - delWallet=w; - } + }else if(message instanceof ActionSearchMyWalletAnswer){ + ActionSearchMyWalletAnswer asmwa = (ActionSearchMyWalletAnswer) message; + setAmount(asmwa.w.amount); + getSender().tell(new ActionInvalidate(name), getSelf()); + }else if(message instanceof ActionInvalidate){ + ActionInvalidate ai = (ActionInvalidate) message; + backedUpNeighbors.remove(ai.name); + }else if(message instanceof ActionSearchWalletReference){ + ActionSearchWalletReference aswr = (ActionSearchWalletReference) message; + System.out.println("I search for you"+aswr.name); + if(this.name.equals(aswr.name)){ + getSender().tell(new ActionSearchWalletReferenceAnswer(aswr.name,getAddress()),getSelf()); + }else if(backedUpNeighbors.containsKey(aswr.name)){ + getSender().tell(new ActionSearchWalletReferenceAnswer(aswr.name,backedUpNeighbors.get(aswr.name).getAddress()),getSelf()); + } else if(knownNeighbors.containsKey(aswr.name)){ + getSender().tell(new ActionSearchWalletReferenceAnswer(aswr.name,getAddress(knownNeighbors.get(aswr.name))),getSelf()); + } else if (System.currentTimeMillis()<aswr.ttl){ + //for(ActorRef actor : knownNeighbors.values()){ + // actor.tell(aswr,getSelf()); + //} + //Because Sender is maybe unknown + //getSender().tell(aswr, getSelf()); } - log(""+synchronizedNeighbors); - invalidateWallet(delWallet); - log(""+synchronizedNeighbors); - }else if(action instanceof InvokePerformTransactionAction){ - InvokePerformTransactionAction invokePerformTransactionAction = (InvokePerformTransactionAction) action; - performTransaction(invokePerformTransactionAction.getWalletPointer(),invokePerformTransactionAction.getAmount()); - addMoneyAmount(-invokePerformTransactionAction.getAmount()); - }else if(action instanceof PerformTransactionAction){ - PerformTransactionAction performTransactionAction = (PerformTransactionAction) action; - if(performTransactionAction.getWalletPointer().address.equals(actorToString(getSelf()))){ - getSender().tell(new TransactionPerformedAction(), getSelf()); - receiveTransaction(performTransactionAction.getAmount()); - }else{ - for(Wallet syncwallet : synchronizedNeighbors){ - if(syncwallet.address.equals(performTransactionAction.getWalletPointer().address)){ - syncwallet.addMoneyAmount(performTransactionAction.getAmount()); - } - } + }else if(message instanceof ActionSearchWalletReferenceAnswer){ + ActionSearchWalletReferenceAnswer aswra = (ActionSearchWalletReferenceAnswer) message; + ActorRef target = getContext().actorSelection(aswra.address).anchor(); + addKnownNeighbor(aswra.name,target); + }else if(message instanceof ActionInvokeSentMoney){ + ActionInvokeSentMoney aism = (ActionInvokeSentMoney) message; + + send(aism.name, aism.amount); + }else if(message instanceof ActionInvokeSentMoney2){ + if(knownNeighbors.containsKey(name)){ + addAmount(-amount); + knownNeighbors.get(name).tell(new ActionReceiveTransaction(amount), getSelf()); } - }else if(action instanceof WaitForPerformTransactionAction){ - WaitForPerformTransactionAction waitForPerformTransactionAction = (WaitForPerformTransactionAction) action; - if(!isAlive){ - for(Wallet neighbor : synchronizedNeighbors){ - if(neighbor.address.equals(waitForPerformTransactionAction.getWalletPointer().address)){ - neighbor.addMoneyAmount(waitForPerformTransactionAction.getAmount()); - log("have to update "+neighbor.address+"amount because he is offline"); - } - - } - + }else if(message instanceof ActionReceiveTransaction){ + ActionReceiveTransaction art = (ActionReceiveTransaction) message; + System.out.println(message.getClass().getSimpleName()+" "+art.amount); + addAmount(art.amount); + }else if(message instanceof ActionStoreOrUpdate){ + ActionStoreOrUpdate asou = (ActionStoreOrUpdate) message; + backedUpNeighbors.put(asou.w.name, asou.w); + }else if(message instanceof ActionGetAmount){ + ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(getAddress(),getName(),amount); + getSender().tell(agaa, getSelf()); + }else if(message instanceof ActionInvokeLeave){ + leave(); + }else if(message instanceof ActionInvokeRevive){ + + }else{ + unhandled(message); + System.err.println("Unexpected Error: "+message+" not handeld"); + } + } + + private void addKnownNeighbor(String key, ActorRef value) { + if(!knownNeighbors.containsKey(key)){ + System.out.println(knownNeighbors.keySet()+" does not contain "+key); + knownNeighbors.put(key,value); + if(gui!=null){ + gui.addKnownAddress(key); } - }else if(action instanceof TransactionPerformedAction){ - isAlive=true; + System.out.println(key+"-->"+value); } } - private void addKnownaddress(WalletPointer senderPointer) { - if(!allKnownNeighbors.contains(senderPointer) - &&getSender().compareTo(getSelf())!=0){ - - - this.allKnownNeighbors.add(senderPointer); - gui.addKnownAddress(senderPointer.address); - } + private void log(String string) { + System.out.println(getSelf()+": "+string); } - + @Override public void preStart() throws Exception { - super.preStart(); - log("prestart"+preknownNeighbour); - this.address=actorToString(getSelf()); - gui.setAddress(address); - addMoneyAmount(100); + isActive=true; + if(gui!=null){ + gui.setAddress(getAddress()); + } + String path = "akka.tcp://Core@127.0.0.1:1234/user/Main"; + System.out.println(getContext().provider().getExternalAddressFor(getSelf().path().address())); + //log("my address should be "+getAddress()); + //log(""+preknownNeighbour); + //knownNeighbors.put(getName(),getSelf()); + + System.out.println(knownNeighbors); if(preknownNeighbour!=null){ - String p = actorToString(preknownNeighbour); - addKnownaddress(new WalletPointer(p)); - preknownNeighbour.tell(new JoinAction(new WalletPointer(address)), getSelf()); + knownNeighbors.put(preknownNeighbourName,preknownNeighbour); + preknownNeighbour.tell(new ActionJoin(), getSelf()); + ActionJoinAnswer aja = new ActionJoinAnswer(); + aja.someNeighbors.putAll(knownNeighbors); + preknownNeighbour.tell(aja, getSelf()); + } + setAmount(100); + remoteSuperVisorActor.tell(new ActionJoin(), getSelf()); } - private void log(String string) { - String logMsg = actorToString(getSender()).replace("akka://MySystem/user/", "")+" said "+ string+" to me("+actorToString(getSelf()).replace("akka://MySystem/user/", "")+")"; - System.out.println(logMsg); - gui.addLogMsg(string); + @Override + public void postStop() throws Exception { + leave(); + super.postStop(); } - - public static Props props(String preknownNeighbour, String walletCounter) { - return Props.create(new WalletCreator(preknownNeighbour,walletCounter)); - } - public static String actorToString(ActorRef actor){ - return actor.path().toStringWithAddress(actor.path().address()); - } - public static ActorRef stringToActor(String path){ - return Main.getSingleton().getSystem().actorFor(path); + public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { + return Props.create(Wallet.class,new WalletCreator(preknownNeighbour,preknownNeighbourName,walletName,remoteSuperVisorActor)); } - public void addMoneyAmount(int moneyAmount) { - this.moneyAmount += moneyAmount; - this.gui.setAmount(this.moneyAmount); - } - @Override - public String toString() { - return address+"'s wallet"; - } - - public void invokePerformTransaction(WalletPointer walletPointer, int i) { - getSelf().tell(new InvokePerformTransactionAction(walletPointer, i),getSelf()); - } - @Override - public void send(String address, int amount) { - invokePerformTransaction(new WalletPointer(address), amount); - } - - public void store() { - getSelf().tell(new InvokeStoreOrUpdateAction(), getSelf()); - } - - public void leave() { - store(); - getSelf().tell(new InvokeExitAction(), getSelf()); + public boolean equals(Object obj) { + if(obj instanceof Wallet){ + Wallet wobj = (Wallet) obj; + return amount==wobj.amount&&name.equals(wobj.name); + } + return false; } public void setGui(IWalletGuiControle gui) { this.gui=gui; } + + Semaphore mutex = new Semaphore(1); + public void setAmount(int amount){ + try { + mutex.acquire(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + this.amount = amount; + if(remoteSuperVisorActor != null){ + remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf()); + } + if(gui!=null){ + gui.setAmount(this.amount); + } + mutex.release(); + } } diff --git a/JavaAkkaFuCoin/src/fucoin/WalletCreator.java b/JavaAkkaFuCoin/src/fucoin/WalletCreator.java index 19852aa1d5f70e5f6a351f1653d97f476d75ae71..0193d09bfc26a8f5cfb0ad2a50b89852090375c1 100644 --- a/JavaAkkaFuCoin/src/fucoin/WalletCreator.java +++ b/JavaAkkaFuCoin/src/fucoin/WalletCreator.java @@ -1,85 +1,34 @@ package fucoin; -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JList; -import javax.swing.JPanel; -import javax.swing.JTextField; - -import fucoin.actions.InvokePerformTransactionAction; +import akka.actor.ActorRef; +import akka.japi.Creator; import fucoin.gui.IWalletGuiControle; import fucoin.gui.WalletGui; -import akka.japi.Creator; -import akkateststuff.Node; +public class WalletCreator implements Creator<Wallet> { -public class WalletCreator implements Creator<Wallet>{ + private ActorRef preknownNeighbour; + private String walletName; + private ActorRef remoteSuperVisorActor; + private String preknownNeighbourName; - private String preknownNeighbour; - private String walletCounter; - - public WalletCreator(String preknownNeighbour, String walletCounter) { + public WalletCreator(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { this.preknownNeighbour=preknownNeighbour; - this.walletCounter=walletCounter; + this.preknownNeighbourName=preknownNeighbourName; + this.walletName=walletName; + this.remoteSuperVisorActor=remoteSuperVisorActor; + } @Override public Wallet create() throws Exception { + Wallet wallet = new Wallet(preknownNeighbour,preknownNeighbourName, walletName,remoteSuperVisorActor); - Wallet tempwallet = new Wallet(preknownNeighbour); - IWalletGuiControle gui = new WalletGui(tempwallet); - tempwallet.setGui(gui); - - - /* - Wallet tempwallet = new Wallet(preknownNeighbour,log); - JFrame frame = new JFrame("Wallet:"+walletCounter); - frame.setLayout(new GridLayout(3,1)); - - - JList<String> list = new JList<String>(log); - JTextField dest = new JTextField(); - JPanel panel = new JPanel(); - panel.setLayout(new GridLayout(3, 1)); - JButton storebutton = new JButton("Store"); - storebutton.addActionListener(new ActionListener() { - Wallet wallet = tempwallet; - @Override - public void actionPerformed(ActionEvent e) { - wallet.store(); - } - }); - JButton exitbutton = new JButton("Exit"); - exitbutton.addActionListener(new ActionListener() { - Wallet wallet = tempwallet; - @Override - public void actionPerformed(ActionEvent e) { - wallet.exit(); - frame.dispose(); - } - }); - JButton send10Btn = new JButton("Send10"); - send10Btn.addActionListener(new ActionListener() { - Wallet wallet = tempwallet; - @Override - public void actionPerformed(ActionEvent e) { - - wallet.invokePerformTransaction(new WalletPointer(dest.getText()), 10); - } - }); - panel.add(storebutton); - panel.add(exitbutton); - panel.add(send10Btn); - frame.add(list); - frame.add(dest); - frame.add(panel); - frame.setSize(300, 300); - frame.setVisible(true);*/ - return tempwallet; - } +// IWalletGuiControle gui = new WalletGui(wallet); +// wallet.setGui(gui); + return wallet; + } + + } diff --git a/JavaAkkaFuCoin/src/fucoin/WalletPointer.java b/JavaAkkaFuCoin/src/fucoin/WalletPointer.java deleted file mode 100644 index 44d50d99d4edbf9ea0bbfca801d63db655955097..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/WalletPointer.java +++ /dev/null @@ -1,29 +0,0 @@ -package fucoin; - -import akka.actor.ActorRef; - -public class WalletPointer { - public String address; - - public WalletPointer(String address) { - this.address=address; - } - - @Override - public String toString() { - ActorRef actor = Wallet.stringToActor(address); - return actor.path().toStringWithAddress(actor.path().address()); - } - - @Override - public boolean equals(Object obj) { - if(obj instanceof WalletPointer){ - return ((WalletPointer)obj).address.equals(address); - } - return false; - } - - - - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionGetAmount.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionGetAmount.java new file mode 100644 index 0000000000000000000000000000000000000000..9c486c8199c3aa36bf3847fd12e992e21b14010a --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionGetAmount.java @@ -0,0 +1,5 @@ +package fucoin.actions; + +public class ActionGetAmount { + +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionGetAmountAnswer.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionGetAmountAnswer.java new file mode 100644 index 0000000000000000000000000000000000000000..a7c53bdcb468f5477d79522a1e685367fad51e2d --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionGetAmountAnswer.java @@ -0,0 +1,15 @@ +package fucoin.actions; + +public class ActionGetAmountAnswer { + + public String address; + public String name; + public int amount; + + public ActionGetAmountAnswer(String address, String name, int amount) { + this.address=address; + this.name=name; + this.amount=amount; + } + +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeLeave.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeLeave.java new file mode 100644 index 0000000000000000000000000000000000000000..f30bb2385fc010e142bac65ee3d88ba3feb2acfa --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeLeave.java @@ -0,0 +1,5 @@ +package fucoin.actions; + +public class ActionInvokeLeave { + +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeRevive.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeRevive.java new file mode 100644 index 0000000000000000000000000000000000000000..182c5e9d3b2343282d0dea7f2a73d6d9ea4e0c8d --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeRevive.java @@ -0,0 +1,5 @@ +package fucoin.actions; + +public class ActionInvokeRevive { + +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeSentMoney.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeSentMoney.java new file mode 100644 index 0000000000000000000000000000000000000000..6b9cd1ae80fc10476bba4920f38a58d6b246c0ab --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeSentMoney.java @@ -0,0 +1,12 @@ +package fucoin.actions; + +import java.io.Serializable; + +public class ActionInvokeSentMoney implements Serializable{ + public final String name; + public final int amount; + public ActionInvokeSentMoney(String name, int amount) { + this.name=name; + this.amount = amount; + } +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeSentMoney2.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeSentMoney2.java new file mode 100644 index 0000000000000000000000000000000000000000..d915a3a28b3f1292e3ff1dec7d8f3d1105de5d45 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeSentMoney2.java @@ -0,0 +1,12 @@ +package fucoin.actions; + +import java.io.Serializable; + +public class ActionInvokeSentMoney2 implements Serializable{ + public final String name; + public final int amount; + public ActionInvokeSentMoney2(String name, int amount) { + this.name=name; + this.amount = amount; + } +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeUpdate.java b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeUpdate.java new file mode 100644 index 0000000000000000000000000000000000000000..14780e0b420658caae3ddfb38cf70fb86b068681 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/actions/ActionInvokeUpdate.java @@ -0,0 +1,5 @@ +package fucoin.actions; + +public class ActionInvokeUpdate { + +} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/FindWalletAction.java b/JavaAkkaFuCoin/src/fucoin/actions/FindWalletAction.java deleted file mode 100644 index f28dfbd209db8478b3521c32da733bf319ec466d..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/FindWalletAction.java +++ /dev/null @@ -1,15 +0,0 @@ -package fucoin.actions; - -public class FindWalletAction { - - private String searchedName; - - public FindWalletAction(String searchedName) { - this.searchedName=searchedName; - } - - public String getSearchedName() { - return searchedName; - } - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/FindWalletResponseAction.java b/JavaAkkaFuCoin/src/fucoin/actions/FindWalletResponseAction.java deleted file mode 100644 index edbf4f19910fc34bd63242192407ed6ce5f7abd3..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/FindWalletResponseAction.java +++ /dev/null @@ -1,20 +0,0 @@ -package fucoin.actions; - -public class FindWalletResponseAction { - - private String foundneighbour; - private int moneyAmount; - - public FindWalletResponseAction(String foundneighbour, int moneyAmount) { - this.foundneighbour=foundneighbour; - this.moneyAmount=moneyAmount; - } - - public String getFoundneighbour() { - return foundneighbour; - } - - public int getMoneyAmount() { - return moneyAmount; - } -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/InvalidateAction.java b/JavaAkkaFuCoin/src/fucoin/actions/InvalidateAction.java deleted file mode 100644 index b2e025a7e7f900e2fbef476f7e65a13de783f4e9..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/InvalidateAction.java +++ /dev/null @@ -1,15 +0,0 @@ -package fucoin.actions; - -public class InvalidateAction { - - private String invalidWallet; - - public InvalidateAction(String invalidWallet) { - this.invalidWallet=invalidWallet; - } - - public String getInvalidWallet() { - return invalidWallet; - } - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/InvokeExitAction.java b/JavaAkkaFuCoin/src/fucoin/actions/InvokeExitAction.java deleted file mode 100644 index 653c97de69131ce03196e35b62358f499ed09e8f..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/InvokeExitAction.java +++ /dev/null @@ -1,9 +0,0 @@ -package fucoin.actions; - -public class InvokeExitAction { -@Override -public String toString() { - // TODO Auto-generated method stub - return "invoke exit"; -} -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/InvokeFindWalletAction.java b/JavaAkkaFuCoin/src/fucoin/actions/InvokeFindWalletAction.java deleted file mode 100644 index 72724bba017725e13dba26bfe35356b2af60fbcd..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/InvokeFindWalletAction.java +++ /dev/null @@ -1,5 +0,0 @@ -package fucoin.actions; - -public class InvokeFindWalletAction { - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/InvokePerformTransactionAction.java b/JavaAkkaFuCoin/src/fucoin/actions/InvokePerformTransactionAction.java deleted file mode 100644 index 7818d432ef30e8093bb09191d29efe1f80c1fcf4..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/InvokePerformTransactionAction.java +++ /dev/null @@ -1,23 +0,0 @@ -package fucoin.actions; - -import fucoin.WalletPointer; - -public class InvokePerformTransactionAction { - - private WalletPointer walletPointer; - private int amount; - - public InvokePerformTransactionAction(WalletPointer walletPointer, int amount) { - this.walletPointer=walletPointer; - this.amount=amount; - } - - public WalletPointer getWalletPointer() { - return walletPointer; - } - - public int getAmount() { - return amount; - } - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/InvokeStoreOrUpdateAction.java b/JavaAkkaFuCoin/src/fucoin/actions/InvokeStoreOrUpdateAction.java deleted file mode 100644 index 0fd2858b0f41434ce585287e845540ab38437bb9..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/InvokeStoreOrUpdateAction.java +++ /dev/null @@ -1,11 +0,0 @@ -package fucoin.actions; - -import fucoin.Wallet; - -public class InvokeStoreOrUpdateAction { -@Override -public String toString() { - // TODO Auto-generated method stub - return "invoke store or update"; -} -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/JoinAction.java b/JavaAkkaFuCoin/src/fucoin/actions/JoinAction.java deleted file mode 100644 index 98de44c1e4880b42746df287b9413e60ba59e6de..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/JoinAction.java +++ /dev/null @@ -1,17 +0,0 @@ -package fucoin.actions; - -import fucoin.WalletPointer; - -public class JoinAction { - - private WalletPointer newMember; - - public JoinAction(WalletPointer walletPointer) { - this.newMember=walletPointer; - } - - public WalletPointer getNewMember() { - return newMember; - } - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/JoinActionRespond.java b/JavaAkkaFuCoin/src/fucoin/actions/JoinActionRespond.java deleted file mode 100644 index 6fc97a965f684a49c981dd6bb2c1a7ee92671f54..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/JoinActionRespond.java +++ /dev/null @@ -1,22 +0,0 @@ -package fucoin.actions; - -import java.util.Vector; - -import fucoin.WalletPointer; - -public class JoinActionRespond { - private Vector<WalletPointer> allKnownNeighbors; - - @Override - public String toString() { - return "you joined successfully here are your new neighbours "+allKnownNeighbors; - } - - public void setKnownNeighbors(Vector<WalletPointer> allKnownNeighbors) { - this.allKnownNeighbors=allKnownNeighbors; - } - - public Vector<WalletPointer> getAllKnownNeighbors() { - return allKnownNeighbors; - } -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/PerformTransactionAction.java b/JavaAkkaFuCoin/src/fucoin/actions/PerformTransactionAction.java deleted file mode 100644 index 5144911b48e69eaef8a474919a263cce53a38bad..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/PerformTransactionAction.java +++ /dev/null @@ -1,23 +0,0 @@ -package fucoin.actions; - -import fucoin.WalletPointer; - -public class PerformTransactionAction { - - private WalletPointer walletPointer; - private int amount; - - public PerformTransactionAction(WalletPointer walletPointer, int amount) { - this.walletPointer=walletPointer; - this.amount=amount; - } - - public WalletPointer getWalletPointer() { - return walletPointer; - } - - public int getAmount() { - return amount; - } - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/StoreOrUpdateAction.java b/JavaAkkaFuCoin/src/fucoin/actions/StoreOrUpdateAction.java deleted file mode 100644 index 694096404d2fb922d60d614a7522fa276ab220b2..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/StoreOrUpdateAction.java +++ /dev/null @@ -1,22 +0,0 @@ -package fucoin.actions; - -import fucoin.Wallet; - -public class StoreOrUpdateAction { - - private Wallet wallet; - - public void setWallet(Wallet wallet) { - this.wallet=wallet; - } - - public Wallet getWallet() { - return wallet; - } - - @Override - public String toString() { - return "store or update"; - } - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/TransactionPerformedAction.java b/JavaAkkaFuCoin/src/fucoin/actions/TransactionPerformedAction.java deleted file mode 100644 index 3c9e1e4c3146385810c94cc815867bba64fa896d..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/TransactionPerformedAction.java +++ /dev/null @@ -1,5 +0,0 @@ -package fucoin.actions; - -public class TransactionPerformedAction { - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/WaitForAnswersAction.java b/JavaAkkaFuCoin/src/fucoin/actions/WaitForAnswersAction.java deleted file mode 100644 index 28062a1598331de2abfe8e533462b130ee059a29..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/WaitForAnswersAction.java +++ /dev/null @@ -1,5 +0,0 @@ -package fucoin.actions; - -public class WaitForAnswersAction { - -} diff --git a/JavaAkkaFuCoin/src/fucoin/actions/WaitForPerformTransactionAction.java b/JavaAkkaFuCoin/src/fucoin/actions/WaitForPerformTransactionAction.java deleted file mode 100644 index f197ffbc4e280f007a7919c19b71da8aa099c06b..0000000000000000000000000000000000000000 --- a/JavaAkkaFuCoin/src/fucoin/actions/WaitForPerformTransactionAction.java +++ /dev/null @@ -1,21 +0,0 @@ -package fucoin.actions; - -import fucoin.WalletPointer; - -public class WaitForPerformTransactionAction { - private WalletPointer walletPointer; - private int amount; - - public WaitForPerformTransactionAction(WalletPointer walletPointer, int amount) { - this.walletPointer=walletPointer; - this.amount=amount; - } - - public WalletPointer getWalletPointer() { - return walletPointer; - } - - public int getAmount() { - return amount; - } -} diff --git a/JavaAkkaFuCoin/src/fucoin/gui/IWalletControle.java b/JavaAkkaFuCoin/src/fucoin/gui/IWalletControle.java index ff7f46cd6f47b6a4938fdb7927646a38a4544012..f56fbde20afd9e9c07bfa34e1ade438ab7d76786 100644 --- a/JavaAkkaFuCoin/src/fucoin/gui/IWalletControle.java +++ b/JavaAkkaFuCoin/src/fucoin/gui/IWalletControle.java @@ -3,6 +3,5 @@ package fucoin.gui; public interface IWalletControle { public void leave(); - public void store(); public void send(String address, int amount); } diff --git a/JavaAkkaFuCoin/src/fucoin/gui/WalletControle.java b/JavaAkkaFuCoin/src/fucoin/gui/WalletControle.java index a9f72e8ad4bb5ddd85d25d1457c0adf87426bd54..9bb8b77b6a5d1ea972366bf15f2f11cdb4d27ab7 100644 --- a/JavaAkkaFuCoin/src/fucoin/gui/WalletControle.java +++ b/JavaAkkaFuCoin/src/fucoin/gui/WalletControle.java @@ -1,24 +1,24 @@ package fucoin.gui; +import fucoin.Wallet; + public class WalletControle implements IWalletControle{ - @Override - public void leave() { - // TODO Auto-generated method stub - + private Wallet wallet; + + public WalletControle(Wallet wallet) { + this.wallet=wallet; } @Override - public void store() { - // TODO Auto-generated method stub - + public void leave() { + wallet.leave(); } @Override - public void send(String address, int amount) { - // TODO Auto-generated method stub - + public void send(String name, int amount) { + wallet.send(name, amount); } } diff --git a/JavaAkkaFuCoin/src/fucoin/gui/WalletCoreGui.java b/JavaAkkaFuCoin/src/fucoin/gui/WalletCoreGui.java new file mode 100644 index 0000000000000000000000000000000000000000..6e2e4c57320d94b9b5cdeb486e5993e05149f3e5 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/gui/WalletCoreGui.java @@ -0,0 +1,51 @@ +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; + +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; + 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); + } +} diff --git a/JavaAkkaFuCoin/src/fucoin/gui/WalletGui.java b/JavaAkkaFuCoin/src/fucoin/gui/WalletGui.java index f6ccfc1eebb5503b426784251547843e048f9d1f..4c6e6b658bd5ef9f27bd4153af1c1602989aaf38 100644 --- a/JavaAkkaFuCoin/src/fucoin/gui/WalletGui.java +++ b/JavaAkkaFuCoin/src/fucoin/gui/WalletGui.java @@ -6,9 +6,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; -import javax.swing.ComboBoxModel; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JComboBox; @@ -56,13 +54,29 @@ public WalletGui(IWalletControle walletControle) { topPanel.add(txtMyAmount); window.add(topPanel); //<hr> - centerPanel.setLayout(new GridLayout(3, 1)); + centerPanel.setLayout(new GridLayout(4, 1)); // Row 1 JPanel centerup = new JPanel(); centerup.setLayout(new BorderLayout()); centerup.add(lblSendTo,BorderLayout.WEST); centerup.add(txtSendTo,BorderLayout.CENTER); centerPanel.add(centerup); + + JPanel centerup2 = new JPanel(); + centerup2.setLayout(new BorderLayout()); + JTextField sendToNewEdt = new JTextField(); + centerup2.add(sendToNewEdt,BorderLayout.CENTER); + JButton addNewButton = new JButton("Add"); + addNewButton.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + txtSendTo.addItem(sendToNewEdt.getText()); + } + }); + centerup2.add(addNewButton,BorderLayout.EAST); + centerPanel.add(centerup2); + // Row 2 JPanel centerdown = new JPanel(); centerdown.setLayout(new GridLayout(1, 3)); @@ -97,7 +111,7 @@ public WalletGui(IWalletControle walletControle) { @Override public void actionPerformed(ActionEvent e) { - walletControle.store(); + } }); @@ -117,9 +131,7 @@ public WalletGui(IWalletControle walletControle) { } }); } -public static void main(String[] args) { - new WalletGui(new WalletControle()); -} + @Override public void setAddress(String address) { txtMyAddress.setText(address); diff --git a/JavaAkkaFuCoin/src/fucoin/supervisor/AmountTableModel.java b/JavaAkkaFuCoin/src/fucoin/supervisor/AmountTableModel.java new file mode 100644 index 0000000000000000000000000000000000000000..fb91ce4813b9500f700e02f407b49c3df6ae71e5 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/supervisor/AmountTableModel.java @@ -0,0 +1,15 @@ +package fucoin.supervisor; + +import javax.swing.table.DefaultTableModel; + +public class AmountTableModel extends DefaultTableModel{ +public AmountTableModel() { + super(new Object[]{"Address","Name","Amount"},0); +} + +public void clear() { + while(getRowCount()>0){ + removeRow(0); + } +} +} diff --git a/JavaAkkaFuCoin/src/fucoin/supervisor/SuperVisor.java b/JavaAkkaFuCoin/src/fucoin/supervisor/SuperVisor.java new file mode 100644 index 0000000000000000000000000000000000000000..5f376773786c9d84966874355dd695bf25251f03 --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/supervisor/SuperVisor.java @@ -0,0 +1,123 @@ +package fucoin.supervisor; + +import java.awt.Label; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Semaphore; + +import akka.actor.ActorRef; +import akka.actor.Props; +import akka.actor.UntypedActor; +import fucoin.AbstractWallet.ActionJoin; +import fucoin.AbstractWallet.ActionStoreOrUpdate; +import fucoin.actions.ActionGetAmount; +import fucoin.actions.ActionGetAmountAnswer; +import fucoin.actions.ActionInvokeUpdate; + +public class SuperVisor extends UntypedActor { + + private List<ActorRef> knownClients = new ArrayList<ActorRef>(); + private Map<String, Map<String, Integer>> amounts = new HashMap<String, Map<String, Integer>>(); + private AmountTableModel amountTableModel; + private Label averageamountLbl; + + public SuperVisor(AmountTableModel amountTableModel, Label averageamountLbl) { + this.amountTableModel = amountTableModel; + this.averageamountLbl = averageamountLbl; + } + + @Override + public void onReceive(Object msg) throws Exception { + if (msg instanceof ActionJoin) { + if (!knownClients.contains(getSender())) { + knownClients.add(getSender()); + } + } else if (msg instanceof ActionInvokeUpdate) { + log("" + knownClients); + for (ActorRef neighbor : knownClients) { + neighbor.tell(new ActionGetAmount(), getSelf()); + } + } else if (msg instanceof ActionGetAmountAnswer) { + ActionGetAmountAnswer agaa = (ActionGetAmountAnswer) msg; + try { + update(agaa.address, agaa.name, agaa.amount); + } catch (Exception ignoreException) { + } + + } else if (msg instanceof ActionStoreOrUpdate) { + ActionStoreOrUpdate asou = (ActionStoreOrUpdate) msg; + try { + update(asou.w.getAddress(), asou.w.name, asou.w.amount); + } catch (Exception ignoreException) { + } + + knownClients.remove(asou.w.getAddress()); + } + } + + private void log(String msg) { + System.out.println(getSelf() + ": " + msg); + } + + Semaphore mutex = new Semaphore(1); + + private void update(String address, String name, int amount) + throws InterruptedException { + + //log(address + ", " + name + ", " + amount); + if (!amounts.containsKey(address)) { + amounts.put(address, new HashMap<String, Integer>()); + } + amounts.get(address).put(name, amount); + amountTableModel.clear(); + int user = 0; + double avgAmount = 0; + for (Entry<String, Map<String, Integer>> process : amounts.entrySet()) { + for (Entry<String, Integer> account : process.getValue().entrySet()) { + // amountTableModel.addRow(new Object[] { process.getKey(), + // account.getKey(), account.getValue() }); + user++; + avgAmount += account.getValue(); + } + } + if (user > 0) { + avgAmount /= user; + } + avgAmount = ((int) (avgAmount * 100) / 100.0); + this.averageamountLbl.setText("" + avgAmount); + + } + + public static Props props() { + return Props.create(SuperVisor.class, new SuperVisorCreator()); + } + + public void updateValues() { + getSelf().tell(new ActionInvokeUpdate(), getSelf()); + } + + public void exit() { + getContext().stop(getSelf()); + } + + @Override + public void postStop() throws Exception { + int user = 0; + double avgAmount = 0; + //System.out.println(amounts); + for (Entry<String, Map<String, Integer>> process : amounts.entrySet()) { + for (Entry<String, Integer> account : process.getValue().entrySet()) { + amountTableModel.addRow(new Object[] { process.getKey(), + account.getKey(), account.getValue() }); + user++; + avgAmount += account.getValue(); + } + } + if (user > 0) { + avgAmount /= user; + } + } +} diff --git a/JavaAkkaFuCoin/src/fucoin/supervisor/SuperVisorCreator.java b/JavaAkkaFuCoin/src/fucoin/supervisor/SuperVisorCreator.java new file mode 100644 index 0000000000000000000000000000000000000000..a67730de47715233853f4bf71a5f6c026193bd8e --- /dev/null +++ b/JavaAkkaFuCoin/src/fucoin/supervisor/SuperVisorCreator.java @@ -0,0 +1,56 @@ +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; + } + +}