Exemplo n.º 1
0
 private Connection(string host, int port, TcpClient tcpClient, ConnectionConfiguration configuration)
 {
     _host = host;
     _port = port;
     _tcpClient = tcpClient;
     _configuration = configuration;
 }
Exemplo n.º 2
0
        public static async Task<INode> ConnectAsync(string host, int port, ConnectionPoolConfiguration poolConfiguration, ConnectionConfiguration connectionConfiguration)
        {
            var masterPool = await ConnectionPool.ConnectAsync(host, port, poolConfiguration, connectionConfiguration);
            var infoCommand = Command.From("INFO");
            var response = await masterPool.SendAsync(infoCommand);

            if (response.IsError)
                throw ExceptionBecause.Node.GotErrorResponseFromNodeInfo(host, port);

            if (response.Value.IsArray)
                throw ExceptionBecause.MalformedData.InNodeInfo(host, port, response);

            var slavePoolTasks = response.Value.ToString()
                .Split(new [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                .Where(s => s.StartsWith("slave"))
                .Select(slaveInfo =>
                {
                    var components = slaveInfo.Split(',');

                    if (components.Length != 5)
                        throw ExceptionBecause.MalformedData.InMasterSlaveInfo(host, port, slaveInfo);

                    var nodeHost = components[2];
                    var nodePort = int.Parse(components[3]);

                    return ConnectionPool.ConnectAsync(nodeHost, nodePort, poolConfiguration, connectionConfiguration);
                });

            var slavePools = await Task.WhenAll(slavePoolTasks);

            return new Node(host, port, masterPool, slavePools);
        }
Exemplo n.º 3
0
        public static async Task<IConnection> ConnectAsync(string host, int port, ConnectionConfiguration configuration)
        {
            var tcpClient = new TcpClient();
            
            var connection = new Connection(host, port, tcpClient, configuration);
            await connection.Connect();

            return connection;
        }
Exemplo n.º 4
0
        public static async Task<IConnectionPool> ConnectAsync(string host, int port, ConnectionPoolConfiguration poolConfiguration, ConnectionConfiguration connectionConfiguration)
        {
            var tasks = new List<Task<IConnection>>();

            for (var i = 0; i < poolConfiguration.ConnectionsPerPool; i++)
                tasks.Add(Connection.ConnectAsync(host, port, connectionConfiguration));

            var connections = await Task.WhenAll(tasks);

            return new ConnectionPool(poolConfiguration, connections);
        }