Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 65 lines (56 sloc) 2.009 kb
31f00864 » Hoelzlwimmer Andreas
2011-03-25 Added C++ translations
1 //
2 // Simple Pirate queue
3 // This is identical to the LRU pattern, with no reliability mechanisms
4 // at all. It depends on the client for recovery. Runs forever.
5 //
6 // Andreas Hoelzlwimmer <andreas.hoelzlwimmer@fh-hagenberg.at
7 #include "zmsg.hpp"
8 #include <queue>
9809c940 » hintjens
2011-02-28 Pirate patterns added
9
31f00864 » Hoelzlwimmer Andreas
2011-03-25 Added C++ translations
10 #define MAX_WORKERS 100
9809c940 » hintjens
2011-02-28 Pirate patterns added
11
31f00864 » Hoelzlwimmer Andreas
2011-03-25 Added C++ translations
12 int main (void)
13 {
14 s_version_assert (2, 1);
9809c940 » hintjens
2011-02-28 Pirate patterns added
15
31f00864 » Hoelzlwimmer Andreas
2011-03-25 Added C++ translations
16 // Prepare our context and sockets
17 zmq::context_t context(1);
18 zmq::socket_t frontend (context, ZMQ_XREP);
19 zmq::socket_t backend (context, ZMQ_XREP);
20 frontend.bind("tcp://*:5555"); // For clients
21 backend.bind("tcp://*:5556"); // For workers
22
23 // Queue of available workers
24 std::queue<std::string> worker_queue;
25
26 while (1) {
27 zmq::pollitem_t items [] = {
28 { backend, 0, ZMQ_POLLIN, 0 },
29 { frontend, 0, ZMQ_POLLIN, 0 }
30 };
31 // Poll frontend only if we have available workers
32 if (worker_queue.size())
33 zmq::poll (items, 2, -1);
34 else
35 zmq::poll (items, 1, -1);
36
37 // Handle worker activity on backend
38 if (items [0].revents & ZMQ_POLLIN) {
39 zmsg zm(backend);
40 //zmsg_t *zmsg = zmsg_recv (backend);
41
42 // Use worker address for LRU routing
43 assert (worker_queue.size() < MAX_WORKERS);
44 worker_queue.push(zm.unwrap());
45
46 // Return reply to client if it's not a READY
47 if (strcmp (zm.address(), "READY") == 0)
48 zm.clear();
49 else
50 zm.send (frontend);
51 }
52 if (items [1].revents & ZMQ_POLLIN) {
53 // Now get next client request, route to next worker
54 zmsg zm(frontend);
55 // REQ socket in worker needs an envelope delimiter
56 zm.wrap(worker_queue.front().c_str(), "");
57 zm.send(backend);
58
59 // Dequeue and drop the next worker address
60 worker_queue.pop();
61 }
62 }
63 // We never exit the main loop
64 return 0;
65 }
Something went wrong with that request. Please try again.