diff --git a/src/fucoin/gui/SuperVisorGuiControl.java b/src/fucoin/gui/SuperVisorGuiControl.java new file mode 100644 index 0000000000000000000000000000000000000000..d27cf2c7fc32540e0b37df3ff1a1e61645b7da39 --- /dev/null +++ b/src/fucoin/gui/SuperVisorGuiControl.java @@ -0,0 +1,8 @@ +package fucoin.gui; + +public interface SuperVisorGuiControl { + /** + * Call from SuperVisorImpl after poison pill or kill + */ + void onLeave(); +} diff --git a/src/fucoin/gui/SuperVisorGuiControlImpl.java b/src/fucoin/gui/SuperVisorGuiControlImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..1ca6922eea794d6c728594a6757d36e19891812a --- /dev/null +++ b/src/fucoin/gui/SuperVisorGuiControlImpl.java @@ -0,0 +1,62 @@ +package fucoin.gui; + +import fucoin.supervisor.SuperVisorImpl; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +public class SuperVisorGuiControlImpl implements SuperVisorGuiControl { + private SuperVisorImpl superVisor; + private JFrame frame; + + public SuperVisorGuiControlImpl(SuperVisorImpl sv) { + superVisor = sv; + init(); + } + + private void init() { + //Show AWT window for runtime information + frame = new JFrame("Server"); + frame.setLayout(new GridLayout(3, 2)); + frame.add(new Label("All Amounts:")); + + //Init Amount Table and SuperVisorImpl + + JTable amountListView = new JTable(superVisor.getAmountTableModel()); + 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 -> superVisor.updateValues()); + frame.add(updateBtn); + + //Exit Button and shutdown supervisor + JButton exitBtn = new JButton("exit"); + exitBtn.addActionListener(e -> { + superVisor.exit(); + frame.setVisible(false); + frame.dispose(); + }); + frame.add(exitBtn); + frame.setSize(200, 400); + frame.setVisible(true); + + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + super.windowClosing(e); + superVisor.exit(); + } + }); + } + + @Override + public void onLeave() { + frame.dispose(); + } +} diff --git a/src/fucoin/gui/WalletController.java b/src/fucoin/gui/WalletControl.java similarity index 70% rename from src/fucoin/gui/WalletController.java rename to src/fucoin/gui/WalletControl.java index c32fd53ad389880deb02a25c3458473c064f9533..96e378c976473c541b1fd6506a6e97fd4c196ccb 100644 --- a/src/fucoin/gui/WalletController.java +++ b/src/fucoin/gui/WalletControl.java @@ -1,6 +1,6 @@ package fucoin.gui; -public interface WalletController { +public interface WalletControl { void leave(); void send(String address, int amount); diff --git a/src/fucoin/gui/WalletControllerImpl.java b/src/fucoin/gui/WalletControlImpl.java similarity index 72% rename from src/fucoin/gui/WalletControllerImpl.java rename to src/fucoin/gui/WalletControlImpl.java index 954bf863e759278c9ad939b5825870956a757ce3..be00024296039257536af22d3c48e6dc3b2fadcb 100644 --- a/src/fucoin/gui/WalletControllerImpl.java +++ b/src/fucoin/gui/WalletControlImpl.java @@ -3,11 +3,11 @@ package fucoin.gui; import fucoin.wallet.WalletImpl; -public class WalletControllerImpl implements WalletController { +public class WalletControlImpl implements WalletControl { private WalletImpl wallet; - public WalletControllerImpl(WalletImpl wallet) { + public WalletControlImpl(WalletImpl wallet) { this.wallet = wallet; } diff --git a/src/fucoin/gui/WalletGuiController.java b/src/fucoin/gui/WalletGuiControl.java similarity index 85% rename from src/fucoin/gui/WalletGuiController.java rename to src/fucoin/gui/WalletGuiControl.java index 3e1be4c4481d3452c9c961ddd67d73efaf402523..d19b454540d1d399e3981da6b37b268cf4de767a 100644 --- a/src/fucoin/gui/WalletGuiController.java +++ b/src/fucoin/gui/WalletGuiControl.java @@ -1,7 +1,7 @@ package fucoin.gui; -public interface WalletGuiController { +public interface WalletGuiControl { void setAddress(String address); void setAmount(int amount); diff --git a/src/fucoin/gui/WalletGuiControllerImpl.java b/src/fucoin/gui/WalletGuiControlImpl.java similarity index 95% rename from src/fucoin/gui/WalletGuiControllerImpl.java rename to src/fucoin/gui/WalletGuiControlImpl.java index 59f87b162b12e8e2738d069e518e51b05184ff96..b3f89044b39861daebbba4477a2fe5b24130ace7 100644 --- a/src/fucoin/gui/WalletGuiControllerImpl.java +++ b/src/fucoin/gui/WalletGuiControlImpl.java @@ -5,7 +5,7 @@ import java.awt.*; import java.awt.event.*; import java.util.Enumeration; -public class WalletGuiControllerImpl implements WalletGuiController { +public class WalletGuiControlImpl implements WalletGuiControl { private DefaultListModel<LogMessage> log = new DefaultListModel<>(); @@ -30,7 +30,7 @@ public class WalletGuiControllerImpl implements WalletGuiController { private JScrollPane logPane = new JScrollPane(txtLog); private JCheckBox showDebug; - public WalletGuiControllerImpl(WalletController walletControle) { + public WalletGuiControlImpl(WalletControl walletControl) { window.setSize(400, 600); window.setLayout(new GridLayout(3, 1)); @@ -102,7 +102,7 @@ public class WalletGuiControllerImpl implements WalletGuiController { window.setVisible(true); btnSend.addActionListener(e -> { - walletControle.send(txtSendTo.getSelectedItem().toString(), + walletControl.send(txtSendTo.getSelectedItem().toString(), Integer.parseInt(txtSendAmount.getText())); }); @@ -117,7 +117,7 @@ public class WalletGuiControllerImpl implements WalletGuiController { @Override public void windowClosing(WindowEvent e) { System.out.println("window closing"); - walletControle.leave(); + walletControl.leave(); super.windowClosing(e); } @@ -125,7 +125,7 @@ public class WalletGuiControllerImpl implements WalletGuiController { @Override public void windowClosed(WindowEvent e) { System.out.println("window closing"); - walletControle.leave(); + walletControl.leave(); super.windowClosing(e); } }); diff --git a/src/fucoin/supervisor/SuperVisorCreator.java b/src/fucoin/supervisor/SuperVisorCreator.java index 70ea2960acb04e203089ccd762415040c9673dd6..088ea7845b3540d8812434c244d48f00d8a6f865 100644 --- a/src/fucoin/supervisor/SuperVisorCreator.java +++ b/src/fucoin/supervisor/SuperVisorCreator.java @@ -1,6 +1,8 @@ package fucoin.supervisor; import akka.japi.Creator; +import fucoin.gui.SuperVisorGuiControl; +import fucoin.gui.SuperVisorGuiControlImpl; import javax.swing.*; import java.awt.*; @@ -13,36 +15,9 @@ public class SuperVisorCreator 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); - + SuperVisorImpl sv = new SuperVisorImpl(); + SuperVisorGuiControl superVisorGuiControl = new SuperVisorGuiControlImpl(sv); + sv.setGuiControl(superVisorGuiControl); return sv; } diff --git a/src/fucoin/supervisor/SuperVisorImpl.java b/src/fucoin/supervisor/SuperVisorImpl.java index 224efaf18423f3b600c74449d3bb3b35c642311d..e7c7c2d56f9186b27e4885865e5df9f754a7238e 100644 --- a/src/fucoin/supervisor/SuperVisorImpl.java +++ b/src/fucoin/supervisor/SuperVisorImpl.java @@ -1,11 +1,12 @@ package fucoin.supervisor; import akka.actor.Props; -import fucoin.wallet.AbstractNode; import fucoin.actions.Action; import fucoin.actions.persist.ActionInvokeUpdate; import fucoin.actions.transaction.ActionGetAmountAnswer; import fucoin.actions.transaction.SuperVisorAction; +import fucoin.gui.SuperVisorGuiControl; +import fucoin.wallet.AbstractNode; import java.util.ArrayList; import java.util.HashMap; @@ -14,8 +15,21 @@ import java.util.Map; import java.util.Map.Entry; public class SuperVisorImpl extends AbstractNode { + private AmountTableModel amountTableModel; + private Map<Long, DistributedCommittedTransferRequest> requestQueue; + + private SuperVisorGuiControl gui; + + public SuperVisorImpl() { + this.amountTableModel = new AmountTableModel(); + } + + public void setGuiControl(SuperVisorGuiControl gui) { + this.gui = gui; + } + public SuperVisorImpl(AmountTableModel amountTableModel) { this.amountTableModel = amountTableModel; } @@ -28,14 +42,11 @@ public class SuperVisorImpl extends AbstractNode { if (msg instanceof ActionGetAmountAnswer) { ActionGetAmountAnswer answer = (ActionGetAmountAnswer) msg; amountTableModel.updateTable(answer.address, answer.name, answer.amount); - } /* TODO: Whats happened here?? Why we can invoke doAction of abstract class? */ - else if (msg instanceof SuperVisorAction) { + } /* TODO: Whats happened here?? Why we can invoke doAction of abstract class? */ else if (msg instanceof SuperVisorAction) { ((Action) msg).doAction(this); } } - private Map<Long, DistributedCommittedTransferRequest> requestQueue; - public static Props props() { return Props.create(new SuperVisorCreator()); } @@ -46,6 +57,9 @@ public class SuperVisorImpl extends AbstractNode { public void exit() { getContext().stop(getSelf()); + if (gui != null) { + gui.onLeave(); + } } @Override @@ -94,4 +108,12 @@ public class SuperVisorImpl extends AbstractNode { public void deleteRequest(DistributedCommittedTransferRequest request) { requestQueue.remove(request.getId()); } + + public AmountTableModel getAmountTableModel() { + return amountTableModel; + } + + public void setAmountTableModel(AmountTableModel amountTableModel) { + this.amountTableModel = amountTableModel; + } } diff --git a/src/fucoin/wallet/Wallet.java b/src/fucoin/wallet/Wallet.java index 78d1870df9cc82f85d1b72fb8841920e5ea4afe4..1c8b56af978016ecb47cad530ca63c86e8207919 100644 --- a/src/fucoin/wallet/Wallet.java +++ b/src/fucoin/wallet/Wallet.java @@ -1,9 +1,9 @@ package fucoin.wallet; -import fucoin.gui.WalletController; +import fucoin.gui.WalletControl; -public interface Wallet extends WalletController { +public interface Wallet extends WalletControl { //Vector<WalletPointer> join(); void storeOrUpdate(WalletImpl w); diff --git a/src/fucoin/wallet/WalletCreator.java b/src/fucoin/wallet/WalletCreator.java index d35360dcd828842c89b65135e8557b2e2b16b136..677991475fe5ad40c69b251bcd44f5529cad3e6d 100644 --- a/src/fucoin/wallet/WalletCreator.java +++ b/src/fucoin/wallet/WalletCreator.java @@ -2,8 +2,7 @@ package fucoin.wallet; import akka.actor.ActorRef; import akka.japi.Creator; -import fucoin.gui.WalletGuiController; -import fucoin.gui.WalletGuiControllerImpl; +import fucoin.gui.WalletGuiControlImpl; public class WalletCreator implements Creator<AbstractWallet> { private ActorRef preKnownNeighbour; @@ -25,7 +24,7 @@ public class WalletCreator implements Creator<AbstractWallet> { WalletImpl wallet = new WalletImpl(preKnownNeighbour, preKnownNeighbourName, walletName, remoteSuperVisorActor); - WalletGuiControllerImpl gui = new WalletGuiControllerImpl(wallet); + WalletGuiControlImpl gui = new WalletGuiControlImpl(wallet); wallet.setGui(gui); return wallet; } diff --git a/src/fucoin/wallet/WalletImpl.java b/src/fucoin/wallet/WalletImpl.java index 83ee800751e6ac09dff630d90638208eb4ee388c..b737fef1cce55671dc85d9e9ca5f3fce4732a184 100644 --- a/src/fucoin/wallet/WalletImpl.java +++ b/src/fucoin/wallet/WalletImpl.java @@ -9,14 +9,14 @@ import fucoin.actions.persist.ActionInvokeLeave; import fucoin.actions.persist.ActionInvokeRevive; import fucoin.actions.transaction.ActionGetAmountAnswer; import fucoin.actions.transaction.ActionInvokeSentMoney; -import fucoin.gui.WalletController; -import fucoin.gui.WalletGuiController; +import fucoin.gui.WalletControl; +import fucoin.gui.WalletGuiControl; -public class WalletImpl extends AbstractWallet implements WalletController { +public class WalletImpl extends AbstractWallet implements WalletControl { private ActorRef preKnownNeighbour; private ActorRef remoteSuperVisorActor; - private WalletGuiController gui; + private WalletGuiControl gui; private String preKnownNeighbourName; private boolean isActive; @@ -134,11 +134,11 @@ public class WalletImpl extends AbstractWallet implements WalletController { this.remoteSuperVisorActor = remoteSuperVisorActor; } - public WalletGuiController getGui() { + public WalletGuiControl getGui() { return gui; } - public void setGui(WalletGuiController gui) { + public void setGui(WalletGuiControl gui) { this.gui = gui; }