Migrated core and extension modules to Kotlin

(Except BitmessageContext and Bytes)
This commit is contained in:
2017-06-06 16:36:07 +02:00
parent 811625c051
commit fa0e53289c
213 changed files with 9846 additions and 11067 deletions

View File

@ -78,7 +78,7 @@ public abstract class AbstractConnection {
this.host = new NetworkAddress.Builder().ipv6(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).port(0).build();
this.node = node;
this.listener = context.getNetworkListener();
this.syncTimeout = (syncTimeout > 0 ? UnixTime.now(+syncTimeout) : 0);
this.syncTimeout = (syncTimeout > 0 ? UnixTime.now() + syncTimeout : 0);
this.requestedObjects = Collections.newSetFromMap(new ConcurrentHashMap<InventoryVector, Boolean>(10_000));
this.ivCache = new ConcurrentHashMap<>();
this.sendingQueue = new ConcurrentLinkedDeque<>();
@ -146,7 +146,7 @@ public abstract class AbstractConnection {
missing.removeAll(commonRequestedObjects.keySet());
LOG.trace("Received inventory with " + originalSize + " elements, of which are "
+ missing.size() + " missing.");
send(new GetData.Builder().inventory(missing).build());
send(new GetData(missing));
}
private void receiveMessage(GetData getData) {
@ -195,9 +195,7 @@ public abstract class AbstractConnection {
}
public void offer(InventoryVector iv) {
sendingQueue.offer(new Inv.Builder()
.addInventoryVector(iv)
.build());
sendingQueue.offer(new Inv(Collections.singletonList(iv)));
updateIvCache(Collections.singletonList(iv));
}
@ -210,7 +208,7 @@ public abstract class AbstractConnection {
}
private void cleanupIvCache() {
Long fiveMinutesAgo = UnixTime.now(-5 * MINUTE);
long fiveMinutesAgo = UnixTime.now() - 5 * MINUTE;
for (Map.Entry<InventoryVector, Long> entry : ivCache.entrySet()) {
if (entry.getValue() < fiveMinutesAgo) {
ivCache.remove(entry.getKey());
@ -256,15 +254,13 @@ public abstract class AbstractConnection {
private void sendAddresses() {
List<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(1000, streams);
sendingQueue.offer(new Addr.Builder().addresses(addresses).build());
sendingQueue.offer(new Addr(addresses));
}
private void sendInventory() {
List<InventoryVector> inventory = ctx.getInventory().getInventory(streams);
for (int i = 0; i < inventory.size(); i += 50000) {
sendingQueue.offer(new Inv.Builder()
.inventory(inventory.subList(i, Math.min(inventory.size(), i + 50000)))
.build());
sendingQueue.offer(new Inv(inventory.subList(i, Math.min(inventory.size(), i + 50000))));
}
}
@ -328,10 +324,6 @@ public abstract class AbstractConnection {
protected abstract void send(MessagePayload payload);
public enum Mode {SERVER, CLIENT, SYNC}
public enum State {CONNECTING, ACTIVE, DISCONNECTED}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -344,4 +336,8 @@ public abstract class AbstractConnection {
public int hashCode() {
return Objects.hash(node);
}
public enum Mode {SERVER, CLIENT, SYNC}
public enum State {CONNECTING, ACTIVE, DISCONNECTED}
}

View File

@ -26,7 +26,7 @@ import java.util.Iterator;
import java.util.List;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.CLIENT;
import static ch.dissem.bitmessage.networking.DefaultNetworkHandler.NETWORK_MAGIC_NUMBER;
import static ch.dissem.bitmessage.constants.Network.NETWORK_MAGIC_NUMBER;
/**
* @author Christian Basler

View File

@ -35,6 +35,7 @@ import java.net.Socket;
import java.util.*;
import java.util.concurrent.*;
import static ch.dissem.bitmessage.constants.Network.NETWORK_MAGIC_NUMBER;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SERVER;
import static ch.dissem.bitmessage.networking.AbstractConnection.State.ACTIVE;
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
@ -193,7 +194,7 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
}
return new Property("network", null,
new Property("connectionManager", running ? "running" : "stopped"),
new Property("connections", null, streamProperties),
new Property("connections", streamProperties),
new Property("requestedObjects", requestedObjects.size())
);
}
@ -222,7 +223,7 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
if (connection.knowsOf(next)) {
List<InventoryVector> ivs = distribution.get(connection);
if (ivs.size() == GetData.MAX_INVENTORY_SIZE) {
connection.send(new GetData.Builder().inventory(ivs).build());
connection.send(new GetData(ivs));
ivs.clear();
}
ivs.add(next);
@ -241,7 +242,7 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
for (Connection connection : distribution.keySet()) {
List<InventoryVector> ivs = distribution.get(connection);
if (!ivs.isEmpty()) {
connection.send(new GetData.Builder().inventory(ivs).build());
connection.send(new GetData(ivs));
}
}
}

View File

@ -37,12 +37,10 @@ public class ServerRunnable implements Runnable, Closeable {
private final InternalContext ctx;
private final ServerSocket serverSocket;
private final DefaultNetworkHandler networkHandler;
private final NetworkHandler.MessageListener listener;
public ServerRunnable(InternalContext ctx, DefaultNetworkHandler networkHandler) throws IOException {
this.ctx = ctx;
this.networkHandler = networkHandler;
this.listener = ctx.getNetworkListener();
this.serverSocket = new ServerSocket(ctx.getPort());
}

View File

@ -25,7 +25,9 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.exception.NodeException;
import ch.dissem.bitmessage.factory.V3MessageReader;
import ch.dissem.bitmessage.networking.AbstractConnection;
import ch.dissem.bitmessage.ports.NetworkHandler;
import ch.dissem.bitmessage.utils.DebugUtils;
import ch.dissem.bitmessage.utils.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -39,11 +41,14 @@ import java.nio.channels.*;
import java.util.*;
import java.util.concurrent.*;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.*;
import static ch.dissem.bitmessage.constants.Network.HEADER_SIZE;
import static ch.dissem.bitmessage.constants.Network.NETWORK_MAGIC_NUMBER;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.CLIENT;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SERVER;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SYNC;
import static ch.dissem.bitmessage.networking.AbstractConnection.State.ACTIVE;
import static ch.dissem.bitmessage.networking.AbstractConnection.State.DISCONNECTED;
import static ch.dissem.bitmessage.utils.Collections.selectRandom;
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
import static ch.dissem.bitmessage.utils.ThreadFactoryBuilder.pool;
import static java.nio.channels.SelectionKey.*;
@ -434,7 +439,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
if (connection.knowsOf(next) && !connection.requested(next)) {
List<InventoryVector> ivs = distribution.get(connection);
if (ivs.size() == GetData.MAX_INVENTORY_SIZE) {
connection.send(new GetData.Builder().inventory(ivs).build());
connection.send(new GetData(ivs));
ivs.clear();
}
ivs.add(next);
@ -458,7 +463,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
for (ConnectionInfo connection : distribution.keySet()) {
List<InventoryVector> ivs = distribution.get(connection);
if (!ivs.isEmpty()) {
connection.send(new GetData.Builder().inventory(ivs).build());
connection.send(new GetData(ivs));
}
}
}
@ -474,9 +479,9 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
for (long stream : connection.getStreams()) {
streams.add(stream);
if (connection.getMode() == SERVER) {
inc(incomingConnections, stream);
DebugUtils.inc(incomingConnections, stream);
} else {
inc(outgoingConnections, stream);
DebugUtils.inc(outgoingConnections, stream);
}
}
}
@ -495,7 +500,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
}
return new Property("network", null,
new Property("connectionManager", isRunning() ? "running" : "stopped"),
new Property("connections", null, streamProperties),
new Property("connections", streamProperties),
new Property("requestedObjects", requestedObjects.size())
);
}