forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrpc.java
144 lines (132 loc) · 5.42 KB
/
Grpc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2016 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import javax.net.ssl.SSLSession;
/**
* Stuff that are part of the public API but are not bound to particular classes, e.g., static
* methods, constants, attribute and context keys.
*/
public final class Grpc {
private Grpc() {
}
/**
* Attribute key for the remote address of a transport.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
@TransportAttr
public static final Attributes.Key<SocketAddress> TRANSPORT_ATTR_REMOTE_ADDR =
Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_REMOTE_ADDR");
/**
* Attribute key for the local address of a transport.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
@TransportAttr
public static final Attributes.Key<SocketAddress> TRANSPORT_ATTR_LOCAL_ADDR =
Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_LOCAL_ADDR");
/**
* Attribute key for SSL session of a transport.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
@TransportAttr
public static final Attributes.Key<SSLSession> TRANSPORT_ATTR_SSL_SESSION =
Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_SSL_SESSION");
/**
* Annotation for transport attributes. It follows the annotation semantics defined
* by {@link Attributes}.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4972")
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface TransportAttr {}
/**
* Annotation for Channel attributes. It follows the annotation semantics defined by {@link
* Attributes}.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/00000000")
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface ChannelAttr {}
/**
* Creates a channel builder with a target string and credentials. The target can be either a
* valid {@link NameResolver}-compliant URI, or an authority string.
*
* <p>A {@code NameResolver}-compliant URI is an absolute hierarchical URI as defined by {@link
* java.net.URI}. Example URIs:
* <ul>
* <li>{@code "dns:///foo.googleapis.com:8080"}</li>
* <li>{@code "dns:///foo.googleapis.com"}</li>
* <li>{@code "dns:///%5B2001:db8:85a3:8d3:1319:8a2e:370:7348%5D:443"}</li>
* <li>{@code "dns://8.8.8.8/foo.googleapis.com:8080"}</li>
* <li>{@code "dns://8.8.8.8/foo.googleapis.com"}</li>
* <li>{@code "zookeeper://zk.example.com:9900/example_service"}</li>
* </ul>
*
* <p>An authority string will be converted to a {@code NameResolver}-compliant URI, which has
* the scheme from the name resolver with the highest priority (e.g. {@code "dns"}),
* no authority, and the original authority string as its path after properly escaped.
* We recommend libraries to specify the schema explicitly if it is known, since libraries cannot
* know which NameResolver will be default during runtime.
* Example authority strings:
* <ul>
* <li>{@code "localhost"}</li>
* <li>{@code "127.0.0.1"}</li>
* <li>{@code "localhost:8080"}</li>
* <li>{@code "foo.googleapis.com:8080"}</li>
* <li>{@code "127.0.0.1:8080"}</li>
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]"}</li>
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"}</li>
* </ul>
*/
public static ManagedChannelBuilder<?> newChannelBuilder(
String target, ChannelCredentials creds) {
return ManagedChannelRegistry.getDefaultRegistry().newChannelBuilder(target, creds);
}
/**
* Creates a channel builder from a host, port, and credentials. The host and port are combined to
* form an authority string and then passed to {@link #newChannelBuilder(String,
* ChannelCredentials)}. IPv6 addresses are properly surrounded by square brackets ("[]").
*/
public static ManagedChannelBuilder<?> newChannelBuilderForAddress(
String host, int port, ChannelCredentials creds) {
return newChannelBuilder(authorityFromHostAndPort(host, port), creds);
}
/**
* Combine a host and port into an authority string.
*/
// A copy of GrpcUtil.authorityFromHostAndPort
private static String authorityFromHostAndPort(String host, int port) {
try {
return new URI(null, null, host, port, null, null, null).getAuthority();
} catch (URISyntaxException ex) {
throw new IllegalArgumentException("Invalid host or port: " + host + " " + port, ex);
}
}
/**
* Static factory for creating a new ServerBuilder.
*
* @param port the port to listen on
* @param creds the server identity
*/
public static ServerBuilder<?> newServerBuilderForPort(int port, ServerCredentials creds) {
return ServerRegistry.getDefaultRegistry().newServerBuilderForPort(port, creds);
}
}