From 3b8438f9e79b552c2f06ab6b313fec56479b9f12 Mon Sep 17 00:00:00 2001 From: Joop Schilder Date: Fri, 19 Jun 2020 17:04:39 +0200 Subject: [PATCH] Initial commit --- .gitignore | 6 +++++ composer.json | 12 +++++++++ composer.lock | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ file_generator | 16 ++++++++++++ request_file | 12 +++++++++ save_file | 10 ++++++++ storage_server | 36 ++++++++++++++++++++++++++ 7 files changed, 161 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100755 file_generator create mode 100755 request_file create mode 100755 save_file create mode 100755 storage_server diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..786143a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/vendor/ +/.idea/ + +/out/ +/documents/ + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a3276cd --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "joopschilder/zmq-fileserver", + "authors": [ + { + "name": "Joop Schilder", + "email": "jnmschilder@protonmail.com" + } + ], + "require": { + "fzaninotto/faker": "^1.9" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..2a95c1a --- /dev/null +++ b/composer.lock @@ -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" +} diff --git a/file_generator b/file_generator new file mode 100755 index 0000000..2c82428 --- /dev/null +++ b/file_generator @@ -0,0 +1,16 @@ +#!/usr/bin/env php +' . 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); +} diff --git a/request_file b/request_file new file mode 100755 index 0000000..ca4de11 --- /dev/null +++ b/request_file @@ -0,0 +1,12 @@ +#!/usr/bin/env php + ' . 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); diff --git a/save_file b/save_file new file mode 100755 index 0000000..219cec8 --- /dev/null +++ b/save_file @@ -0,0 +1,10 @@ +#!/usr/bin/env php +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]); diff --git a/storage_server b/storage_server new file mode 100755 index 0000000..68301e0 --- /dev/null +++ b/storage_server @@ -0,0 +1,36 @@ +#!/usr/bin/env php +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'); +}