Updated serialization package. Updated RMI tests to include both network
connections and local connections.
This commit is contained in:
parent
efb7ef9770
commit
3aee8c93a7
23
test/dorkbox/network/rmi/Config.java
Normal file
23
test/dorkbox/network/rmi/Config.java
Normal 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);
|
||||||
|
}
|
|
@ -4,8 +4,23 @@ package dorkbox.network.rmi;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
class MessageWithTestCow implements RmiMessages {
|
class MessageWithTestCow {
|
||||||
public int number;
|
public int number;
|
||||||
public String text;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ package dorkbox.network.rmi;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -49,6 +48,7 @@ import dorkbox.network.Configuration;
|
||||||
import dorkbox.network.Server;
|
import dorkbox.network.Server;
|
||||||
import dorkbox.network.connection.Connection;
|
import dorkbox.network.connection.Connection;
|
||||||
import dorkbox.network.connection.ConnectionImpl;
|
import dorkbox.network.connection.ConnectionImpl;
|
||||||
|
import dorkbox.network.connection.EndPointBase;
|
||||||
import dorkbox.network.connection.Listener;
|
import dorkbox.network.connection.Listener;
|
||||||
import dorkbox.network.serialization.Serialization;
|
import dorkbox.network.serialization.Serialization;
|
||||||
import dorkbox.util.exceptions.InitializationException;
|
import dorkbox.util.exceptions.InitializationException;
|
||||||
|
@ -168,11 +168,11 @@ class RmiGlobalTest extends BaseTest {
|
||||||
assertEquals(123.0F, slow, 0.0001D);
|
assertEquals(123.0F, slow, 0.0001D);
|
||||||
|
|
||||||
|
|
||||||
// Test sending a reference to a remote object.
|
// Test sending a reference to a remote object (the receiving end should receive the IMPL object, not the proxy object)
|
||||||
MessageWithTestCow m = new MessageWithTestCow();
|
MessageWithTestCow m = new MessageWithTestCow(test);
|
||||||
m.number = 678;
|
m.number = 678;
|
||||||
m.text = "sometext";
|
m.text = "sometext";
|
||||||
m.testCow = test;
|
|
||||||
connection.send()
|
connection.send()
|
||||||
.TCP(m)
|
.TCP(m)
|
||||||
.flush();
|
.flush();
|
||||||
|
@ -189,11 +189,34 @@ class RmiGlobalTest extends BaseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public
|
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 configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.udpPort = udpPort;
|
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
register(configuration.serialization);
|
register(configuration.serialization);
|
||||||
|
@ -218,27 +241,22 @@ class RmiGlobalTest extends BaseTest {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void connected(final Connection connection) {
|
void connected(final Connection connection) {
|
||||||
try {
|
connection.getRemoteObject(CLIENT_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
|
||||||
connection.getRemoteObject(CLIENT_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestCow remoteObject) {
|
||||||
void created(final TestCow remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
System.err.println("Running test for: Server (LOCAL) -> Client (REMOTE)");
|
||||||
System.err.println("Running test for: Server -> Client");
|
runTest(connection, globalRemoteClientObject, remoteObject, CLIENT_GLOBAL_OBJECT_ID);
|
||||||
runTest(connection, globalRemoteClientObject, remoteObject, CLIENT_GLOBAL_OBJECT_ID);
|
System.err.println("Done with test for: Server (LOCAL) -> Client (REMOTE)");
|
||||||
System.err.println("Done with test for: Server -> Client");
|
}
|
||||||
}
|
}.start();
|
||||||
}.start();
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -247,13 +265,13 @@ class RmiGlobalTest extends BaseTest {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void received(Connection connection, MessageWithTestCow m) {
|
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();
|
final int id = object.id();
|
||||||
assertEquals(SERVER_GLOBAL_OBJECT_ID, 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);
|
stopEndPoints(2000);
|
||||||
}
|
}
|
||||||
|
@ -262,9 +280,7 @@ class RmiGlobalTest extends BaseTest {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
configuration = new Configuration();
|
configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.udpPort = udpPort;
|
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
register(configuration.serialization);
|
register(configuration.serialization);
|
||||||
|
@ -290,36 +306,36 @@ class RmiGlobalTest extends BaseTest {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void received(final Connection connection, MessageWithTestCow m) {
|
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();
|
final int id = object.id();
|
||||||
assertEquals(CLIENT_GLOBAL_OBJECT_ID, 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
|
// 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>() {
|
||||||
connection.getRemoteObject(SERVER_GLOBAL_OBJECT_ID, new RemoteObjectCallback<TestCow>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestCow remoteObject) {
|
||||||
void created(final TestCow remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
System.err.println("Running test for: Client (LOCAL) -> Server (REMOTE)");
|
||||||
System.err.println("Running test for: Client -> Server");
|
runTest(connection, globalRemoteServerObject, remoteObject, SERVER_GLOBAL_OBJECT_ID);
|
||||||
runTest(connection, globalRemoteServerObject, remoteObject, SERVER_GLOBAL_OBJECT_ID);
|
System.err.println("Done with test for: Client (LOCAL) -> Server (REMOTE)");
|
||||||
System.err.println("Done with test for: Client -> Server");
|
}
|
||||||
}
|
}.start();
|
||||||
}.start();
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ import dorkbox.network.Client;
|
||||||
import dorkbox.network.Configuration;
|
import dorkbox.network.Configuration;
|
||||||
import dorkbox.network.Server;
|
import dorkbox.network.Server;
|
||||||
import dorkbox.network.connection.Connection;
|
import dorkbox.network.connection.Connection;
|
||||||
|
import dorkbox.network.connection.EndPointBase;
|
||||||
import dorkbox.network.connection.Listener;
|
import dorkbox.network.connection.Listener;
|
||||||
import dorkbox.network.serialization.Serialization;
|
import dorkbox.network.serialization.Serialization;
|
||||||
import dorkbox.util.exceptions.InitializationException;
|
import dorkbox.util.exceptions.InitializationException;
|
||||||
|
@ -37,6 +38,31 @@ import dorkbox.util.exceptions.SecurityException;
|
||||||
public
|
public
|
||||||
class RmiSendObjectOverrideMethodTest extends BaseTest {
|
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
|
* 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.
|
* 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
|
* 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.
|
* instead of the method that would NORMALLY be called.
|
||||||
*/
|
*/
|
||||||
@Test
|
|
||||||
public
|
public
|
||||||
void rmi() throws InitializationException, SecurityException, IOException, InterruptedException {
|
void rmi(final Config config) throws InitializationException, SecurityException, IOException, InterruptedException {
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
configuration.serialization.registerRmiImplementation(TestObject.class, TestObjectImpl.class);
|
configuration.serialization.registerRmiImplementation(TestObject.class, TestObjectImpl.class);
|
||||||
|
@ -97,8 +121,7 @@ class RmiSendObjectOverrideMethodTest extends BaseTest {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
configuration = new Configuration();
|
configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
configuration.serialization.registerRmiInterface(TestObject.class);
|
configuration.serialization.registerRmiInterface(TestObject.class);
|
||||||
|
@ -113,48 +136,42 @@ class RmiSendObjectOverrideMethodTest extends BaseTest {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void connected(final Connection connection) {
|
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...
|
||||||
// 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>() {
|
||||||
connection.getRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestObject remoteObject) {
|
||||||
void created(final TestObject remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
remoteObject.setOther(43.21f);
|
||||||
remoteObject.setOther(43.21f);
|
|
||||||
|
|
||||||
// Normal remote method call.
|
// Normal remote method call.
|
||||||
assertEquals(43.21f, remoteObject.other(), .0001f);
|
assertEquals(43.21f, remoteObject.other(), .0001f);
|
||||||
|
|
||||||
// Make a remote method call that returns another remote proxy object.
|
// 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.
|
// the "test" object exists in the REMOTE side, as does the "OtherObject" that is created.
|
||||||
// here we have a proxy to both of them.
|
// here we have a proxy to both of them.
|
||||||
OtherObject otherObject = remoteObject.getOtherObject();
|
OtherObject otherObject = remoteObject.getOtherObject();
|
||||||
|
|
||||||
// Normal remote method call on the second object.
|
// Normal remote method call on the second object.
|
||||||
otherObject.setValue(12.34f);
|
otherObject.setValue(12.34f);
|
||||||
|
|
||||||
float value = otherObject.value();
|
float value = otherObject.value();
|
||||||
assertEquals(12.34f, value, .0001f);
|
assertEquals(12.34f, value, .0001f);
|
||||||
|
|
||||||
// When a proxy object is sent, the other side receives its ACTUAL object (not a proxy of it), because
|
// 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.
|
// that is where that object acutally exists.
|
||||||
// we have to manually flush, since we are in a separate thread that does not auto-flush.
|
// we have to manually flush, since we are in a separate thread that does not auto-flush.
|
||||||
connection.send()
|
connection.send()
|
||||||
.TCP(otherObject)
|
.TCP(otherObject)
|
||||||
.flush();
|
.flush();
|
||||||
}
|
}
|
||||||
}.start();
|
}.start();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ import dorkbox.network.Client;
|
||||||
import dorkbox.network.Configuration;
|
import dorkbox.network.Configuration;
|
||||||
import dorkbox.network.Server;
|
import dorkbox.network.Server;
|
||||||
import dorkbox.network.connection.Connection;
|
import dorkbox.network.connection.Connection;
|
||||||
|
import dorkbox.network.connection.EndPointBase;
|
||||||
import dorkbox.network.connection.Listener;
|
import dorkbox.network.connection.Listener;
|
||||||
import dorkbox.network.serialization.Serialization;
|
import dorkbox.network.serialization.Serialization;
|
||||||
import dorkbox.util.exceptions.InitializationException;
|
import dorkbox.util.exceptions.InitializationException;
|
||||||
|
@ -56,16 +57,39 @@ import dorkbox.util.exceptions.SecurityException;
|
||||||
public
|
public
|
||||||
class RmiSendObjectTest extends BaseTest {
|
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
|
* 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.
|
* uses the first remote object to get the second remote object.
|
||||||
*/
|
*/
|
||||||
@Test
|
|
||||||
public
|
public
|
||||||
void rmi() throws InitializationException, SecurityException, IOException, InterruptedException {
|
void rmi(final Config config) throws InitializationException, SecurityException, IOException, InterruptedException {
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
configuration.serialization.registerRmiImplementation(TestObject.class, TestObjectImpl.class);
|
configuration.serialization.registerRmiImplementation(TestObject.class, TestObjectImpl.class);
|
||||||
|
@ -99,8 +123,7 @@ class RmiSendObjectTest extends BaseTest {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
configuration = new Configuration();
|
configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
configuration.serialization.registerRmiInterface(TestObject.class);
|
configuration.serialization.registerRmiInterface(TestObject.class);
|
||||||
|
@ -116,42 +139,37 @@ class RmiSendObjectTest extends BaseTest {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void connected(final Connection connection) {
|
void connected(final Connection connection) {
|
||||||
try {
|
connection.createRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
|
||||||
connection.getRemoteObject(TestObject.class, new RemoteObjectCallback<TestObject>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestObject remoteObject) {
|
||||||
void created(final TestObject remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
remoteObject.setOther(43.21f);
|
||||||
remoteObject.setOther(43.21f);
|
|
||||||
|
|
||||||
// Normal remote method call.
|
// Normal remote method call.
|
||||||
assertEquals(43.21f, remoteObject.other(), 0.0001F);
|
assertEquals(43.21f, remoteObject.other(), 0.0001F);
|
||||||
|
|
||||||
// Make a remote method call that returns another remote proxy object.
|
// Make a remote method call that returns another remote proxy object.
|
||||||
OtherObject otherObject = remoteObject.getOtherObject();
|
OtherObject otherObject = remoteObject.getOtherObject();
|
||||||
|
|
||||||
// Normal remote method call on the second object.
|
// Normal remote method call on the second object.
|
||||||
otherObject.setValue(12.34f);
|
otherObject.setValue(12.34f);
|
||||||
float value = otherObject.value();
|
float value = otherObject.value();
|
||||||
assertEquals(12.34f, value, 0.0001F);
|
assertEquals(12.34f, value, 0.0001F);
|
||||||
|
|
||||||
// When a remote proxy object is sent, the other side receives its actual remote object.
|
// 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.
|
// we have to manually flush, since we are in a separate thread that does not auto-flush.
|
||||||
connection.send()
|
connection.send()
|
||||||
.TCP(otherObject)
|
.TCP(otherObject)
|
||||||
.flush();
|
.flush();
|
||||||
}
|
}
|
||||||
}.start();
|
}.start();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,6 @@ package dorkbox.network.rmi;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -48,6 +47,7 @@ import dorkbox.network.Client;
|
||||||
import dorkbox.network.Configuration;
|
import dorkbox.network.Configuration;
|
||||||
import dorkbox.network.Server;
|
import dorkbox.network.Server;
|
||||||
import dorkbox.network.connection.Connection;
|
import dorkbox.network.connection.Connection;
|
||||||
|
import dorkbox.network.connection.EndPointBase;
|
||||||
import dorkbox.network.connection.Listener;
|
import dorkbox.network.connection.Listener;
|
||||||
import dorkbox.network.connection.Listeners;
|
import dorkbox.network.connection.Listeners;
|
||||||
import dorkbox.network.serialization.Serialization;
|
import dorkbox.network.serialization.Serialization;
|
||||||
|
@ -159,10 +159,9 @@ class RmiTest extends BaseTest {
|
||||||
|
|
||||||
|
|
||||||
// Test sending a reference to a remote object.
|
// Test sending a reference to a remote object.
|
||||||
MessageWithTestCow m = new MessageWithTestCow();
|
MessageWithTestCow m = new MessageWithTestCow(test);
|
||||||
m.number = 678;
|
m.number = 678;
|
||||||
m.text = "sometext";
|
m.text = "sometext";
|
||||||
m.testCow = test;
|
|
||||||
connection.send()
|
connection.send()
|
||||||
.TCP(m)
|
.TCP(m)
|
||||||
.flush();
|
.flush();
|
||||||
|
@ -179,11 +178,45 @@ class RmiTest extends BaseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public
|
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 configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.udpPort = udpPort;
|
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
register(configuration.serialization);
|
register(configuration.serialization);
|
||||||
|
@ -208,7 +241,7 @@ class RmiTest extends BaseTest {
|
||||||
void received(final Connection connection, MessageWithTestCow m) {
|
void received(final 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 -> Server");
|
||||||
|
|
||||||
TestCow object = m.testCow;
|
TestCow object = m.getTestCow();
|
||||||
final int id = object.id();
|
final int id = object.id();
|
||||||
assertEquals(1, id);
|
assertEquals(1, id);
|
||||||
System.err.println("Finished test for: Client -> Server");
|
System.err.println("Finished test for: Client -> Server");
|
||||||
|
@ -217,37 +250,30 @@ class RmiTest extends BaseTest {
|
||||||
System.err.println("Starting test for: Server -> Client");
|
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
|
// 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...
|
||||||
// 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>() {
|
||||||
connection.getRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestCow remoteObject) {
|
||||||
void created(final TestCow remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
System.err.println("Running test for: Server -> Client");
|
||||||
System.err.println("Running test for: Server -> Client");
|
runTests(connection, remoteObject, 2);
|
||||||
runTests(connection, remoteObject, 2);
|
System.err.println("Done with test for: Server -> Client");
|
||||||
System.err.println("Done with test for: Server -> Client");
|
}
|
||||||
}
|
}.start();
|
||||||
}.start();
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
configuration = new Configuration();
|
configuration = new Configuration();
|
||||||
configuration.tcpPort = tcpPort;
|
config.apply(configuration);
|
||||||
configuration.udpPort = udpPort;
|
|
||||||
configuration.host = host;
|
|
||||||
|
|
||||||
configuration.serialization = Serialization.DEFAULT();
|
configuration.serialization = Serialization.DEFAULT();
|
||||||
register(configuration.serialization);
|
register(configuration.serialization);
|
||||||
|
@ -271,28 +297,23 @@ class RmiTest extends BaseTest {
|
||||||
void connected(final Connection connection) {
|
void connected(final Connection connection) {
|
||||||
System.err.println("Starting test for: Client -> Server");
|
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...
|
||||||
// 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>() {
|
||||||
connection.getRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestCow remoteObject) {
|
||||||
void created(final TestCow remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
System.err.println("Running test for: Client -> Server");
|
||||||
System.err.println("Running test for: Client -> Server");
|
runTests(connection, remoteObject, 1);
|
||||||
runTests(connection, remoteObject, 1);
|
System.err.println("Done with test for: Client -> Server");
|
||||||
System.err.println("Done with test for: Client -> Server");
|
}
|
||||||
}
|
}.start();
|
||||||
}.start();
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -303,7 +324,7 @@ class RmiTest extends BaseTest {
|
||||||
void received(Connection connection, MessageWithTestCow m) {
|
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 -> Server");
|
||||||
|
|
||||||
TestCow object = m.testCow;
|
TestCow object = m.getTestCow();
|
||||||
final int id = object.id();
|
final int id = object.id();
|
||||||
assertEquals(2, id);
|
assertEquals(2, id);
|
||||||
System.err.println("Finished test for: Client -> Server");
|
System.err.println("Finished test for: Client -> Server");
|
||||||
|
|
|
@ -1,15 +1,10 @@
|
||||||
package dorkbox.network.rmi;
|
package dorkbox.network.rmi;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
class TestCowBaseImpl implements TestCowBase {
|
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
|
public
|
||||||
TestCowBaseImpl() {
|
TestCowBaseImpl() {
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
package dorkbox.network.rmi;
|
package dorkbox.network.rmi;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
class TestCowImpl extends TestCowBaseImpl implements TestCow {
|
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 int moos;
|
||||||
private final int id = ID_COUNTER.getAndIncrement();
|
private final int id = ID_COUNTER.getAndIncrement();
|
||||||
|
|
||||||
|
@ -23,14 +28,14 @@ class TestCowImpl extends TestCowBaseImpl implements TestCow {
|
||||||
public
|
public
|
||||||
void moo(String value) {
|
void moo(String value) {
|
||||||
this.moos += 2;
|
this.moos += 2;
|
||||||
System.out.println("Moo! " + this.moos + " :" + value);
|
System.out.println("Moo! " + this.moos + ": " + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void moo(String value, long delay) {
|
void moo(String value, long delay) {
|
||||||
this.moos += 4;
|
this.moos += 4;
|
||||||
System.out.println("Moo! " + this.moos + " :" + value + " (" + delay + ")");
|
System.out.println("Moo! " + this.moos + ": " + value + " (" + delay + ")");
|
||||||
try {
|
try {
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
package dorkbox.network.rmi.multiJVM;
|
package dorkbox.network.rmi.multiJVM;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ch.qos.logback.classic.Level;
|
import ch.qos.logback.classic.Level;
|
||||||
|
@ -98,35 +94,30 @@ class TestClient
|
||||||
void connected(final Connection connection) {
|
void connected(final Connection connection) {
|
||||||
System.err.println("Starting test for: Client -> Server");
|
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...
|
||||||
// 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>() {
|
||||||
connection.getRemoteObject(TestCow.class, new RemoteObjectCallback<TestCow>() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void created(final TestCow remoteObject) {
|
||||||
void created(final TestCow remoteObject) {
|
// MUST run on a separate thread because remote object method invocations are blocking
|
||||||
// MUST run on a separate thread because remote object method invocations are blocking
|
new Thread() {
|
||||||
new Thread() {
|
@Override
|
||||||
@Override
|
public
|
||||||
public
|
void run() {
|
||||||
void run() {
|
RmiTest.runTests(connection, remoteObject, 1);
|
||||||
RmiTest.runTests(connection, remoteObject, 1);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000L);
|
Thread.sleep(1000L);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
|
||||||
|
|
||||||
System.err.println("DONE");
|
|
||||||
client.stop();
|
|
||||||
}
|
}
|
||||||
}.start();
|
|
||||||
}
|
System.err.println("DONE");
|
||||||
});
|
client.stop();
|
||||||
} catch (IOException e) {
|
}
|
||||||
e.printStackTrace();
|
}.start();
|
||||||
fail();
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user