forked from TrueCloudLab/frostfs-sdk-java
[#1] Refactor client structure
add session logic add network logic Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
2481774545
commit
dc0eef770c
26 changed files with 868 additions and 302 deletions
|
@ -1,89 +1,26 @@
|
|||
package info.FrostFS.sdk;
|
||||
|
||||
import frostFS.container.ContainerServiceGrpc;
|
||||
import frostFS.netmap.NetmapServiceGrpc;
|
||||
import frostFS.object.ObjectServiceGrpc;
|
||||
import frostFS.session.SessionServiceGrpc;
|
||||
import io.grpc.Channel;
|
||||
import io.grpc.netty.GrpcSslContexts;
|
||||
import io.grpc.ChannelCredentials;
|
||||
import io.grpc.netty.NettyChannelBuilder;
|
||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class GrpcClient {
|
||||
private static final Logger log = LoggerFactory.getLogger(GrpcClient.class);
|
||||
|
||||
private final ContainerServiceGrpc.ContainerServiceBlockingStub containerServiceBlockingClient;
|
||||
private final NetmapServiceGrpc.NetmapServiceBlockingStub netmapServiceBlockingClient;
|
||||
private final ObjectServiceGrpc.ObjectServiceBlockingStub objectServiceBlockingClient;
|
||||
private final ObjectServiceGrpc.ObjectServiceStub objectServiceClient;
|
||||
private final SessionServiceGrpc.SessionServiceBlockingStub sessionServiceBlockingClient;
|
||||
|
||||
public GrpcClient(String host) {
|
||||
Channel channel = initGrpcChannel(host);
|
||||
this.containerServiceBlockingClient = ContainerServiceGrpc.newBlockingStub(channel);
|
||||
this.netmapServiceBlockingClient = NetmapServiceGrpc.newBlockingStub(channel);
|
||||
this.objectServiceBlockingClient = ObjectServiceGrpc.newBlockingStub(channel);
|
||||
this.objectServiceClient = ObjectServiceGrpc.newStub(channel);
|
||||
this.sessionServiceBlockingClient = SessionServiceGrpc.newBlockingStub(channel);
|
||||
}
|
||||
|
||||
public static Channel initGrpcChannel(String host) {
|
||||
URI uri;
|
||||
public static Channel initGrpcChannel(String host, ChannelCredentials creds) {
|
||||
try {
|
||||
uri = new URI(host);
|
||||
URI uri = new URI(host);
|
||||
var channelBuilder = isNull(creds) ? NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort())
|
||||
: NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort(), creds);
|
||||
|
||||
return channelBuilder.usePlaintext().build();
|
||||
} catch (URISyntaxException exp) {
|
||||
var message = String.format("Host %s has invalid format. Error: %s", host, exp.getMessage());
|
||||
log.error(message);
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
var channelBuilder = NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort())
|
||||
.usePlaintext();
|
||||
|
||||
switch (uri.getScheme()) {
|
||||
case "https":
|
||||
try {
|
||||
channelBuilder.sslContext(
|
||||
GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build()
|
||||
);
|
||||
} catch (SSLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
case "http":
|
||||
break;
|
||||
default:
|
||||
var message = String.format("Host %s has invalid URI scheme: %s", host, uri.getScheme());
|
||||
log.error(message);
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
return channelBuilder.build();
|
||||
}
|
||||
|
||||
public ContainerServiceGrpc.ContainerServiceBlockingStub getContainerServiceBlockingClient() {
|
||||
return containerServiceBlockingClient;
|
||||
}
|
||||
|
||||
public NetmapServiceGrpc.NetmapServiceBlockingStub getNetmapServiceBlockingClient() {
|
||||
return netmapServiceBlockingClient;
|
||||
}
|
||||
|
||||
public ObjectServiceGrpc.ObjectServiceBlockingStub getObjectServiceBlockingClient() {
|
||||
return objectServiceBlockingClient;
|
||||
}
|
||||
|
||||
public ObjectServiceGrpc.ObjectServiceStub getObjectServiceClient() {
|
||||
return objectServiceClient;
|
||||
}
|
||||
|
||||
public SessionServiceGrpc.SessionServiceBlockingStub getSessionServiceBlockingClient() {
|
||||
return sessionServiceBlockingClient;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue