1
1
/*
2
- * Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton,
2
+ * Copyright (c) 2009 - 2024 Deutsches Elektronen-Synchroton,
3
3
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
4
4
*
5
5
* This library is free software; you can redistribute it and/or modify
22
22
import java .io .IOException ;
23
23
import java .net .InetAddress ;
24
24
import java .net .InetSocketAddress ;
25
+ import java .util .concurrent .ExecutorService ;
25
26
import java .util .concurrent .TimeUnit ;
26
27
27
28
public class OncRpcClient implements AutoCloseable {
@@ -52,14 +53,19 @@ public OncRpcClient(InetSocketAddress socketAddress, int protocol) {
52
53
}
53
54
54
55
public OncRpcClient (InetSocketAddress socketAddress , int protocol , int localPort , IoStrategy ioStrategy , String serviceName ) {
55
- _socketAddress = socketAddress ;
56
- _rpcsvc = new OncRpcSvcBuilder ()
56
+ this (socketAddress , new OncRpcSvcBuilder ()
57
57
.withClientMode ()
58
58
.withPort (localPort )
59
59
.withIpProtocolType (protocol )
60
60
.withIoStrategy (ioStrategy )
61
61
.withServiceName (serviceName )
62
- .build ();
62
+ .build ());
63
+ }
64
+
65
+
66
+ private OncRpcClient (InetSocketAddress socketAddress , OncRpcSvc clientSvc ) {
67
+ _socketAddress = socketAddress ;
68
+ _rpcsvc = clientSvc ;
63
69
}
64
70
65
71
public RpcTransport connect () throws IOException {
@@ -82,4 +88,99 @@ public RpcTransport connect(long timeout, TimeUnit timeUnit) throws IOException
82
88
public void close () throws IOException {
83
89
_rpcsvc .stop ();
84
90
}
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
+
85
186
}
0 commit comments