Constrain class type passed to registerProtocolInitiator()

This commit is contained in:
Ross Nicoll 2016-10-03 18:53:05 +01:00
parent 62a9dfe900
commit 53ecb0ecd9
4 changed files with 6 additions and 5 deletions

View File

@ -114,7 +114,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, val networkMap
return smm.add(loggerName, logic).resultFuture
}
override fun registerProtocolInitiator(markerClass: KClass<*>, protocolFactory: (Party) -> ProtocolLogic<*>) {
override fun <P : ProtocolLogic<*>> registerProtocolInitiator(markerClass: KClass<out P>, protocolFactory: (Party) -> ProtocolLogic<*>) {
require(markerClass !in protocolFactories) { "${markerClass.java.name} has already been used to register a protocol" }
log.debug { "Registering ${markerClass.java.name}" }
protocolFactories[markerClass.java] = protocolFactory

View File

@ -72,15 +72,16 @@ abstract class ServiceHubInternal : ServiceHub {
/**
* Register the protocol factory we wish to use when a initiating party attempts to communicate with us. The
* registration is done against a marker [KClass] which is sent in the session handsake by the other party. If this
* registration is done against a marker [KClass] which is sent in the session handshake by the other party. If this
* marker class has been registered then the corresponding factory will be used to create the protocol which will
* communicate with the other side. If there is no mapping then the session attempt is rejected.
* @param markerClass The marker [KClass] present in a session initiation attempt, which is a 1:1 mapping to a [Class]
* using the <pre>::class</pre> construct. Any marker class can be used, with the default being the class of the initiating
* protocol. This enables the registration to be of the form: registerProtocolInitiator(InitiatorProtocol::class, ::InitiatedProtocol)
* @param protocolFactory The protocol factory generating the initiated protocol.
* @param R the return type of the protocol logic
*/
abstract fun registerProtocolInitiator(markerClass: KClass<*>, protocolFactory: (Party) -> ProtocolLogic<*>)
abstract fun <P : ProtocolLogic<*>> registerProtocolInitiator(markerClass: KClass<out P>, protocolFactory: (Party) -> ProtocolLogic<*>)
/**
* Return the protocol factory that has been registered with [markerClass], or null if no factory is found.

View File

@ -75,7 +75,7 @@ open class MockServiceHubInternal(
return smm.add(loggerName, logic).resultFuture
}
override fun registerProtocolInitiator(markerClass: KClass<*>, protocolFactory: (Party) -> ProtocolLogic<*>) {
override fun <P : ProtocolLogic<*>> registerProtocolInitiator(markerClass: KClass<out P>, protocolFactory: (Party) -> ProtocolLogic<*>) {
protocolFactories[markerClass.java] = protocolFactory
}

View File

@ -141,7 +141,7 @@ fun getFreeLocalPorts(hostName: String, numberToAlloc: Int): List<HostAndPort> {
* @return Returns a [ListenableFuture] holding the single [ProtocolStateMachine] created by the request.
*/
inline fun <R, reified P : ProtocolLogic<R>> AbstractNode.initiateSingleShotProtocol(
markerClass: KClass<*>,
markerClass: KClass<out ProtocolLogic<*>>,
noinline protocolFactory: (Party) -> P): ListenableFuture<ProtocolStateMachine<R>> {
services.registerProtocolInitiator(markerClass, protocolFactory)