Initial commit

This commit is contained in:
Joop Schilder 2020-06-19 17:04:39 +02:00
commit 3b8438f9e7
7 changed files with 161 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/vendor/
/.idea/
/out/
/documents/

12
composer.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "joopschilder/zmq-fileserver",
"authors": [
{
"name": "Joop Schilder",
"email": "jnmschilder@protonmail.com"
}
],
"require": {
"fzaninotto/faker": "^1.9"
}
}

69
composer.lock generated Normal file
View File

@ -0,0 +1,69 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "541af5d4dea7e6b160f304dd7ba3073f",
"packages": [
{
"name": "fzaninotto/faker",
"version": "v1.9.1",
"source": {
"type": "git",
"url": "https://github.com/fzaninotto/Faker.git",
"reference": "fc10d778e4b84d5bd315dad194661e091d307c6f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/fzaninotto/Faker/zipball/fc10d778e4b84d5bd315dad194661e091d307c6f",
"reference": "fc10d778e4b84d5bd315dad194661e091d307c6f",
"shasum": ""
},
"require": {
"php": "^5.3.3 || ^7.0"
},
"require-dev": {
"ext-intl": "*",
"phpunit/phpunit": "^4.8.35 || ^5.7",
"squizlabs/php_codesniffer": "^2.9.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.9-dev"
}
},
"autoload": {
"psr-4": {
"Faker\\": "src/Faker/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "François Zaninotto"
}
],
"description": "Faker is a PHP library that generates fake data for you.",
"keywords": [
"data",
"faker",
"fixtures"
],
"time": "2019-12-12T13:22:17+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "1.1.0"
}

16
file_generator Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env php
<?php
$argc < 2 and die('Usage: file_generator <namespace>' . PHP_EOL);
require_once __DIR__ . '/vendor/autoload.php';
$push = (new ZMQContext)->getSocket(ZMQ::SOCKET_PUSH)->connect('ipc:///tmp/storage_server_sink.ipc');
$faker = Faker\Factory::create();
$id = 1;
while (true) {
$document = file_get_contents("https://twitter.com/{$faker->firstName}");
$push->send($argv[1], ZMQ::MODE_SNDMORE)
->send($id++ . '.html', ZMQ::MODE_SNDMORE)
->send($document);
sleep(2);
}

12
request_file Executable file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env php
<?php
$argc < 3 and die('Usage: request_file <namespace> <name>' . PHP_EOL);
$req = (new ZMQContext)->getSocket(ZMQ::SOCKET_REQ)
->connect('ipc:///tmp/storage_server_query.ipc')
->send(@$argv[1], ZMQ::MODE_SNDMORE)
->send(@$argv[2]);
print($req->recv());
print(PHP_EOL);

10
save_file Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env php
<?php
$argc < 4 and die('Usage: save_file NAMESPACE NAME CONTENTS' . PHP_EOL);
(new ZMQContext)
->getSocket(ZMQ::SOCKET_PUSH)->connect('ipc:///tmp/storage_server_sink.ipc')
->send(@$argv[1], ZMQ::MODE_SNDMORE)
->send(@$argv[2], ZMQ::MODE_SNDMORE)
->send(@$argv[3]);

36
storage_server Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
is_dir($rootDir = rtrim($argv[1] ?? __DIR__ . '/documents', '/')) or mkdir($rootDir, 0777, true);
$zmq = new ZMQContext();
$pullSocket = $zmq->getSocket(ZMQ::SOCKET_PULL)->bind('ipc:///tmp/storage_server_sink.ipc');
$replySocket = $zmq->getSocket(ZMQ::SOCKET_REP)->bind('ipc:///tmp/storage_server_query.ipc');
$poll = new ZMQPoll();
$poll->add($pullSocket, ZMQ::POLL_IN);
$poll->add($replySocket, ZMQ::POLL_IN);
$readable = $writable = [];
while (true) {
$poll->poll($readable, $writable);
foreach ($readable as $socket) {
$socket === $pullSocket and handleIncomingFile();
$socket === $replySocket and handleIncomingRequest();
}
}
function handleIncomingFile(): void
{
global $pullSocket, $rootDir;
$namespace = $pullSocket->recv(ZMQ::MODE_SNDMORE);
$name = $pullSocket->recv(ZMQ::MODE_SNDMORE);
$contents = $pullSocket->recv();
is_dir($targetDir = "$rootDir/$namespace") or @mkdir($targetDir, 0777, true);
file_put_contents("$targetDir/$name", $contents) and print('S');
}
function handleIncomingRequest(): void
{
global $replySocket, $rootDir;
$namespace = $replySocket->recv(ZMQ::MODE_SNDMORE);
$name = $replySocket->recv();
$replySocket->send(@file_get_contents("$rootDir/{$namespace}/{$name}") ?: 'NOT_FOUND') and print('R');
}