Skip to content

Commit

Permalink
Merge pull request #6 from itsMatoosh/version-0.3.1
Browse files Browse the repository at this point in the history
Version 0.3.1
  • Loading branch information
itsMatoosh authored Jan 15, 2019
2 parents 9b5b72a + a55363b commit f624a3e
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ allprojects {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
group 'me.matoosh.undernet'
version '0.3'
version '0.3.1'
}
subprojects {
repositories {
Expand Down
4 changes: 4 additions & 0 deletions shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ dependencies {
implementation group: 'org.apache.commons', name: 'commons-crypto', version: '1.0.0'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.45'
}

jar {
baseName = 'UnderNet-base'
}
26 changes: 20 additions & 6 deletions shared/src/main/java/me/matoosh/undernet/p2p/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import me.matoosh.undernet.p2p.router.data.message.NodeNeighborsRequest;
import me.matoosh.undernet.p2p.router.data.message.tunnel.MessageTunnel;
import me.matoosh.undernet.p2p.router.data.message.tunnel.MessageTunnelManager;
import me.matoosh.undernet.p2p.router.data.message.tunnel.MessageTunnelSide;
import me.matoosh.undernet.p2p.router.data.message.tunnel.TunnelControlMessage;
import me.matoosh.undernet.p2p.router.data.resource.Resource;
import me.matoosh.undernet.p2p.router.data.resource.ResourceManager;
import me.matoosh.undernet.p2p.router.data.resource.transfer.ResourceTransferHandler;
import me.matoosh.undernet.p2p.router.server.Server;
Expand All @@ -47,7 +47,7 @@ public class Router extends EventHandler {
/**
* The interval of the control loop.
*/
public static final int controlLoopInterval = 30;
public static final int controlLoopInterval = 15;
/**
* The logger.
*/
Expand Down Expand Up @@ -199,7 +199,7 @@ public void start(NetworkIdentity networkIdentity) {
}

/**
* Control loop running every 30 seconds.
* Control loop running every 15 seconds.
*/
private void controlLoop() {
logger.info("Checking if everything is running smoothly...");
Expand All @@ -216,24 +216,38 @@ private void controlLoop() {
//Checking resource transfer activity.
for (int i = 0; i < resourceManager.inboundHandlers.size(); i++) {
ResourceTransferHandler transferHandler = resourceManager.inboundHandlers.get(i);
if (System.currentTimeMillis() > transferHandler.getLastMessageTime() + controlLoopInterval * 1000)
if (System.currentTimeMillis() > transferHandler.getLastMessageTime() + 2 * controlLoopInterval * 1000)
transferHandler.callError(new TimeoutException());
}
for (int i = 0; i < resourceManager.outboundHandlers.size(); i++) {
ResourceTransferHandler transferHandler = resourceManager.outboundHandlers.get(i);
if (System.currentTimeMillis() > transferHandler.getLastMessageTime() + controlLoopInterval * 1000)
if (System.currentTimeMillis() > transferHandler.getLastMessageTime() + 2 * controlLoopInterval * 1000)
transferHandler.callError(new TimeoutException());
}

//Checking tunnel activity.
for (int i = 0; i < messageTunnelManager.messageTunnels.size(); i++) {
MessageTunnel tunnel = messageTunnelManager.messageTunnels.get(i);

if (System.currentTimeMillis() > tunnel.getLastMessageTime() + 2 * controlLoopInterval * 1000)
if (System.currentTimeMillis() > tunnel.getLastMessageTime() + 4 * controlLoopInterval * 1000)
messageTunnelManager.closeTunnel(tunnel);
else if (tunnel.isKeepAlive()) tunnel.sendMessage(new TunnelControlMessage());
}

//Sending pending transfers.
if(resourceManager.outboundHandlers.size() == 0) {
for (int i = 0; i < resourceManager.queuedResources.size(); i++) {
Resource r = resourceManager.queuedResources.get(i);
if (neighborNodesManager.getClosestTo(r.getNetworkID()) != Node.self) {
resourceManager.queuedResources.remove(i);
resourceManager.startPush(r);
break;
} else {
resourceManager.queuedResources.remove(i);
}
}
}

EventManager.callEvent(new RouterControlLoopEvent(this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public class ResourceManager extends Manager {
public ArrayList<ResourceTransferHandler> inboundHandlers = new ArrayList<>();

/**
* The last transfer id.
* The queued resources that need to be sent.
*/
public int lastTransferId;
public ArrayList<Resource> queuedResources = new ArrayList<>();

/**
* The logger of the class.
Expand All @@ -61,7 +61,6 @@ public class ResourceManager extends Manager {
*/
public ResourceManager(Router router) {
super(router);
this.lastTransferId = -1;
}

/**
Expand Down Expand Up @@ -168,7 +167,7 @@ public ArrayList<FileResource> getStoredFileResources() {
* Starts a push of a resource.
* @param resource
*/
private void startPush(Resource resource) {
public void startPush(Resource resource) {
//Creating push tunnel
MessageTunnel tunnel = router.messageTunnelManager.createTunnel(Node.self.getIdentity().getNetworkId(), resource.getNetworkID(), MessageTunnelSide.ORIGIN);
startPush(resource, tunnel);
Expand All @@ -179,7 +178,7 @@ private void startPush(Resource resource) {
* @param resource
* @param tunnel
*/
private void startPush(Resource resource, MessageTunnel tunnel) {
public void startPush(Resource resource, MessageTunnel tunnel) {
//Getting the transfer handler.
ResourceTransferHandler transferHandler = resource.getTransferHandler(ResourceTransferType.OUTBOUND, tunnel, -1, this.router);
logger.info("Outbound {} resource transfer, transfer id: {}", resource.getResourceType(), transferHandler.getTransferId());
Expand Down Expand Up @@ -320,13 +319,27 @@ else if(messageReceivedEvent.networkMessage.getContent().getType() == MsgType.RE
for (FileResource file :
getStoredFileResources()) {
if(router.neighborNodesManager.getClosestTo(file.getNetworkID()) != Node.self) {
startPush(file);
boolean duplicate = false;
for (Resource r :
queuedResources) {
if (r == file) duplicate = true;
}
if(duplicate) continue;

queuedResources.add(file);
}
}
for (FlagResource flag :
flagResources) {
if(router.neighborNodesManager.getClosestTo(flag.getNetworkID()) != Node.self) {
startPush(flag);
boolean duplicate = false;
for (Resource r :
queuedResources) {
if (r == flag) duplicate = true;
}
if(duplicate) continue;

queuedResources.add(flag);
}
}
}
Expand All @@ -343,7 +356,6 @@ protected void registerEvents() {
EventManager.registerEvent(ResourceTransferFinishedEvent.class);
EventManager.registerEvent(ResourceTransferErrorEvent.class);
EventManager.registerEvent(ResourceTransferDataSentEvent.class);
EventManager.registerEvent(ResourceTransferDataReceivedEvent.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import me.matoosh.undernet.event.EventManager;
import me.matoosh.undernet.event.resource.transfer.ResourceTransferDataReceivedEvent;
import me.matoosh.undernet.event.resource.transfer.ResourceTransferDataSentEvent;
import me.matoosh.undernet.event.resource.transfer.ResourceTransferErrorEvent;
import me.matoosh.undernet.event.resource.transfer.ResourceTransferFinishedEvent;
import me.matoosh.undernet.p2p.router.Router;
import me.matoosh.undernet.p2p.router.data.message.ResourceDataChunkRequest;
import me.matoosh.undernet.p2p.router.data.message.ResourceDataMessage;
Expand Down Expand Up @@ -35,9 +33,9 @@ public class FileTransferHandler extends ResourceTransferHandler {
private long fileLength;

/**
* The standard buffer size for file chunks (100kb)
* The standard buffer size for file chunks (1MB)
*/
public static final int BUFFER_SIZE = Short.MAX_VALUE;
public static final int BUFFER_SIZE = 1048576;

/**
* The amount of bytes written from the received chunks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public ResourceTransferHandler(Resource resource, ResourceTransferType transferT
router.resourceManager.inboundHandlers.add(this);
}

setLastMessageTime(System.currentTimeMillis() - 10000);

EventManager.callEvent(new ResourceTransferStartedEvent(this));
}

Expand Down Expand Up @@ -113,7 +115,7 @@ public void close() {
* @param dataMessage
*/
public void callDataReceived(ResourceDataMessage dataMessage) {
lastMessageTime = System.currentTimeMillis();
setLastMessageTime(System.currentTimeMillis());
onDataReceived(dataMessage);
}

Expand Down
7 changes: 7 additions & 0 deletions standalone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ distZip.shouldRunAfter(build)
jar {
// Keep jar clean:
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
baseName = 'UnderNet'

manifest {
attributes 'Main-Class': 'me.matoosh.undernet.standalone.UnderNetStandalone',
Expand All @@ -25,4 +26,10 @@ jar {
// How-to add class path:
// http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
// https://gist.github.com/simon04/6865179
}
distZip {
baseName = 'UnderNet'
}
distTar {
baseName = 'UnderNet'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.matoosh.undernet.UnderNet;
import me.matoosh.undernet.file.StandaloneFileManager;
import me.matoosh.undernet.identity.NetworkIdentity;
import me.matoosh.undernet.p2p.router.InterfaceStatus;
import me.matoosh.undernet.standalone.config.StandaloneConfig;
import me.matoosh.undernet.standalone.config.StandaloneConfigManager;
import me.matoosh.undernet.standalone.serialization.SerializationTools;
Expand Down Expand Up @@ -139,6 +140,10 @@ private static void setup() {
* @param identity
*/
public static void setNetworkIdentity(NetworkIdentity identity, File identityFile) {
if(UnderNet.router.status != InterfaceStatus.STOPPED) {
logger.warn("Can't change identity while the router is running!");
return;
}
if(identity == null || identityFile == null || !identity.isCorrect()) {
logger.warn("Network Identity incorrect, creating a new identity!");
identity = new NetworkIdentity();
Expand All @@ -149,6 +154,9 @@ public static void setNetworkIdentity(NetworkIdentity identity, File identityFil
//Setting the identity.
logger.info("Setting the current UnderNet identity to: {}", identity.getNetworkId().getStringValue());
UnderNetStandalone.networkIdentity = identity;
if(MainFrame.instance != null && MainFrame.instance.frame != null) {
MainFrame.instance.frame.repaint();
}

//Save the changed identity.
try {
Expand Down
Loading

0 comments on commit f624a3e

Please sign in to comment.