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

create Wallet package, use AbstractWallet instead WalletImpl, rename classes, apply lint

parent f85f20d9
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
Showing
with 139 additions and 312 deletions
......@@ -16,11 +16,12 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.4-M1</version>
<version>2.4.7</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package fucoin;
import java.io.Serializable;
import java.util.HashMap;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
public abstract class AbstractNode extends UntypedActor implements Serializable {
// Returns the akka-style address as String, which
// could be converted to an ActorRef object later
public String getAddress() {
return getAddress(getSelf());
}
public String getAddress(ActorRef self) {
return self.path().toSerializationFormatWithAddress(self.path().address());
}
// The which receives Action objects
public abstract void onReceive(Object message);
// Holds references to neighbors that were in
// contact with this wallet during runtime;
// The key corresponds to the Wallet's name
private transient HashMap<String, ActorRef> knownNeighbors = new HashMap<String, ActorRef>();
// Holds references to neighbors this wallet
// synchronizes itself to (the Wallet object);
// The key corresponds to the Wallet's name
public transient HashMap<String, ActorRef> localNeighbors = new HashMap<String, ActorRef>();
// Holds all Wallets from network participants
// which synchronize their state (Wallet object)
// with us;
// The key corresponds to the Wallet's name
public transient HashMap<String, AbstractWallet> backedUpNeighbors = new HashMap<String, AbstractWallet>();
public transient HashMap<ActorRef, Integer> amounts = new HashMap<ActorRef, Integer>();
public boolean addKnownNeighbor(String key, ActorRef value) {
if(!knownNeighbors.containsKey(key)){
knownNeighbors.put(key,value);
return true;
}
return false;
}
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
package fucoin;
public abstract class AbstractWallet extends AbstractNode{
// Constructor
public AbstractWallet(String name) {
this.name = name;
}
// Returns the name of this wallet, e.g. "Lieschen Müller"
public String getName() {
return this.name;
}
// Performs housekeeping operations, e.g. pushes
// backedUpNeighbor-entries to other neighbors
public abstract void leave();
// The amount this wallet currently holds
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;
}
}
package fucoin;
import java.util.Vector;
import fucoin.gui.IWalletControle;
public interface IWallet extends IWalletControle{
//Vector<WalletPointer> join();
void storeOrUpdate(Wallet w);
void invalidateWallet(Wallet w);
void receiveTransaction(int amount);
//Vector<WalletPointer> searchWallet(String adress);
}
......@@ -10,69 +10,27 @@ import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import fucoin.actions.join.ServerActionJoin;
import fucoin.actions.transaction.ActionInvokeSentMoney;
import fucoin.supervisor.SuperVisor;
import fucoin.supervisor.SuperVisorImpl;
import fucoin.wallet.WalletImpl;
public class Main {
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(SuperVisor.props(),"SuperVisor");
ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(),"SuperVisorImpl");
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");
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);
//a2.tell(new ActionInvokeSentMoney("Main", 200), a2);
//activeActors.add(a1);
/*
int maxrounds = 100;
int maxactors = 10;
for(int actor=0; actor<maxactors;actor++){
activeActors.add(system.actorOf(Wallet.props(a1,"Main","Main"+actor,superVisorActor),"Main"+actor));
}
List<List<ActorRef>> offline = new ArrayList<List<ActorRef>>();
for(int listnr=0; listnr<maxrounds;listnr++){
offline.add(new ArrayList<ActorRef>());
}
for(int timestep=0; timestep<maxrounds;timestep++){
System.out.println("timestamp:"+timestep);
List<ActorRef> removedActors = new ArrayList<ActorRef>();
for(ActorRef actor:activeActors){
if(Math.random()<0.6){
actor.tell(new ActionInvokeSentMoney("Main"+(int)Math.floor(Math.random()*maxactors), (int) (Math.round(Math.random()*100))), actor);
}
if(Math.random()<0.2){
removedActors.add(actor);
int offtime = timestep+(int)(Math.random()*6)+2;
offline.get(Math.min(offtime, maxrounds-1)).add(actor);
}
}
activeActors.removeAll(removedActors);
for(ActorRef actorName:offline.get(timestep)){
actorName.tell(new ActionInvokeRevive(), actorName);
activeActors.add(actorName);
}
for(ActorRef removedActor : removedActors){
removedActor.tell(new ActionInvokeLeave(), removedActor);
}
Thread.sleep(1000);
System.out.println("timestamp end:"+timestep);
System.out.println("activeActors:"+activeActors);
System.out.println("revived"+offline.get(timestep));
}
superVisorActor.tell(new ActionInvokeUpdate(), superVisorActor);
*/
}
private static void startSupervisor() {
}
}
......@@ -2,13 +2,13 @@ package fucoin;
import java.io.File;
import akka.actor.ActorPath;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Address;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import fucoin.wallet.WalletImpl;
public class MainRemote {
public static ActorRef remoteSuperVisorActor;
......@@ -23,10 +23,10 @@ public class MainRemote {
Address address = new Address("akka.tcp", "Core", "127.0.0.1", 1234);
System.out.println(address);
String path = "akka.tcp://Core@127.0.0.1:1234/user/Main";
System.out.println(system.actorSelection(ActorPath.fromString(path)));
//System.out.println(system.actorSelection(ActorPath.fromString(path)));
System.out.println(ActorPath.isValidPathElement(""+address+"/user/Main"));
ActorRef a1 = system.actorOf(Wallet.props(null,"","Main2",remoteSuperVisorActor),"Main2");
//System.out.println(ActorPath.isValidPathElement(""+address+"/user/Main"));
ActorRef a1 = system.actorOf(WalletImpl.props(null,"","Main2",remoteSuperVisorActor),"Main2");
}
}
package fucoin;
import akka.actor.ActorRef;
import akka.japi.Creator;
import fucoin.gui.IWalletGuiControle;
import fucoin.gui.WalletGui;
public class WalletCreator implements Creator<Wallet> {
private ActorRef preknownNeighbour;
private String walletName;
private ActorRef remoteSuperVisorActor;
private String preknownNeighbourName;
public WalletCreator(ActorRef preknownNeighbour, String preknownNeighbourName, String walletName, ActorRef remoteSuperVisorActor) {
this.preknownNeighbour=preknownNeighbour;
this.preknownNeighbourName=preknownNeighbourName;
this.walletName=walletName;
this.remoteSuperVisorActor=remoteSuperVisorActor;
}
@Override
public Wallet create() throws Exception {
Wallet wallet = new Wallet(preknownNeighbour,preknownNeighbourName, walletName,remoteSuperVisorActor);
IWalletGuiControle gui = new WalletGui(wallet);
wallet.setGui(gui);
return wallet;
}
}
package fucoin.actions;
import fucoin.AbstractNode;
import fucoin.wallet.AbstractNode;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
......
......@@ -2,11 +2,11 @@ package fucoin.actions;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
import fucoin.wallet.AbstractWallet;
public abstract class ClientAction extends Action<Wallet>{
public abstract class ClientAction extends Action<AbstractWallet>{
@Override
protected abstract void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet abstractNode);
UntypedActorContext context, AbstractWallet abstractNode);
}
......@@ -2,17 +2,17 @@ package fucoin.actions.join;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.AbstractNode;
import fucoin.wallet.AbstractNode;
//Used to join the network (a pre known participant/Wallet must be known)
public class ActionJoin extends GeneralAction{
//Used to join the network (a pre known participant/WalletImpl must be known)
public class ActionJoin extends GeneralAction {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractNode node) {
ActionJoinAnswer aja = new ActionJoinAnswer();
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractNode node) {
ActionJoinAnswer aja = new ActionJoinAnswer();
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
}
}
package fucoin.actions.join;
import java.util.HashMap;
import java.util.Map.Entry;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
import fucoin.actions.ClientAction;
import fucoin.actions.persist.ActionSearchMyWallet;
import fucoin.wallet.AbstractWallet;
import java.util.HashMap;
import java.util.Map.Entry;
// Returns some neighbors that might be used as known
// and/or local neighbors
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("Addressed to "+self.path().name()+" from "+sender.path().name()+": someNeighbors:"+someNeighbors);
for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){
wallet.addKnownNeighbor(neighbor.getKey(),neighbor.getValue());
}
for(Entry<String, ActorRef> neighbor : someNeighbors.entrySet()){
neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self);
}
}
public class ActionJoinAnswer extends ClientAction {
public final HashMap<String, ActorRef> someNeighbors = new HashMap<>();
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
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());
}
for (Entry<String, ActorRef> neighbor : someNeighbors.entrySet()) {
neighbor.getValue().tell(new ActionSearchMyWallet(wallet.getName()), self);
}
}
}
package fucoin.actions.join;
import fucoin.AbstractNode;
import fucoin.actions.Action;
import fucoin.wallet.AbstractNode;
public abstract class GeneralAction extends Action<AbstractNode> {
......
package fucoin.actions.join;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.AbstractNode;
import fucoin.Wallet;
import fucoin.actions.ClientAction;
public abstract class Join extends ClientAction{
public abstract class Join extends ClientAction {
}
......@@ -4,24 +4,24 @@ import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.actions.transaction.ActionInvokeDistributedCommitedTransfer;
import fucoin.actions.transaction.SuperVisorAction;
import fucoin.supervisor.SuperVisor;
import fucoin.supervisor.SuperVisorImpl;
public class ServerActionJoin extends SuperVisorAction {
private String name;
private String name;
public ServerActionJoin(String name) {
this.name = name;
}
public ServerActionJoin(String name) {
this.name = name;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisor node) {
ActionJoinAnswer aja = new ActionJoinAnswer();
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
node.addKnownNeighbor(name, sender);
self.tell(
new ActionInvokeDistributedCommitedTransfer(self, sender, 100),
sender);
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, SuperVisorImpl node) {
ActionJoinAnswer aja = new ActionJoinAnswer();
aja.someNeighbors.putAll(node.getKnownNeighbors());
sender.tell(aja, self);
node.addKnownNeighbor(name, sender);
self.tell(
new ActionInvokeDistributedCommitedTransfer(self, sender, 100),
sender);
}
}
......@@ -2,18 +2,21 @@ package fucoin.actions.persist;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.AbstractNode;
import fucoin.Wallet;
import fucoin.wallet.AbstractWallet;
/**
* May be used to delete a stored WalletImpl on another participant
*/
public class ActionInvalidate extends Persist {
public final String name;
// May be used to delete a stored Wallet on another participant
public class ActionInvalidate extends Persist{
public final String name;
public ActionInvalidate(String name) {
this.name = name;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
wallet.backedUpNeighbors.remove(name);
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.backedUpNeighbors.remove(name);
}
}
......@@ -2,23 +2,23 @@ package fucoin.actions.persist;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
import fucoin.wallet.AbstractWallet;
public class ActionInvokeLeave extends Persist{
public class ActionInvokeLeave extends Persist {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
for(ActorRef neighbor : wallet.getKnownNeighbors().values()){
if(self.compareTo(neighbor)!=0){
neighbor.tell(new ActionStoreOrUpdate(wallet), self);
}
}
wallet.setActive(false);
wallet.backedUpNeighbors.clear();
wallet.getKnownNeighbors().clear();
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
for (ActorRef neighbor : wallet.getKnownNeighbors().values()) {
if (self.compareTo(neighbor) != 0) {
neighbor.tell(new ActionStoreOrUpdate(wallet), self);
}
}
wallet.setActive(false);
wallet.backedUpNeighbors.clear();
wallet.getKnownNeighbors().clear();
}
}
......@@ -2,16 +2,16 @@ package fucoin.actions.persist;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
import fucoin.actions.join.ActionJoin;
import fucoin.wallet.AbstractWallet;
public class ActionInvokeRevive extends Persist{
public class ActionInvokeRevive extends Persist {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
wallet.setActive(true);
wallet.getPreknownNeighbour().tell(new ActionJoin(), self);
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.setActive(true);
wallet.getPreknownNeighbour().tell(new ActionJoin(), self);
}
}
......@@ -2,15 +2,15 @@ package fucoin.actions.persist;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;
import fucoin.wallet.AbstractWallet;
public class ActionInvokeUpdate extends Persist{
public class ActionInvokeUpdate extends Persist {
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
// TODO Auto-generated method stub
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
// TODO Auto-generated method stub
}
}
......@@ -2,27 +2,28 @@ package fucoin.actions.persist;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.AbstractWallet;
import fucoin.Wallet;
import fucoin.wallet.AbstractWallet;
// Used to search a Wallet by name, i.e. the own wallet if we just
// joined the network; If a receiving participant holds the stored Wallet,
// Used to search a WalletImpl by name, i.e. the own wallet if we just
// joined the network; If a receiving participant holds the stored WalletImpl,
// he returns it, otherwise, he might use gossiping methods to go on
// with the search;
// Note: You should also forward the sender (the participant who actually
// searches for this Wallet, so that it can be returnd the direct way)
public class ActionSearchMyWallet extends Persist{
// searches for this WalletImpl, so that it can be returnd the direct way)
public class ActionSearchMyWallet extends Persist {
public final String name;
public ActionSearchMyWallet(String name) {
this.name = name;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
wallet.addKnownNeighbor(name, sender);
AbstractWallet storedWallet =wallet.backedUpNeighbors.get(name);
if(storedWallet!=null){
sender.tell(new ActionSearchMyWalletAnswer(storedWallet), self);
}
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.addKnownNeighbor(name, sender);
AbstractWallet storedWallet = wallet.backedUpNeighbors.get(name);
if (storedWallet != null) {
sender.tell(new ActionSearchMyWalletAnswer(storedWallet), self);
}
}
}
......@@ -2,19 +2,22 @@ package fucoin.actions.persist;
import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.AbstractWallet;
import fucoin.Wallet;
import fucoin.wallet.AbstractWallet;
// Used to return a searched Wallet
/**
* Used to return a searched WalletImpl
*/
public class ActionSearchMyWalletAnswer extends Persist {
public final AbstractWallet w;
public final AbstractWallet w;
public ActionSearchMyWalletAnswer(AbstractWallet w) {
this.w = w;
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, Wallet wallet) {
wallet.setAmount(w.getAmount());
sender.tell(new ActionInvalidate(wallet.name), self);
}
@Override
protected void onAction(ActorRef sender, ActorRef self,
UntypedActorContext context, AbstractWallet wallet) {
wallet.setAmount(w.getAmount());
sender.tell(new ActionInvalidate(wallet.name), self);
}
}
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