ZIO
Python and C++ interface to ZeroMQ and Zyre
test_actor.cpp
Go to the documentation of this file.
1 
5 #include "zio/actor.hpp"
6 #include "zio/logging.hpp"
7 #include "zio/main.hpp"
8 
9 #include <thread>
10 #include <sstream>
11 
12 #include <chrono>
13 
14 
15 static
16 void myactor(zio::socket_t& link, std::string greeting, bool fast_exit)
17 {
18  zio::debug("myactor: {}", greeting);
19 
20  // ready
21  link.send(zio::message_t{}, zio::send_flags::none);
22 
23  zio::message_t msg;
24  zio::debug("myactor: wait for app protocol message");
25  auto res1 = link.recv(msg);
26  assert(res1);
27  zio::debug("myactor: got protocol message size {}", msg.size());
28  assert(msg.size() == 2);
29  assert(msg.to_string() == "hi");
30 
31  if (fast_exit) {
32  zio::debug("myactor: exit early");
33  return;
34  }
35 
36  zio::debug("myactor: simulating 1 second of work, try to Ctrl-c me");
37  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
38 
39 
40  zio::debug("myactor: waiting for termination message");
41  zio::message_t rmsg;
42  auto res2 = link.recv(rmsg);
43  assert(res2);
44  assert(rmsg.to_string() == "$TERM");
45  zio::debug("myactor: exiting");
46 }
47 
48 
49 int main()
50 {
51  zio::init_all();
52 
54 
55  zio::debug("in main, test 1");
56  {
57  zio::zactor_t actor(ctx, myactor, "hello world", false);
58  zio::debug("1 in main, sleep");
59  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
60  zio::debug("1 in main, send protocol message");
61  actor.link().send(zio::message_t{"hi",2}, zio::send_flags::dontwait);
62  zio::debug("1 in main, leaving context");
63  }
64  zio::debug("in main, test 2");
65  {
66  zio::zactor_t actor(ctx, myactor, "hello world", false);
67  zio::debug("2 in main, no sleep, send protocol actor");
68  actor.link().send(zio::message_t{"hi",2}, zio::send_flags::dontwait);
69  zio::debug("2 in main, leaving context");
70  }
71  zio::debug("in main, test 3");
72  {
73  zio::zactor_t actor(ctx, myactor, "fast exit", true);
74  zio::debug("3 in main, sleep");
75  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
76  zio::debug("3 in main, send protocol message");
77  actor.link().send(zio::message_t{"hi",2}, zio::send_flags::dontwait);
78  zio::debug("3 in main, leaving context");
79  }
80  zio::debug("in main, test 4");
81  {
82  zio::zactor_t actor(ctx, myactor, "fast exit", true);
83  zio::debug("4 in main, no sleep, send protocol message");
84  actor.link().send(zio::message_t{"hi",2}, zio::send_flags::dontwait);
85  zio::debug("4 in main, leaving context");
86  }
87 
88  zio::debug("in main, exiting");
89 
90 }
size_t send(const void *buf_, size_t len_, int flags_=0)
Definition: zmq.hpp:1345
std::string to_string() const
Definition: zmq.hpp:563
int main()
Definition: test_actor.cpp:49
socket_ref link()
Definition: actor.hpp:99
void init_all()
Definition: main.cpp:22
size_t size() const ZMQ_NOTHROW
Definition: zmq.hpp:488