forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new PHP examples for flclient1 and flserver1
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/* | ||
* Freelance Client - Model 1 | ||
* Uses REQ socket to query one or more services | ||
* | ||
* Author: Rob Gagnon <rgagnon24(at)gmail(dot)com> | ||
*/ | ||
|
||
$request_timeout = 1000; // ms | ||
$max_retries = 3; # Before we abandon | ||
|
||
/** | ||
* @param ZMQContext $ctx | ||
* @param string $endpoint | ||
* @param string $request | ||
*/ | ||
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); | ||
|
||
$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; | ||
} | ||
} | ||
|
||
$poll->remove($client); | ||
$poll = null; | ||
$client = null; | ||
return $reply; | ||
} | ||
|
||
$context = new ZMQContext(); | ||
$request = 'Hello world'; | ||
$reply = null; | ||
|
||
$cmd = array_shift($argv); | ||
$endpoints = count($argv); | ||
if ($endpoints == 0) { | ||
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); | ||
} | ||
} 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); | ||
} | ||
} | ||
|
||
if (isset($reply)) { | ||
print "Service is running OK\n"; | ||
} |
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,23 @@ | ||
<?php | ||
/* | ||
* Freelance server - Model 1 | ||
* Trivial echo service | ||
* | ||
* Author: Rob Gagnon <rgagnon24(at)gmail(dot)com> | ||
*/ | ||
|
||
if (count($argv) < 2) { | ||
printf("I: Syntax: %s <endpoint>\n", $argv[0]); | ||
exit; | ||
} | ||
|
||
$endpoint = $argv[1]; | ||
$context = new ZMQContext(); | ||
$server = $context->getSocket(ZMQ::SOCKET_REP); | ||
$server->bind($endpoint); | ||
|
||
printf("I: Echo service is ready at %s\n", $endpoint); | ||
while(true) { | ||
$msg = $server->recvMulti(); | ||
$server->sendMulti($msg); | ||
} |