Query Network State Client
The QueryNetworkStateClient will allow you to interact with a server running the QueryNetworkStateService. It provides an object-oriented
wrapper for the gRPC library, with the ability to retrieve information about the state of the network. This is done with the following 3 steps:
- Create a gRPC connection to the server.
- Create an instance of the
QueryNetworkStateClientusing your gRPC connection. - Use your
QueryNetworkStateClientto retrieve the state of the network. - Use your
QueryNetworkStateClientto report the status of applying the state of the network.
Creating a gRPC channel
The channel gRPC channel can be directly from the gRPC library, or the channel wrapped in our GrpcChannel helper, which implements the AutoClosable interface
and performs shutdown operations for you. At its most basic, this can be achieved with:
- Java
- Kotlin
import com.zepben.ewb.streaming.grpc.GrpcChannel;
import io.grpc.ManagedChannelBuilder;
var channel = new GrpcChannel(ManagedChannelBuilder.forAddress(host, port).usePlaintext().build());
import com.zepben.ewb.streaming.grpc.GrpcChannel
import io.grpc.ManagedChannelBuilder
val channel = GrpcChannel(ManagedChannelBuilder.forAddress(host, port).usePlaintext().build())
For more in depth options for using a gRPC channel, see the gRPC documentation, or look up a tutorial.
Using a gRPC channel with your client
Using your gRPC channel with the QueryNetworkStateClient is as simple as passing it to the constructor.
- Java
- Kotlin
import com.zepben.ewb.streaming.get.QueryNetworkStateClient;
var client = new QueryNetworkStateClient(channel);
import com.zepben.ewb.streaming.get.QueryNetworkStateClient
val client = QueryNetworkStateClient(channel)
Using your client to query the network state
Now that you have a client, you can use it to query the state of the network on the connected server.
Querying current network state
The current state of the network between two date/times can be retrieved using the getCurrentStates or the getCurrentStatesStream function on the QueryNetworkStateClient.
For Java interoperability, prefer using the getCurrentStatesStream function, as it returns a Stream compatible with Java's standard library.
- Java
- Kotlin
import com.zepben.ewb.streaming.data.CurrentStateEventBatch;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Stream;
Stream<CurrentStateEventBatch> response = client.getCurrentStatesStream(1, LocalDateTime.now().plusDays(-1), LocalDateTime.now());
response.forEach(events -> {
// process the list of events here.
});
import java.time.LocalDateTime
val response = client.getCurrentStatesS(1, LocalDateTime.now().plusDays(-1), LocalDateTime.now())
response.forEach { events ->
// process the list of events here.
}
Sending current network state statuses
When applying the current state of the network, you should send a status response to report how the update went.
- Java
- Kotlin
import com.zepben.ewb.streaming.data.BatchSuccessful;
client.reportBatchStatus(new BatchSuccessful(1));
import java.time.LocalDateTime
client.reportBatchStatus(BatchSuccessful(1))