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

Consistent logging for supervisor and wallets

parent 734f7100
Branches
No related tags found
1 merge request!5Configuration system
Showing
with 276 additions and 112 deletions
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class Example extends JFrame {
public Example() {
super();
OuterView theGUI = new OuterView();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(theGUI);
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Example();
}
});
}
}
class OuterView extends JPanel {
private String innerValue = "";
public OuterView() {
super();
InnerView innerPanel = new InnerView();
innerPanel.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(InnerView.COMBO_CHANGED)) {
innerValue = evt.getNewValue().toString();
System.out.println("new value from inside of OuterView: "
+ innerValue);
}
}
});
JButton button = new JButton("display OuterView's model");
button.addActionListener(new ButtonListener());
add(innerPanel);
add(button);
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("button was clicked. innerValue: " + innerValue);
}
}
}
class InnerView extends JPanel {
public static final String COMBO_CHANGED = "Combo Changed";
// private SwingPropertyChangeSupport pcSupport = new
// SwingPropertyChangeSupport(this);
String oldValue = "";
public InnerView() {
super();
String[] items = new String[] { "item 1", "item 2", "item 3" };
JComboBox comboBox = new JComboBox(items);
comboBox.addActionListener(new ComboBoxListener());
add(comboBox);
}
private class ComboBoxListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
String text = ((JComboBox) ae.getSource()).getSelectedItem()
.toString();
firePropertyChange(COMBO_CHANGED, oldValue, text);
oldValue = text;
System.out.println("store " + text + " in InnerView's model");
}
}
}
\ No newline at end of file
......@@ -68,9 +68,4 @@ public abstract class AbstractNode extends UntypedActor implements Serializable
public HashMap<String, ActorRef> getKnownNeighbors() {
return knownNeighbors;
}
public void log(String string) {
System.out.println(getSelf().path().name() + ": " + string);
}
}
\ No newline at end of file
......@@ -29,7 +29,7 @@ public class ActionJoinAnswer extends ClientAction {
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.log("Addressed to " + self.path().name() + " from " + sender.path().name() + ": someNeighbors:" + someNeighbors);
wallet.addLogMsg("Addressed to " + self.path().name() + " from " + sender.path().name() + ": someNeighbors:" + someNeighbors);
// your neighbours? my neighbours!
for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) {
......
......@@ -23,7 +23,7 @@ public class ActionSearchWalletReference extends Search {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.log(wallet.getKnownNeighbors() + "contains " + name + "?");
wallet.addLogMsg(wallet.getKnownNeighbors() + "contains " + name + "?");
ttl.add(self);
ActionSearchWalletReferenceAnswer answer = null;
if (this.name.equals(wallet.getName())) {
......
......@@ -39,7 +39,7 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.log("ActionCommitDistributedCommittedTransfer is granted? " + granted);
wallet.addLogMsg("ActionCommitDistributedCommittedTransfer is granted? " + granted);
if (granted) {
Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
......@@ -49,21 +49,21 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
if (source.compareTo(self) == 0) {
wallet.setAmount(wallet.getAmount() - amount);
wallet.logTransactionSuccess("Sent " + amount + " FUC to " + target.path().name());
wallet.addTransactionLogMessageSuccess("Sent " + amount + " FUC to " + target.path().name());
} else if (target.compareTo(self) == 0) {
wallet.setAmount(wallet.getAmount() + amount);
wallet.logTransactionSuccess("Received " + amount + " FUC from " + source.path().name());
wallet.addTransactionLogMessageSuccess("Received " + amount + " FUC from " + source.path().name());
}
} else {
wallet.log("abort transaction with id" + id);
wallet.addLogMsg("abort transaction with id" + id);
if (source.compareTo(self) == 0) {
wallet.logTransactionFail("Failed to send " + amount + " FUC to " + target.path().name() + " (Commit has not been granted)");
wallet.addTransactionLogMessageFail("Failed to send " + amount + " FUC to " + target.path().name() + " (Commit has not been granted)");
}
}
wallet.log("wallet.amounts:" + wallet.amounts);
wallet.addLogMsg("wallet.amounts:" + wallet.amounts);
}
}
......@@ -21,7 +21,7 @@ public class ActionInvokeDistributedCommittedTransfer extends CoordinatorTransac
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) {
superVisor.log("invoke transaction " + source.path().name() +
superVisor.addLogMsg("invoke transaction " + source.path().name() +
" sends " + amount +
" to " + target.path().name());
......
......@@ -18,7 +18,7 @@ public class ActionInvokeSentMoney extends Transaction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.log(wallet.getKnownNeighbors() + "");
wallet.addLogMsg(wallet.getKnownNeighbors() + "");
if (wallet.getKnownNeighbors().containsKey(name)) {
wallet.getRemoteSuperVisorActor().tell(
new ActionInvokeDistributedCommittedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender);
......
......@@ -27,8 +27,8 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) {
superVisor.log("" + superVisor.getKnownNeighbors());
superVisor.log("granted?" + granted);
superVisor.addLogMsg("" + superVisor.getKnownNeighbors());
superVisor.addLogMsg("granted?" + granted);
DistributedCommittedTransferRequest request = superVisor.getRequest(id);
if (granted) {
if (request == null)//unknown DistributedCommittedTransferRequest ignore
......@@ -37,6 +37,7 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
if (newCount == superVisor.getKnownNeighbors().size()) {
ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source, target, amount, true, timestamp, id);
superVisor.addTransactionLogMessageSuccess("Transfer of " + amount + " FUC from" + source.path().name() + " to " + target.path().name());
for (ActorRef neighbor : request.getAnswers()) {
neighbor.tell(acdct, self);
}
......@@ -45,7 +46,7 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
} else {
//A client wants to rollback
if (request != null) {
superVisor.log("Client does not grant commit of " + amount + " FUC (" + source.path().name() + " -> " + target.path().name() + ")");
superVisor.addTransactionLogMessageFail("Client does not grant commit of " + amount + " FUC (" + source.path().name() + " -> " + target.path().name() + ")");
ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source, target, amount, false, timestamp, id);
for (ActorRef neighbor : request.getAnswers()) {
neighbor.tell(acdct, self);
......
package fucoin.gui;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class FilteredLogModel extends AbstractListModel<LogMessage> {
private List<LogMessage> log;
private List<LogMessage> filteredLog;
private boolean filterTransactions = false;
public FilteredLogModel() {
super();
this.
log = new ArrayList<>();
filteredLog = new ArrayList<>();
}
public void setTransactionFilter() {
filterTransactions = true;
refilter();
}
public void clearFilter(){
filterTransactions = false;
refilter();
}
private void refilter() {
filteredLog.clear();
for (LogMessage msg : log) {
if (!filterTransactions
|| (msg.getContext() == LogMessage.Context.TRANSACTION_SUCCESS
|| msg.getContext() == LogMessage.Context.TRANSACTION_FAIL)) {
filteredLog.add(msg);
}
}
fireContentsChanged(this, 0, getSize()-1);
}
@Override
public int getSize() {
return filteredLog.size();
}
@Override
public LogMessage getElementAt(int index) {
return filteredLog.get(index);
}
public void addElement(LogMessage message) {
log.add(message);
refilter();
}
}
package fucoin.gui;
public interface SuperVisorGuiControl {
public interface SuperVisorGuiControl extends TransactionLogger {
/**
* Call from SuperVisorImpl after poison pill or kill
*/
void onLeave();
void log(String message);
}
......@@ -4,16 +4,20 @@ import fucoin.supervisor.SuperVisorImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class SuperVisorGuiControlImpl implements SuperVisorGuiControl {
private SuperVisorImpl superVisor;
private JFrame frame;
private DefaultListModel<String> log = new DefaultListModel<>();
private JList<String> txtLog = new JList<>(log);
private FilteredLogModel log = new FilteredLogModel();
private JList<LogMessage> txtLog = new JList<>(log);
private JScrollPane logPane = new JScrollPane(txtLog);
private JCheckBox showDebug;
public SuperVisorGuiControlImpl(SuperVisorImpl sv) {
superVisor = sv;
......@@ -31,7 +35,23 @@ public class SuperVisorGuiControlImpl implements SuperVisorGuiControl {
JTable amountListView = new JTable(superVisor.getAmountTableModel());
contentPanel.add(new JScrollPane(amountListView));
contentPanel.add(logPane);
JPanel logPanel = new JPanel(new BorderLayout());
txtLog.setCellRenderer(new LogCellRenderer());
showDebug = new JCheckBox("Show debug messages in transaction log");
showDebug.setSelected(true);
showDebug.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
log.clearFilter();
} else {
log.setTransactionFilter();
}
});
logPanel.add(showDebug, BorderLayout.NORTH);
logPanel.add(logPane, BorderLayout.CENTER);
contentPanel.add(logPanel);
frame.add(contentPanel, BorderLayout.CENTER);
......@@ -60,16 +80,27 @@ public class SuperVisorGuiControlImpl implements SuperVisorGuiControl {
frame.dispose();
}
@Override
public void log(String message) {
// One day, we may have a server log GUI as well..
// Until then, we just print it to the console
//System.out.println(message);
private void log(LogMessage logMessage) {
SwingUtilities.invokeLater(() -> {
log.addElement(message);
log.addElement(logMessage);
// auto scroll to the bottom
txtLog.ensureIndexIsVisible(log.size() - 1);
txtLog.ensureIndexIsVisible(log.getSize() - 1);
});
}
@Override
public void addLogMsg(String msg) {
log(new LogMessage(msg));
}
@Override
public void addTransactionLogMessageSuccess(String message) {
log(new LogMessage(message, LogMessage.Context.TRANSACTION_SUCCESS));
}
@Override
public void addTransactionLogMessageFail(String message) {
log(new LogMessage(message, LogMessage.Context.TRANSACTION_FAIL));
}
}
package fucoin.gui;
/**
* All nodes implementing some kind of logging should implement this interface.
*/
public interface TransactionLogger {
/**
* Adds a debug log message or a generic log message to the log
* @param message
*/
public void addLogMsg(String message);
/**
* Adds a log message concerning a successful transaction
* @param message
*/
public void addTransactionLogMessageSuccess(String message);
/**
* Adds a log message concerning a failed transaction
* @param message
*/
public void addTransactionLogMessageFail(String message);
}
package fucoin.gui;
public interface WalletGuiControl {
public interface WalletGuiControl extends TransactionLogger {
void setAddress(String address);
void setAmount(int amount);
void addKnownAddress(String address);
void addLogMsg(String msg);
void addTransactionLogMessageSuccess(String message);
void addTransactionLogMessageFail(String message);
/**
* Tell the GUI, that the wallet is a remote wallet.
*/
......
......@@ -10,7 +10,7 @@ import java.util.Enumeration;
public class WalletGuiControlImpl implements WalletGuiControl {
private DefaultListModel<LogMessage> log = new DefaultListModel<>();
private FilteredLogModel log = new FilteredLogModel();
private JFrame window = new JFrame("test");
private JPanel topPanel = new JPanel();
......@@ -110,12 +110,14 @@ public class WalletGuiControlImpl implements WalletGuiControl {
bottomPanel.setLayout(new BorderLayout());
log.setTransactionFilter();
showDebug = new JCheckBox("Show debug messages in transaction log");
showDebug.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
txtLog.setModel(log);
log.clearFilter();
} else {
updateFilteredLog();
log.setTransactionFilter();
}
});
......@@ -222,27 +224,8 @@ public class WalletGuiControlImpl implements WalletGuiControl {
SwingUtilities.invokeLater(() -> {
log.addElement(logMessage);
if (!showDebug.isSelected()) {
updateFilteredLog();
}
// auto scroll to the bottom
txtLog.ensureIndexIsVisible(log.size() - 1);
txtLog.ensureIndexIsVisible(log.getSize() - 1);
});
}
private void updateFilteredLog() {
DefaultListModel<LogMessage> filteredLog = new DefaultListModel<>();
Enumeration<LogMessage> elements = log.elements();
while (elements.hasMoreElements()) {
LogMessage logMessage = elements.nextElement();
LogMessage.Context context = logMessage.getContext();
if (context == LogMessage.Context.TRANSACTION_FAIL || context == LogMessage.Context.TRANSACTION_SUCCESS) {
filteredLog.addElement(logMessage);
}
}
txtLog.setModel(filteredLog);
}
}
......@@ -7,6 +7,7 @@ import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.SuperVisorAction;
import fucoin.gui.SuperVisorGuiControl;
import fucoin.AbstractNode;
import fucoin.gui.TransactionLogger;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -14,7 +15,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SuperVisorImpl extends AbstractNode {
public class SuperVisorImpl extends AbstractNode implements TransactionLogger{
private AmountTableModel amountTableModel;
......@@ -116,9 +117,28 @@ public class SuperVisorImpl extends AbstractNode {
this.amountTableModel = amountTableModel;
}
public void log(String message) {
if (this.gui != null) {
this.gui.log(message);
@Override
public void addLogMsg(String message) {
if (gui != null) {
gui.addLogMsg(message);
} else {
System.out.println(message);
}
}
@Override
public void addTransactionLogMessageSuccess(String message) {
if (gui != null) {
gui.addTransactionLogMessageSuccess(message);
} else {
System.out.println(message);
}
}
@Override
public void addTransactionLogMessageFail(String message) {
if (gui != null) {
gui.addTransactionLogMessageFail(message);
} else {
System.out.println(message);
}
......
......@@ -2,13 +2,14 @@ package fucoin.wallet;
import akka.actor.ActorRef;
import fucoin.AbstractNode;
import fucoin.gui.TransactionLogger;
import java.io.Serializable;
/**
*
*/
public abstract class AbstractWallet extends AbstractNode implements Serializable {
public abstract class AbstractWallet extends AbstractNode implements Serializable, TransactionLogger {
/**
* Currently amount of this wallet
......@@ -94,20 +95,6 @@ public abstract class AbstractWallet extends AbstractNode implements Serializabl
*/
public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor);
/**
* Appends a message related to a successful transaction to the log
*
* @param msg
*/
public abstract void logTransactionSuccess(String msg);
/**
* Appends a message related to a successful transaction to the log
*
* @param msg
*/
public abstract void logTransactionFail(String msg);
/**
* Sends amount FUCs to the wallet with the address adress
*
......
......@@ -43,7 +43,7 @@ public class WalletImpl extends AbstractWallet {
*/
public void addAmount(int amount) {
setAmount(this.getAmount() + amount);
log(" My amount is now " + this.getAmount());
addLogMsg(" My amount is now " + this.getAmount());
}
@Override
......@@ -54,7 +54,7 @@ public class WalletImpl extends AbstractWallet {
@Override
public void onReceive(Object message) {
log(getSender().path().name() + " invokes " + getSelf().path().name() + " to do " + message.getClass().getSimpleName());
addLogMsg(getSender().path().name() + " invokes " + getSelf().path().name() + " to do " + message.getClass().getSimpleName());
if (message instanceof ActionInvokeRevive) {
((ActionInvokeRevive) message).doAction(this);
}
......@@ -161,25 +161,13 @@ public class WalletImpl extends AbstractWallet {
this.gui = gui;
}
public String getPreKnownNeighbourName() {
return preKnownNeighbourName;
}
public void setPreKnownNeighbourName(String preKnownNeighbourName) {
this.preKnownNeighbourName = preKnownNeighbourName;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@Override
public boolean addKnownNeighbor(String key, ActorRef value) {
log(key + " is newNeighbor of " + name + "?" + !getKnownNeighbors().containsKey(key));
addLogMsg(key + " is newNeighbor of " + name + "?" + !getKnownNeighbors().containsKey(key));
if (getKnownNeighbors().containsKey(key) || key.equals(name)) {
return false;
}
......@@ -191,38 +179,34 @@ public class WalletImpl extends AbstractWallet {
return newNeighbor;
}
public void send(String address, int amount) {
getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
}
@Override
public void log(String msg) {
public void addLogMsg(String message) {
if (gui != null) {
gui.addLogMsg(msg);
gui.addLogMsg(message);
} else {
System.out.println(msg);
System.out.println(message);
}
}
@Override
public void logTransactionSuccess(String msg) {
public void addTransactionLogMessageSuccess(String message) {
if (gui != null) {
gui.addTransactionLogMessageSuccess(msg);
gui.addTransactionLogMessageSuccess(message);
} else {
System.out.println(msg);
System.out.println(message);
}
}
@Override
public void logTransactionFail(String msg) {
public void addTransactionLogMessageFail(String message) {
if (gui != null) {
gui.addTransactionLogMessageFail(msg);
gui.addTransactionLogMessageFail(message);
} else {
System.out.println(msg);
System.out.println(message);
}
}
@Override
public void send(String address, int amount) {
getSelf().tell(new ActionInvokeSentMoney(address, amount), getSelf());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment