Search in sources :

Example 71 with SocketAddress

use of java.net.SocketAddress in project flink by apache.

the class ConnectionUtils method tryToConnect.

/**
	 *
	 * @param fromAddress The address to connect from.
	 * @param toSocket The socket address to connect to.
	 * @param timeout The timeout fr the connection.
	 * @param logFailed Flag to indicate whether to log failed attempts on info level
	 *                  (failed attempts are always logged on DEBUG level).
	 * @return True, if the connection was successful, false otherwise.
	 * @throws IOException Thrown if the socket cleanup fails.
	 */
private static boolean tryToConnect(InetAddress fromAddress, SocketAddress toSocket, int timeout, boolean logFailed) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Trying to connect to (" + toSocket + ") from local address " + fromAddress + " with timeout " + timeout);
    }
    try (Socket socket = new Socket()) {
        // port 0 = let the OS choose the port
        SocketAddress bindP = new InetSocketAddress(fromAddress, 0);
        // machine
        socket.bind(bindP);
        socket.connect(toSocket, timeout);
        return true;
    } catch (Exception ex) {
        String message = "Failed to connect from address '" + fromAddress + "': " + ex.getMessage();
        if (LOG.isDebugEnabled()) {
            LOG.debug(message, ex);
        } else if (logFailed) {
            LOG.info(message);
        }
        return false;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) IOException(java.io.IOException) LeaderRetrievalException(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalException) UnknownHostException(java.net.UnknownHostException)

Example 72 with SocketAddress

use of java.net.SocketAddress in project camel by apache.

the class MinaComponent method createVmEndpoint.

// Implementation methods
//-------------------------------------------------------------------------
protected MinaEndpoint createVmEndpoint(String uri, MinaConfiguration configuration) {
    boolean minaLogger = configuration.isMinaLogger();
    boolean sync = configuration.isSync();
    List<IoFilter> filters = configuration.getFilters();
    IoAcceptor acceptor = new VmPipeAcceptor();
    SocketAddress address = new VmPipeAddress(configuration.getPort());
    IoConnector connector = new VmPipeConnector();
    // connector config
    configureCodecFactory("MinaProducer", connector.getDefaultConfig(), configuration);
    if (minaLogger) {
        connector.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, connector.getFilterChain());
    // acceptor connectorConfig
    configureCodecFactory("MinaConsumer", acceptor.getDefaultConfig(), configuration);
    if (minaLogger) {
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, acceptor.getFilterChain());
    MinaEndpoint endpoint = new MinaEndpoint(uri, this);
    endpoint.setAddress(address);
    endpoint.setAcceptor(acceptor);
    endpoint.setConnector(connector);
    endpoint.setConfiguration(configuration);
    // set sync or async mode after endpoint is created
    if (sync) {
        endpoint.setExchangePattern(ExchangePattern.InOut);
    } else {
        endpoint.setExchangePattern(ExchangePattern.InOnly);
    }
    return endpoint;
}
Also used : VmPipeAcceptor(org.apache.mina.transport.vmpipe.VmPipeAcceptor) IoAcceptor(org.apache.mina.common.IoAcceptor) LoggingFilter(org.apache.mina.filter.LoggingFilter) IoFilter(org.apache.mina.common.IoFilter) VmPipeAddress(org.apache.mina.transport.vmpipe.VmPipeAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) IoConnector(org.apache.mina.common.IoConnector) VmPipeConnector(org.apache.mina.transport.vmpipe.VmPipeConnector)

Example 73 with SocketAddress

use of java.net.SocketAddress in project camel by apache.

the class MinaComponent method createDatagramEndpoint.

protected MinaEndpoint createDatagramEndpoint(String uri, MinaConfiguration configuration) {
    boolean minaLogger = configuration.isMinaLogger();
    long timeout = configuration.getTimeout();
    boolean transferExchange = configuration.isTransferExchange();
    boolean sync = configuration.isSync();
    List<IoFilter> filters = configuration.getFilters();
    ExecutorService acceptorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaDatagramAcceptor");
    ExecutorService connectorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaDatagramConnector");
    ExecutorService workerPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaThreadPool");
    IoAcceptor acceptor = new DatagramAcceptor(acceptorPool);
    IoConnector connector = new DatagramConnector(connectorPool);
    SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    if (transferExchange) {
        throw new IllegalArgumentException("transferExchange=true is not supported for datagram protocol");
    }
    DatagramConnectorConfig connectorConfig = new DatagramConnectorConfig();
    // must use manual thread model according to Mina documentation
    connectorConfig.setThreadModel(ThreadModel.MANUAL);
    configureDataGramCodecFactory("MinaProducer", connectorConfig, configuration);
    connectorConfig.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
    if (minaLogger) {
        connectorConfig.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, connectorConfig.getFilterChain());
    // set connect timeout to mina in seconds
    connectorConfig.setConnectTimeout((int) (timeout / 1000));
    DatagramAcceptorConfig acceptorConfig = new DatagramAcceptorConfig();
    // must use manual thread model according to Mina documentation
    acceptorConfig.setThreadModel(ThreadModel.MANUAL);
    configureDataGramCodecFactory("MinaConsumer", acceptorConfig, configuration);
    acceptorConfig.setDisconnectOnUnbind(true);
    // reuse address is default true for datagram
    acceptorConfig.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
    if (minaLogger) {
        acceptorConfig.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, acceptorConfig.getFilterChain());
    MinaEndpoint endpoint = new MinaEndpoint(uri, this);
    endpoint.setAddress(address);
    endpoint.setAcceptor(acceptor);
    endpoint.setAcceptorConfig(acceptorConfig);
    endpoint.setConnector(connector);
    endpoint.setConnectorConfig(connectorConfig);
    endpoint.setConfiguration(configuration);
    // enlist threads pools in use on endpoint
    endpoint.addThreadPool(acceptorPool);
    endpoint.addThreadPool(connectorPool);
    endpoint.addThreadPool(workerPool);
    // set sync or async mode after endpoint is created
    if (sync) {
        endpoint.setExchangePattern(ExchangePattern.InOut);
    } else {
        endpoint.setExchangePattern(ExchangePattern.InOnly);
    }
    return endpoint;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ExecutorFilter(org.apache.mina.filter.executor.ExecutorFilter) LoggingFilter(org.apache.mina.filter.LoggingFilter) IoFilter(org.apache.mina.common.IoFilter) DatagramConnector(org.apache.mina.transport.socket.nio.DatagramConnector) DatagramConnectorConfig(org.apache.mina.transport.socket.nio.DatagramConnectorConfig) DatagramAcceptorConfig(org.apache.mina.transport.socket.nio.DatagramAcceptorConfig) DatagramAcceptor(org.apache.mina.transport.socket.nio.DatagramAcceptor) IoAcceptor(org.apache.mina.common.IoAcceptor) ExecutorService(java.util.concurrent.ExecutorService) IoConnector(org.apache.mina.common.IoConnector) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 74 with SocketAddress

use of java.net.SocketAddress in project camel by apache.

the class MinaProducer method openConnection.

private void openConnection() {
    SocketAddress address = getEndpoint().getAddress();
    connector = getEndpoint().getConnector();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {} using connector: {} timeout: {} millis.", new Object[] { address, connector, timeout });
    }
    IoHandler ioHandler = new ResponseHandler(getEndpoint());
    // connect and wait until the connection is established
    ConnectFuture future = connector.connect(address, ioHandler, getEndpoint().getConnectorConfig());
    future.join();
    session = future.getSession();
}
Also used : ConnectFuture(org.apache.mina.common.ConnectFuture) SocketAddress(java.net.SocketAddress) IoHandler(org.apache.mina.common.IoHandler)

Example 75 with SocketAddress

use of java.net.SocketAddress in project camel by apache.

the class MllpSocketUtil method getAddressString.

public static String getAddressString(Socket socket) {
    String localAddressString = "null";
    String remoteAddressString = "null";
    if (socket != null) {
        SocketAddress localSocketAddress = socket.getLocalSocketAddress();
        if (localSocketAddress != null) {
            localAddressString = localSocketAddress.toString();
        }
        SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();
        if (remoteSocketAddress != null) {
            remoteAddressString = remoteSocketAddress.toString();
        }
    }
    return String.format("%s -> %s", localAddressString, remoteAddressString);
}
Also used : SocketAddress(java.net.SocketAddress)

Aggregations

SocketAddress (java.net.SocketAddress)355 InetSocketAddress (java.net.InetSocketAddress)279 IOException (java.io.IOException)79 Test (org.junit.Test)73 Socket (java.net.Socket)48 InetAddress (java.net.InetAddress)35 Channel (org.jboss.netty.channel.Channel)33 SocketException (java.net.SocketException)27 UnknownHostException (java.net.UnknownHostException)25 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)23 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)23 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)20 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)19 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 Proxy (java.net.Proxy)18 HashMap (java.util.HashMap)17 DatagramPacket (java.net.DatagramPacket)16 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.apache.log4j.Logger)16 Test (org.testng.annotations.Test)16