forked from eBay/NuRaft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_start.cxx
65 lines (52 loc) · 2.35 KB
/
quick_start.cxx
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
/************************************************************************
Copyright 2017-2019 eBay Inc.
Author/Developer(s): Jung-Sang Ahn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#include "echo_state_machine.hxx"
#include "in_memory_state_mgr.hxx"
#include "libnuraft/nuraft.hxx"
#include <chrono>
#include <string>
#include <thread>
using namespace nuraft;
int main(int argc, char** argv) {
// Replace with your logger, state machine, and state manager.
ptr<logger> my_logger = nullptr;
ptr<state_machine> my_state_machine = cs_new<echo_state_machine>();
ptr<state_mgr> my_state_manager =
cs_new<inmem_state_mgr>(1, "localhost:12345");
asio_service::options asio_opt; // your Asio options
raft_params params; // your Raft parameters
// Initialize Raft server listening on port 12345.
// It will organize a single-node Raft cluster.
raft_launcher launcher;
int port_number = 12345;
ptr<raft_server> server = launcher.init(my_state_machine,
my_state_manager,
my_logger,
port_number,
asio_opt,
params);
// Need to wait for initialization.
while (!server->is_initialized()) {
std::this_thread::sleep_for( std::chrono::milliseconds(100) );
}
// Append a log, containing a string `hello world` and its 4-byte length.
std::string msg = "hello world";
ptr<buffer> log = buffer::alloc(sizeof(int) + msg.size());
buffer_serializer bs_log(log);
bs_log.put_str(msg);
server->append_entries({log});
// Shutdown.
launcher.shutdown();
return 0;
}