1818
1919import static java .util .concurrent .ForkJoinPool .defaultForkJoinWorkerThreadFactory ;
2020
21+ import com .google .common .base .Preconditions ;
2122import com .google .common .util .concurrent .UncaughtExceptionHandlers ;
2223import com .google .protobuf .ByteString ;
24+ import io .grpc .ChannelCredentials ;
25+ import io .grpc .InsecureChannelCredentials ;
2326import io .grpc .ManagedChannel ;
2427import io .grpc .ManagedChannelBuilder ;
2528import io .grpc .Status ;
29+ import io .grpc .TlsChannelCredentials ;
2630import io .grpc .benchmarks .proto .Messages ;
2731import io .grpc .benchmarks .proto .Messages .Payload ;
2832import io .grpc .benchmarks .proto .Messages .SimpleRequest ;
2933import io .grpc .benchmarks .proto .Messages .SimpleResponse ;
30- import io .grpc .internal .testing .TestUtils ;
31- import io .grpc .netty .GrpcSslContexts ;
3234import io .grpc .netty .NettyChannelBuilder ;
3335import io .grpc .okhttp .OkHttpChannelBuilder ;
34- import io .grpc .okhttp . internal . Platform ;
36+ import io .grpc .testing . TlsTesting ;
3537import io .netty .channel .epoll .EpollDomainSocketChannel ;
3638import io .netty .channel .epoll .EpollEventLoopGroup ;
3739import io .netty .channel .epoll .EpollSocketChannel ;
@@ -79,15 +81,10 @@ public static boolean parseBoolean(String value) {
7981 /**
8082 * Parse a {@link SocketAddress} from the given string.
8183 */
82- public static SocketAddress parseSocketAddress (String value ) {
84+ public static SocketAddress parseServerSocketAddress (String value ) {
8385 if (value .startsWith (UNIX_DOMAIN_SOCKET_PREFIX )) {
84- // Unix Domain Socket address.
85- // Create the underlying file for the Unix Domain Socket.
86- String filePath = value .substring (UNIX_DOMAIN_SOCKET_PREFIX .length ());
87- File file = new File (filePath );
88- if (!file .isAbsolute ()) {
89- throw new IllegalArgumentException ("File path must be absolute: " + filePath );
90- }
86+ DomainSocketAddress domainAddress = parseUnixSocketAddress (value );
87+ File file = new File (domainAddress .path ());
9188 try {
9289 if (file .createNewFile ()) {
9390 // If this application created the file, delete it when the application exits.
@@ -96,8 +93,7 @@ public static SocketAddress parseSocketAddress(String value) {
9693 } catch (IOException ex ) {
9794 throw new RuntimeException (ex );
9895 }
99- // Create the SocketAddress referencing the file.
100- return new DomainSocketAddress (file );
96+ return domainAddress ;
10197 } else {
10298 // Standard TCP/IP address.
10399 String [] parts = value .split (":" , 2 );
@@ -111,37 +107,24 @@ public static SocketAddress parseSocketAddress(String value) {
111107 }
112108 }
113109
114- private static OkHttpChannelBuilder newOkHttpClientChannel (
115- SocketAddress address , boolean tls , boolean testca ) {
116- InetSocketAddress addr = (InetSocketAddress ) address ;
117- OkHttpChannelBuilder builder =
118- OkHttpChannelBuilder .forAddress (addr .getHostName (), addr .getPort ());
119- if (!tls ) {
120- builder .usePlaintext ();
121- } else if (testca ) {
122- try {
123- builder .sslSocketFactory (TestUtils .newSslSocketFactoryForCa (
124- Platform .get ().getProvider (),
125- TestUtils .loadCert ("ca.pem" )));
126- } catch (Exception e ) {
127- throw new RuntimeException (e );
128- }
110+ private static DomainSocketAddress parseUnixSocketAddress (String value ) {
111+ Preconditions .checkArgument (
112+ value .startsWith (UNIX_DOMAIN_SOCKET_PREFIX ),
113+ "Must start with %s: %s" , UNIX_DOMAIN_SOCKET_PREFIX , value );
114+ // Unix Domain Socket address.
115+ // Create the underlying file for the Unix Domain Socket.
116+ String filePath = value .substring (UNIX_DOMAIN_SOCKET_PREFIX .length ());
117+ File file = new File (filePath );
118+ if (!file .isAbsolute ()) {
119+ throw new IllegalArgumentException ("File path must be absolute: " + filePath );
129120 }
130- return builder ;
121+ // Create the SocketAddress referencing the file.
122+ return new DomainSocketAddress (file );
131123 }
132124
133- private static NettyChannelBuilder newNettyClientChannel (Transport transport ,
134- SocketAddress address , boolean tls , boolean testca , int flowControlWindow )
135- throws IOException {
136- NettyChannelBuilder builder =
137- NettyChannelBuilder .forAddress (address ).flowControlWindow (flowControlWindow );
138- if (!tls ) {
139- builder .usePlaintext ();
140- } else if (testca ) {
141- File cert = TestUtils .loadCert ("ca.pem" );
142- builder .sslContext (GrpcSslContexts .forClient ().trustManager (cert ).build ());
143- }
144-
125+ private static NettyChannelBuilder configureNetty (
126+ NettyChannelBuilder builder , Transport transport , int flowControlWindow ) {
127+ builder .flowControlWindow (flowControlWindow );
145128 DefaultThreadFactory tf = new DefaultThreadFactory ("client-elg-" , true /*daemon */ );
146129 switch (transport ) {
147130 case NETTY_NIO :
@@ -194,17 +177,38 @@ public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
194177 /**
195178 * Create a {@link ManagedChannel} for the given parameters.
196179 */
197- public static ManagedChannel newClientChannel (Transport transport , SocketAddress address ,
180+ public static ManagedChannel newClientChannel (Transport transport , String target ,
198181 boolean tls , boolean testca , @ Nullable String authorityOverride ,
199182 int flowControlWindow , boolean directExecutor ) {
183+ ChannelCredentials credentials ;
184+ if (tls ) {
185+ if (testca ) {
186+ try {
187+ credentials = TlsChannelCredentials .newBuilder ()
188+ .trustManager (TlsTesting .loadCert ("ca.pem" ))
189+ .build ();
190+ } catch (IOException ex ) {
191+ throw new RuntimeException (ex );
192+ }
193+ } else {
194+ credentials = TlsChannelCredentials .create ();
195+ }
196+ } else {
197+ credentials = InsecureChannelCredentials .create ();
198+ }
200199 ManagedChannelBuilder <?> builder ;
201200 if (transport == Transport .OK_HTTP ) {
202- builder = newOkHttpClientChannel (address , tls , testca );
201+ builder = OkHttpChannelBuilder .forTarget (target , credentials )
202+ .flowControlWindow (flowControlWindow );
203203 } else {
204- try {
205- builder = newNettyClientChannel (transport , address , tls , testca , flowControlWindow );
206- } catch (Exception e ) {
207- throw new RuntimeException (e );
204+ if (target .startsWith (UNIX_DOMAIN_SOCKET_PREFIX )) {
205+ builder = configureNetty (
206+ NettyChannelBuilder .forAddress (parseUnixSocketAddress (target ), credentials ),
207+ transport , flowControlWindow );
208+ } else {
209+ builder = configureNetty (
210+ NettyChannelBuilder .forTarget (target , credentials ),
211+ transport , flowControlWindow );
208212 }
209213 }
210214 if (authorityOverride != null ) {
0 commit comments