Skip to content
Snippets Groups Projects
Unverified Commit f85f20d9 authored by Luca Keidel's avatar Luca Keidel
Browse files

Fixed wallet and server GUI

parent b084390d
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
Showing
with 605 additions and 495 deletions
/bin/ /bin/
/target/ /target/
.idea/
*.iml
...@@ -17,11 +17,15 @@ public abstract class AbstractWallet extends AbstractNode{ ...@@ -17,11 +17,15 @@ public abstract class AbstractWallet extends AbstractNode{
public abstract void leave(); public abstract void leave();
// The amount this wallet currently holds // The amount this wallet currently holds
public int amount; protected int amount;
// The name of this wallet (does never change, no // The name of this wallet (does never change, no
// duplicates in network assumed) // duplicates in network assumed)
public final String name; public final String name;
public int getAmount(){
return amount;
}
} }
...@@ -10,6 +10,7 @@ import com.typesafe.config.Config; ...@@ -10,6 +10,7 @@ import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigFactory;
import fucoin.actions.join.ServerActionJoin; import fucoin.actions.join.ServerActionJoin;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.supervisor.SuperVisor; import fucoin.supervisor.SuperVisor;
...@@ -22,7 +23,7 @@ public class Main { ...@@ -22,7 +23,7 @@ public class Main {
Config config = ConfigFactory.parseFile(file); Config config = ConfigFactory.parseFile(file);
ActorSystem system = ActorSystem.create("Core", config); ActorSystem system = ActorSystem.create("Core", config);
ActorRef superVisorActor = system.actorOf(SuperVisor.props(),"SuperVisor"); ActorRef superVisorActor = system.actorOf(SuperVisor.props(),"SuperVisor");
List<ActorRef> activeActors = new ArrayList<ActorRef>(); List<ActorRef> activeActors = new ArrayList<>();
ActorRef a1 = system.actorOf(Wallet.props(null,"","Main",superVisorActor),"Main"); ActorRef a1 = system.actorOf(Wallet.props(null,"","Main",superVisorActor),"Main");
ActorRef a2 = system.actorOf(Wallet.props(a1,"Main","Main2",superVisorActor),"Main2"); ActorRef a2 = system.actorOf(Wallet.props(a1,"Main","Main2",superVisorActor),"Main2");
superVisorActor.tell(new ServerActionJoin("Main"), a1); superVisorActor.tell(new ServerActionJoin("Main"), a1);
......
...@@ -13,161 +13,181 @@ import fucoin.actions.transaction.ActionInvokeSentMoney; ...@@ -13,161 +13,181 @@ import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.gui.IWalletControle; import fucoin.gui.IWalletControle;
import fucoin.gui.IWalletGuiControle; import fucoin.gui.IWalletGuiControle;
public class Wallet extends AbstractWallet implements IWalletControle{ public class Wallet extends AbstractWallet implements IWalletControle {
private ActorRef preknownNeighbour; private ActorRef preknownNeighbour;
private ActorRef remoteSuperVisorActor; private ActorRef remoteSuperVisorActor;
private IWalletGuiControle gui; private IWalletGuiControle gui;
private String preknownNeighbourName; private String preknownNeighbourName;
private boolean isActive; private boolean isActive;
public Wallet(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { public Wallet(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
super(walletName); super(walletName);
this.preknownNeighbourName=preknownNeighbourName; this.preknownNeighbourName = preknownNeighbourName;
this.preknownNeighbour=preknownNeighbour; this.preknownNeighbour = preknownNeighbour;
this.remoteSuperVisorActor=remoteSuperVisorActor; this.remoteSuperVisorActor = remoteSuperVisorActor;
} }
public void addAmount(int amount) { public void addAmount(int amount) {
setAmount(this.amount+amount); setAmount(this.amount + amount);
log(" My amount is now "+this.amount); log(" My amount is now " + this.amount);
}
}
@Override
@Override public void leave() {
public void leave() { getSelf().tell(new ActionInvokeLeave(), getSelf());
getSelf().tell(new ActionInvokeLeave(), getSelf()); }
}
@Override
@Override public void onReceive(Object message) {
public void onReceive(Object message) {
log(message.getClass().getSimpleName()); //log(getSender().path().name() + " invokes " + getSelf().path().name() + " to do " + message.getClass().getSimpleName());
if (message instanceof ActionInvokeRevive) {
//log(getSender().path().name()+" invokes "+getSelf().path().name()+" to do "+message.getClass().getSimpleName()); ((ActionInvokeRevive) message).doAction(this);
if(message instanceof ActionInvokeRevive){ }
((ActionInvokeRevive) message).doAction(this); if (!isActive && !(message instanceof ActionInvokeRevive)) return;
} //System.out.println(message);
if(!isActive&&!(message instanceof ActionInvokeRevive))return; if (message instanceof ClientAction) {
//System.out.println(message); ((ClientAction) message).doAction(this);
if(message instanceof ClientAction){ }
((ClientAction) message).doAction(this);
} }
} @Override
public void preStart() throws Exception {
@Override isActive = true;
public void preStart() throws Exception { if (gui != null) {
isActive=true; gui.setAddress(getAddress());
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()));
String path = "akka.tcp://Core@127.0.0.1:1234/user/Main"; //log("my address should be "+getAddress());
//System.out.println(getContext().provider().getExternalAddressFor(getSelf().path().address())); //log(""+preknownNeighbour);
//log("my address should be "+getAddress()); //knownNeighbors.put(getName(),getSelf());
//log(""+preknownNeighbour);
//knownNeighbors.put(getName(),getSelf()); //System.out.println(knownNeighbors);
if (preknownNeighbour != null) {
//System.out.println(knownNeighbors); addKnownNeighbor(preknownNeighbourName, preknownNeighbour);
if(preknownNeighbour!=null){ preknownNeighbour.tell(new ActionJoin(), getSelf());
addKnownNeighbor(preknownNeighbourName,preknownNeighbour); ActionJoinAnswer aja = new ActionJoinAnswer();
preknownNeighbour.tell(new ActionJoin(), getSelf()); aja.someNeighbors.putAll(getKnownNeighbors());
ActionJoinAnswer aja = new ActionJoinAnswer(); aja.someNeighbors.put(name, getSelf());
aja.someNeighbors.putAll(getKnownNeighbors()); preknownNeighbour.tell(aja, getSelf());
aja.someNeighbors.put(name, getSelf());
preknownNeighbour.tell(aja, getSelf()); }
//setAmount(100);
} //remoteSuperVisorActor.tell(new ServerActionJoin(name), getSelf());
//setAmount(100); }
//remoteSuperVisorActor.tell(new ServerActionJoin(name), getSelf());
} @Override
public void postStop() throws Exception {
@Override leave();
public void postStop() throws Exception { super.postStop();
leave();
super.postStop(); }
}
public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
return Props.create(Wallet.class, new WalletCreator(preknownNeighbour, preknownNeighbourName, walletName, remoteSuperVisorActor));
public static Props props(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) { }
return Props.create(Wallet.class,new WalletCreator(preknownNeighbour,preknownNeighbourName,walletName,remoteSuperVisorActor));
} @Override
public boolean equals(Object obj) {
@Override if (obj instanceof Wallet) {
public boolean equals(Object obj) { Wallet wobj = (Wallet) obj;
if(obj instanceof Wallet){ return amount == wobj.amount && name.equals(wobj.name);
Wallet wobj = (Wallet) obj; }
return amount==wobj.amount&&name.equals(wobj.name); return false;
} }
return false;
} public void setGui(IWalletGuiControle gui) {
this.gui = gui;
public void setGui(IWalletGuiControle gui) { }
this.gui=gui;
} public void setAmount(int amount) {
this.amount = amount;
public void setAmount(int amount){ if (remoteSuperVisorActor != null) {
this.amount = amount; remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf());
if(remoteSuperVisorActor != null){ }
remoteSuperVisorActor.tell(new ActionGetAmountAnswer(getAddress(), getName(), amount), getSelf()); if (gui != null) {
} gui.setAmount(this.amount);
if(gui!=null){ }
gui.setAmount(this.amount); }
}
} public int getAmount() {
return amount;
public ActorRef getPreknownNeighbour() { }
return preknownNeighbour;
} public ActorRef getPreknownNeighbour() {
return preknownNeighbour;
public ActorRef getRemoteSuperVisorActor() { }
return remoteSuperVisorActor;
} public ActorRef getRemoteSuperVisorActor() {
return remoteSuperVisorActor;
public IWalletGuiControle getGui() { }
return gui;
} public IWalletGuiControle getGui() {
return gui;
public String getPreknownNeighbourName() { }
return preknownNeighbourName;
} public String getPreknownNeighbourName() {
return preknownNeighbourName;
public boolean isActive() { }
return isActive;
} public boolean isActive() {
return isActive;
@Override }
public boolean addKnownNeighbor(String key, ActorRef value) {
log(key+" is newNeighbor?"+!getKnownNeighbors().containsKey(key)); @Override
if(getKnownNeighbors().containsKey(key)||key.equals(name)){ public boolean addKnownNeighbor(String key, ActorRef value) {
return false; System.out.println(key + " is newNeighbor of "+name+"?" + !getKnownNeighbors().containsKey(key));
} if (getKnownNeighbors().containsKey(key) || key.equals(name)) {
boolean newNeighbor = super.addKnownNeighbor(key, value); return false;
if(gui!=null&&newNeighbor){ }
gui.addKnownAddress(key);
} boolean newNeighbor = super.addKnownNeighbor(key, value);
return newNeighbor; if (gui != null && newNeighbor) {
} gui.addKnownAddress(key);
}
public void setPreknownNeighbour(ActorRef preknownNeighbour) { return newNeighbor;
this.preknownNeighbour = preknownNeighbour; }
}
public void setPreknownNeighbour(ActorRef preknownNeighbour) {
public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) { this.preknownNeighbour = preknownNeighbour;
this.remoteSuperVisorActor = remoteSuperVisorActor; }
}
public void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor) {
public void setPreknownNeighbourName(String preknownNeighbourName) { this.remoteSuperVisorActor = remoteSuperVisorActor;
this.preknownNeighbourName = preknownNeighbourName; }
}
public void setPreknownNeighbourName(String preknownNeighbourName) {
public void setActive(boolean isActive) { this.preknownNeighbourName = preknownNeighbourName;
this.isActive = isActive; }
}
@Override
@Override public void log(String msg) {
public void send(String address, int amount) { if (gui != null) {
getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf()); gui.addLogMsg(msg);
} } else {
System.out.println(msg);
}
}
public void logTransaction(String msg){
if (gui != null) {
gui.addTransactionLogMessage(msg);
} else {
System.out.println(msg);
}
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@Override
public void send(String address, int amount) {
getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
}
} }
...@@ -15,7 +15,7 @@ public class ActionJoinAnswer extends ClientAction{ ...@@ -15,7 +15,7 @@ public class ActionJoinAnswer extends ClientAction{
public final HashMap<String, ActorRef> someNeighbors = new HashMap<>(); public final HashMap<String, ActorRef> someNeighbors = new HashMap<>();
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) { UntypedActorContext context, Wallet wallet) {
log("someNeighbors:"+someNeighbors); log("Addressed to "+self.path().name()+" from "+sender.path().name()+": someNeighbors:"+someNeighbors);
for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){ for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){
wallet.addKnownNeighbor(neighbor.getKey(),neighbor.getValue()); wallet.addKnownNeighbor(neighbor.getKey(),neighbor.getValue());
} }
......
...@@ -14,7 +14,7 @@ public class ActionSearchMyWalletAnswer extends Persist { ...@@ -14,7 +14,7 @@ public class ActionSearchMyWalletAnswer extends Persist {
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) { UntypedActorContext context, Wallet wallet) {
wallet.setAmount(w.amount); wallet.setAmount(w.getAmount());
sender.tell(new ActionInvalidate(wallet.name), self); sender.tell(new ActionInvalidate(wallet.name), self);
} }
} }
...@@ -6,52 +6,59 @@ import fucoin.Wallet; ...@@ -6,52 +6,59 @@ import fucoin.Wallet;
import fucoin.actions.ClientAction; import fucoin.actions.ClientAction;
import fucoin.supervisor.DistributedCommitedTransferRequest; import fucoin.supervisor.DistributedCommitedTransferRequest;
public class ActionCommitDistributedCommitedTransfer extends ClientAction{ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
private ActorRef source; private ActorRef source;
private ActorRef target; private ActorRef target;
private int amount; protected int amount;
private boolean granted; private boolean granted;
private long timestamp; private long timestamp;
private long id; private long id;
public ActionCommitDistributedCommitedTransfer(ActorRef source, public ActionCommitDistributedCommitedTransfer(ActorRef source,
ActorRef target, int amount, boolean granted, long timestamp, long id) { ActorRef target, int amount, boolean granted, long timestamp, long id) {
this.source=source; this.source = source;
this.target=target; this.target = target;
this.amount=amount; this.amount = amount;
this.granted=granted; this.granted = granted;
this.timestamp=timestamp; this.timestamp = timestamp;
this.id=id; this.id = id;
} }
public ActionCommitDistributedCommitedTransfer( public ActionCommitDistributedCommitedTransfer(
DistributedCommitedTransferRequest outdatedRequest) { DistributedCommitedTransferRequest outdatedRequest) {
this.source=outdatedRequest.getSource(); this.source = outdatedRequest.getSource();
this.target=outdatedRequest.getTarget(); this.target = outdatedRequest.getTarget();
this.amount=0; this.amount = 0;
this.granted=false; this.granted = false;
this.timestamp=outdatedRequest.getTimeout(); this.timestamp = outdatedRequest.getTimeout();
this.id=outdatedRequest.getId(); this.id = outdatedRequest.getId();
} }
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) { UntypedActorContext context, Wallet wallet) {
log("ActionCommitDistributedCommitedTransfer is granted?"+granted); System.out.println(self.path().name() + ": ActionCommitDistributedCommitedTransfer is granted?" + granted);
if(granted){ if (granted) {
Integer sourceAmount = wallet.amounts.getOrDefault(source,0);
Integer targetAmount = wallet.amounts.getOrDefault(target,0); Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
wallet.amounts.put(source,sourceAmount-amount); Integer targetAmount = wallet.amounts.getOrDefault(target, 0);
wallet.amounts.put(target,targetAmount+amount); wallet.amounts.put(source, sourceAmount - amount);
if(source.compareTo(self)==0)wallet.amount-=amount; wallet.amounts.put(target, targetAmount + amount);
else if(target.compareTo(self)==0)wallet.amount+=amount;
wallet.log("have now "+wallet.amounts.get(self)+" Fucoins"); if (source.compareTo(self) == 0) {
}else{ wallet.setAmount(wallet.getAmount() - amount);
log("abort transaction with id"+id); wallet.logTransaction("Sent " + amount + " FUC to " + target.path().name());
} } else if (target.compareTo(self) == 0) {
log("wallet.amounts:"+wallet.amounts); wallet.setAmount(wallet.getAmount() + amount);
} wallet.logTransaction("Received " + amount + " FUC from " + source.path().name());
}
} else {
log("abort transaction with id" + id);
}
log("wallet.amounts:" + wallet.amounts);
}
} }
...@@ -9,7 +9,7 @@ public class ActionGetAmount extends Transaction { ...@@ -9,7 +9,7 @@ public class ActionGetAmount extends Transaction {
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) { UntypedActorContext context, Wallet wallet) {
ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(wallet.getAddress(),wallet.getName(),wallet.amount); ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(wallet.getAddress(),wallet.getName(),wallet.getAmount());
sender.tell(agaa, self); sender.tell(agaa, self);
} }
......
package fucoin.actions.transaction;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
public class ActionInvokeSentMoney2 extends Transaction{
public final String name;
public final int amount;
public ActionInvokeSentMoney2(String name, int amount) {
this.name=name;
this.amount = amount;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
if(wallet.getKnownNeighbors().containsKey(name)){
wallet.addAmount(-amount);
wallet.getKnownNeighbors().get(name).tell(new ActionReceiveTransaction(amount), self);
}
}
}
...@@ -4,30 +4,31 @@ import akka.actor.ActorRef; ...@@ -4,30 +4,31 @@ import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
import fucoin.Wallet; import fucoin.Wallet;
public class ActionPrepareDistributedCommitedTransfer extends Transaction{ public class ActionPrepareDistributedCommitedTransfer extends Transaction {
private ActorRef source; private ActorRef source;
private ActorRef target; private ActorRef target;
private int amount; private int amount;
private long timestamp; private long timestamp;
private long id; private long id;
public ActionPrepareDistributedCommitedTransfer(ActorRef source, public ActionPrepareDistributedCommitedTransfer(ActorRef source,
ActorRef target, int amount, long timestamp, long id) { ActorRef target, int amount, long timestamp, long id) {
this.source=source; this.source = source;
this.target=target; this.target = target;
this.amount=amount; this.amount = amount;
this.timestamp=timestamp; this.timestamp = timestamp;
this.id=id; this.id = id;
} }
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) { UntypedActorContext context, Wallet wallet) {
boolean granted = sender.compareTo(source)==0 //sender is supervisor(bank) has allways money boolean granted = amount > 0 &&
||(wallet.amounts.containsKey(source) //sender is unknown, might be valid (sender.compareTo(source) == 0 //sender is supervisor(bank) has always money
&&wallet.amounts.getOrDefault(source,0)>=amount) ; //sender have enough money || (wallet.amounts.containsKey(source) //sender is unknown, might be valid
sender.tell(new ActionPrepareDistributedCommitedTransferAnswer(source, target, amount,timestamp,granted,id),self); && wallet.amounts.getOrDefault(source, 0) >= amount)); //sender have enough money
} sender.tell(new ActionPrepareDistributedCommitedTransferAnswer(source, target, amount, timestamp, granted, id), self);
}
} }
...@@ -34,6 +34,7 @@ public class ActionPrepareDistributedCommitedTransferAnswer extends CoordinatorT ...@@ -34,6 +34,7 @@ public class ActionPrepareDistributedCommitedTransferAnswer extends CoordinatorT
if(request==null)//unknown DistributedCommitedTransferRequest ignore if(request==null)//unknown DistributedCommitedTransferRequest ignore
return; return;
int newCount = request.addPositiveAnswer(sender); int newCount = request.addPositiveAnswer(sender);
System.out.println(newCount+" have agreed on request"+id);
if(newCount == superVisor.getKnownNeighbors().size()){ if(newCount == superVisor.getKnownNeighbors().size()){
ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,true,timestamp,id); ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,true,timestamp,id);
for(ActorRef neighbor : request.getAnswers()){ for(ActorRef neighbor : request.getAnswers()){
......
...@@ -13,6 +13,6 @@ public class ActionReceiveTransaction extends Transaction { ...@@ -13,6 +13,6 @@ public class ActionReceiveTransaction extends Transaction {
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) { UntypedActorContext context, Wallet wallet) {
wallet.addAmount(wallet.amount); wallet.addAmount(wallet.getAmount());
} }
} }
...@@ -6,6 +6,7 @@ public interface IWalletGuiControle { ...@@ -6,6 +6,7 @@ public interface IWalletGuiControle {
public void setAmount(int amount); public void setAmount(int amount);
public void addKnownAddress(String address); public void addKnownAddress(String address);
public void addLogMsg(String msg); public void addLogMsg(String msg);
public void addTransactionLogMessage(String message);
......
package fucoin.gui;
public class LogMessage {
private String message;
private boolean transactionRelated;
public LogMessage(String message, boolean transactionRelated){
this.message = message;
this.transactionRelated = transactionRelated;
}
public String getMessage() {
return message;
}
public boolean isTransactionRelated() {
return transactionRelated;
}
@Override
public String toString(){
return getMessage();
}
}
package fucoin.gui; package fucoin.gui;
import java.awt.BorderLayout; import com.sun.tools.javac.comp.Flow;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class WalletGui implements IWalletGuiControle{
DefaultListModel<String> log = new DefaultListModel<String>();
private JFrame window = new JFrame("test");
JPanel topPanel = new JPanel();
JLabel lblMyAddress = new JLabel("My Address:");
JTextField txtMyAddress = new JTextField("<MyAddress>");
JLabel lblEmpty = new JLabel("");
JLabel lblMyAmount = new JLabel("My FUCs");
JTextField txtMyAmount = new JTextField("<MyFUCs>");
JPanel centerPanel = new JPanel();
JLabel lblSendTo = new JLabel("Send to:");
JComboBox<String> txtSendTo = new JComboBox<String>();
JLabel lblSendAmount = new JLabel("Amount:");
JTextField txtSendAmount = new JTextField("<Amount>");
JButton btnSend = new JButton("Send");
JButton btnSearch = new JButton("Search");
JButton btnStore = new JButton("Store");
JButton btnExit = new JButton("Exit");
JPanel bottomPanel = new JPanel();
JList<String> txtLog = new JList<String>(log);
public WalletGui(IWalletControle walletControle) {
window.setSize(400, 600);
window.setLayout(new GridLayout(3, 1));
topPanel.setLayout(new GridLayout(2, 3));
// Row 1
topPanel.add(lblMyAddress);
topPanel.add(txtMyAddress);
topPanel.add(lblEmpty);
// Row 2
topPanel.add(lblMyAmount);
topPanel.add(txtMyAmount);
window.add(topPanel);
//<hr>
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));
centerdown.add(lblSendAmount);
centerdown.add(txtSendAmount);
centerdown.add(btnSend);
centerPanel.add(centerdown);
//centerPanel.add(new JLabel(""));
// Row 3
JPanel centerdown2 = new JPanel();
centerdown2.setLayout(new GridLayout(1, 3));
centerdown2.add(btnSearch);
centerdown2.add(btnStore);
centerdown2.add(btnExit);
centerPanel.add(centerdown2);
window.add(centerPanel);
//<hr>
bottomPanel.setLayout(new GridLayout(1, 1));
bottomPanel.add(txtLog);
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()));
}
});
btnStore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.dispose();
}
});
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("window closing");
walletControle.leave();
super.windowClosing(e);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("window closing");
walletControle.leave();
super.windowClosing(e);
}
});
}
@Override import java.awt.*;
public void setAddress(String address) { import java.awt.event.*;
txtMyAddress.setText(address); import java.util.Enumeration;
window.setTitle(address);
}
@Override
public void setAmount(int amount) {
txtMyAmount.setText(""+amount);
}
@Override import javax.swing.*;
public void addKnownAddress(String address) {
public class WalletGui implements IWalletGuiControle {
txtSendTo.addItem(address);
} private DefaultListModel<LogMessage> log = new DefaultListModel<>();
@Override
public void addLogMsg(String msg) { private JFrame window = new JFrame("test");
log.addElement(msg); private JPanel topPanel = new JPanel();
} private JLabel lblMyAddress = new JLabel("My Address:");
private JTextField txtMyAddress = new JTextField("<MyAddress>");
private JLabel lblEmpty = new JLabel("");
private JLabel lblMyAmount = new JLabel("My FUCs");
private JTextField txtMyAmount = new JTextField("<MyFUCs>");
private JPanel centerPanel = new JPanel();
private JLabel lblSendTo = new JLabel("Send to:");
private JComboBox<String> txtSendTo = new JComboBox<>();
private JLabel lblSendAmount = new JLabel("Amount:");
private JTextField txtSendAmount = new JTextField("");
private JButton btnSend = new JButton("Send");
private JButton btnSearch = new JButton("Search");
private JButton btnStore = new JButton("Store");
private JButton btnExit = new JButton("Exit");
private JPanel bottomPanel = new JPanel();
private JList<LogMessage> txtLog = new JList<>(log);
private JScrollPane logPane = new JScrollPane(txtLog);
private JCheckBox showDebug;
public WalletGui(IWalletControle walletControle) {
window.setSize(400, 600);
window.setLayout(new GridLayout(3, 1));
topPanel.setLayout(new GridLayout(2, 3));
// Row 1
topPanel.add(lblMyAddress);
topPanel.add(txtMyAddress);
topPanel.add(lblEmpty);
// Row 2
topPanel.add(lblMyAmount);
topPanel.add(txtMyAmount);
window.add(topPanel);
//<hr>
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));
centerdown.add(lblSendAmount);
centerdown.add(txtSendAmount);
centerdown.add(btnSend);
centerPanel.add(centerdown);
//centerPanel.add(new JLabel(""));
// Row 3
JPanel centerdown2 = new JPanel();
centerdown2.setLayout(new GridLayout(1, 3));
centerdown2.add(btnSearch);
centerdown2.add(btnStore);
centerdown2.add(btnExit);
centerPanel.add(centerdown2);
window.add(centerPanel);
//<hr>
bottomPanel.setLayout(new BorderLayout());
showDebug = new JCheckBox("Show debug messages in transaction log");
showDebug.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
txtLog.setModel(log);
} else {
updateFilteredLog();
}
});
bottomPanel.add(showDebug, BorderLayout.NORTH);
bottomPanel.add(logPane, BorderLayout.CENTER);
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()));
}
});
btnStore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.dispose();
}
});
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("window closing");
walletControle.leave();
super.windowClosing(e);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("window closing");
walletControle.leave();
super.windowClosing(e);
}
});
}
@Override
public void setAddress(String address) {
txtMyAddress.setText(address);
window.setTitle(address);
}
@Override
public void setAmount(int amount) {
txtMyAmount.setText(String.valueOf(amount));
}
@Override
public void addKnownAddress(String address) {
txtSendTo.addItem(address);
}
@Override
public void addLogMsg(String msg) {
log(new LogMessage(msg, false));
}
@Override
public void addTransactionLogMessage(String message) {
log(new LogMessage(message, true));
}
private void log(LogMessage logMessage) {
SwingUtilities.invokeLater(() -> {
log.addElement(logMessage);
if (!showDebug.isSelected()) {
updateFilteredLog();
}
// auto scroll to the bottom
txtLog.ensureIndexIsVisible(log.size() - 1);
});
}
private void updateFilteredLog() {
DefaultListModel<LogMessage> filteredLog = new DefaultListModel<>();
Enumeration<LogMessage> elements = log.elements();
while (elements.hasMoreElements()) {
LogMessage logMessage = elements.nextElement();
if (logMessage.isTransactionRelated()) {
filteredLog.addElement(logMessage);
}
}
txtLog.setModel(filteredLog);
}
} }
package fucoin.supervisor; package fucoin.supervisor;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import java.util.Vector;
public class AmountTableModel extends DefaultTableModel{ public class AmountTableModel extends DefaultTableModel {
public AmountTableModel() {
super(new Object[]{"Address","Name","Amount"},0);
}
public void clear() { public AmountTableModel() {
while(getRowCount()>0){ super(new Object[]{"Address", "Name", "Amount"}, 0);
removeRow(0); }
}
} public void clear() {
while (getRowCount() > 0) {
removeRow(0);
}
}
public void updateTable(String address, String name, int amount) {
Vector<Object> rows = this.getDataVector();
for (int i = 0; i < rows.size(); i++) {
if (rows.get(i) instanceof Vector){
Vector<Object> row = (Vector<Object>) rows.get(i);
if(row.get(0).equals(address)){
setValueAt(amount, i, 2);
return;
}
}
}
this.addRow(new Object[]{address, name, amount});
}
} }
...@@ -14,83 +14,95 @@ import akka.actor.ActorRef; ...@@ -14,83 +14,95 @@ import akka.actor.ActorRef;
import akka.actor.Props; import akka.actor.Props;
import fucoin.AbstractNode; import fucoin.AbstractNode;
import fucoin.actions.Action; import fucoin.actions.Action;
import fucoin.actions.ClientAction;
import fucoin.actions.persist.ActionInvokeUpdate; import fucoin.actions.persist.ActionInvokeUpdate;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.SuperVisorAction;
public class SuperVisor extends AbstractNode { public class SuperVisor extends AbstractNode {
private AmountTableModel amountTableModel; private AmountTableModel amountTableModel;
private Label averageamountLbl; private Label averageamountLbl;
public SuperVisor(AmountTableModel amountTableModel, Label averageamountLbl) { public SuperVisor(AmountTableModel amountTableModel, Label averageamountLbl) {
this.amountTableModel = amountTableModel; this.amountTableModel = amountTableModel;
this.averageamountLbl = averageamountLbl; this.averageamountLbl = averageamountLbl;
} }
@Override @Override
public void onReceive(Object msg) { public void onReceive(Object msg) {
log(msg.getClass().getSimpleName());
// dirty but necessary since ActionGetAmountAnswer is a
((Action) msg).doAction(this); // ClientAction for some reason
} if (msg instanceof ActionGetAmountAnswer) {
ActionGetAmountAnswer answer = (ActionGetAmountAnswer) msg;
Semaphore mutex = new Semaphore(1); amountTableModel.updateTable(answer.address, answer.name, answer.amount);
private Map<Long,DistributedCommitedTransferRequest> requestQueue; } else if (msg instanceof SuperVisorAction) {
((Action) msg).doAction(this);
public static Props props() { }
return Props.create(SuperVisor.class, new SuperVisorCreator()); }
}
Semaphore mutex = new Semaphore(1);
public void updateValues() { private Map<Long, DistributedCommitedTransferRequest> requestQueue;
getSelf().tell(new ActionInvokeUpdate(), getSelf());
} public static Props props() {
return Props.create(SuperVisor.class, new SuperVisorCreator());
public void exit() { }
getContext().stop(getSelf());
} public void updateValues() {
getSelf().tell(new ActionInvokeUpdate(), getSelf());
@Override }
public void postStop() throws Exception {
super.postStop(); public void exit() {
} getContext().stop(getSelf());
}
public void addDistributedCommitedTransferRequest(
DistributedCommitedTransferRequest request) { @Override
requestQueue.put(request.getId(),request); public void postStop() throws Exception {
} super.postStop();
}
@Override
public void preStart() throws Exception { public void addDistributedCommitedTransferRequest(
super.preStart(); DistributedCommitedTransferRequest request) {
requestQueue = new HashMap<Long,DistributedCommitedTransferRequest>(); System.out.println("Füge Request in queue ein: " + request.getId());
self().tell(new ActionUpdateQueue(), self()); requestQueue.put(request.getId(), request);
} }
/**
* filters the request for outdated and removes them @Override
* @return deleted outdated request public void preStart() throws Exception {
*/ super.preStart();
public List<DistributedCommitedTransferRequest> updateList(){ requestQueue = new HashMap<>();
List<Long> deletesIds = new ArrayList<Long>(); self().tell(new ActionUpdateQueue(), self());
List<DistributedCommitedTransferRequest> deletes = new ArrayList<DistributedCommitedTransferRequest>(); }
for(Entry<Long, DistributedCommitedTransferRequest> outdatedRequest : requestQueue.entrySet()){
if(outdatedRequest.getValue().getTimeout()<System.currentTimeMillis()){ /**
deletesIds.add(outdatedRequest.getKey()); * filters the request for outdated and removes them
deletes.add(outdatedRequest.getValue()); *
} * @return deleted outdated request
} */
for(Long delete : deletesIds){ public List<DistributedCommitedTransferRequest> updateList() {
requestQueue.remove(delete); List<Long> deletesIds = new ArrayList<Long>();
} List<DistributedCommitedTransferRequest> deletes = new ArrayList<DistributedCommitedTransferRequest>();
for (Entry<Long, DistributedCommitedTransferRequest> outdatedRequest : requestQueue.entrySet()) {
return deletes; if (outdatedRequest.getValue().getTimeout() < System.currentTimeMillis()) {
} deletesIds.add(outdatedRequest.getKey());
deletes.add(outdatedRequest.getValue());
public DistributedCommitedTransferRequest getRequest(Long id) { }
DistributedCommitedTransferRequest searchedrequest = requestQueue.get(id); }
return searchedrequest ; for (Long delete : deletesIds) {
} requestQueue.remove(delete);
}
public void deleteRequest(DistributedCommitedTransferRequest request) {
requestQueue.remove(request.getId()); return deletes;
} }
public DistributedCommitedTransferRequest getRequest(Long id) {
DistributedCommitedTransferRequest searchedrequest = requestQueue.get(id);
return searchedrequest;
}
public void deleteRequest(DistributedCommitedTransferRequest request) {
requestQueue.remove(request.getId());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment