-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontcheck.cxx
162 lines (127 loc) · 3.55 KB
/
contcheck.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
*
*/
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include "zmq.hpp"
#define COMMAND_PORT "tcp://localhost:5560"
const char *g_com_endpoint = "tcp://*:5560";
enum RunState {
SM_INIT,
SM_IDLE,
SM_RUNNING,
SM_END,
SM_EXIT,
};
class TestEb {
private:
int c_state = SM_INIT;
public:
void set_state(int state) {c_state = state ; return;};
int get_state() {return c_state;};
void monitor() {std::cout << "mon" << std::endl; return;};
protected:
};
int tasks = 0;
void init_sequence(TestEb *eb, int tasks)
{
std::cout << "init_sequence" << std::endl;
return;
}
void print_state(int state)
{
std::cout << "State: " << state << std::endl;
return;
}
int command_loop(zmq::context_t &context, std::string &com_url)
{
zmq::socket_t socket(context, ZMQ_PUSH);
socket.connect(com_url);
while (true) {
std::string oneline;
std::cout << "> ";
std::cin >> oneline;
#if 0
if (oneline == "run") eb->set_state(SM_RUNNING);
if (oneline == "idle") eb->set_state(SM_IDLE);
if (oneline == "stop") eb->set_state(SM_IDLE);
if (oneline == "init") init_sequence(eb, tasks);
if (oneline == "end") eb->set_state(SM_END);
if ((oneline == "state")
|| (oneline == "status")
|| (oneline == "stat")) {
print_state(eb->get_state()) ;
std::cout << std::endl;
}
if (oneline == "mon") eb->monitor();
#endif
if (oneline == "exit") break;
if (oneline == "quit") break;
if (oneline == "q") break;
std::cout << "size: " << oneline.size() << std::endl;
zmq::message_t message(oneline.size());
memcpy(reinterpret_cast<void *>(message.data()),
oneline.c_str(), oneline.size());
try {
socket.send(message);
} catch (zmq::error_t &e) {
std::cerr << "#E zmq send err. " << e.what() << std::endl;
return -1;
}
}
std::cout << "# break loop" << std::endl;
socket.close();
return 0;
}
int remote_command_loop(TestEb *eb, zmq::context_t &context)
{
zmq::socket_t comport(context, ZMQ_PULL);
comport.bind(g_com_endpoint);
while (true) {
zmq::message_t message;
bool rc;
try {
//rc = comport.recv(&message, ZMQ_NOBLOCK);
rc = comport.recv(&message);
} catch (zmq::error_t &e) {
std::cerr << "#E leb command loop recv err." << e.what() << std::endl;
continue;
}
if (! rc) {
std::cout << "." << std::flush;
usleep(100);
continue;
}
char word[message.size() + 1];
memcpy(word, reinterpret_cast<char *>(message.data()), message.size());
word[message.size()] = '\0';
std::string oneline(word);
std::cout << "#D " << message.size() << " " << oneline << std::endl;
if (oneline == "run") eb->set_state(SM_RUNNING);
if (oneline == "idle") eb->set_state(SM_IDLE);
if (oneline == "stop") eb->set_state(SM_IDLE);
if (oneline == "init") init_sequence(eb, tasks);
if (oneline == "end") eb->set_state(SM_END);
if ((oneline == "state")
|| (oneline == "status")
|| (oneline == "stat")) {
print_state(eb->get_state()) ;
std::cout << std::endl;
}
if (oneline == "mon") eb->monitor();
if (oneline == "exit") break;
if (oneline == "quit") break;
if (oneline == "q") break;
}
return 0;
}
int main(int argc, char* argv[])
{
std::string com_url(COMMAND_PORT);
TestEb *eb = new TestEb();
zmq::context_t context(1);
remote_command_loop(eb, context);
return 0;
}