Update Network State Service
An implementation of the UpdateNetworkStateService
will provide an object-oriented wrapper for the gRPC library, with the ability to update information about
the state of the network. This is done with the following 3 steps:
- Create callbacks which will be called when gRPC messages are received from a client.
- Register the callbacks with an instance of the
UpdateNetworkStateService
. - Add your
UpdateNetworkStateService
instance as a gRPC service.
The below examples make use of a helper library provided by Zepben, which is included as a transitive dependency of the SDK, or can be added directly from Maven Central.
You do NOT have to do this, you can register the services directly with a Netty gRPC (or other) server instead.
The classes you need for performing these actions can be imported from the SDK:
import com.zepben.evolve.conn.grpc.GrpcServer
import com.zepben.evolve.streaming.data.CurrentStateEvent
import com.zepben.evolve.streaming.data.SetCurrentStatesStatus
import com.zepben.evolve.streaming.mutations.UpdateNetworkStateService
Creating callbacks
Each supported message in UpdateNetworkStateService
requires a callback, which will be triggered when any client messages are received. Implementation of these
callbacks can be done as either lambdas, or full method/function implementations that can be passed as method references.
onSetCurrentStates
The onSetCurrentStates
callback is triggered for each request passing in a batch of current state events, and should return a
CompletableFuture
of batch result response to reflect the success or failure of the update process.
// Using a lambda expression
var updateCurrentStatesLambda = { batchId: Long, events: List<CurrentStateEvent> ->
// process updating of events here and return a CompletableFuture of batch result response
}
// Using a method
class UpdateNetworkStateServiceImpl {
fun updateCurrentStates(batchId: Long, events: List<CurrentStateEvent>): CompletableFuture<SetCurrentStatesStatus> {
// process updating of events here and return a CompletableFuture of batch result response
}
}
Registering callbacks
Registering the callbacks with the service is as simple as passing them into the UpdateNetworkStateService
constructor.
// Using lambda expressions
val service = UpdateNetworkStateService(updateCurrentStatesLambda)
// Using method references
class UpdateNetworkStateServiceImpl {
val service = UpdateNetworkStateService(::updateCurrentStates);
}
Registering the service
For the above code to have any effect, you need to register the service with a gRPC server. Once this has been done, you should start to receive callbacks for each request sent from a gRPC client.
fun main() {
val grpcServer = object : GrpcServer(9001) {
init {
serverBuilder.addService(service)
}
}
grpcServer.start()
}
Putting it all together
Putting each of the steps above together, you can build the scaffold of a working application
- Kotlin Lambdas
- Kotlin Methods
import com.zepben.evolve.conn.grpc.GrpcServer
import com.zepben.evolve.streaming.data.CurrentStateEvent
import com.zepben.evolve.streaming.mutations.UpdateNetworkStateService
fun main() {
val service = UpdateNetworkStateService(onSetCurrentStates = { batchId: Long, events: List<CurrentStateEvent> ->
// process updating of events here and return a CompletableFuture of batch result response
})
val grpcServer = object : GrpcServer(9001) {
init {
serverBuilder.addService(service)
}
}
grpcServer.start()
}
import com.zepben.evolve.conn.grpc.GrpcServer
import com.zepben.evolve.streaming.data.CurrentStateEvent
import com.zepben.evolve.streaming.data.SetCurrentStatesStatus
import com.zepben.evolve.streaming.mutations.UpdateNetworkStateService
class UpdateNetworkStateServiceImpl {
val service = UpdateNetworkStateService(::updateCurrentStates)
fun updateCurrentStates(batchId: Long, events: List<CurrentStateEvent>): CompletableFuture<SetCurrentStatesStatus> {
// process updating of events here and return a CompletableFuture of batch result response
}
}
fun main() {
val service = UpdateNetworkStateServiceImpl().service
val grpcServer = object : GrpcServer(9001) {
init {
serverBuilder.addService(service)
}
}
grpcServer.start()
}