<commit_before>Async sockets library
=====================

[![Build Status][master-travis-image]][master-travis-url] 
[![Coverage Status][master-coverall-image]][master-coverall-url]
[![SensioLabsInsight][master-sensiolabs-image]][master-sensiolabs-url]
[![Dependency Status][master-versioneye-image]][master-versioneye-url]

Async sockets is the library for asynchronous work with sockets based on php streams. Library's workflow process is built on event model, but also select like and common syncronous models are supported.

## Features

- multiple requests execution at once
- dynamically adding new request during working process
- timeouts handling during connection and IO operations 
- separate timeout values for each socket
- custom sockets setup by php stream contexts
- error handling is based on exceptions
- sends notification about socket events to the system 

## Installation

The recommended way to install async sockets library is through composer

```
$ composer require 'edefimov/async-sockets':dev-master
```

[master-travis-image]: https://img.shields.io/travis/edefimov/async-sockets/master.svg?style=flat
[master-travis-url]: https://travis-ci.org/edefimov/async-sockets
[master-coverall-image]: https://img.shields.io/coveralls/edefimov/async-sockets/master.svg?style=flat<commit_msg>Added sample to readme<commit_after>Async sockets library
=====================

[![Build Status][master-travis-image]][master-travis-url] 
[![Coverage Status][master-coverall-image]][master-coverall-url]
[![SensioLabsInsight][master-sensiolabs-image]][master-sensiolabs-url]
[![Dependency Status][master-versioneye-image]][master-versioneye-url]
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.4-777bb4.svg)](https://php.net/)

Async sockets is the library for asynchronous work with sockets based on php streams. 
Library's workflow process is built on event model, but also select like and common syncronous models are supported.

## Features

- multiple requests execution at once
- full control over timeouts
- dynamically adding new request during working process
- separate timeout values for each socket
- custom sockets setup by php stream contexts
- custom user context for each socket
- stop request either for certain socket or for all of them
- error handling is based on exceptions
- sends notification about socket events to the system

## What is it for
Async sockets library will be good for such tasks like executing multiple requests at once. When you have several
 servers with different delays the response from the fastest ones will be delivered earlier than from the slowest. 
 This allows to have maximum delay at size of timeout assigned for the slowest server.
 
## Installation

The recommended way to install async sockets library is through composer

```
$ composer require 'edefimov/async-sockets':dev-master
```

## Example usage

#### Step 1. Create AsyncSocketFactory at point where you want to start request
```php
$factory = new AsyncSocketFactory();
```

#### Step 2. Create RequestExecutor and proper amount of sockets
```php
$client        = $factory->createSocket(AsyncSocketFactory::SOCKET_CLIENT);
$anotherClient = $factory->createSocket(AsyncSocketFactory::SOCKET_CLIENT);

$executor = $factory->createRequestExecutor();
```

#### Step 3. Add your sockets into RequestExecutor
```php
$executor->addSocket($client, RequestExecutorInterface::OPERATION_WRITE, [
    RequestExecutorInterface::META_ADDRESS => 'tls://github.com:443',
    RequestExecutorInterface::META_CONNECTION_TIMEOUT => 30,
    RequestExecutorInterface::META_IO_TIMEOUT => 5,
]);
$executor->addSocket($anotherClient, RequestExecutorInterface::OPERATION_WRITE, [
    RequestExecutorInterface::META_ADDRESS => 'tls://packagist.org:443',
    RequestExecutorInterface::META_CONNECTION_TIMEOUT => 10,
    RequestExecutorInterface::META_IO_TIMEOUT => 2,
]);
```

#### Step 4. Add handlers for events you are interested in
```php
$executor->addHandler([
    EventType::INITIALIZE   => [$this, 'onInitialize'],
    EventType::CONNECTED    => [$this, 'onConnected'],
    EventType::WRITE        => [$this, 'onWrite'],
    EventType::READ         => [$this, 'onRead'],
    EventType::DISCONNECTED => [$this, 'onDisconnected'],
    EventType::FINALIZE     => [$this, 'onFinalize'],
    EventType::EXCEPTION    => [$this, 'onException'],
    EventType::TIMEOUT      => [$this, 'onTimeout'],
]);
```

#### Step 5. Execute it!
```php
$executor->executeRequest();
```

See full example [here](https://github.com/edefimov/async-sockets/blob/master/demos/Demo/RequestExecutorClient.php)

[master-travis-image]: https://img.shields.io/travis/edefimov/async-sockets/master.svg?style=flat
[master-travis-url]: https://travis-ci.org/edefimov/async-sockets
[master-coverall-image]: https://img.shields.io/coveralls/edefimov/async-sockets/master.svg?style=flat
{'fraction_non_alphanumeric': 0.12730627306273062, 'fraction_numerical': 0.005535055350553505, 'mean_word_length': 5.402887139107611, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 9, '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': '1128024', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1360, 'n_words': 399}