-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_worker.cpp
More file actions
66 lines (55 loc) · 2 KB
/
Copy pathbasic_worker.cpp
File metadata and controls
66 lines (55 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @file basic_worker.cpp
* @brief Minimal end-to-end example using the built-in agent::Worker.
*
* This is the introductory example referenced in the top-level README. It
* creates the library's built-in agent::Worker, starts it on a couple of
* threads, serializes a small "image" message with FlatBuffers, and submits a
* handful of messages for the worker to process.
*
* The agent::Worker::ProcessMessage implementation simply logs the message it
* received; see custom_worker.cpp for an example that does real work.
*/
#include "agent/Worker.hpp"
#include "agent/agent_config.hpp"
#include "Message_generated.h"
#include <chrono>
#include <thread>
#include <vector>
#include <cstdint>
#include <flatbuffers/flatbuffers.h>
int main()
{
// Create the built-in worker, give it an ID and a name.
agent::Worker worker(0, "BasicWorker");
// Run the worker with two threads.
worker.Run(2);
// Give the threads a moment to spin up.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Serialize a small 3x3 "image" message using FlatBuffers.
flatbuffers::FlatBufferBuilder builder(AGENT_FB_BUFFER_SIZE);
auto pixels = builder.CreateVector(
std::vector<std::int8_t>{
0, 0, 1,
2, 2, 1,
4, 0, 1
}
);
auto message = agent::Messages::CreateMessage(builder, 0, 3, 3, pixels);
builder.Finish(message);
auto buffer = builder.GetBufferPointer();
auto size = builder.GetSize();
// Simulate receiving asynchronous messages: send three messages per
// iteration, four times, separated by about half a second.
for (int i = 0; i < 4; ++i)
{
worker.AddMessage(buffer, size);
worker.AddMessage(buffer, size);
worker.AddMessage(buffer, size);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
// Tell the worker threads to quit and let them drain.
worker.SetQuit();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
return 0;
}