-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cpp
68 lines (61 loc) · 1.64 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <thread>
#include "td/td/telegram/td_json_client.h"
#include "td/td/telegram/Log.h"
void* client;
int should_stop = 0;
void proc_thread_input();
void proc_thread_output();
void trigger_cli_event(std::string);
void set_verbosity(char const *);
int main(int argc, char const *argv[])
{
set_verbosity(argv[1]);
client = td_json_client_create();
trigger_cli_event("client_created");
std::thread thread_input(proc_thread_input);
std::thread thread_output(proc_thread_output);
thread_input.join();
should_stop = 1;
thread_output.join();
trigger_cli_event("exited");
return 0;
}
void proc_thread_input() {
std::string input;
while (std::cin) {
getline(std::cin, input);
if (input == "exit") {
break;
}
if (input.empty()) {
continue;
}
if (input.rfind("verbose ", 0) == 0) {
// verbose<space>
// 01234567
set_verbosity(input.substr(7).c_str());
continue;
}
td_json_client_send(client, input.c_str());
}
}
void proc_thread_output() {
const char* output;
while (should_stop == 0) {
output = td_json_client_receive(client, 1);
if (output != NULL) {
std::cout << output << std::endl;
}
}
}
void trigger_cli_event(std::string event_name) {
std::cout << "{\"@cli\":{\"event\":\"" << event_name << "\"}}" << std::endl;
}
void set_verbosity(char const *argv) {
if (argv == NULL) return;
auto level = atoi(argv);
td::Log::set_verbosity_level(level);
trigger_cli_event("verbosity_set");
}