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/
/target/
.idea/
*.iml
......@@ -17,11 +17,15 @@ public abstract class AbstractWallet extends AbstractNode{
public abstract void leave();
// The amount this wallet currently holds
public int amount;
protected int amount;
// The name of this wallet (does never change, no
// duplicates in network assumed)
public final String name;
public int getAmount(){
return amount;
}
}
......@@ -10,6 +10,7 @@ import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import fucoin.actions.join.ServerActionJoin;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.supervisor.SuperVisor;
......@@ -22,7 +23,7 @@ public class Main {
Config config = ConfigFactory.parseFile(file);
ActorSystem system = ActorSystem.create("Core", config);
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 a2 = system.actorOf(Wallet.props(a1,"Main","Main2",superVisorActor),"Main2");
superVisorActor.tell(new ServerActionJoin("Main"), a1);
......
......@@ -31,7 +31,6 @@ public class Wallet extends AbstractWallet implements IWalletControle{
public void addAmount(int amount) {
setAmount(this.amount + amount);
log(" My amount is now " + this.amount);
}
@Override
......@@ -41,7 +40,6 @@ public class Wallet extends AbstractWallet implements IWalletControle{
@Override
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) {
......@@ -116,6 +114,10 @@ public class Wallet extends AbstractWallet implements IWalletControle{
}
}
public int getAmount() {
return amount;
}
public ActorRef getPreknownNeighbour() {
return preknownNeighbour;
}
......@@ -138,10 +140,11 @@ public class Wallet extends AbstractWallet implements IWalletControle{
@Override
public boolean addKnownNeighbor(String key, ActorRef value) {
log(key+" is newNeighbor?"+!getKnownNeighbors().containsKey(key));
System.out.println(key + " is newNeighbor of "+name+"?" + !getKnownNeighbors().containsKey(key));
if (getKnownNeighbors().containsKey(key) || key.equals(name)) {
return false;
}
boolean newNeighbor = super.addKnownNeighbor(key, value);
if (gui != null && newNeighbor) {
gui.addKnownAddress(key);
......@@ -161,6 +164,23 @@ public class Wallet extends AbstractWallet implements IWalletControle{
this.preknownNeighbourName = preknownNeighbourName;
}
@Override
public void log(String msg) {
if (gui != null) {
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;
}
......
......@@ -15,7 +15,7 @@ public class ActionJoinAnswer extends ClientAction{
public final HashMap<String, ActorRef> someNeighbors = new HashMap<>();
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
log("someNeighbors:"+someNeighbors);
log("Addressed to "+self.path().name()+" from "+sender.path().name()+": someNeighbors:"+someNeighbors);
for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){
wallet.addKnownNeighbor(neighbor.getKey(),neighbor.getValue());
}
......
......@@ -14,7 +14,7 @@ public class ActionSearchMyWalletAnswer extends Persist {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
wallet.setAmount(w.amount);
wallet.setAmount(w.getAmount());
sender.tell(new ActionInvalidate(wallet.name), self);
}
}
......@@ -10,7 +10,7 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction{
private ActorRef source;
private ActorRef target;
private int amount;
protected int amount;
private boolean granted;
private long timestamp;
private long id;
......@@ -39,15 +39,22 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction{
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
log("ActionCommitDistributedCommitedTransfer is granted?"+granted);
System.out.println(self.path().name() + ": ActionCommitDistributedCommitedTransfer is granted?" + granted);
if (granted) {
Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
Integer targetAmount = wallet.amounts.getOrDefault(target, 0);
wallet.amounts.put(source, sourceAmount - amount);
wallet.amounts.put(target, targetAmount + amount);
if(source.compareTo(self)==0)wallet.amount-=amount;
else if(target.compareTo(self)==0)wallet.amount+=amount;
wallet.log("have now "+wallet.amounts.get(self)+" Fucoins");
if (source.compareTo(self) == 0) {
wallet.setAmount(wallet.getAmount() - amount);
wallet.logTransaction("Sent " + amount + " FUC to " + target.path().name());
} else if (target.compareTo(self) == 0) {
wallet.setAmount(wallet.getAmount() + amount);
wallet.logTransaction("Received " + amount + " FUC from " + source.path().name());
}
} else {
log("abort transaction with id" + id);
}
......
......@@ -9,7 +9,7 @@ public class ActionGetAmount extends Transaction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(wallet.getAddress(),wallet.getName(),wallet.amount);
ActionGetAmountAnswer agaa = new ActionGetAmountAnswer(wallet.getAddress(),wallet.getName(),wallet.getAmount());
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);
}
}
}
......@@ -24,9 +24,10 @@ public class ActionPrepareDistributedCommitedTransfer extends Transaction{
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
boolean granted = sender.compareTo(source)==0 //sender is supervisor(bank) has allways money
boolean granted = amount > 0 &&
(sender.compareTo(source) == 0 //sender is supervisor(bank) has always money
|| (wallet.amounts.containsKey(source) //sender is unknown, might be valid
&&wallet.amounts.getOrDefault(source,0)>=amount) ; //sender have enough money
&& 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
if(request==null)//unknown DistributedCommitedTransferRequest ignore
return;
int newCount = request.addPositiveAnswer(sender);
System.out.println(newCount+" have agreed on request"+id);
if(newCount == superVisor.getKnownNeighbors().size()){
ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,true,timestamp,id);
for(ActorRef neighbor : request.getAnswers()){
......
......@@ -13,6 +13,6 @@ public class ActionReceiveTransaction extends Transaction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
wallet.addAmount(wallet.amount);
wallet.addAmount(wallet.getAmount());
}
}
......@@ -6,6 +6,7 @@ public interface IWalletGuiControle {
public void setAmount(int amount);
public void addKnownAddress(String address);
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;
import java.awt.BorderLayout;
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;
import com.sun.tools.javac.comp.Flow;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import javax.swing.*;
public class WalletGui implements IWalletGuiControle {
DefaultListModel<String> log = new DefaultListModel<String>();
private DefaultListModel<LogMessage> log = new DefaultListModel<>();
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);
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));
......@@ -94,8 +87,20 @@ public WalletGui(IWalletControle walletControle) {
centerPanel.add(centerdown2);
window.add(centerPanel);
//<hr>
bottomPanel.setLayout(new GridLayout(1, 1));
bottomPanel.add(txtLog);
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);
......@@ -132,6 +137,7 @@ public WalletGui(IWalletControle walletControle) {
super.windowClosing(e);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("window closing");
......@@ -146,18 +152,51 @@ public void setAddress(String address) {
txtMyAddress.setText(address);
window.setTitle(address);
}
@Override
public void setAmount(int amount) {
txtMyAmount.setText(""+amount);
txtMyAmount.setText(String.valueOf(amount));
}
@Override
public void addKnownAddress(String address) {
txtSendTo.addItem(address);
}
@Override
public void addLogMsg(String msg) {
log.addElement(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;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
public class AmountTableModel extends DefaultTableModel {
public AmountTableModel() {
super(new Object[]{"Address", "Name", "Amount"}, 0);
}
......@@ -12,4 +14,20 @@ public void clear() {
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,7 +14,10 @@ import akka.actor.ActorRef;
import akka.actor.Props;
import fucoin.AbstractNode;
import fucoin.actions.Action;
import fucoin.actions.ClientAction;
import fucoin.actions.persist.ActionInvokeUpdate;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.SuperVisorAction;
public class SuperVisor extends AbstractNode {
......@@ -29,10 +32,16 @@ public class SuperVisor extends AbstractNode {
@Override
public void onReceive(Object msg) {
log(msg.getClass().getSimpleName());
// dirty but necessary since ActionGetAmountAnswer is a
// ClientAction for some reason
if (msg instanceof ActionGetAmountAnswer) {
ActionGetAmountAnswer answer = (ActionGetAmountAnswer) msg;
amountTableModel.updateTable(answer.address, answer.name, answer.amount);
} else if (msg instanceof SuperVisorAction) {
((Action) msg).doAction(this);
}
}
Semaphore mutex = new Semaphore(1);
private Map<Long, DistributedCommitedTransferRequest> requestQueue;
......@@ -56,17 +65,20 @@ public class SuperVisor extends AbstractNode {
public void addDistributedCommitedTransferRequest(
DistributedCommitedTransferRequest request) {
System.out.println("Füge Request in queue ein: " + request.getId());
requestQueue.put(request.getId(), request);
}
@Override
public void preStart() throws Exception {
super.preStart();
requestQueue = new HashMap<Long,DistributedCommitedTransferRequest>();
requestQueue = new HashMap<>();
self().tell(new ActionUpdateQueue(), self());
}
/**
* filters the request for outdated and removes them
*
* @return deleted outdated request
*/
public List<DistributedCommitedTransferRequest> updateList() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment