下面列出了java.net.Inet4Address#getLoopbackAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private SimpleServiceGrpc.SimpleServiceBlockingStub getBlockingStub(
final UpstreamTlsContext upstreamTlsContext, String overrideAuthority)
throws URISyntaxException {
URI expectedUri = new URI("sdstest://localhost:" + port);
fakeNameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).build();
NameResolverRegistry.getDefaultRegistry().register(fakeNameResolverFactory);
XdsChannelBuilder channelBuilder =
XdsChannelBuilder.forTarget("sdstest://localhost:" + port);
if (overrideAuthority != null) {
channelBuilder = channelBuilder.overrideAuthority(overrideAuthority);
}
InetSocketAddress socketAddress =
new InetSocketAddress(Inet4Address.getLoopbackAddress(), port);
Attributes attrs =
(upstreamTlsContext != null)
? Attributes.newBuilder()
.set(XdsAttributes.ATTR_UPSTREAM_TLS_CONTEXT, upstreamTlsContext)
.build()
: Attributes.EMPTY;
fakeNameResolverFactory.setServers(
ImmutableList.of(new EquivalentAddressGroup(socketAddress, attrs)));
return SimpleServiceGrpc.newBlockingStub(cleanupRule.register(channelBuilder.build()));
}
public SingleClientUdpTracker(int interval, int leechers, int seeders) {
try {
this.serverSocket = new DatagramSocket(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 0));
} catch (SocketException e) {
throw new RuntimeException("Failed to create server socket", e);
}
this.interval = interval;
this.leechers = leechers;
this.seeders = seeders;
}
@Before
public void setup() throws Exception {
expr = TimeSeriesMetricExpression.valueOf("1 + 2");
CompletableFuture<Integer> portFuture = new CompletableFuture<>();
server = new CollectHistoryServer(history);
final Thread serverThread = new Thread(() -> {
OncRpcServerTransport transport = null;
try {
transport = new OncRpcUdpServerTransport(server, Inet4Address.getLoopbackAddress(), 0, server.info, 32768);
transport.setCharacterEncoding("UTF-8");
portFuture.complete(transport.getPort());
server.run(new OncRpcServerTransport[]{transport});
} catch (IOException | OncRpcException ex) {
LOG.log(Level.SEVERE, "server " + server + " failed to start", ex);
portFuture.completeExceptionally(ex);
} finally {
if (transport != null) transport.close();
}
});
serverThread.setDaemon(true);
serverThread.start();
final int port = portFuture.get(15, TimeUnit.SECONDS);
LOG.log(Level.INFO, "chosen port: {0}", port);
LOG.log(Level.INFO, "started server {0}", server);
client = new Client(Inet4Address.getLoopbackAddress(), port, OncRpcProtocols.ONCRPC_UDP);
LOG.log(Level.INFO, "started client {0}", client);
}
public void startServer() {
try {
ServerSocket serverSocket = new ServerSocket(2221, 50, Inet4Address.getLoopbackAddress());
ConnectionListener connectionListener = new ConnectionListener(serverSocket, this);
connectionListener.start();
serverStarted = true;
} catch (IOException ex) {
LOG.error(ex.getMessage(), ex);
serverStarted = false;
}
}
public UdpTrackerConnection(SingleClientUdpTracker tracker) {
InetSocketAddress localAddress = new InetSocketAddress(Inet4Address.getLoopbackAddress(), 0);
this.worker = new UdpMessageWorker(localAddress, tracker.getServerAddress(), mock(IRuntimeLifecycleBinder.class));
LOGGER.info("Established connection (local: {}, remote: {}", localAddress, tracker.getServerAddress());
}