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

Implemented colors for log messages and a log message for failed transactions

parent 8b79cd4c
Branches
No related tags found
1 merge request!3Preliminary result of the software
Showing
with 102 additions and 21 deletions
.DS_Store 0 → 100644
File added
......@@ -30,7 +30,7 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
DistributedCommittedTransferRequest outdatedRequest) {
this.source = outdatedRequest.getSource();
this.target = outdatedRequest.getTarget();
this.amount = 0;
this.amount = outdatedRequest.getAmount();
this.granted = false;
this.timestamp = outdatedRequest.getTimeout();
this.id = outdatedRequest.getId();
......@@ -49,14 +49,19 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
if (source.compareTo(self) == 0) {
wallet.setAmount(wallet.getAmount() - amount);
wallet.logTransaction("Sent " + amount + " FUC to " + target.path().name());
wallet.logTransactionSuccess("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());
wallet.logTransactionSuccess("Received " + amount + " FUC from " + source.path().name());
}
} else {
wallet.log("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.log("wallet.amounts:" + wallet.amounts);
}
......
......@@ -27,7 +27,7 @@ public class ActionInvokeDistributedCommittedTransfer extends CoordinatorTransac
long timeout = System.currentTimeMillis() + 500;
DistributedCommittedTransferRequest ds = new DistributedCommittedTransferRequest(source, target, timeout);
DistributedCommittedTransferRequest ds = new DistributedCommittedTransferRequest(source, target, amount, timeout);
superVisor.addDistributedCommitedTransferRequest(ds);
ActionPrepareDistributedCommittedTransfer apdct = new ActionPrepareDistributedCommittedTransfer(source, target, amount, timeout, ds.getId());
for (ActorRef neighbor : superVisor.getKnownNeighbors().values()) {
......
......@@ -45,6 +45,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()+")");
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.awt.*;
public class LogCellRenderer extends DefaultListCellRenderer {
private static final Color SUCCESS_COLOR = new Color(92, 184, 92);
private static final Color FAIL_COLOR = new Color(217, 83, 79);
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof LogMessage) {
LogMessage msg = (LogMessage) value;
switch (msg.getContext()) {
case TRANSACTION_SUCCESS:
setForeground(SUCCESS_COLOR);
break;
case TRANSACTION_FAIL:
setForeground(FAIL_COLOR);
break;
case DEBUG:
break;
}
}
return this;
}
}
......@@ -3,20 +3,28 @@ package fucoin.gui;
public class LogMessage {
public enum Context {
TRANSACTION_SUCCESS, TRANSACTION_FAIL, DEBUG
};
private String message;
private boolean transactionRelated;
private Context context;
public LogMessage(String message){
this(message, Context.DEBUG);
}
public LogMessage(String message, boolean transactionRelated) {
public LogMessage(String message, Context context) {
this.message = message;
this.transactionRelated = transactionRelated;
this.context = context;
}
public String getMessage() {
return message;
}
public boolean isTransactionRelated() {
return transactionRelated;
public Context getContext() {
return context;
}
@Override
......
......@@ -10,5 +10,7 @@ public interface WalletGuiControl {
void addLogMsg(String msg);
void addTransactionLogMessage(String message);
void addTransactionLogMessageSuccess(String message);
void addTransactionLogMessageFail(String message);
}
......@@ -132,6 +132,8 @@ public class WalletGuiControlImpl implements WalletGuiControl {
});
txtLog.setCellRenderer(new LogCellRenderer());
btnExit.addActionListener(e -> window.dispose());
window.addWindowListener(new WindowAdapter() {
......@@ -171,12 +173,17 @@ public class WalletGuiControlImpl implements WalletGuiControl {
@Override
public void addLogMsg(String msg) {
log(new LogMessage(msg, false));
log(new LogMessage(msg));
}
@Override
public void addTransactionLogMessageSuccess(String message) {
log(new LogMessage(message, LogMessage.Context.TRANSACTION_SUCCESS));
}
@Override
public void addTransactionLogMessage(String message) {
log(new LogMessage(message, true));
public void addTransactionLogMessageFail(String message) {
log(new LogMessage(message, LogMessage.Context.TRANSACTION_FAIL));
}
private void log(LogMessage logMessage) {
......@@ -198,7 +205,8 @@ public class WalletGuiControlImpl implements WalletGuiControl {
Enumeration<LogMessage> elements = log.elements();
while (elements.hasMoreElements()) {
LogMessage logMessage = elements.nextElement();
if (logMessage.isTransactionRelated()) {
LogMessage.Context context = logMessage.getContext();
if (context == LogMessage.Context.TRANSACTION_FAIL || context == LogMessage.Context.TRANSACTION_SUCCESS) {
filteredLog.addElement(logMessage);
}
}
......
......@@ -13,14 +13,19 @@ public class DistributedCommittedTransferRequest extends Transaction {
private final static Random random = new Random(System.currentTimeMillis() + System.nanoTime());
private ActorRef source;
private ActorRef target;
private int amount;
private long timeout;
private long id;
private List<ActorRef> answers = new LinkedList<>();
public DistributedCommittedTransferRequest(ActorRef source, ActorRef target,
public DistributedCommittedTransferRequest(ActorRef source, ActorRef target, int amount,
long timeout) {
this.source = source;
this.target = target;
this.amount = amount;
this.timeout = timeout;
this.id = random.nextLong();
}
......@@ -55,4 +60,8 @@ public class DistributedCommittedTransferRequest extends Transaction {
public long getId() {
return id;
}
public int getAmount(){
return amount;
}
}
......@@ -94,11 +94,18 @@ public abstract class AbstractWallet extends AbstractNode implements Serializabl
public abstract void setRemoteSuperVisorActor(ActorRef remoteSuperVisorActor);
/**
* Appends a transaction related message to the log
* Appends a message related to a successful transaction to the log
*
* @param msg
*/
public abstract void logTransaction(String 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
......
package fucoin.wallet;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.Props;
import akka.util.Timeout;
import fucoin.actions.ClientAction;
import fucoin.actions.join.ActionJoin;
import fucoin.actions.join.ActionJoinAnswer;
......@@ -200,9 +198,19 @@ public class WalletImpl extends AbstractWallet {
@Override
public void logTransaction(String msg) {
public void logTransactionSuccess(String msg) {
if (gui != null) {
gui.addTransactionLogMessage(msg);
gui.addTransactionLogMessageSuccess(msg);
} else {
System.out.println(msg);
}
}
@Override
public void logTransactionFail(String msg) {
if (gui != null) {
gui.addTransactionLogMessageFail(msg);
} else {
System.out.println(msg);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment