Permalink
Browse files

fixed coding standard according to PSR compliance

  • Loading branch information...
1 parent 7162472 commit 2ec0aeb0f4fbb65b44663a6a98e223365060e927 @brikou brikou committed Nov 30, 2012
Showing with 2,831 additions and 2,726 deletions.
  1. +115 −112 examples/PHP/asyncsrv.php
  2. +45 −43 examples/PHP/flclient1.php
  3. +79 −74 examples/PHP/flclient2.php
  4. +6 −6 examples/PHP/flserver1.php
  5. +12 −12 examples/PHP/flserver2.php
  6. +6 −6 examples/PHP/hwclient.php
  7. +9 −9 examples/PHP/hwserver.php
  8. +1 −1 examples/PHP/identity.php
  9. +135 −131 examples/PHP/lbbroker.php
  10. +111 −106 examples/PHP/lbbroker2.php
  11. +45 −43 examples/PHP/lpclient.php
  12. +16 −16 examples/PHP/lpserver.php
  13. +359 −343 examples/PHP/mdbroker.php
  14. +139 −133 examples/PHP/mdcliapi.php
  15. +112 −105 examples/PHP/mdcliapi2.php
  16. +9 −9 examples/PHP/mdclient.php
  17. +10 −10 examples/PHP/mdclient2.php
  18. +1 −1 examples/PHP/mdp.php
  19. +5 −5 examples/PHP/mdworker.php
  20. +79 −73 examples/PHP/mdwrkapi.php
  21. +4 −4 examples/PHP/mmiecho.php
  22. +16 −17 examples/PHP/mspoller.php
  23. +26 −26 examples/PHP/msreader.php
  24. +33 −34 examples/PHP/mtrelay.php
  25. +23 −22 examples/PHP/mtserver.php
  26. +27 −28 examples/PHP/peering1.php
  27. +131 −129 examples/PHP/peering2.php
  28. +179 −176 examples/PHP/peering3.php
  29. +131 −124 examples/PHP/ppqueue.php
  30. +64 −63 examples/PHP/ppworker.php
  31. +7 −7 examples/PHP/psenvpub.php
  32. +6 −6 examples/PHP/psenvsub.php
  33. +26 −27 examples/PHP/rrbroker.php
  34. +5 −5 examples/PHP/rrclient.php
  35. +10 −10 examples/PHP/rrworker.php
  36. +44 −44 examples/PHP/rtdealer.php
  37. +42 −41 examples/PHP/rtreq.php
  38. +2 −2 examples/PHP/rtrouter.php
  39. +38 −40 examples/PHP/spqueue.php
  40. +19 −19 examples/PHP/spworker.php
  41. +41 −41 examples/PHP/suisnail.php
  42. +7 −7 examples/PHP/syncpub.php
  43. +6 −6 examples/PHP/syncsub.php
  44. +7 −7 examples/PHP/tasksink.php
  45. +7 −7 examples/PHP/tasksink2.php
  46. +8 −8 examples/PHP/taskvent.php
  47. +9 −9 examples/PHP/taskwork.php
  48. +21 −21 examples/PHP/taskwork2.php
  49. +19 −18 examples/PHP/ticlient.php
  50. +67 −62 examples/PHP/titanic.php
  51. +85 −83 examples/PHP/tripping.php
  52. +3 −3 examples/PHP/version.php
  53. +4 −4 examples/PHP/wuclient.php
  54. +11 −11 examples/PHP/wuproxy.php
  55. +7 −7 examples/PHP/wuserver.php
  56. +22 −21 examples/PHP/zhelpers.php
  57. +379 −348 examples/PHP/zmsg.php
  58. +1 −1 examples/PHP/zmsg_test.php
View
@@ -1,47 +1,48 @@
<?php
/*
* Asynchronous client-to-server (DEALER to ROUTER)
- *
+ *
* While this example runs in a single process, that is just to make
* it easier to start and stop the example. Each task has its own
* context and conceptually acts as a separate process.
* @author Ian Barber <ian(dot)barber(at)gmail(dot)com>
*/
-include "zmsg.php";
+include 'zmsg.php';
/* ---------------------------------------------------------------------
* This is our client task
* It connects to the server, and then sends a request once per second
* It collects responses as they arrive, and it prints them out. We will
* run several client tasks in parallel, each with a different random ID.
*/
-function client_task() {
- $context = new ZMQContext();
- $client = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
-
- // Generate printable identity for the client
- $identity = sprintf ("%04X", rand(0, 0x10000));
- $client->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $identity);
- $client->connect("tcp://localhost:5570");
-
- $read = $write = array();
- $poll = new ZMQPoll();
- $poll->add($client, ZMQ::POLL_IN);
-
- $request_nbr = 0;
- while(true) {
- // Tick once per second, pulling in arriving messages
- for($centitick = 0; $centitick < 100; $centitick++) {
- $events = $poll->poll($read, $write, 1000);
- $zmsg = new Zmsg($client);
- if($events) {
- $zmsg->recv();
- printf ("%s: %s%s", $identity, $zmsg->body(), PHP_EOL);
- }
- }
- $zmsg = new Zmsg($client);
- $zmsg->body_fmt("request #%d", ++$request_nbr)->send();
- }
+function client_task()
+{
+ $context = new ZMQContext();
+ $client = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
+
+ // Generate printable identity for the client
+ $identity = sprintf ("%04X", rand(0, 0x10000));
+ $client->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $identity);
+ $client->connect("tcp://localhost:5570");
+
+ $read = $write = array();
+ $poll = new ZMQPoll();
+ $poll->add($client, ZMQ::POLL_IN);
+
+ $request_nbr = 0;
+ while (true) {
+ // Tick once per second, pulling in arriving messages
+ for ($centitick = 0; $centitick < 100; $centitick++) {
+ $events = $poll->poll($read, $write, 1000);
+ $zmsg = new Zmsg($client);
+ if ($events) {
+ $zmsg->recv();
+ printf ("%s: %s%s", $identity, $zmsg->body(), PHP_EOL);
+ }
+ }
+ $zmsg = new Zmsg($client);
+ $zmsg->body_fmt("request #%d", ++$request_nbr)->send();
+ }
}
/* ---------------------------------------------------------------------
@@ -51,96 +52,98 @@ function client_task() {
* one request at a time but one client can talk to multiple workers at
* once.
*/
-function server_task() {
-
- // Launch pool of worker threads, precise number is not critical
- for($thread_nbr = 0; $thread_nbr < 5; $thread_nbr++) {
- $pid = pcntl_fork();
- if($pid == 0) {
- server_worker();
- exit();
- }
- }
-
- $context = new ZMQContext();
-
- // Frontend socket talks to clients over TCP
- $frontend = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
- $frontend->bind("tcp://*:5570");
-
- // Backend socket talks to workers over ipc
- $backend = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
- $backend->bind("ipc://backend");
-
- // Connect backend to frontend via a queue device
- // We could do this:
- // $device = new ZMQDevice($frontend, $backend);
- // But doing it ourselves means we can debug this more easily
-
- $read = $write = array();
- // Switch messages between frontend and backend
- while(true) {
- $poll = new ZMQPoll();
- $poll->add($frontend, ZMQ::POLL_IN);
- $poll->add($backend, ZMQ::POLL_IN);
-
- $poll->poll($read, $write);
- foreach($read as $socket) {
- $zmsg = new Zmsg($socket);
- $zmsg->recv();
- if($socket === $frontend) {
- //echo "Request from client:";
- //echo $zmsg->__toString();
- $zmsg->set_socket($backend)->send();
- } else if($socket === $backend) {
- //echo "Request from worker:";
- //echo $zmsg->__toString();
- $zmsg->set_socket($frontend)->send();
- }
- }
- }
+function server_task()
+{
+ // Launch pool of worker threads, precise number is not critical
+ for ($thread_nbr = 0; $thread_nbr < 5; $thread_nbr++) {
+ $pid = pcntl_fork();
+ if ($pid == 0) {
+ server_worker();
+ exit();
+ }
+ }
+
+ $context = new ZMQContext();
+
+ // Frontend socket talks to clients over TCP
+ $frontend = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
+ $frontend->bind("tcp://*:5570");
+
+ // Backend socket talks to workers over ipc
+ $backend = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
+ $backend->bind("ipc://backend");
+
+ // Connect backend to frontend via a queue device
+ // We could do this:
+ // $device = new ZMQDevice($frontend, $backend);
+ // But doing it ourselves means we can debug this more easily
+
+ $read = $write = array();
+ // Switch messages between frontend and backend
+ while (true) {
+ $poll = new ZMQPoll();
+ $poll->add($frontend, ZMQ::POLL_IN);
+ $poll->add($backend, ZMQ::POLL_IN);
+
+ $poll->poll($read, $write);
+ foreach ($read as $socket) {
+ $zmsg = new Zmsg($socket);
+ $zmsg->recv();
+ if ($socket === $frontend) {
+ //echo "Request from client:";
+ //echo $zmsg->__toString();
+ $zmsg->set_socket($backend)->send();
+ } elseif ($socket === $backend) {
+ //echo "Request from worker:";
+ //echo $zmsg->__toString();
+ $zmsg->set_socket($frontend)->send();
+ }
+ }
+ }
}
-function server_worker() {
- $context = new ZMQContext();
- $worker = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
- $worker->connect("ipc://backend");
- $zmsg = new Zmsg($worker);
-
- while(true) {
- // The DEALER socket gives us the address envelope and message
- $zmsg->recv();
- assert($zmsg->parts() == 2);
-
- // Send 0..4 replies back
- $replies = rand(0,4);
- for($reply = 0; $reply < $replies; $reply++) {
- // Sleep for some fraction of a second
- usleep(rand(0,1000) + 1);
- $zmsg->send(Zmsg::NOCLEAR);
- }
-
- }
+function server_worker()
+{
+ $context = new ZMQContext();
+ $worker = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
+ $worker->connect("ipc://backend");
+ $zmsg = new Zmsg($worker);
+
+ while (true) {
+ // The DEALER socket gives us the address envelope and message
+ $zmsg->recv();
+ assert($zmsg->parts() == 2);
+
+ // Send 0..4 replies back
+ $replies = rand(0,4);
+ for ($reply = 0; $reply < $replies; $reply++) {
+ // Sleep for some fraction of a second
+ usleep(rand(0,1000) + 1);
+ $zmsg->send(Zmsg::NOCLEAR);
+ }
+
+ }
}
/* This main thread simply starts several clients, and a server, and then
* waits for the server to finish.
*/
-function main() {
- for($num_clients = 0; $num_clients < 3; $num_clients++) {
- $pid = pcntl_fork();
- if($pid == 0) {
- client_task();
- exit();
- }
- }
-
- $pid = pcntl_fork();
- if($pid == 0) {
- server_task();
- exit();
- }
-
+function main()
+{
+ for ($num_clients = 0; $num_clients < 3; $num_clients++) {
+ $pid = pcntl_fork();
+ if ($pid == 0) {
+ client_task();
+ exit();
+ }
+ }
+
+ $pid = pcntl_fork();
+ if ($pid == 0) {
+ server_task();
+ exit();
+ }
+
}
main();
View
@@ -14,32 +14,34 @@
* @param string $endpoint
* @param string $request
*/
-function try_request($ctx, $endpoint, $request) {
- global $request_timeout;
+function try_request($ctx, $endpoint, $request)
+{
+ global $request_timeout;
- printf("I: Trying echo service at %s...\n", $endpoint);
- $client = $ctx->getSocket(ZMQ::SOCKET_REQ);
- $client->connect($endpoint);
- $client->send($request);
+ printf("I: Trying echo service at %s...\n", $endpoint);
+ $client = $ctx->getSocket(ZMQ::SOCKET_REQ);
+ $client->connect($endpoint);
+ $client->send($request);
- $poll = new ZMQPoll();
- $poll->add($client, ZMQ::POLL_IN);
- $readable = $writable = array();
+ $poll = new ZMQPoll();
+ $poll->add($client, ZMQ::POLL_IN);
+ $readable = $writable = array();
- $events = $poll->poll($readable, $writable, $request_timeout);
- $reply = null;
- foreach($readable as $sock) {
- if ($sock == $client) {
- $reply = $client->recvMulti();
- } else {
- $reply = null;
- }
- }
+ $events = $poll->poll($readable, $writable, $request_timeout);
+ $reply = null;
+ foreach ($readable as $sock) {
+ if ($sock == $client) {
+ $reply = $client->recvMulti();
+ } else {
+ $reply = null;
+ }
+ }
- $poll->remove($client);
- $poll = null;
- $client = null;
- return $reply;
+ $poll->remove($client);
+ $poll = null;
+ $client = null;
+
+ return $reply;
}
$context = new ZMQContext();
@@ -49,31 +51,31 @@ function try_request($ctx, $endpoint, $request) {
$cmd = array_shift($argv);
$endpoints = count($argv);
if ($endpoints == 0) {
- printf("I: syntax: %s <endpoint> ...\n", $cmd);
- exit;
+ printf("I: syntax: %s <endpoint> ...\n", $cmd);
+ exit;
}
if ($endpoints == 1) {
- // For one endpoint, we retry N times
- $endpoint = $argv[0];
- for($retries = 0; $retries < $max_retries; $retries++) {
- $reply = try_request($context, $endpoint, $request);
- if (isset($reply)) {
- break; // Success
- }
- printf("W: No response from %s, retrying\n", $endpoint);
- }
+ // For one endpoint, we retry N times
+ $endpoint = $argv[0];
+ for ($retries = 0; $retries < $max_retries; $retries++) {
+ $reply = try_request($context, $endpoint, $request);
+ if (isset($reply)) {
+ break; // Success
+ }
+ printf("W: No response from %s, retrying\n", $endpoint);
+ }
} else {
- // For multiple endpoints, try each at most once
- foreach($argv as $endpoint) {
- $reply = try_request($context, $endpoint, $request);
- if (isset($reply)) {
- break; // Success
- }
- printf("W: No response from %s\n", $endpoint);
- }
+ // For multiple endpoints, try each at most once
+ foreach ($argv as $endpoint) {
+ $reply = try_request($context, $endpoint, $request);
+ if (isset($reply)) {
+ break; // Success
+ }
+ printf("W: No response from %s\n", $endpoint);
+ }
}
if (isset($reply)) {
- print "Service is running OK\n";
-}
+ print "Service is running OK\n";
+}
Oops, something went wrong.

0 comments on commit 2ec0aeb

Please sign in to comment.