use of java.net.InetSocketAddress in project j2objc by google.
the class DatagramChannelImpl method connect.
@Override
public synchronized DatagramChannel connect(SocketAddress address) throws IOException {
// must be open
checkOpen();
// status must be un-connected.
if (connected) {
throw new IllegalStateException();
}
// check the address
InetSocketAddress inetSocketAddress = SocketChannelImpl.validateAddress(address);
InetAddress remoteAddress = inetSocketAddress.getAddress();
int remotePort = inetSocketAddress.getPort();
try {
begin();
NetworkBridge.connect(fd, remoteAddress, remotePort);
} catch (ConnectException e) {
// ConnectException means connect fail, not exception
} finally {
end(true);
}
// address state held by the channel and the socket up to date.
if (!isBound) {
onBind(true);
}
// Keep the connected state held by the channel and the socket up to date.
onConnect(remoteAddress, remotePort, true);
return this;
}
use of java.net.InetSocketAddress in project grpc-java by grpc.
the class Utils method parseSocketAddress.
/**
* Parse a {@link SocketAddress} from the given string.
*/
public static SocketAddress parseSocketAddress(String value) {
if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
// Unix Domain Socket address.
// Create the underlying file for the Unix Domain Socket.
String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
File file = new File(filePath);
if (!file.isAbsolute()) {
throw new IllegalArgumentException("File path must be absolute: " + filePath);
}
try {
if (file.createNewFile()) {
// If this application created the file, delete it when the application exits.
file.deleteOnExit();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
// Create the SocketAddress referencing the file.
return new DomainSocketAddress(file);
} else {
// Standard TCP/IP address.
String[] parts = value.split(":", 2);
if (parts.length < 2) {
throw new IllegalArgumentException("Address must be a unix:// path or be in the form host:port. Got: " + value);
}
String host = parts[0];
int port = Integer.parseInt(parts[1]);
return new InetSocketAddress(host, port);
}
}
use of java.net.InetSocketAddress in project grpc-java by grpc.
the class Utils method newOkhttpClientChannel.
private static OkHttpChannelBuilder newOkhttpClientChannel(SocketAddress address, boolean tls, boolean testca, @Nullable String authorityOverride) {
InetSocketAddress addr = (InetSocketAddress) address;
OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress(addr.getHostName(), addr.getPort());
if (tls) {
builder.negotiationType(io.grpc.okhttp.NegotiationType.TLS);
SSLSocketFactory factory;
if (testca) {
builder.overrideAuthority(GrpcUtil.authorityFromHostAndPort(authorityOverride, addr.getPort()));
try {
factory = TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
}
builder.sslSocketFactory(factory);
} else {
builder.negotiationType(io.grpc.okhttp.NegotiationType.PLAINTEXT);
}
return builder;
}
use of java.net.InetSocketAddress in project grpc-java by grpc.
the class TransportBenchmark method setUp.
@Setup
public void setUp() throws Exception {
AbstractServerImplBuilder<?> serverBuilder;
AbstractManagedChannelImplBuilder<?> channelBuilder;
switch(transport) {
case INPROCESS:
{
String name = "bench" + Math.random();
serverBuilder = InProcessServerBuilder.forName(name);
channelBuilder = InProcessChannelBuilder.forName(name);
break;
}
case NETTY:
{
InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
serverBuilder = NettyServerBuilder.forAddress(address);
channelBuilder = NettyChannelBuilder.forAddress(address).negotiationType(NegotiationType.PLAINTEXT);
break;
}
case NETTY_LOCAL:
{
String name = "bench" + Math.random();
LocalAddress address = new LocalAddress(name);
serverBuilder = NettyServerBuilder.forAddress(address).channelType(LocalServerChannel.class);
channelBuilder = NettyChannelBuilder.forAddress(address).channelType(LocalChannel.class).negotiationType(NegotiationType.PLAINTEXT);
break;
}
case NETTY_EPOLL:
{
InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
// Reflection used since they are only available on linux.
Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
EventLoopGroup group = (EventLoopGroup) groupClass.getConstructor().newInstance();
@SuppressWarnings("unchecked") Class<? extends ServerChannel> serverChannelClass = (Class<? extends ServerChannel>) Class.forName("io.netty.channel.epoll.EpollServerSocketChannel");
serverBuilder = NettyServerBuilder.forAddress(address).bossEventLoopGroup(group).workerEventLoopGroup(group).channelType(serverChannelClass);
@SuppressWarnings("unchecked") Class<? extends Channel> channelClass = (Class<? extends Channel>) Class.forName("io.netty.channel.epoll.EpollSocketChannel");
channelBuilder = NettyChannelBuilder.forAddress(address).eventLoopGroup(group).channelType(channelClass).negotiationType(NegotiationType.PLAINTEXT);
groupToShutdown = group;
break;
}
case OKHTTP:
{
int port = pickUnusedPort();
InetSocketAddress address = new InetSocketAddress("localhost", port);
serverBuilder = NettyServerBuilder.forAddress(address);
channelBuilder = OkHttpChannelBuilder.forAddress("localhost", port).negotiationType(io.grpc.okhttp.NegotiationType.PLAINTEXT);
break;
}
default:
throw new Exception("Unknown transport: " + transport);
}
if (direct) {
serverBuilder.directExecutor();
// Because blocking stubs avoid the executor, this doesn't do much.
channelBuilder.directExecutor();
}
server = serverBuilder.addService(new AsyncServer.BenchmarkServiceImpl()).build();
server.start();
channel = channelBuilder.build();
stub = BenchmarkServiceGrpc.newBlockingStub(channel);
// Wait for channel to start
stub.unaryCall(SimpleRequest.getDefaultInstance());
}
use of java.net.InetSocketAddress in project grpc-java by grpc.
the class AbstractManagedChannelImplBuilderTest method makeTargetStringForDirectAddress_scopedIpv6.
@Test
public void makeTargetStringForDirectAddress_scopedIpv6() throws Exception {
InetSocketAddress address = new InetSocketAddress("0:0:0:0:0:0:0:0%0", 10005);
assertEquals("/0:0:0:0:0:0:0:0%0:10005", address.toString());
String target = AbstractManagedChannelImplBuilder.makeTargetStringForDirectAddress(address);
URI uri = new URI(target);
assertEquals("directaddress:////0:0:0:0:0:0:0:0%250:10005", target);
assertEquals(target, uri.toString());
}
Aggregations