Updated serialization package. Updated RMI tests to include both network

connections and local connections.
This commit is contained in:
nathan 2018-01-22 15:58:20 +01:00
parent efb7ef9770
commit 3aee8c93a7
9 changed files with 337 additions and 236 deletions

View File

@ -0,0 +1,23 @@
/*
* Copyright 2018 dorkbox, llc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.network.rmi;
import dorkbox.network.Configuration;
public
interface Config {
void apply(Configuration configuration);
}

View File

@ -4,8 +4,23 @@ package dorkbox.network.rmi;
*
*/
public
class MessageWithTestCow implements RmiMessages {
class MessageWithTestCow {
public int number;
public String text;
public TestCow testCow;
private TestCow testCow;
private
MessageWithTestCow() {
// for kryo
}
public
MessageWithTestCow(final TestCow test) {
testCow = test;
}
public
TestCow getTestCow() {
return testCow;
}
}

View File

@ -37,7 +37,6 @@ package dorkbox.network.rmi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
@ -49,6 +48,7 @@ import dorkbox.network.Configuration;
import dorkbox.network.Server;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.EndPointBase;
import dorkbox.network.connection.Listener;
import dorkbox.network.serialization.Serialization;
import dorkbox.util.exceptions.InitializationException;
@ -168,11 +168,11 @@ class RmiGlobalTest extends BaseTest {
assertEquals(123.0F, slow, 0.0001D);
// Test sending a reference to a remote object.
MessageWithTestCow m = new MessageWithTestCow();
// Test sending a reference to a remote object (the receiving end should receive the IMPL object, not the proxy object)
MessageWithTestCow m = new MessageWithTestCow(test);
m.number = 678;
m.text = "sometext";
m.testCow = test;
connection.send()
.TCP(m)
.flush();
@ -189,11 +189,34 @@ class RmiGlobalTest extends BaseTest {
@Test
public
void rmi() throws InitializationException, SecurityException, IOException, InterruptedException {
void rmiNetwork() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host;
}
});
}
@Test
public
void rmiLocal() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.localChannelName = EndPointBase.LOCAL_CHANNEL;
}
});
}
public
void rmi(final Config config) throws InitializationException, SecurityException, IOException, InterruptedException {
Configuration configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
register(configuration.serialization);
@ -218,27 +241,22 @@ class RmiGlobalTest extends BaseTest {
@Override
public
void connected(final Connection connection) {
try {
connection.getRemoteObject(CLIENT_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Server -> Client");
runTest(connection, globalRemoteClientObject, remoteObject, CLIENT_GLOBAL_OBJECT_ID);
System.err.println("Done with test for: Server -> Client");
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
connection.getRemoteObject(CLIENT_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Server (LOCAL) -> Client (REMOTE)");
runTest(connection, globalRemoteClientObject, remoteObject, CLIENT_GLOBAL_OBJECT_ID);
System.err.println("Done with test for: Server (LOCAL) -> Client (REMOTE)");
}
}.start();
}
});
}
});
@ -247,13 +265,13 @@ class RmiGlobalTest extends BaseTest {
@Override
public
void received(Connection connection, MessageWithTestCow m) {
System.err.println("Received finish signal for test for: Client -> Server");
System.err.println("Received finish signal for test for: Client (LOCAL) -> Server (REMOTE)");
TestCow object = m.testCow;
TestCow object = m.getTestCow();
final int id = object.id();
assertEquals(SERVER_GLOBAL_OBJECT_ID, id);
System.err.println("Finished test for: Client -> Server");
System.err.println("Finished test for: Client (LOCAL) -> Server (REMOTE)");
stopEndPoints(2000);
}
@ -262,9 +280,7 @@ class RmiGlobalTest extends BaseTest {
// ----
configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
register(configuration.serialization);
@ -290,36 +306,36 @@ class RmiGlobalTest extends BaseTest {
@Override
public
void received(final Connection connection, MessageWithTestCow m) {
System.err.println("Received finish signal for test for: Server -> Client");
System.err.println("Received finish signal for test for: Server (LOCAL) -> Client (REMOTE)");
TestCow object = m.testCow;
// this TestCow object should be the implementation, not the proxy.
TestCow object = m.getTestCow();
final int id = object.id();
assertEquals(CLIENT_GLOBAL_OBJECT_ID, id);
System.err.println("Finished test for: Server -> Client");
System.err.println("Finished test for: Server (LOCAL) -> Client (REMOTE)");
// connection.send()
// .TCP(m)
// .flush();
// normally this is in the 'connected', but we do it here, so that it's more linear and easier to debug
try {
connection.getRemoteObject(SERVER_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Client -> Server");
runTest(connection, globalRemoteServerObject, remoteObject, SERVER_GLOBAL_OBJECT_ID);
System.err.println("Done with test for: Client -> Server");
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
connection.getRemoteObject(SERVER_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Client (LOCAL) -> Server (REMOTE)");
runTest(connection, globalRemoteServerObject, remoteObject, SERVER_GLOBAL_OBJECT_ID);
System.err.println("Done with test for: Client (LOCAL) -> Server (REMOTE)");
}
}.start();
}
});
}
});

View File

@ -28,6 +28,7 @@ import dorkbox.network.Client;
import dorkbox.network.Configuration;
import dorkbox.network.Server;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.EndPointBase;
import dorkbox.network.connection.Listener;
import dorkbox.network.serialization.Serialization;
import dorkbox.util.exceptions.InitializationException;
@ -37,6 +38,31 @@ import dorkbox.util.exceptions.SecurityException;
public
class RmiSendObjectOverrideMethodTest extends BaseTest {
@Test
public
void rmiNetwork() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.tcpPort = tcpPort;
configuration.host = host;
}
});
}
@Test
public
void rmiLocal() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.localChannelName = EndPointBase.LOCAL_CHANNEL;
}
});
}
/**
* In this test the server has two objects in an object space. The client
* uses the first remote object to get the second remote object.
@ -60,12 +86,10 @@ class RmiSendObjectOverrideMethodTest extends BaseTest {
* The implType (if it exists, with the same name, and with the same signature + connection parameter) will be called from the interface
* instead of the method that would NORMALLY be called.
*/
@Test
public
void rmi() throws InitializationException, SecurityException, IOException, InterruptedException {
void rmi(final Config config) throws InitializationException, SecurityException, IOException, InterruptedException {
Configuration configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
configuration.serialization.registerRmiImplementation(TestObject.class, TestObjectImpl.class);
@ -97,8 +121,7 @@ class RmiSendObjectOverrideMethodTest extends BaseTest {
// ----
configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
configuration.serialization.registerRmiInterface(TestObject.class);
@ -113,48 +136,42 @@ class RmiSendObjectOverrideMethodTest extends BaseTest {
@Override
public
void connected(final Connection connection) {
try {
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.getRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
@Override
public
void created(final TestObject remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
remoteObject.setOther(43.21f);
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.createRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
@Override
public
void created(final TestObject remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
remoteObject.setOther(43.21f);
// Normal remote method call.
assertEquals(43.21f, remoteObject.other(), .0001f);
// Normal remote method call.
assertEquals(43.21f, remoteObject.other(), .0001f);
// Make a remote method call that returns another remote proxy object.
// the "test" object exists in the REMOTE side, as does the "OtherObject" that is created.
// here we have a proxy to both of them.
OtherObject otherObject = remoteObject.getOtherObject();
// Make a remote method call that returns another remote proxy object.
// the "test" object exists in the REMOTE side, as does the "OtherObject" that is created.
// here we have a proxy to both of them.
OtherObject otherObject = remoteObject.getOtherObject();
// Normal remote method call on the second object.
otherObject.setValue(12.34f);
// Normal remote method call on the second object.
otherObject.setValue(12.34f);
float value = otherObject.value();
assertEquals(12.34f, value, .0001f);
float value = otherObject.value();
assertEquals(12.34f, value, .0001f);
// When a proxy object is sent, the other side receives its ACTUAL object (not a proxy of it), because
// that is where that object acutally exists.
// we have to manually flush, since we are in a separate thread that does not auto-flush.
connection.send()
.TCP(otherObject)
.flush();
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
// When a proxy object is sent, the other side receives its ACTUAL object (not a proxy of it), because
// that is where that object acutally exists.
// we have to manually flush, since we are in a separate thread that does not auto-flush.
connection.send()
.TCP(otherObject)
.flush();
}
}.start();
}
});
}
});

View File

@ -47,6 +47,7 @@ import dorkbox.network.Client;
import dorkbox.network.Configuration;
import dorkbox.network.Server;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.EndPointBase;
import dorkbox.network.connection.Listener;
import dorkbox.network.serialization.Serialization;
import dorkbox.util.exceptions.InitializationException;
@ -56,16 +57,39 @@ import dorkbox.util.exceptions.SecurityException;
public
class RmiSendObjectTest extends BaseTest {
@Test
public
void rmiNetwork() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.tcpPort = tcpPort;
configuration.host = host;
}
});
}
@Test
public
void rmiLocal() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.localChannelName = EndPointBase.LOCAL_CHANNEL;
}
});
}
/**
* In this test the server has two objects in an object space. The client
* uses the first remote object to get the second remote object.
*/
@Test
public
void rmi() throws InitializationException, SecurityException, IOException, InterruptedException {
void rmi(final Config config) throws InitializationException, SecurityException, IOException, InterruptedException {
Configuration configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
configuration.serialization.registerRmiImplementation(TestObject.class, TestObjectImpl.class);
@ -99,8 +123,7 @@ class RmiSendObjectTest extends BaseTest {
// ----
configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
configuration.serialization.registerRmiInterface(TestObject.class);
@ -116,42 +139,37 @@ class RmiSendObjectTest extends BaseTest {
@Override
public
void connected(final Connection connection) {
try {
connection.getRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
@Override
public
void created(final TestObject remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
remoteObject.setOther(43.21f);
connection.createRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
@Override
public
void created(final TestObject remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
remoteObject.setOther(43.21f);
// Normal remote method call.
assertEquals(43.21f, remoteObject.other(), 0.0001F);
// Normal remote method call.
assertEquals(43.21f, remoteObject.other(), 0.0001F);
// Make a remote method call that returns another remote proxy object.
OtherObject otherObject = remoteObject.getOtherObject();
// Make a remote method call that returns another remote proxy object.
OtherObject otherObject = remoteObject.getOtherObject();
// Normal remote method call on the second object.
otherObject.setValue(12.34f);
float value = otherObject.value();
assertEquals(12.34f, value, 0.0001F);
// Normal remote method call on the second object.
otherObject.setValue(12.34f);
float value = otherObject.value();
assertEquals(12.34f, value, 0.0001F);
// When a remote proxy object is sent, the other side receives its actual remote object.
// we have to manually flush, since we are in a separate thread that does not auto-flush.
connection.send()
.TCP(otherObject)
.flush();
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
// When a remote proxy object is sent, the other side receives its actual remote object.
// we have to manually flush, since we are in a separate thread that does not auto-flush.
connection.send()
.TCP(otherObject)
.flush();
}
}.start();
}
});
}
});

View File

@ -37,7 +37,6 @@ package dorkbox.network.rmi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
@ -48,6 +47,7 @@ import dorkbox.network.Client;
import dorkbox.network.Configuration;
import dorkbox.network.Server;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.EndPointBase;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.Listeners;
import dorkbox.network.serialization.Serialization;
@ -159,10 +159,9 @@ class RmiTest extends BaseTest {
// Test sending a reference to a remote object.
MessageWithTestCow m = new MessageWithTestCow();
MessageWithTestCow m = new MessageWithTestCow(test);
m.number = 678;
m.text = "sometext";
m.testCow = test;
connection.send()
.TCP(m)
.flush();
@ -179,11 +178,45 @@ class RmiTest extends BaseTest {
@Test
public
void rmi() throws InitializationException, SecurityException, IOException, InterruptedException {
void rmiNetwork() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host;
}
});
// have to reset the object ID counter
TestCowImpl.ID_COUNTER.set(1);
Thread.sleep(2000L);
}
@Test
public
void rmiLocal() throws InitializationException, SecurityException, IOException, InterruptedException {
rmi(new Config() {
@Override
public
void apply(final Configuration configuration) {
configuration.localChannelName = EndPointBase.LOCAL_CHANNEL;
}
});
// have to reset the object ID counter
TestCowImpl.ID_COUNTER.set(1);
Thread.sleep(2000L);
}
public
void rmi(final Config config) throws InitializationException, SecurityException, IOException, InterruptedException {
Configuration configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
register(configuration.serialization);
@ -208,7 +241,7 @@ class RmiTest extends BaseTest {
void received(final Connection connection, MessageWithTestCow m) {
System.err.println("Received finish signal for test for: Client -> Server");
TestCow object = m.testCow;
TestCow object = m.getTestCow();
final int id = object.id();
assertEquals(1, id);
System.err.println("Finished test for: Client -> Server");
@ -217,37 +250,30 @@ class RmiTest extends BaseTest {
System.err.println("Starting test for: Server -> Client");
// normally this is in the 'connected', but we do it here, so that it's more linear and easier to debug
try {
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.getRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Server -> Client");
runTests(connection, remoteObject, 2);
System.err.println("Done with test for: Server -> Client");
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.createRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Server -> Client");
runTests(connection, remoteObject, 2);
System.err.println("Done with test for: Server -> Client");
}
}.start();
}
});
}
});
// ----
configuration = new Configuration();
configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host;
config.apply(configuration);
configuration.serialization = Serialization.DEFAULT();
register(configuration.serialization);
@ -271,28 +297,23 @@ class RmiTest extends BaseTest {
void connected(final Connection connection) {
System.err.println("Starting test for: Client -> Server");
try {
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.getRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Client -> Server");
runTests(connection, remoteObject, 1);
System.err.println("Done with test for: Client -> Server");
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.createRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
System.err.println("Running test for: Client -> Server");
runTests(connection, remoteObject, 1);
System.err.println("Done with test for: Client -> Server");
}
}.start();
}
});
}
});
@ -303,7 +324,7 @@ class RmiTest extends BaseTest {
void received(Connection connection, MessageWithTestCow m) {
System.err.println("Received finish signal for test for: Client -> Server");
TestCow object = m.testCow;
TestCow object = m.getTestCow();
final int id = object.id();
assertEquals(2, id);
System.err.println("Finished test for: Client -> Server");

View File

@ -1,15 +1,10 @@
package dorkbox.network.rmi;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
*/
public
class TestCowBaseImpl implements TestCowBase {
// has to start at 1, because UDP method invocations ignore return values
static final AtomicInteger ID_COUNTER = new AtomicInteger(1);
public
TestCowBaseImpl() {
}

View File

@ -1,10 +1,15 @@
package dorkbox.network.rmi;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
*/
public
class TestCowImpl extends TestCowBaseImpl implements TestCow {
// has to start at 1, because UDP method invocations ignore return values
static final AtomicInteger ID_COUNTER = new AtomicInteger(1);
private int moos;
private final int id = ID_COUNTER.getAndIncrement();
@ -23,14 +28,14 @@ class TestCowImpl extends TestCowBaseImpl implements TestCow {
public
void moo(String value) {
this.moos += 2;
System.out.println("Moo! " + this.moos + " :" + value);
System.out.println("Moo! " + this.moos + ": " + value);
}
@Override
public
void moo(String value, long delay) {
this.moos += 4;
System.out.println("Moo! " + this.moos + " :" + value + " (" + delay + ")");
System.out.println("Moo! " + this.moos + ": " + value + " (" + delay + ")");
try {
Thread.sleep(delay);
} catch (InterruptedException e) {

View File

@ -1,9 +1,5 @@
package dorkbox.network.rmi.multiJVM;
import static org.junit.Assert.fail;
import java.io.IOException;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
@ -98,35 +94,30 @@ class TestClient
void connected(final Connection connection) {
System.err.println("Starting test for: Client -> Server");
try {
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.getRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
RmiTest.runTests(connection, remoteObject, 1);
// if this is called in the dispatch thread, it will block network comms while waiting for a response and it won't work...
connection.createRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
@Override
public
void created(final TestCow remoteObject) {
// MUST run on a separate thread because remote object method invocations are blocking
new Thread() {
@Override
public
void run() {
RmiTest.runTests(connection, remoteObject, 1);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("DONE");
client.stop();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}.start();
}
});
} catch (IOException e) {
e.printStackTrace();
fail();
}
System.err.println("DONE");
client.stop();
}
}.start();
}
});
}
});