Skip to content

Commit

Permalink
Added a test for the ObjectIntMap, improved the code coverage & reworked
Browse files Browse the repository at this point in the history
the server discovery.
  • Loading branch information
crykn committed Aug 15, 2019
1 parent 3f9cc2e commit 2060384
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 30 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![KryoNet](https://raw.github.com/wiki/EsotericSoftware/kryonet/images/logo.jpg)

[![Release](https://jitpack.io/v/crykn/kryonet.svg)](https://jitpack.io/#crykn/kryonet)
[![Release](https://jitpack.io/v/crykn/kryonet.svg)](https://jitpack.io/#crykn/kryonet) [![Build Status](https://travis-ci.org/crykn/quakemonkey.svg?branch=master)](https://travis-ci.org/crykn/quakemonkey)

A fork of [KryoNet](https://github.com/EsotericSoftware/kryonet/), a Java library that provides a clean and simple API for efficient network communication.

Expand All @@ -13,6 +13,7 @@ This fork was specifically made for [ProjektGG](https://github.com/eskalon/Proje
* Includes a fix for the common Android 5.0 crash ([#106](https://github.com/EsotericSoftware/kryonet/issues/106), [#120](https://github.com/EsotericSoftware/kryonet/issues/106))
* The LAN Host Discovery is now available to Non-Kryo-Serializations ([#127](https://github.com/EsotericSoftware/kryonet/issues/127))
* Kryonet now uses a [gradle](https://gradle.org/) build setup
* Improved the tests as well as the code coverage
* Java 9+ is supported
* Various improvements to the documentation (also takes care of [#35](https://github.com/EsotericSoftware/kryonet/issues/35), [#44](https://github.com/EsotericSoftware/kryonet/issues/44), [#124](https://github.com/EsotericSoftware/kryonet/issues/124), [#137](https://github.com/EsotericSoftware/kryonet/issues/137))
* And a lot more minor fixes and changes
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/com/esotericsoftware/kryonet/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
Expand All @@ -41,7 +42,7 @@
import java.nio.channels.Selector;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -608,27 +609,27 @@ private void broadcast(int udpPort, DatagramSocket socket)
dataBuffer.flip();
byte[] data = new byte[dataBuffer.limit()];
dataBuffer.get(data);
for (NetworkInterface iface : Collections
.list(NetworkInterface.getNetworkInterfaces())) {
for (InetAddress address : Collections
.list(iface.getInetAddresses())) {
// Java 1.5 doesn't support getting the subnet mask, so try the
// two most common.
byte[] ip = address.getAddress();
ip[3] = -1; // 255.255.255.0
try {
socket.send(new DatagramPacket(data, data.length,
InetAddress.getByAddress(ip), udpPort));
} catch (Exception ignored) {
}
ip[2] = -1; // 255.255.0.0
try {
socket.send(new DatagramPacket(data, data.length,
InetAddress.getByAddress(ip), udpPort));
} catch (Exception ignored) {
}

Enumeration<NetworkInterface> interfaces = NetworkInterface
.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();

if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue;
}

for (InterfaceAddress address : networkInterface
.getInterfaceAddresses()) {
InetAddress broadcast = address.getBroadcast();
if (broadcast == null)
continue;

socket.send(new DatagramPacket(data, data.length, broadcast,
udpPort));
}
}

if (DEBUG)
debug("kryonet", "Broadcasted host discovery on port: " + udpPort);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.esotericsoftware.kryonet;

public class KryoNetException extends RuntimeException {
private static final long serialVersionUID = 1L;

public KryoNetException() {
super();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/esotericsoftware/kryonet/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Server implements EndPoint {
private ServerSocketChannel serverChannel;
private UdpConnection udp;
private Connection[] connections = {};
private IntMap<Connection> pendingConnections = new IntMap();
private IntMap<Connection> pendingConnections = new IntMap<>();
Listener[] listeners = {};
private Object listenerLock = new Object();
private int nextConnectionID = 1;
Expand Down Expand Up @@ -614,7 +614,7 @@ private void addConnection(Connection connection) {
}

void removeConnection(Connection connection) {
ArrayList<Connection> temp = new ArrayList(Arrays.asList(connections));
ArrayList<Connection> temp = new ArrayList<>(Arrays.asList(connections));
temp.remove(connection);
connections = temp.toArray(new Connection[temp.size()]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
* @author Nathan Sweet <misc@n4te.com>
*/
class TcpConnection {
private static final int IPTOS_LOWDELAY = 0x10;


SocketChannel socketChannel;
int keepAliveMillis = 8000;
final ByteBuffer readBuffer, writeBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* int, Class...)
*/
public class TimeoutException extends RuntimeException {
private static final long serialVersionUID = 1L;

public TimeoutException() {
super();
}
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/com/esotericsoftware/kryonet/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void testJson() throws IOException {
new JsonSerialization());
startEndPoint(server);
server.bind(tcpPort, udpPort);
server.addListener(new Listener() {
Listener listener;
server.addListener(listener = new Listener() {
public void connected(Connection connection) {
connection.sendTCP(dataTCP);
connection.sendUDP(dataUDP); // Note UDP ping pong stops if a
Expand Down Expand Up @@ -104,6 +105,12 @@ public void received(Connection connection, Object object) {

waitForThreads(5000);

if (fail == null) {
server.removeListener(listener);
assertEquals(0, server.listeners.length);
}


if (fail != null)
fail(fail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ abstract public class KryoNetTestCase extends TestCase {
static public String host = "localhost";
static public int tcpPort = 54555, udpPort = 54777;

private ArrayList<Thread> threads = new ArrayList();
ArrayList<EndPoint> endPoints = new ArrayList();
private ArrayList<Thread> threads = new ArrayList<>();
ArrayList<EndPoint> endPoints = new ArrayList<>();
private Timer timer;
boolean fail;

public KryoNetTestCase() {
// Log.TRACE();
Log.TRACE();
// Log.DEBUG();
Log.setLogger(new Logger() {
public void log(int level, String category, String message,
Expand Down Expand Up @@ -99,7 +99,7 @@ public void run() {
};
timer.schedule(failTask, 13000);
while (true) {
for (Iterator iter = threads.iterator(); iter.hasNext();) {
for (Iterator<Thread> iter = threads.iterator(); iter.hasNext();) {
Thread thread = (Thread) iter.next();
if (!thread.isAlive())
iter.remove();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.esotericsoftware.kryonet.util;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class ObjectIntMapTest {

@Test(expected = IllegalArgumentException.class)
public void testConstructor() {
new ObjectIntMap(-1, 0);
}

@Test(expected = IllegalArgumentException.class)
public void testConstructor2() {
new ObjectIntMap((1 << 30) + 1, 0);
}

@Test(expected = IllegalArgumentException.class)
public void testConstructor3() {
new ObjectIntMap(15, 0);
}

@Test(expected = IllegalArgumentException.class)
public void testConstructor4() {
new ObjectIntMap(15, -1);
}

@Test
public void testConstructor5() {
ObjectIntMap tmp = new ObjectIntMap(15, 2);
tmp.put("S", 3);
tmp.put("A", 11);
tmp.put("S", 2);

ObjectIntMap map2 = new ObjectIntMap<>(tmp);

assertEquals(tmp.capacity, map2.capacity);
assertArrayEquals(tmp.keyTable, map2.keyTable);
assertEquals(tmp.size, map2.size);
assertEquals(tmp.stashSize, map2.stashSize);
assertArrayEquals(tmp.valueTable, map2.valueTable);
}

@Test(expected = NullPointerException.class)
public void testPut() {
ObjectIntMap tmp = new ObjectIntMap(15, 2);
tmp.put(null, 3);
}

@Test
public void testStuff() {
ObjectIntMap tmp = new ObjectIntMap(4, 2);

// toString() - 1
assertEquals("{}", tmp.toString());

// put(x, y)
tmp.put("S", 3);
tmp.put("A", 11);

assertEquals(3, tmp.get("S", -1));
assertEquals(2, tmp.size);

tmp.put("S", 2);
tmp.put("K", 5);

assertEquals(2, tmp.get("S", -1));

// remove(x, y)
assertEquals(5, tmp.remove("K", -1));

// toString() - 2
assertEquals("{S=2, A=11}", tmp.toString());

// ensureCapacity(x)
assertEquals(4, tmp.capacity);

tmp.ensureCapacity(5);
assertEquals(4, tmp.capacity);

tmp.ensureCapacity(50);
assertEquals(32, tmp.capacity);

// findKex(y)
assertEquals("A", tmp.findKey(11));

// containsKey/Value(x)
assertTrue(tmp.containsKey("S"));
assertFalse(tmp.containsKey("G"));
assertTrue(tmp.containsValue(11));
assertFalse(tmp.containsValue(26));
}

}

0 comments on commit 2060384

Please sign in to comment.