<commit_before>﻿// Copyright (c) 2012, Event Store LLP
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//  
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the Event Store LLP nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//  

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using EventStore.ClientAPI.ClientOperations;
using EventStore.ClientAPI.Common.Log;
using EventStore.ClientAPI.Common.Utils;
using EventStore.ClientAPI.Connection;
using EventStore.ClientAPI.Exceptions;
using EventStore.ClientAPI.SystemData;
using EventStore.ClientAPI.Transport.Tcp;
using Ensure = EventStore.ClientAPI.Common.Utils.Ensure;
using System.Linq;

namespace EventStore.ClientAPI
{
    public class EventStoreConnection : IDisposable
    {
        private readonly ILogger _log;

        private IPEndPoint _tcpEndPoint;
        private readonly TcpConnector _connector;
        private TcpTypedConnection _connection;
        private readonly object _connectionLock = new object();
        private volatile bool _active;

        private readonly SubscriptionsChannel _subscriptionsChannel;

        private readonly Common.Concurrent.ConcurrentQueue<IClientOperation> _queue = new Common.Concurrent.ConcurrentQueue<IClientOperation>();
        private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, WorkItem> _inProgress = new System.Collections.Concurrent.ConcurrentDictionary<Guid, WorkItem>();
        private int _inProgressCount;

        private DateTime _lastReconnectionTimestamp;
        private readonly Stopwatch _reconnectionStopwatch = new Stopwatch();
        private readonly Stopwatch _timeoutCheckStopwatch = new Stopwatch();
        private int _reconnectionsCount;

        private Thread _worker;
        private volatile bool _stopping;

        private readonly ConnectionSettings _settings;

        private EventStoreConnection(ConnectionSettings settings)
        {
            _settings = settings;

            LogManager.RegisterLogger(settings.Log);
            _log = LogManager.GetLogger();

            _connector = new TcpConnector();
            _subscriptionsChannel = new SubscriptionsChannel(_connector);
        }

        public static EventStoreConnection Create()
        {
            return new EventStoreConnection(ConnectionSettings.Default);
        }

        public static EventStoreConnection Create(ConnectionSettings settings)
        {
            Ensure.NotNull(settings, "settings");
            return new EventStoreConnection(settings);
        }

        public void Connect(IPEndPoint tcpEndPoint)
        {
            ConnectAsync(tcpEndPoint).Wait();
        }

        public Task ConnectAsync(IPEndPoint tcpEndPoint)
        {
            Ensure.NotNull(tcpEndPoint, "tcpEndPoint");
            return EstablishConnectionAsync(tcpEndPoint);
        }

        public void Connect(string clusterDns, int maxDiscoverAttempts = 10, int port = 30777)
        {
            ConnectAsync(clusterDns, maxDiscoverAttempts, port).Wait();
        }

        public Task ConnectAsync(string clusterDns, int maxDiscoverAttempts = 10, int port = 30777)
        {
            Ensure.NotNullOrEmpty(clusterDns, "clusterDns");
            Ensure.Positive(maxDiscoverAttempts, "maxDiscoverAttempts");
            Ensure.Nonnegative(port, "port");
<commit_msg>add overload that takes string in form of IP:port<commit_after>﻿// Copyright (c) 2012, Event Store LLP
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//  
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the Event Store LLP nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//  

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using EventStore.ClientAPI.ClientOperations;
using EventStore.ClientAPI.Common.Log;
using EventStore.ClientAPI.Common.Utils;
using EventStore.ClientAPI.Connection;
using EventStore.ClientAPI.Exceptions;
using EventStore.ClientAPI.SystemData;
using EventStore.ClientAPI.Transport.Tcp;
using Ensure = EventStore.ClientAPI.Common.Utils.Ensure;
using System.Linq;

namespace EventStore.ClientAPI
{
    public class EventStoreConnection : IDisposable
    {
        private readonly ILogger _log;

        private IPEndPoint _tcpEndPoint;
        private readonly TcpConnector _connector;
        private TcpTypedConnection _connection;
        private readonly object _connectionLock = new object();
        private volatile bool _active;

        private readonly SubscriptionsChannel _subscriptionsChannel;

        private readonly Common.Concurrent.ConcurrentQueue<IClientOperation> _queue = new Common.Concurrent.ConcurrentQueue<IClientOperation>();
        private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, WorkItem> _inProgress = new System.Collections.Concurrent.ConcurrentDictionary<Guid, WorkItem>();
        private int _inProgressCount;

        private DateTime _lastReconnectionTimestamp;
        private readonly Stopwatch _reconnectionStopwatch = new Stopwatch();
        private readonly Stopwatch _timeoutCheckStopwatch = new Stopwatch();
        private int _reconnectionsCount;

        private Thread _worker;
        private volatile bool _stopping;

        private readonly ConnectionSettings _settings;

        private EventStoreConnection(ConnectionSettings settings)
        {
            _settings = settings;

            LogManager.RegisterLogger(settings.Log);
            _log = LogManager.GetLogger();

            _connector = new TcpConnector();
            _subscriptionsChannel = new SubscriptionsChannel(_connector);
        }

        public static EventStoreConnection Create()
        {
            return new EventStoreConnection(ConnectionSettings.Default);
        }

        public static EventStoreConnection Create(ConnectionSettings settings)
        {
            Ensure.NotNull(settings, "settings");
            return new EventStoreConnection(settings);
        }

        public void Connect(string endPoint)
        {
            var pieces = endPoint.Split(':');
            if(pieces.Count() != 2) throw new ArgumentOutOfRangeException("endPoint", "endpoint should be of the form ip:port");
            Connect(new IPEndPoint(IPAddress.Parse(pieces[0]), int.Parse(pieces[1])));
        }

        public void Connect(IPEndPoint tcpEndPoint)
        {
            ConnectAsync(tcpEndPoint).Wait();
        }

        public Task ConnectAsync(IPEndPoint tcpEndPoint)
        {
            Ensure.NotNull(tcpEndPoint, "tcpEndPoint");
            return EstablishConnectionAsync(tcpEndPoint);
        }

        public void Connect(string clusterDns, int maxDiscoverAttempts = 10, int port = 30777)
        {
            ConnectAsync(clusterDns, maxDiscoverAttempts, port).Wait();
        }

        public Task ConnectAsync(string clusterDns, int maxDiscoverAttempts = 10, int port = 30777)
        {
            Ensure.NotNullOrEmpty(clusterDns, "clusterDns");
            Ensure.Positive(maxDiscoverAttempts, "maxDiscoverAttempts");
            Ensure.Nonnegative(port, "port");

{'fraction_non_alphanumeric': 0.06776077325815545, 'fraction_numerical': 0.003926701570680628, 'mean_word_length': 3.371919014084507, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7148563', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2337, 'n_words': 850}