Search in sources :

Example 1 with TcpClientConfig

use of com.bonree.brfs.common.net.tcp.client.TcpClientConfig in project BRFS by zhangnianli.

the class TcpClientUtils method getClient.

public static TcpDiskNodeClient getClient(String host, int port, int export, int timeout) throws InterruptedException, IOException {
    TcpClient<BaseMessage, BaseResponse> tcpClient = group.createClient(new TcpClientConfig() {

        @Override
        public SocketAddress remoteAddress() {
            return new InetSocketAddress(host, port);
        }

        @Override
        public int connectTimeoutMillis() {
            return timeout;
        }
    });
    TcpClient<ReadObject, FileContentPart> readerClient = group2.createClient(new AsyncFileReaderCreateConfig() {

        @Override
        public SocketAddress remoteAddress() {
            return new InetSocketAddress(host, export);
        }

        @Override
        public int connectTimeoutMillis() {
            return timeout;
        }

        @Override
        public int maxPendingRead() {
            return 0;
        }
    });
    return new TcpDiskNodeClient(tcpClient, readerClient);
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) TcpDiskNodeClient(com.bonree.brfs.disknode.client.TcpDiskNodeClient) TcpClientConfig(com.bonree.brfs.common.net.tcp.client.TcpClientConfig) InetSocketAddress(java.net.InetSocketAddress) ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject) FileContentPart(com.bonree.brfs.common.net.tcp.file.client.FileContentPart) AsyncFileReaderCreateConfig(com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with TcpClientConfig

use of com.bonree.brfs.common.net.tcp.client.TcpClientConfig in project BRFS by zhangnianli.

the class TcpDiskNodeConnectionPool method getConnection.

@Override
public DiskNodeConnection getConnection(String serviceGroup, String serviceId) {
    TcpDiskNodeConnection connection = connectionCache.get(serviceGroup, serviceId);
    if (connection != null) {
        if (connection.isValid()) {
            return connection;
        }
        synchronized (connectionCache) {
            connectionCache.remove(serviceGroup, serviceId);
        }
    }
    try {
        synchronized (connectionCache) {
            Service service = serviceManager.getServiceById(serviceGroup, serviceId);
            if (service == null) {
                return null;
            }
            TcpClient<BaseMessage, BaseResponse> client = tcpClientGroup.createClient(new TcpClientConfig() {

                @Override
                public SocketAddress remoteAddress() {
                    return new InetSocketAddress(service.getHost(), service.getPort());
                }

                @Override
                public int connectTimeoutMillis() {
                    return 3000;
                }
            }, executor);
            if (client == null) {
                return null;
            }
            connection = new TcpDiskNodeConnection(client);
            connectionCache.put(serviceGroup, serviceId, connection);
        }
        return connection;
    } catch (Exception e) {
        LOG.error("connect tcp connection to disk node error", e);
    }
    return null;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) TcpClientConfig(com.bonree.brfs.common.net.tcp.client.TcpClientConfig) InetSocketAddress(java.net.InetSocketAddress) Service(com.bonree.brfs.common.service.Service) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)2 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)2 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)1 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)1 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)1 Service (com.bonree.brfs.common.service.Service)1 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)1