Skip to content
Snippets Groups Projects
Commit 9c09dff0 authored by Simon Könnecke's avatar Simon Könnecke
Browse files

rename Factory to Creator, typos, code formatting

parent fe89c93b
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
Showing
with 140 additions and 131 deletions
package fucoin; package fucoin;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.ActorSystem; import akka.actor.ActorSystem;
import com.typesafe.config.Config; 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.supervisor.SuperVisorImpl; import fucoin.supervisor.SuperVisorImpl;
import fucoin.wallet.WalletImpl; import fucoin.wallet.WalletImpl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Main { public class Main {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
File file = new File("application.conf"); File file = new File("application.conf");
System.out.println("config found? " + file.exists()); System.out.println("config found? " + file.exists());
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(SuperVisorImpl.props(),"SuperVisorImpl"); ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
List<ActorRef> activeActors = new ArrayList<>(); List<ActorRef> activeActors = new ArrayList<>();
ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Main",superVisorActor),"Main"); ActorRef a1 = system.actorOf(WalletImpl.props(null, "", "Main", superVisorActor), "Main");
ActorRef a2 = system.actorOf(WalletImpl.props(a1,"Main","Main2",superVisorActor),"Main2"); ActorRef a2 = system.actorOf(WalletImpl.props(a1, "Main", "Main2", superVisorActor), "Main2");
superVisorActor.tell(new ServerActionJoin("Main"), a1); superVisorActor.tell(new ServerActionJoin("Main"), a1);
superVisorActor.tell(new ServerActionJoin("Main2"), a2); superVisorActor.tell(new ServerActionJoin("Main2"), a2);
} }
private static void startSupervisor() { private static void startSupervisor() {
} }
} }
package fucoin.actions; package fucoin.actions;
import fucoin.wallet.AbstractNode;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
import fucoin.wallet.AbstractNode;
public abstract class Action<T extends AbstractNode> { public abstract class Action<T extends AbstractNode> {
private ActorRef self; private ActorRef self;
public final void doAction(T abstractNode) {
this.self = abstractNode.getSelf();
onAction(abstractNode.getSender(), abstractNode.getSelf(),
abstractNode.getContext(), abstractNode);
}
protected abstract void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, T abstractNode);
public final void doAction(T abstractNode){ public void log(String string) {
this.self=abstractNode.getSelf(); System.out.println(self.path().name() + ": " + string);
onAction(abstractNode.getSender(),abstractNode.getSelf(),abstractNode.getContext(),abstractNode); }
}
protected abstract void onAction(ActorRef sender, ActorRef self, UntypedActorContext context, T abstractNode);
public void log(String string) {
System.out.println(self.path().name()+": "+string);
}
} }
...@@ -4,9 +4,9 @@ import akka.actor.ActorRef; ...@@ -4,9 +4,9 @@ import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
import fucoin.wallet.AbstractWallet; import fucoin.wallet.AbstractWallet;
public abstract class ClientAction extends Action<AbstractWallet>{ public abstract class ClientAction extends Action<AbstractWallet> {
@Override @Override
protected abstract void onAction(ActorRef sender, ActorRef self, protected abstract void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet abstractNode); UntypedActorContext context, AbstractWallet abstractNode);
} }
...@@ -2,7 +2,7 @@ package fucoin.actions.join; ...@@ -2,7 +2,7 @@ package fucoin.actions.join;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
import fucoin.actions.transaction.ActionInvokeDistributedCommitedTransfer; import fucoin.actions.transaction.ActionInvokeDistributedCommittedTransfer;
import fucoin.actions.transaction.SuperVisorAction; import fucoin.actions.transaction.SuperVisorAction;
import fucoin.supervisor.SuperVisorImpl; import fucoin.supervisor.SuperVisorImpl;
...@@ -21,7 +21,7 @@ public class ServerActionJoin extends SuperVisorAction { ...@@ -21,7 +21,7 @@ public class ServerActionJoin extends SuperVisorAction {
sender.tell(aja, self); sender.tell(aja, self);
node.addKnownNeighbor(name, sender); node.addKnownNeighbor(name, sender);
self.tell( self.tell(
new ActionInvokeDistributedCommitedTransfer(self, sender, 100), new ActionInvokeDistributedCommittedTransfer(self, sender, 100),
sender); sender);
} }
} }
...@@ -11,7 +11,7 @@ public class ActionInvokeRevive extends Persist { ...@@ -11,7 +11,7 @@ public class ActionInvokeRevive extends Persist {
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) { UntypedActorContext context, AbstractWallet wallet) {
wallet.setActive(true); wallet.setActive(true);
wallet.getPreknownNeighbour().tell(new ActionJoin(), self); wallet.getPreKnownNeighbour().tell(new ActionJoin(), self);
} }
} }
...@@ -18,6 +18,6 @@ public class ActionSearchMyWalletAnswer extends Persist { ...@@ -18,6 +18,6 @@ public class ActionSearchMyWalletAnswer extends Persist {
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) { UntypedActorContext context, AbstractWallet wallet) {
wallet.setAmount(w.getAmount()); wallet.setAmount(w.getAmount());
sender.tell(new ActionInvalidate(wallet.name), self); sender.tell(new ActionInvalidate(wallet.getName()), self);
} }
} }
...@@ -16,6 +16,6 @@ public class ActionStoreOrUpdate extends Persist { ...@@ -16,6 +16,6 @@ public class ActionStoreOrUpdate extends Persist {
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) { UntypedActorContext context, AbstractWallet wallet) {
wallet.backedUpNeighbors.put(w.name, w); wallet.backedUpNeighbors.put(w.getName(), w);
} }
} }
...@@ -7,12 +7,14 @@ import fucoin.wallet.AbstractWallet; ...@@ -7,12 +7,14 @@ import fucoin.wallet.AbstractWallet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
//Used to return a WalletImpl reference (akka-style string which can /**
// be transformed to an ActorRef) * Used to return a WalletImpl reference (akka-style string which can
* be transformed to an ActorRef)
*/
public class ActionSearchWalletReference extends Search { public class ActionSearchWalletReference extends Search {
public final String name; public final String name;
public final List<ActorRef> ttl = new ArrayList<ActorRef>(); public final List<ActorRef> ttl = new ArrayList<>();
public ActionSearchWalletReference(String name) { public ActionSearchWalletReference(String name) {
this.name = name; this.name = name;
......
...@@ -2,6 +2,6 @@ package fucoin.actions.search; ...@@ -2,6 +2,6 @@ package fucoin.actions.search;
import fucoin.actions.ClientAction; import fucoin.actions.ClientAction;
public abstract class Search extends ClientAction{ public abstract class Search extends ClientAction {
} }
...@@ -3,10 +3,10 @@ package fucoin.actions.transaction; ...@@ -3,10 +3,10 @@ package fucoin.actions.transaction;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
import fucoin.actions.ClientAction; import fucoin.actions.ClientAction;
import fucoin.supervisor.DistributedCommitedTransferRequest; import fucoin.supervisor.DistributedCommittedTransferRequest;
import fucoin.wallet.AbstractWallet; import fucoin.wallet.AbstractWallet;
public class ActionCommitDistributedCommitedTransfer extends ClientAction { public class ActionCommitDistributedCommittedTransfer extends ClientAction {
private ActorRef source; private ActorRef source;
private ActorRef target; private ActorRef target;
...@@ -16,8 +16,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { ...@@ -16,8 +16,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
private long id; private long id;
public ActionCommitDistributedCommitedTransfer(ActorRef source, ActorRef target, public ActionCommitDistributedCommittedTransfer(ActorRef source, ActorRef target,
int amount, boolean granted, long timestamp, long id) { 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;
...@@ -26,8 +26,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { ...@@ -26,8 +26,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
this.id = id; this.id = id;
} }
public ActionCommitDistributedCommitedTransfer( public ActionCommitDistributedCommittedTransfer(
DistributedCommitedTransferRequest outdatedRequest) { DistributedCommittedTransferRequest outdatedRequest) {
this.source = outdatedRequest.getSource(); this.source = outdatedRequest.getSource();
this.target = outdatedRequest.getTarget(); this.target = outdatedRequest.getTarget();
this.amount = 0; this.amount = 0;
...@@ -39,7 +39,7 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction { ...@@ -39,7 +39,7 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
@Override @Override
protected void onAction(ActorRef sender, ActorRef self, protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) { UntypedActorContext context, AbstractWallet wallet) {
System.out.println(self.path().name() + ": ActionCommitDistributedCommitedTransfer is granted?" + granted); System.out.println(self.path().name() + ": ActionCommitDistributedCommittedTransfer is granted?" + granted);
if (granted) { if (granted) {
Integer sourceAmount = wallet.amounts.getOrDefault(source, 0); Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
......
package fucoin.actions.transaction;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.supervisor.DistributedCommitedTransferRequest;
import fucoin.supervisor.SuperVisorImpl;
public class ActionInvokeDistributedCommitedTransfer extends CoordinatorTransaction{
private ActorRef source;
private ActorRef target;
private int amount;
public ActionInvokeDistributedCommitedTransfer(ActorRef source,
ActorRef target, int amount) {
this.source=source;
this.target=target;
this.amount=amount;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) {
log("invoke transaction "+source.path().name()+" sends "+amount+" to "+target.path().name());
long timeout = System.currentTimeMillis()+500;
DistributedCommitedTransferRequest ds = new DistributedCommitedTransferRequest(source,target,timeout);
superVisor.addDistributedCommitedTransferRequest(ds);
ActionPrepareDistributedCommittedTransfer apdct = new ActionPrepareDistributedCommittedTransfer(source,target,amount,timeout,ds.getId());
for(ActorRef neighbor : superVisor.getKnownNeighbors().values()){
neighbor.tell(apdct, self);
}
}
}
package fucoin.actions.transaction;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.supervisor.DistributedCommittedTransferRequest;
import fucoin.supervisor.SuperVisorImpl;
public class ActionInvokeDistributedCommittedTransfer extends CoordinatorTransaction {
private ActorRef source;
private ActorRef target;
private int amount;
public ActionInvokeDistributedCommittedTransfer(ActorRef source,
ActorRef target, int amount) {
this.source = source;
this.target = target;
this.amount = amount;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) {
log("invoke transaction " + source.path().name() +
" sends " + amount +
" to " + target.path().name());
long timeout = System.currentTimeMillis() + 500;
DistributedCommittedTransferRequest ds = new DistributedCommittedTransferRequest(source, target, timeout);
superVisor.addDistributedCommitedTransferRequest(ds);
ActionPrepareDistributedCommittedTransfer apdct = new ActionPrepareDistributedCommittedTransfer(source, target, amount, timeout, ds.getId());
for (ActorRef neighbor : superVisor.getKnownNeighbors().values()) {
neighbor.tell(apdct, self);
}
}
}
...@@ -21,7 +21,7 @@ public class ActionInvokeSentMoney extends Transaction { ...@@ -21,7 +21,7 @@ public class ActionInvokeSentMoney extends Transaction {
log(wallet.getKnownNeighbors() + ""); log(wallet.getKnownNeighbors() + "");
if (wallet.getKnownNeighbors().containsKey(name)) { if (wallet.getKnownNeighbors().containsKey(name)) {
wallet.getRemoteSuperVisorActor().tell( wallet.getRemoteSuperVisorActor().tell(
new ActionInvokeDistributedCommitedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender); new ActionInvokeDistributedCommittedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender);
} else { } else {
ActionSearchWalletReference aswr = new ActionSearchWalletReference(name); ActionSearchWalletReference aswr = new ActionSearchWalletReference(name);
for (ActorRef neighbor : wallet.getKnownNeighbors().values()) { for (ActorRef neighbor : wallet.getKnownNeighbors().values()) {
......
package fucoin.actions.transaction; package fucoin.actions.transaction;
import fucoin.supervisor.DistributedCommitedTransferRequest; import fucoin.supervisor.DistributedCommittedTransferRequest;
import fucoin.supervisor.SuperVisorImpl; import fucoin.supervisor.SuperVisorImpl;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
...@@ -29,14 +29,14 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator ...@@ -29,14 +29,14 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
UntypedActorContext context, SuperVisorImpl superVisor) { UntypedActorContext context, SuperVisorImpl superVisor) {
log(""+superVisor.getKnownNeighbors()); log(""+superVisor.getKnownNeighbors());
log("granted?"+granted); log("granted?"+granted);
DistributedCommitedTransferRequest request = superVisor.getRequest(id); DistributedCommittedTransferRequest request = superVisor.getRequest(id);
if(granted){ if(granted){
if(request==null)//unknown DistributedCommitedTransferRequest ignore if(request==null)//unknown DistributedCommittedTransferRequest ignore
return; return;
int newCount = request.addPositiveAnswer(sender); int newCount = request.addPositiveAnswer(sender);
System.out.println(newCount+" have agreed on request"+id); 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); ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source,target,amount,true,timestamp,id);
for(ActorRef neighbor : request.getAnswers()){ for(ActorRef neighbor : request.getAnswers()){
neighbor.tell(acdct, self); neighbor.tell(acdct, self);
} }
...@@ -45,7 +45,7 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator ...@@ -45,7 +45,7 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
}else{ }else{
//A client wants to rollback //A client wants to rollback
if(request!=null){ if(request!=null){
ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(source,target,amount,false,timestamp,id); ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source,target,amount,false,timestamp,id);
for(ActorRef neighbor : request.getAnswers()){ for(ActorRef neighbor : request.getAnswers()){
neighbor.tell(acdct, self); neighbor.tell(acdct, self);
} }
......
...@@ -5,7 +5,7 @@ import akka.actor.UntypedActorContext; ...@@ -5,7 +5,7 @@ import akka.actor.UntypedActorContext;
import fucoin.wallet.AbstractWallet; import fucoin.wallet.AbstractWallet;
/** /**
* Used to send (positive amount) or retreive money (negative amount) * Used to send (positive amount) or retrieve money (negative amount)
*/ */
public class ActionReceiveTransaction extends Transaction { public class ActionReceiveTransaction extends Transaction {
final public int amount; final public int amount;
......
package fucoin.actions.transaction; package fucoin.actions.transaction;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.Action; import fucoin.actions.Action;
import fucoin.supervisor.SuperVisorImpl; import fucoin.supervisor.SuperVisorImpl;
public abstract class SuperVisorAction extends Action<SuperVisorImpl>{ public abstract class SuperVisorAction extends Action<SuperVisorImpl> {
} }
package fucoin.supervisor; package fucoin.supervisor;
import java.util.List;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.UntypedActorContext; import akka.actor.UntypedActorContext;
import fucoin.actions.transaction.ActionCommitDistributedCommitedTransfer; import fucoin.actions.transaction.ActionCommitDistributedCommittedTransfer;
import fucoin.actions.transaction.SuperVisorAction; import fucoin.actions.transaction.SuperVisorAction;
public class ActionUpdateQueue extends SuperVisorAction{ import java.util.List;
@Override public class ActionUpdateQueue extends SuperVisorAction {
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) { @Override
protected void onAction(ActorRef sender, ActorRef self,
List<DistributedCommitedTransferRequest> deletes = superVisor.updateList(); UntypedActorContext context, SuperVisorImpl superVisor) {
for(DistributedCommitedTransferRequest outdatedRequest : deletes){ List<DistributedCommittedTransferRequest> deletes = superVisor.updateList();
ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(outdatedRequest);
for(ActorRef neighbor : superVisor.getKnownNeighbors().values()){ for (DistributedCommittedTransferRequest outdatedRequest : deletes) {
neighbor.tell(acdct, self); ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(outdatedRequest);
} for (ActorRef neighbor : superVisor.getKnownNeighbors().values()) {
} neighbor.tell(acdct, self);
sleep(self,context,1000); }
self.tell(this, self); }
} sleep(self, context, 1000);
self.tell(this, self);
private void sleep(ActorRef self, UntypedActorContext context, int sleeptime) { }
try {
context.unwatch(self); private void sleep(ActorRef self, UntypedActorContext context, int sleeptime) {
Thread.sleep(sleeptime); try {
context.watch(self); context.unwatch(self);
} catch (InterruptedException e) { Thread.sleep(sleeptime);
e.printStackTrace(); context.watch(self);
} } catch (InterruptedException e) {
} e.printStackTrace();
}
}
} }
...@@ -17,7 +17,7 @@ public class AmountTableModel extends DefaultTableModel { ...@@ -17,7 +17,7 @@ public class AmountTableModel extends DefaultTableModel {
public void updateTable(String address, String name, int amount) { public void updateTable(String address, String name, int amount) {
Vector<Object> rows = this.getDataVector(); Vector rows = this.getDataVector();
for (int i = 0; i < rows.size(); i++) { for (int i = 0; i < rows.size(); i++) {
if (rows.get(i) instanceof Vector){ if (rows.get(i) instanceof Vector){
Vector<Object> row = (Vector<Object>) rows.get(i); Vector<Object> row = (Vector<Object>) rows.get(i);
......
...@@ -9,16 +9,16 @@ import java.util.LinkedList; ...@@ -9,16 +9,16 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public class DistributedCommitedTransferRequest extends Transaction { public class DistributedCommittedTransferRequest extends Transaction {
private final static Random random = new Random(System.currentTimeMillis() + System.nanoTime()); private final static Random random = new Random(System.currentTimeMillis() + System.nanoTime());
private ActorRef source; private ActorRef source;
private ActorRef target; private ActorRef target;
private long timeout; private long timeout;
private long id; private long id;
private List<ActorRef> answers = new LinkedList<ActorRef>(); private List<ActorRef> answers = new LinkedList<>();
public DistributedCommitedTransferRequest(ActorRef source, ActorRef target, public DistributedCommittedTransferRequest(ActorRef source, ActorRef target,
long timeout) { long timeout) {
this.source = source; this.source = source;
this.target = target; this.target = target;
this.timeout = timeout; this.timeout = timeout;
......
...@@ -9,7 +9,7 @@ import java.awt.*; ...@@ -9,7 +9,7 @@ import java.awt.*;
* Create SuperVisor with a AWT Window. * Create SuperVisor with a AWT Window.
* The window displays the information from the supervisor. * The window displays the information from the supervisor.
*/ */
public class SuperVisorFactory implements Creator<SuperVisorImpl> { public class SuperVisorCreator implements Creator<SuperVisorImpl> {
@Override @Override
public SuperVisorImpl create() throws Exception { public SuperVisorImpl create() throws Exception {
......
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