<?php /* * Cross-connected ROUTER sockets addressing each other * @author Ian Barber <ian(dot)barber(at)gmail(dot)com> */ include "zhelpers.php"; $context = new ZMQContext(); $worker = new ZMQSocket($context, ZMQ::SOCKET_ROUTER); $worker->setSockOpt(ZMQ::SOCKOPT_IDENTITY, "WORKER"); $worker->bind("ipc://rtrouter.ipc"); $server = new ZMQSocket($context, ZMQ::SOCKET_ROUTER); $server->setSockOpt(ZMQ::SOCKOPT_IDENTITY, "SERVER"); $server->connect("ipc://rtrouter.ipc"); sleep(1); $server->send("WORKER", ZMQ::MODE_SNDMORE); $server->send("", ZMQ::MODE_SNDMORE); $server->send("send to worker"); s_dump($worker); $worker->send("SERVER", ZMQ::MODE_SNDMORE); $worker->send("", ZMQ::MODE_SNDMORE); $worker->send("send to server"); s_dump($server);
<?php /* * Demonstrate identities as used by the request-reply pattern. Run this * program by itself. Note that the utility functions s_ are provided by * zhelpers.h. It gets boring for everyone to keep repeating this code. * @author Ian Barber <ian(dot)barber(at)gmail(dot)com> */ include 'zhelpers.php'; $context = new ZMQContext(); $sink = new ZMQSocket($context, ZMQ::SOCKET_ROUTER); $sink->bind("inproc://example"); // First allow 0MQ to set the identity $anonymous = new ZMQSocket($context, ZMQ::SOCKET_REQ); $anonymous->connect("inproc://example"); $anonymous->send("ROUTER uses a generated UUID"); s_dump($sink); // Then set the identity ourselves $identified = new ZMQSocket($context, ZMQ::SOCKET_REQ); $identified->setSockOpt(ZMQ::SOCKOPT_IDENTITY, "PEER2"); $identified->connect("inproc://example"); $identified->send("ROUTER socket uses REQ's socket identity"); s_dump($sink);
<?php /* * Custom routing Router to Papa (ROUTER to REP) * @author Ian Barber <ian(dot)barber(at)gmail(dot)com>a */ include "zhelpers.php"; // We will do this all in one thread to emphasize the sequence // of events... $context = new ZMQContext(); $client = new ZMQSocket($context, ZMQ::SOCKET_ROUTER); $client->bind("inproc://routing"); $worker = new ZMQSocket($context, ZMQ::SOCKET_REP); $worker->setSockOpt(ZMQ::SOCKOPT_IDENTITY, "A"); $worker->connect("inproc://routing"); // Send papa address, address stack, empty part, and request $client->send("A", ZMQ::MODE_SNDMORE); $client->send("address 3", ZMQ::MODE_SNDMORE); $client->send("address 2", ZMQ::MODE_SNDMORE); $client->send("address 1", ZMQ::MODE_SNDMORE); $client->send("", ZMQ::MODE_SNDMORE); $client->send("This is the workload"); // Worker should get just the workload s_dump($worker); // We don't play with envelopes in the worker $worker->send("This is the reply"); // Now dump what we got off the ROUTER socket... s_dump($client);