Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
let ixi module connect to ict instead of the other way around
Browse files Browse the repository at this point in the history
  • Loading branch information
vuapo-eth committed Dec 29, 2018
1 parent 493862c commit 42b34a0
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 63 deletions.
9 changes: 3 additions & 6 deletions src/main/java/org/iota/ict/Ict.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,12 @@ public Ict(Properties properties) {
sender.start();
receiver.start();

remoteIctImplementation = properties.ixiEnabled ? createRemoteIctImplementation(properties.ixis) : null;
remoteIctImplementation = properties.ixiEnabled ? createRemoteIctImplementation() : null;
}

private RemoteIctImplementation createRemoteIctImplementation(List<String> ixis) {
private RemoteIctImplementation createRemoteIctImplementation() {
try {
RemoteIctImplementation remoteIctImplementation = new RemoteIctImplementation(this);
for (String ixi : ixis)
remoteIctImplementation.connectToIxi(ixi);
return remoteIctImplementation;
return new RemoteIctImplementation(this);
} catch (Throwable t) {
ErrorHandler.handleError(LOGGER, t, "failed to enable IXI");
return null;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/iota/ict/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public static void main(String[] args) {

logger.info("Starting new Ict '" + properties.name + "' (version: " + Constants.ICT_VERSION + ")");

if (!properties.ixiEnabled && properties.ixis.size() > 0)
logger.warn("Not running any IXI modules because IXI is disabled. To enable IXI, set 'ixi_enabled = true' in your config file.");

Ict ict;
try {
ict = new Ict(properties);
Expand Down
38 changes: 9 additions & 29 deletions src/main/java/org/iota/ict/ixi/IxiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.iota.ict.Ict;
import org.iota.ict.model.Transaction;
import org.iota.ict.network.event.GossipFilter;
import org.iota.ict.network.event.GossipListener;
Expand All @@ -11,6 +12,7 @@

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
Expand All @@ -28,15 +30,15 @@ public abstract class IxiModule {
}

private final String name;
private RemoteIct ict;
private String ictName;
private final IxiModuleAdapter adapter;
private final RemoteIct ict;

public IxiModule(String name) {
this.name = name;
public IxiModule(String name, String ictName) {
try {
adapter = new IxiModuleAdapter(name);
} catch (RemoteException e) {
this.name = name;
new IxiModuleAdapter(name);
ict = (RemoteIct) Naming.lookup("//localhost/" + ictName);
ict.onIxiConnect(name);
} catch (RemoteException | NotBoundException | MalformedURLException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -95,18 +97,6 @@ public void submit(Transaction transaction) {
}
}

private void setIct(String name) {
try {
ict = (RemoteIct) Naming.lookup("//localhost/" + name);
ictName = name;
} catch (Throwable t) {
ErrorHandler.handleError(logger, t, "Failed to accept connection to ict '" + name + "'");
throw new RuntimeException(t);
}
}

public abstract void onIctConnect(String name);

public abstract void onTransactionReceived(GossipReceiveEvent event);

public abstract void onTransactionSubmitted(GossipSubmitEvent event);
Expand Down Expand Up @@ -136,15 +126,5 @@ public void onTransactionReceived(GossipReceiveEvent event) {
public void onTransactionSubmitted(GossipSubmitEvent event) {
IxiModule.this.onTransactionSubmitted(event);
}

@Override
public void onIctConnect(String name) throws RemoteException {
if (ict != null) {
logger.warn("Refusing Ict '" + name + "' (already connected to '" + ictName + "').");
throw new RemoteException("IXI is already connected to Ict '" + ictName + "'");
}
IxiModule.this.setIct(name);
IxiModule.this.onIctConnect(name);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/iota/ict/ixi/RemoteIct.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface RemoteIct extends Remote {
void setGossipFilter(String moduleName, GossipFilter filter) throws RemoteException;

void submit(Transaction transaction) throws RemoteException;

void onIxiConnect(String name) throws RemoteException;
}
13 changes: 7 additions & 6 deletions src/main/java/org/iota/ict/ixi/RemoteIctImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
Expand Down Expand Up @@ -67,13 +68,13 @@ public void onTransactionReceived(GossipReceiveEvent event) {
});
}

public void connectToIxi(String name) {
@Override
public void onIxiConnect(String ixiName) {
try {
RemoteIxiModule ixiModule = (RemoteIxiModule) Naming.lookup("//localhost/" + name);
ixiModulesByName.put(name, ixiModule);
ixiModule.onIctConnect(this.name);
} catch (Throwable t) {
ErrorHandler.handleError(Ict.LOGGER, t, "Failed connecting to IXI");
RemoteIxiModule ixiModule = (RemoteIxiModule) Naming.lookup("//localhost/" + ixiName);
ixiModulesByName.put(ixiName, ixiModule);
} catch (NotBoundException | MalformedURLException | RemoteException e) {
ErrorHandler.handleError(Ict.LOGGER, e, "Failed to accept connecting IXI module '"+ixiName+"'.");
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/iota/ict/ixi/RemoteIxiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ public interface RemoteIxiModule extends Remote {
void onTransactionReceived(GossipReceiveEvent event) throws RemoteException;

void onTransactionSubmitted(GossipSubmitEvent event) throws RemoteException;

void onIctConnect(String name) throws RemoteException;
}
3 changes: 2 additions & 1 deletion src/main/java/org/iota/ict/network/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private void processIncoming(DatagramPacket packet) {
if(sender == null)
return;

sender.stats.receivedAll++;

if(sender.reachedLimitOfAllowedTransactions()) {
sender.stats.ignored++;
return;
Expand All @@ -64,7 +66,6 @@ private void processIncoming(DatagramPacket packet) {
return;
}

sender.stats.receivedAll++;
updateTransactionLog(sender, transaction);
processRequest(sender, transaction);
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/iota/ict/utils/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class Properties {
public int port = 1337;
public long logRoundDuration = 60000;
public List<InetSocketAddress> neighbors = new LinkedList<>();
public List<String> ixis = new LinkedList<>();

public static Properties fromFile(String path) {
java.util.Properties propObject = new java.util.Properties();
Expand Down Expand Up @@ -63,7 +62,6 @@ public Properties() {
neighbors = neighborsFromString(propObject.getProperty(Property.neighbors.name(), ""));
spamEnabled = propObject.getProperty(Property.spam_enabled.name(), DEFAULT_PROPERTIES.spamEnabled + "").toLowerCase().equals("true");
ixiEnabled = propObject.getProperty(Property.ixi_enabled.name(), DEFAULT_PROPERTIES.ixiEnabled + "").toLowerCase().equals("true");
ixis = stringListFromString(propObject.getProperty(Property.ixis.name(), ""));
}

private static List<String> stringListFromString(String string) {
Expand Down Expand Up @@ -152,7 +150,6 @@ java.util.Properties toPropObject() {
propObject.setProperty(Property.neighbors.name(), neighborsToString());
propObject.setProperty(Property.spam_enabled.name(), spamEnabled + "");
propObject.setProperty(Property.ixi_enabled.name(), ixiEnabled + "");
propObject.setProperty(Property.ixis.name(), stringListToString(ixis));
return propObject;
}

Expand Down
18 changes: 5 additions & 13 deletions src/test/java/org/iota/ict/ixi/IxiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ public class IxiTest {
@BeforeClass
public static void setUp() {

ixi = new TestIxi();
Properties properties = new Properties();
properties.minForwardDelay = 1;
properties.minForwardDelay = 0;
properties.maxForwardDelay = 5;
properties.ixis.add(TestIxi.NAME);
properties.ixiEnabled = true;
ict = new Ict(properties);
sleep(100);
Assert.assertTrue("ict could not connect to ixi", ixi.connected);

ixi = new TestIxi(properties.name);
}

@Test
Expand Down Expand Up @@ -106,24 +105,17 @@ private static void sleep(long ms) {
class TestIxi extends IxiModule {

static final String NAME = "simple.ixi";

boolean connected = false;
GossipSubmitEvent receivedGossipSubmitEvent = null;

TestIxi() {
super(NAME);
TestIxi(String ictName) {
super(NAME, ictName);
}

@Override
public void onTransactionReceived(GossipReceiveEvent event) {

}

@Override
public void onIctConnect(String name) {
connected = true;
}

@Override
public void onTransactionSubmitted(GossipSubmitEvent event) {
receivedGossipSubmitEvent = event;
Expand Down

0 comments on commit 42b34a0

Please sign in to comment.