-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented event steam #8
Open
RomanovSci
wants to merge
8
commits into
master
Choose a base branch
from
event-stream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
531ae8c
Implemented event steam
9f26dbb
Added Event Stream documentation to README.md
f00f869
Small fix
75ce459
Refactored event stram
1ae7fe8
Merge branch 'master' into event-stream
b10157a
Changed CASPER_PHP_SDK_TEST_NODE_URL in ci.yml
78f9ff8
Fixed getStateRootHash rpc method params
b4becc3
Merge branch 'master' into event-stream
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Event stream | ||
|
||
Create `EventStream` instance by passing node url and stream path to the constructor. The third argument `$startFromEvent` is not required but can be used to filter out all events with id below `$startFromEvent` value. | ||
|
||
Available stream paths: | ||
- `EventStream::STREAM_PATH_MAIN` - listen system events | ||
- `EventStream::STREAM_PATH_DEPLOYS` - listen deploy events | ||
- `EventStream::STREAM_PATH_SIGS` - listen finality signatures events | ||
|
||
Set event handler callback function to `EventStream` instance with `onEvent()` method. | ||
For event stream aborting use `abort()` method. | ||
|
||
Use `listen()` method for connecting to the node. | ||
|
||
## Example | ||
|
||
```php | ||
$nodeUrl = 'http://localhost:9999'; | ||
$streamPath = \Casper\EventStream\EventStream::STREAM_PATH_MAIN; | ||
$startFromEvent = 12345; | ||
|
||
$es = new Casper\EventStream\EventStream($nodeUrl, $streamPath, $startFromEvent); | ||
$es->onEvent( | ||
function (\Casper\EventStream\Event $event) use ($es) { | ||
if ($event->getId() === null) { | ||
$es->abort(); | ||
return; | ||
} | ||
|
||
echo json_encode($event->getData()) . "\n"; | ||
} | ||
); | ||
|
||
$es->listen(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Casper\EventStream; | ||
|
||
class Event | ||
{ | ||
private ?int $id = null; | ||
|
||
private ?array $data = null; | ||
|
||
/** | ||
* @param string $data | ||
* @throws \Exception | ||
*/ | ||
public function __construct(string $data) | ||
{ | ||
if (preg_match_all("/data:(?<data>.*)/", $data, $match)) { | ||
$this->data = json_decode($match['data'][0], true); | ||
} | ||
|
||
if (preg_match_all("/id:(?<id>.*)/", $data, $match)) { | ||
$this->id = $match['id'][0]; | ||
} | ||
} | ||
|
||
public function getId(): ?string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getData(): ?array | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Casper\EventStream; | ||
|
||
class EventStream | ||
{ | ||
public const STREAM_PATH_MAIN = '/events/main'; | ||
public const STREAM_PATH_DEPLOYS = '/events/deploys'; | ||
public const STREAM_PATH_SIGS = '/events/sigs'; | ||
|
||
private string $nodeUrl; | ||
|
||
private string $streamPath; | ||
|
||
private int $startFromEvent; | ||
|
||
private $onEvent; | ||
|
||
private bool $aborted = false; | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function __construct(string $nodeUrl, string $streamPath, int $startFromEvent = 0) | ||
{ | ||
$this->assertSteamPathIsValid($streamPath); | ||
|
||
$this->nodeUrl = $nodeUrl; | ||
$this->streamPath = $streamPath; | ||
$this->startFromEvent = $startFromEvent; | ||
} | ||
|
||
public function onEvent(callable $onEvent): void | ||
{ | ||
$this->onEvent = $onEvent; | ||
} | ||
|
||
public function abort() | ||
{ | ||
$this->aborted = true; | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function listen(): void | ||
{ | ||
$curl = curl_init($this->nodeUrl . $this->streamPath); | ||
curl_setopt_array($curl, array( | ||
CURLOPT_WRITEFUNCTION => function ($_, $data) { | ||
$event = new Event(trim($data)); | ||
|
||
if (is_callable($this->onEvent) && $event->getId() >= $this->startFromEvent && $event->getData()) { | ||
($this->onEvent)($event); | ||
} | ||
|
||
return strlen($data); | ||
}, | ||
CURLOPT_NOPROGRESS => false, | ||
CURLOPT_PROGRESSFUNCTION => function () { | ||
return $this->aborted; | ||
} | ||
)); | ||
curl_exec($curl); | ||
$error = curl_error($curl); | ||
|
||
if (!$this->aborted && $error) { | ||
throw new \Exception($error); | ||
} | ||
|
||
curl_close($curl); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
private function assertSteamPathIsValid(string $streamPath): void | ||
{ | ||
if (!in_array($streamPath, [self::STREAM_PATH_MAIN, self::STREAM_PATH_DEPLOYS, self::STREAM_PATH_SIGS])) { | ||
throw new \Exception('Invalid stream path'); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RomanovSci we need to add typing here. I guess, the most convenient way would be to have event-specific event classes. The parsing could be moved to the
EventStream
.Note, that events include the standard Casper types inside of them.