2020-07-03 01:45:18 +02:00
|
|
|
/*
|
2020-08-19 15:29:35 +02:00
|
|
|
* Copyright 2020 dorkbox, llc
|
2020-07-03 01:45:18 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-08-12 23:38:56 +02:00
|
|
|
package dorkboxTest.network.rmi
|
2020-07-03 01:45:18 +02:00
|
|
|
|
|
|
|
import dorkbox.network.Client
|
|
|
|
import dorkbox.network.Server
|
|
|
|
import dorkbox.network.connection.Connection
|
2020-08-12 23:38:56 +02:00
|
|
|
import dorkboxTest.network.BaseTest
|
2020-07-03 01:45:18 +02:00
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Test
|
2022-03-15 12:44:52 +01:00
|
|
|
import java.util.concurrent.atomic.*
|
2020-07-03 01:45:18 +02:00
|
|
|
|
2020-08-28 10:22:25 +02:00
|
|
|
|
|
|
|
|
2020-09-01 09:12:30 +02:00
|
|
|
class RmiNestedTest : BaseTest() {
|
2020-07-03 01:45:18 +02:00
|
|
|
companion object {
|
|
|
|
private val idCounter = AtomicInteger()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-06 16:36:56 +02:00
|
|
|
* 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.
|
2020-07-03 01:45:18 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* The MAJOR difference in this version, is that we use an interface to override the methods, so that we can have the RMI system pass
|
|
|
|
* in the connection object.
|
|
|
|
*
|
|
|
|
* Specifically, from CachedMethod.java
|
|
|
|
*
|
|
|
|
* In situations where we want to pass in the Connection (to an RMI method), we have to be able to override method A, with method B.
|
|
|
|
* This is to support calling RMI methods from an interface (that does pass the connection reference) to
|
|
|
|
* an implType, that DOES pass the connection reference. The remote side (that initiates the RMI calls), MUST use
|
|
|
|
* the interface, and the implType may override the method, so that we add the connection as the first in
|
|
|
|
* the list of parameters.
|
|
|
|
*
|
|
|
|
* for example:
|
|
|
|
* Interface: foo(String x)
|
|
|
|
* Impl: foo(Connection c, String x)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-09-01 09:12:30 +02:00
|
|
|
@Test
|
|
|
|
fun biDirectionalDoubleRmi() {
|
2020-07-03 01:45:18 +02:00
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
2021-07-02 11:40:20 +02:00
|
|
|
configuration.serialization.rmi.register(TestObject::class.java, TestObjectAnnotImpl::class.java)
|
|
|
|
configuration.serialization.rmi.register(OtherObject::class.java, OtherObjectImpl::class.java)
|
2020-07-03 01:45:18 +02:00
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onMessage<OtherObject> { message ->
|
2020-07-03 01:45:18 +02:00
|
|
|
// The test is complete when the client sends the OtherObject instance.
|
|
|
|
// this 'object' is the REAL object, not a proxy, because this object is created within this connection.
|
|
|
|
if (message.value() == 12.34f) {
|
|
|
|
stopEndPoints()
|
|
|
|
} else {
|
|
|
|
Assert.fail("Incorrect object value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 03:18:10 +02:00
|
|
|
server.bind()
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
|
|
|
logger.error("Connected")
|
2021-07-06 15:38:53 +02:00
|
|
|
|
|
|
|
rmi.create<TestObject> {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Starting test")
|
2021-04-27 13:30:42 +02:00
|
|
|
setValue(43.21f)
|
2020-08-18 01:50:20 +02:00
|
|
|
|
|
|
|
// Normal remote method call.
|
2021-04-27 13:30:42 +02:00
|
|
|
Assert.assertEquals(43.21f, other(), .0001f)
|
2020-08-18 01:50:20 +02:00
|
|
|
|
|
|
|
// 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.
|
2021-04-27 13:30:42 +02:00
|
|
|
val otherObject: OtherObject = getOtherObject()
|
2020-08-18 01:50:20 +02:00
|
|
|
|
|
|
|
// Normal remote method call on the second object.
|
|
|
|
otherObject.setValue(12.34f)
|
|
|
|
val value = otherObject.value()
|
|
|
|
Assert.assertEquals(12.34f, value, .0001f)
|
|
|
|
|
2020-08-19 01:30:44 +02:00
|
|
|
|
|
|
|
// make sure the "local" object and the "remote" object have the same values
|
2021-04-27 13:30:42 +02:00
|
|
|
Assert.assertEquals(12.34f, getOtherValue(), .0001f)
|
2020-08-19 01:30:44 +02:00
|
|
|
|
|
|
|
|
2020-08-18 01:50:20 +02:00
|
|
|
// When a proxy object is sent, the other side receives its ACTUAL object (not a proxy of it), because
|
|
|
|
// that is where that object actually exists.
|
2021-04-30 22:39:36 +02:00
|
|
|
send(otherObject)
|
2020-08-18 01:50:20 +02:00
|
|
|
}
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:12:30 +02:00
|
|
|
@Test
|
|
|
|
fun doubleRmi() {
|
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
2021-07-02 11:40:20 +02:00
|
|
|
configuration.serialization.rmi.register(TestObject::class.java, TestObjectAnnotImpl::class.java)
|
|
|
|
configuration.serialization.rmi.register(OtherObject::class.java, OtherObjectImpl::class.java)
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onMessage<OtherObject> { message ->
|
2020-09-01 09:12:30 +02:00
|
|
|
// The test is complete when the client sends the OtherObject instance.
|
|
|
|
// this 'object' is the REAL object, not a proxy, because this object is created within this connection.
|
|
|
|
if (message.value() == 12.34f) {
|
|
|
|
stopEndPoints()
|
|
|
|
} else {
|
|
|
|
Assert.fail("Incorrect object value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 03:18:10 +02:00
|
|
|
server.bind()
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
|
|
|
logger.error("Connected")
|
2021-07-06 15:38:53 +02:00
|
|
|
rmi.create<TestObject> {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Starting test")
|
2021-04-27 13:30:42 +02:00
|
|
|
setValue(43.21f)
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
// Normal remote method call.
|
2021-04-27 13:30:42 +02:00
|
|
|
Assert.assertEquals(43.21f, other(), .0001f)
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
// 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.
|
2021-04-27 13:30:42 +02:00
|
|
|
val otherObject: OtherObject = getOtherObject()
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
// Normal remote method call on the second object.
|
|
|
|
otherObject.setValue(12.34f)
|
|
|
|
val value = otherObject.value()
|
|
|
|
Assert.assertEquals(12.34f, value, .0001f)
|
|
|
|
|
|
|
|
|
|
|
|
// make sure the "local" object and the "remote" object have the same values
|
2021-04-27 13:30:42 +02:00
|
|
|
Assert.assertEquals(12.34f, getOtherValue(), .0001f)
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
// When a proxy object is sent, the other side receives its ACTUAL object (not a proxy of it), because
|
|
|
|
// that is where that object actually exists.
|
2021-04-30 22:39:36 +02:00
|
|
|
send(otherObject)
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
2020-09-03 02:40:53 +02:00
|
|
|
waitForThreads()
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun singleRmi() {
|
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
2021-07-02 11:40:20 +02:00
|
|
|
configuration.serialization.rmi.register(TestObject::class.java, TestObjectImpl::class.java)
|
2020-09-01 09:12:30 +02:00
|
|
|
configuration.serialization.register(OtherObjectImpl::class.java)
|
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onMessage<OtherObject> { message ->
|
2020-09-01 09:12:30 +02:00
|
|
|
// The test is complete when the client sends the OtherObject instance.
|
|
|
|
// this 'object' is the REAL object
|
|
|
|
if (message.value() == 43.21f) {
|
|
|
|
stopEndPoints()
|
|
|
|
} else {
|
|
|
|
Assert.fail("Incorrect object value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 03:18:10 +02:00
|
|
|
server.bind()
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
|
|
|
logger.error("Connected")
|
2021-07-02 11:40:20 +02:00
|
|
|
|
2021-07-06 15:38:53 +02:00
|
|
|
rmi.create<TestObject> {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Starting test")
|
2021-04-27 13:30:42 +02:00
|
|
|
setOtherValue(43.21f)
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
// Normal remote method call.
|
2021-04-27 13:30:42 +02:00
|
|
|
Assert.assertEquals(43.21f, getOtherValue(), .0001f)
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
// real object
|
2021-04-27 13:30:42 +02:00
|
|
|
val otherObject: OtherObject = getOtherObject()
|
2020-09-01 09:12:30 +02:00
|
|
|
|
|
|
|
// Normal remote method call on the second object.
|
|
|
|
val value = otherObject.value()
|
|
|
|
Assert.assertEquals(43.21f, 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 actually exists.
|
2021-04-30 22:39:36 +02:00
|
|
|
send(otherObject)
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
2020-09-03 02:40:53 +02:00
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun singleReverseRmi() {
|
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
2021-07-02 11:40:20 +02:00
|
|
|
configuration.serialization.rmi.register(TestObject::class.java, null)
|
2020-09-03 02:40:53 +02:00
|
|
|
configuration.serialization.register(OtherObjectImpl::class.java)
|
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
|
|
|
logger.error("Connected")
|
2021-07-02 11:40:20 +02:00
|
|
|
|
2021-07-06 15:38:53 +02:00
|
|
|
rmi.create<TestObject> {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Starting test")
|
2021-04-27 13:30:42 +02:00
|
|
|
setOtherValue(43.21f)
|
2020-09-03 02:40:53 +02:00
|
|
|
|
|
|
|
// Normal remote method call.
|
2021-04-27 13:30:42 +02:00
|
|
|
Assert.assertEquals(43.21f, getOtherValue(), .0001f)
|
2020-09-03 02:40:53 +02:00
|
|
|
|
|
|
|
// real object
|
2021-04-27 13:30:42 +02:00
|
|
|
val otherObject: OtherObject = getOtherObject()
|
2020-09-03 02:40:53 +02:00
|
|
|
|
|
|
|
// Normal remote method call on the second object.
|
|
|
|
val value = otherObject.value()
|
|
|
|
Assert.assertEquals(43.21f, 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 actually exists.
|
2021-04-30 22:39:36 +02:00
|
|
|
send(otherObject)
|
2020-09-03 02:40:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bind()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
run {
|
|
|
|
val configuration = clientConfig()
|
2021-07-02 11:40:20 +02:00
|
|
|
configuration.serialization.rmi.register(TestObject::class.java, TestObjectImpl::class.java)
|
2020-09-03 02:40:53 +02:00
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onMessage<OtherObject> { message ->
|
2020-09-03 02:40:53 +02:00
|
|
|
// The test is complete when the client sends the OtherObject instance.
|
|
|
|
// this 'object' is the REAL object
|
|
|
|
if (message.value() == 43.21f) {
|
|
|
|
stopEndPoints()
|
|
|
|
} else {
|
|
|
|
Assert.fail("Incorrect object value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-03 02:40:53 +02:00
|
|
|
}
|
|
|
|
waitForThreads()
|
2020-09-01 09:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-03 01:45:18 +02:00
|
|
|
private interface TestObject {
|
2020-08-19 01:30:44 +02:00
|
|
|
suspend fun setValue(aFloat: Float)
|
|
|
|
suspend fun setOtherValue(aFloat: Float)
|
|
|
|
suspend fun getOtherValue(): Float
|
2020-07-03 01:45:18 +02:00
|
|
|
fun other(): Float
|
|
|
|
fun getOtherObject(): OtherObject
|
|
|
|
}
|
|
|
|
|
|
|
|
private interface OtherObject {
|
|
|
|
fun setValue(aFloat: Float)
|
|
|
|
fun value(): Float
|
|
|
|
}
|
|
|
|
|
|
|
|
private class TestObjectImpl : TestObject {
|
|
|
|
@Transient
|
|
|
|
private val ID = idCounter.getAndIncrement()
|
|
|
|
|
|
|
|
private val otherObject: OtherObject = OtherObjectImpl()
|
|
|
|
|
|
|
|
private var aFloat = 0f
|
2020-08-19 01:30:44 +02:00
|
|
|
override suspend fun setValue(aFloat: Float) {
|
2020-07-03 01:45:18 +02:00
|
|
|
throw RuntimeException("Whoops!")
|
|
|
|
}
|
|
|
|
|
2020-08-19 01:30:44 +02:00
|
|
|
suspend fun setValue(connection: Connection, aFloat: Float) {
|
2020-08-28 10:22:25 +02:00
|
|
|
connection.logger.error("receiving")
|
2020-07-03 01:45:18 +02:00
|
|
|
this.aFloat = aFloat
|
|
|
|
}
|
|
|
|
|
2020-08-19 01:30:44 +02:00
|
|
|
override suspend fun setOtherValue(aFloat: Float) {
|
|
|
|
otherObject.setValue(aFloat)
|
|
|
|
}
|
|
|
|
|
|
|
|
override suspend fun getOtherValue(): Float {
|
|
|
|
return otherObject.value()
|
|
|
|
}
|
|
|
|
|
2020-07-03 01:45:18 +02:00
|
|
|
override fun other(): Float {
|
|
|
|
throw RuntimeException("Whoops!")
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:40:20 +02:00
|
|
|
@Suppress("UNUSED_PARAMETER")
|
2020-07-03 01:45:18 +02:00
|
|
|
fun other(connection: Connection): Float {
|
|
|
|
return aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getOtherObject(): OtherObject {
|
2020-09-01 09:12:30 +02:00
|
|
|
return otherObject
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun hashCode(): Int {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class TestObjectAnnotImpl : TestObject {
|
|
|
|
@Transient
|
|
|
|
private val ID = idCounter.getAndIncrement()
|
|
|
|
|
|
|
|
private val otherObject: OtherObject = OtherObjectImpl()
|
|
|
|
|
|
|
|
private var aFloat = 0f
|
|
|
|
override suspend fun setValue(aFloat: Float) {
|
|
|
|
throw RuntimeException("Whoops!")
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun setValue(connection: Connection, aFloat: Float) {
|
|
|
|
connection.logger.error("receiving")
|
|
|
|
this.aFloat = aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override suspend fun setOtherValue(aFloat: Float) {
|
|
|
|
otherObject.setValue(aFloat)
|
|
|
|
}
|
|
|
|
|
|
|
|
override suspend fun getOtherValue(): Float {
|
|
|
|
return otherObject.value()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun other(): Float {
|
2020-07-03 01:45:18 +02:00
|
|
|
throw RuntimeException("Whoops!")
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:40:20 +02:00
|
|
|
@Suppress("UNUSED_PARAMETER")
|
2020-09-01 09:12:30 +02:00
|
|
|
fun other(connection: Connection): Float {
|
|
|
|
return aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getOtherObject(): OtherObject {
|
2020-07-03 01:45:18 +02:00
|
|
|
return otherObject
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun hashCode(): Int {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class OtherObjectImpl : OtherObject {
|
|
|
|
@Transient
|
|
|
|
private val ID = idCounter.getAndIncrement()
|
|
|
|
private var aFloat = 0f
|
|
|
|
override fun setValue(aFloat: Float) {
|
|
|
|
this.aFloat = aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun value(): Float {
|
|
|
|
return aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun hashCode(): Int {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|