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
2222import java .io .IOException ;
2323import java .net .InetAddress ;
2424import java .net .InetSocketAddress ;
25+ import java .util .concurrent .ExecutorService ;
2526import java .util .concurrent .TimeUnit ;
2627
2728public 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}
0 commit comments