Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  fix undefined variable
  add documentation
  add Client::heartbeatWith()
  add possibility to send data through sockets
  • Loading branch information
Baptouuuu committed Dec 10, 2023
2 parents 4f9bb19 + ab55bd7 commit ec6d408
Show file tree
Hide file tree
Showing 8 changed files with 676 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.5.0 - 2023-12-10

### Added

- `Innmind\IO\IO::sockets()`

## 2.4.1 - 2023-12-03

### Fixed
Expand Down
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ composer require innmind/io

## Usage

> [!NOTE]
> examples below use [`innmind/operating-system`](https://github.com/Innmind/OperatingSystem)
### Reading from a stream by chunks

```php
use Innmind\IO\IO;
use Innmind\OperatingSystem\Factory;
use Innmind\TimeContinuum\Earth\ElapsedPeriod;
use Innmind\Stream\Streams;
use Innmind\Immutable\Str;

$os = Factory::build();
$streams = Streams::fromAmbienAuthority();
$io = IO::of($os->sockets()->watch(...));
$chunks = $io
Expand All @@ -46,10 +51,12 @@ The `$chunks` variable is a `Innmind\Innmutable\Sequence` containing `Innmind\Im

```php
use Innmind\IO\IO;
use Innmind\OperatingSystem\Factory;
use Innmind\TimeContinuum\Earth\ElapsedPeriod;
use Innmind\Stream\Streams;
use Innmind\Immutable\Str;

$os = Factory::build();
$streams = Streams::fromAmbienAuthority();
$io = IO::of($os->sockets()->watch(...));
$lines = $io
Expand All @@ -70,6 +77,48 @@ $lines = $io

This is the same as reading by chunks (described above) except that the delimiter is the end of line character `\n`.

### Reading from a socket with a periodic heartbeat

```php
use Innmind\IO\{
IO,
Readable\Frame,
};
use Innmind\OperatingSystem\Factory;
use Innmind\TimeContinuum\Earth\ElapsedPeriod;
use Innmind\Socket\{
Address,
Client,
};
use Innmind\Stream\Streams;
use Innmind\Immutable\{
Str,
Sequence,
};

$socket = Client\Unix::of(Address\Unix::of('/tmp/foo'))->match(
static fn($socket) => $socket,
static fn() => throw new \RuntimeException;
);
$os = Factory::build();
$io = IO::of($os->sockets()->watch(...));
$frame = $io
->sockets()
->clients()
->wrap($socket)
->toEncoding(Str\Encoding::ascii)
->timeoutAfter(ElapsedPeriod::of(1_000))
->heartbeatWith(static fn() => Sequence::of(Str::of('heartbeat')))
->frames(Frame\Line::new())
->one()
->match(
static fn($line) => $line,
static fn() => throw new \RuntimeException,
);
```

This example will wait to read a single from the socket `/tmp/foo.sock` and it will send a `heartbeat` message every second until the expected line is received.

### Reading from a stream

```php
Expand Down Expand Up @@ -137,5 +186,3 @@ This example will:
- `list<string>` is the value passed to `Fold::result()`

You can think of this `fold` operation as a reduce where you can control when to stop iterating by return either `Fold::fail()` or `Fold::result()`.

**Note**: this example use [`innmind/operating-system`](https://github.com/Innmind/OperatingSystem)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require": {
"php": "~8.2",
"innmind/immutable": "~4.15|~5.0",
"innmind/stream": "~4.0"
"innmind/stream": "~4.0",
"innmind/socket": "~6.1"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions src/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public function readable(): Readable
{
return Readable::of($this->watch);
}

/**
* @psalm-mutation-free
*/
public function sockets(): Sockets
{
return Sockets::of($this->watch);
}
}
42 changes: 42 additions & 0 deletions src/Sockets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types = 1);

namespace Innmind\IO;

use Innmind\TimeContinuum\ElapsedPeriod;
use Innmind\Stream\Watch;

final class Sockets
{
/** @var callable(?ElapsedPeriod): Watch */
private $watch;

/**
* @psalm-mutation-free
*
* @param callable(?ElapsedPeriod): Watch $watch
*/
private function __construct(callable $watch)
{
$this->watch = $watch;
}

/**
* @internal
* @psalm-pure
*
* @param callable(?ElapsedPeriod): Watch $watch
*/
public static function of(callable $watch): self
{
return new self($watch);
}

/**
* @psalm-mutation-free
*/
public function clients(): Sockets\Clients
{
return Sockets\Clients::of($this->watch);
}
}
Loading

0 comments on commit ec6d408

Please sign in to comment.