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;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import fucoin.actions.join.ServerActionJoin;
import fucoin.supervisor.SuperVisorImpl;
import fucoin.wallet.WalletImpl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
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");
System.out.println("config found? " + file.exists());
Config config = ConfigFactory.parseFile(file);
ActorSystem system = ActorSystem.create("Core", config);
ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(),"SuperVisorImpl");
List<ActorRef> activeActors = new ArrayList<>();
ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Main",superVisorActor),"Main");
ActorRef a2 = system.actorOf(WalletImpl.props(a1,"Main","Main2",superVisorActor),"Main2");
superVisorActor.tell(new ServerActionJoin("Main"), a1);
superVisorActor.tell(new ServerActionJoin("Main2"), a2);
}
File file = new File("application.conf");
System.out.println("config found? " + file.exists());
Config config = ConfigFactory.parseFile(file);
ActorSystem system = ActorSystem.create("Core", config);
ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
List<ActorRef> activeActors = new ArrayList<>();
ActorRef a1 = system.actorOf(WalletImpl.props(null, "", "Main", superVisorActor), "Main");
ActorRef a2 = system.actorOf(WalletImpl.props(a1, "Main", "Main2", superVisorActor), "Main2");
superVisorActor.tell(new ServerActionJoin("Main"), a1);
superVisorActor.tell(new ServerActionJoin("Main2"), a2);
}
private static void startSupervisor() {
private static void startSupervisor() {
}
}
}
package fucoin.actions;
import fucoin.wallet.AbstractNode;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.wallet.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){
this.self=abstractNode.getSelf();
onAction(abstractNode.getSender(),abstractNode.getSelf(),abstractNode.getContext(),abstractNode);
}
public void log(String string) {
System.out.println(self.path().name() + ": " + string);
}
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;
import akka.actor.UntypedActorContext;
import fucoin.wallet.AbstractWallet;
public abstract class ClientAction extends Action<AbstractWallet>{
@Override
protected abstract void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet abstractNode);
public abstract class ClientAction extends Action<AbstractWallet> {
@Override
protected abstract void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet abstractNode);
}
......@@ -2,7 +2,7 @@ package fucoin.actions.join;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.transaction.ActionInvokeDistributedCommitedTransfer;
import fucoin.actions.transaction.ActionInvokeDistributedCommittedTransfer;
import fucoin.actions.transaction.SuperVisorAction;
import fucoin.supervisor.SuperVisorImpl;
......@@ -21,7 +21,7 @@ public class ServerActionJoin extends SuperVisorAction {
sender.tell(aja, self);
node.addKnownNeighbor(name, sender);
self.tell(
new ActionInvokeDistributedCommitedTransfer(self, sender, 100),
new ActionInvokeDistributedCommittedTransfer(self, sender, 100),
sender);
}
}
......@@ -11,7 +11,7 @@ public class ActionInvokeRevive extends Persist {
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.setActive(true);
wallet.getPreknownNeighbour().tell(new ActionJoin(), self);
wallet.getPreKnownNeighbour().tell(new ActionJoin(), self);
}
}
......@@ -18,6 +18,6 @@ public class ActionSearchMyWalletAnswer extends Persist {
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
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 {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.backedUpNeighbors.put(w.name, w);
wallet.backedUpNeighbors.put(w.getName(), w);
}
}
......@@ -7,12 +7,14 @@ import fucoin.wallet.AbstractWallet;
import java.util.ArrayList;
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 final String name;
public final List<ActorRef> ttl = new ArrayList<ActorRef>();
public final List<ActorRef> ttl = new ArrayList<>();
public ActionSearchWalletReference(String name) {
this.name = name;
......
......@@ -2,6 +2,6 @@ package fucoin.actions.search;
import fucoin.actions.ClientAction;
public abstract class Search extends ClientAction{
public abstract class Search extends ClientAction {
}
......@@ -3,10 +3,10 @@ package fucoin.actions.transaction;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.ClientAction;
import fucoin.supervisor.DistributedCommitedTransferRequest;
import fucoin.supervisor.DistributedCommittedTransferRequest;
import fucoin.wallet.AbstractWallet;
public class ActionCommitDistributedCommitedTransfer extends ClientAction {
public class ActionCommitDistributedCommittedTransfer extends ClientAction {
private ActorRef source;
private ActorRef target;
......@@ -16,8 +16,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
private long id;
public ActionCommitDistributedCommitedTransfer(ActorRef source, ActorRef target,
int amount, boolean granted, long timestamp, long id) {
public ActionCommitDistributedCommittedTransfer(ActorRef source, ActorRef target,
int amount, boolean granted, long timestamp, long id) {
this.source = source;
this.target = target;
this.amount = amount;
......@@ -26,8 +26,8 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
this.id = id;
}
public ActionCommitDistributedCommitedTransfer(
DistributedCommitedTransferRequest outdatedRequest) {
public ActionCommitDistributedCommittedTransfer(
DistributedCommittedTransferRequest outdatedRequest) {
this.source = outdatedRequest.getSource();
this.target = outdatedRequest.getTarget();
this.amount = 0;
......@@ -39,7 +39,7 @@ public class ActionCommitDistributedCommitedTransfer extends ClientAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
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) {
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 {
log(wallet.getKnownNeighbors() + "");
if (wallet.getKnownNeighbors().containsKey(name)) {
wallet.getRemoteSuperVisorActor().tell(
new ActionInvokeDistributedCommitedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender);
new ActionInvokeDistributedCommittedTransfer(self, wallet.getKnownNeighbors().get(name), amount), sender);
} else {
ActionSearchWalletReference aswr = new ActionSearchWalletReference(name);
for (ActorRef neighbor : wallet.getKnownNeighbors().values()) {
......
package fucoin.actions.transaction;
import fucoin.supervisor.DistributedCommitedTransferRequest;
import fucoin.supervisor.DistributedCommittedTransferRequest;
import fucoin.supervisor.SuperVisorImpl;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
......@@ -29,14 +29,14 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
UntypedActorContext context, SuperVisorImpl superVisor) {
log(""+superVisor.getKnownNeighbors());
log("granted?"+granted);
DistributedCommitedTransferRequest request = superVisor.getRequest(id);
DistributedCommittedTransferRequest request = superVisor.getRequest(id);
if(granted){
if(request==null)//unknown DistributedCommitedTransferRequest ignore
if(request==null)//unknown DistributedCommittedTransferRequest 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);
ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(source,target,amount,true,timestamp,id);
for(ActorRef neighbor : request.getAnswers()){
neighbor.tell(acdct, self);
}
......@@ -45,7 +45,7 @@ public class ActionPrepareDistributedCommittedTransferAnswer extends Coordinator
}else{
//A client wants to rollback
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()){
neighbor.tell(acdct, self);
}
......
......@@ -5,7 +5,7 @@ import akka.actor.UntypedActorContext;
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 {
final public int amount;
......
package fucoin.actions.transaction;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.Action;
import fucoin.supervisor.SuperVisorImpl;
public abstract class SuperVisorAction extends Action<SuperVisorImpl>{
public abstract class SuperVisorAction extends Action<SuperVisorImpl> {
}
package fucoin.supervisor;
import java.util.List;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.transaction.ActionCommitDistributedCommitedTransfer;
import fucoin.actions.transaction.ActionCommitDistributedCommittedTransfer;
import fucoin.actions.transaction.SuperVisorAction;
public class ActionUpdateQueue extends SuperVisorAction{
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) {
List<DistributedCommitedTransferRequest> deletes = superVisor.updateList();
for(DistributedCommitedTransferRequest outdatedRequest : deletes){
ActionCommitDistributedCommitedTransfer acdct = new ActionCommitDistributedCommitedTransfer(outdatedRequest);
for(ActorRef neighbor : superVisor.getKnownNeighbors().values()){
neighbor.tell(acdct, self);
}
}
sleep(self,context,1000);
self.tell(this, self);
}
private void sleep(ActorRef self, UntypedActorContext context, int sleeptime) {
try {
context.unwatch(self);
Thread.sleep(sleeptime);
context.watch(self);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
import java.util.List;
public class ActionUpdateQueue extends SuperVisorAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl superVisor) {
List<DistributedCommittedTransferRequest> deletes = superVisor.updateList();
for (DistributedCommittedTransferRequest outdatedRequest : deletes) {
ActionCommitDistributedCommittedTransfer acdct = new ActionCommitDistributedCommittedTransfer(outdatedRequest);
for (ActorRef neighbor : superVisor.getKnownNeighbors().values()) {
neighbor.tell(acdct, self);
}
}
sleep(self, context, 1000);
self.tell(this, self);
}
private void sleep(ActorRef self, UntypedActorContext context, int sleeptime) {
try {
context.unwatch(self);
Thread.sleep(sleeptime);
context.watch(self);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
......@@ -17,7 +17,7 @@ public class AmountTableModel extends DefaultTableModel {
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++) {
if (rows.get(i) instanceof Vector){
Vector<Object> row = (Vector<Object>) rows.get(i);
......
......@@ -9,16 +9,16 @@ import java.util.LinkedList;
import java.util.List;
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 ActorRef source;
private ActorRef target;
private long timeout;
private long id;
private List<ActorRef> answers = new LinkedList<ActorRef>();
private List<ActorRef> answers = new LinkedList<>();
public DistributedCommitedTransferRequest(ActorRef source, ActorRef target,
long timeout) {
public DistributedCommittedTransferRequest(ActorRef source, ActorRef target,
long timeout) {
this.source = source;
this.target = target;
this.timeout = timeout;
......
......@@ -9,7 +9,7 @@ import java.awt.*;
* Create SuperVisor with a AWT Window.
* The window displays the information from the supervisor.
*/
public class SuperVisorFactory implements Creator<SuperVisorImpl> {
public class SuperVisorCreator implements Creator<SuperVisorImpl> {
@Override
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