Skip to content

Commit 299c058

Browse files
committed
rpc: introduce OncRpcClientBuilder
error proof helpert to build a customized OncRpcClient Fixes: #99 Acked-by: Lea Morschel Target: master, 3.2, 3.1 (cherry picked from commit 388014e) Signed-off-by: Tigran Mkrtchyan <[email protected]>
1 parent 9d4db2a commit 299c058

File tree

3 files changed

+137
-21
lines changed

3 files changed

+137
-21
lines changed

oncrpc4j-core/src/main/java/org/dcache/oncrpc4j/rpc/OncRpcClient.java

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton,
2+
* Copyright (c) 2009 - 2024 Deutsches Elektronen-Synchroton,
33
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
44
*
55
* This library is free software; you can redistribute it and/or modify
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.net.InetAddress;
2424
import java.net.InetSocketAddress;
25+
import java.util.concurrent.ExecutorService;
2526
import java.util.concurrent.TimeUnit;
2627

2728
public class OncRpcClient implements AutoCloseable {
@@ -52,14 +53,19 @@ public OncRpcClient(InetSocketAddress socketAddress, int protocol) {
5253
}
5354

5455
public OncRpcClient(InetSocketAddress socketAddress, int protocol, int localPort, IoStrategy ioStrategy, String serviceName) {
55-
_socketAddress = socketAddress;
56-
_rpcsvc = new OncRpcSvcBuilder()
56+
this(socketAddress, new OncRpcSvcBuilder()
5757
.withClientMode()
5858
.withPort(localPort)
5959
.withIpProtocolType(protocol)
6060
.withIoStrategy(ioStrategy)
6161
.withServiceName(serviceName)
62-
.build();
62+
.build());
63+
}
64+
65+
66+
private OncRpcClient(InetSocketAddress socketAddress, OncRpcSvc clientSvc) {
67+
_socketAddress = socketAddress;
68+
_rpcsvc = clientSvc;
6369
}
6470

6571
public RpcTransport connect() throws IOException {
@@ -82,4 +88,99 @@ public RpcTransport connect(long timeout, TimeUnit timeUnit) throws IOException
8288
public void close() throws IOException {
8389
_rpcsvc.stop();
8490
}
91+
92+
public static OncRpcClientBuilder newBuilder() {
93+
return new OncRpcClientBuilder();
94+
}
95+
96+
public static class OncRpcClientBuilder {
97+
98+
private final OncRpcSvcBuilder svcBuilder = new OncRpcSvcBuilder()
99+
.withClientMode()
100+
.withWorkerThreadIoStrategy()
101+
.withSelectorThreadPoolSize(1)
102+
.withWorkerThreadPoolSize(1)
103+
.withoutAutoPublish();
104+
105+
private OncRpcClientBuilder() {
106+
// no direct instantiation
107+
}
108+
109+
public OncRpcClientBuilder withProtocol(int protocol) {
110+
svcBuilder.withIpProtocolType(protocol);
111+
return this;
112+
}
113+
114+
public OncRpcClientBuilder withLocalPort(int localPort) {
115+
svcBuilder.withPort(localPort);
116+
return this;
117+
}
118+
119+
public OncRpcClientBuilder withIoStrategy(IoStrategy ioStrategy) {
120+
svcBuilder.withIoStrategy(ioStrategy);
121+
return this;
122+
}
123+
124+
public OncRpcClientBuilder withServiceName(String serviceName) {
125+
svcBuilder.withServiceName(serviceName);
126+
return this;
127+
}
128+
129+
public OncRpcClientBuilder withWorkerThreadPoolSize(int size) {
130+
svcBuilder.withWorkerThreadPoolSize(size);
131+
return this;
132+
}
133+
134+
public OncRpcClientBuilder withSelectorThreadPoolSize(int size) {
135+
svcBuilder.withSelectorThreadPoolSize(size);
136+
return this;
137+
}
138+
139+
public OncRpcClientBuilder withWorkerThreadIoStrategy() {
140+
svcBuilder.withWorkerThreadIoStrategy();
141+
return this;
142+
}
143+
144+
public OncRpcClientBuilder withRpcService(OncRpcProgram program, RpcDispatchable dispatchable) {
145+
svcBuilder.withRpcService(program, dispatchable);
146+
return this;
147+
}
148+
149+
public OncRpcClientBuilder withWorkerThreadExecutionService(ExecutorService executorService) {
150+
svcBuilder.withWorkerThreadExecutionService(executorService);
151+
return this;
152+
}
153+
154+
public OncRpcClientBuilder withTCP() {
155+
svcBuilder.withTCP();
156+
return this;
157+
}
158+
159+
public OncRpcClientBuilder withUDP() {
160+
svcBuilder.withUDP();
161+
return this;
162+
}
163+
164+
/**
165+
* Build a new {@link OncRpcClient} instance.
166+
*
167+
* @param endpoint the socket address of the remote RPC server
168+
* @return a new {@link OncRpcClient} instance
169+
*/
170+
public OncRpcClient build(InetSocketAddress endpoint) {
171+
return new OncRpcClient(endpoint, svcBuilder.build());
172+
}
173+
174+
/**
175+
* Build a new {@link OncRpcClient} instance.
176+
*
177+
* @param endpoint the address of the remote RPC server
178+
* @param port the port of the remote RPC server
179+
* @return a new {@link OncRpcClient} instance
180+
*/
181+
public OncRpcClient build(InetAddress endpoint, int port) {
182+
return build(new InetSocketAddress(endpoint, port));
183+
}
184+
}
185+
85186
}

oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/ClientServerTest.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package org.dcache.oncrpc4j.rpc;
22

3-
import org.dcache.oncrpc4j.rpc.OncRpcSvc;
4-
import org.dcache.oncrpc4j.rpc.RpcCall;
5-
import org.dcache.oncrpc4j.rpc.OncRpcProgram;
6-
import org.dcache.oncrpc4j.rpc.OncRpcSvcBuilder;
7-
import org.dcache.oncrpc4j.rpc.RpcDispatchable;
8-
import org.dcache.oncrpc4j.rpc.RpcAuthTypeNone;
93
import org.dcache.oncrpc4j.rpc.net.IpProtocolType;
104
import org.dcache.oncrpc4j.xdr.XdrVoid;
115
import org.dcache.oncrpc4j.xdr.XdrString;
@@ -37,7 +31,7 @@ public class ClientServerTest {
3731
private static final int LOST = 4;
3832

3933
private OncRpcSvc svc;
40-
private OncRpcSvc clnt;
34+
private OncRpcClient clnt;
4135
private RpcCall clntCall;
4236

4337
@Before
@@ -90,18 +84,13 @@ public void setUp() throws IOException {
9084
.build();
9185
svc.start();
9286

93-
clnt = new OncRpcSvcBuilder()
94-
.withoutAutoPublish()
87+
clnt = OncRpcClient.newBuilder()
9588
.withTCP()
96-
.withClientMode()
97-
.withWorkerThreadIoStrategy()
98-
.withSelectorThreadPoolSize(1)
99-
.withWorkerThreadPoolSize(1)
10089
.withRpcService(new OncRpcProgram(PROGNUM, PROGVER), upper)
10190
.withServiceName("clnt")
102-
.build();
103-
clnt.start();
104-
RpcTransport t = clnt.connect(svc.getInetSocketAddress(IpProtocolType.TCP));
91+
.build(svc.getInetSocketAddress(IpProtocolType.TCP));
92+
93+
RpcTransport t = clnt.connect();
10594
clntCall = new RpcCall(PROGNUM, PROGVER, new RpcAuthTypeNone(), t);
10695
}
10796

@@ -111,7 +100,7 @@ public void tearDown() throws IOException {
111100
svc.stop();
112101
}
113102
if (clnt != null) {
114-
clnt.stop();
103+
clnt.close();
115104
}
116105
}
117106

oncrpc4j-rpcgen/src/main/java/org/acplt/oncrpc/apps/jrpcgen/jrpcgen.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,8 @@ public static void dumpClient(JrpcgenProgramInfo programInfo) {
19351935
out.println("import java.util.concurrent.TimeoutException;");
19361936
}
19371937
out.println();
1938+
out.println("import org.dcache.oncrpc4j.rpc.OncRpcClient.OncRpcClientBuilder;");
1939+
out.println();
19381940

19391941
out.println("/**");
19401942
out.println(" * The class <code>" + clientClass + "</code> implements the client stub proxy");
@@ -2035,6 +2037,30 @@ public static void dumpClient(JrpcgenProgramInfo programInfo) {
20352037
out.println(" }");
20362038
out.println();
20372039

2040+
out.println(" /**");
2041+
out.println(" * Constructs a <code>" + clientClass + "</code> client stub proxy object");
2042+
out.println(" * from which the " + programInfo.programId + " remote program can be accessed.");
2043+
out.println(" * @param host Internet address of host where to contact the remote program.");
2044+
out.println(" * @param port Port number at host where the remote program can be reached.");
2045+
out.println(" * @param auth {@link RpcAuth} to be used for RPC client authentication.");
2046+
out.println(" * @param program Remote program number.");
2047+
out.println(" * @param version Remote program version number.");
2048+
out.println(" * @param clientBuilder {@link org.dcache.oncrpc4j.rpc.OncRpcClient.OncRpcClientBuilder} to build the client");
2049+
out.println(" * @throws OncRpcException if an ONC/RPC error occurs.");
2050+
out.println(" * @throws IOException if an I/O error occurs.");
2051+
out.println(" */");
2052+
out.println(" public " + clientClass + "(InetAddress host, int port, RpcAuth auth, int program, int version, OncRpcClientBuilder clientBuilder)");
2053+
out.println(" throws OncRpcException, IOException {");
2054+
out.println(" rpcClient = clientBuilder.build(host, port);");
2055+
out.println(" try {");
2056+
out.println(" client = new RpcCall(program, version, auth, rpcClient.connect());");
2057+
out.println(" } catch (IOException e) {");
2058+
out.println(" rpcClient.close();");
2059+
out.println(" throw e;");
2060+
out.println(" } ");
2061+
out.println(" }");
2062+
out.println();
2063+
20382064
out.println(" /**");
20392065
out.println(" * Shutdown client connection.");
20402066
out.println(" *");

0 commit comments

Comments
 (0)