-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
97 lines (87 loc) · 2.87 KB
/
server.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
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
/**
*** Author: R-CO
*** E-mail: [email protected]
*** Date: 2021-01-25
**/
// C++ standard library
#include <cstdlib>
#include <cstring>
#include <exception>
using std::exception;
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
// R-CO's library
#include "message_buffer_define.hpp"
#include "system_message_queue.hpp"
int main(int argc, char* argv[]) {
auto key_receive = 123456;
auto key_send = key_receive + 1;
if (argc >= 2) {
try {
key_receive = std::stoi(argv[1]);
key_send = key_receive + 1;
} catch (const exception& e) {
cerr << "Convert argv[1] to int failed.\n Exception: " << e.what()
<< endl;
return EXIT_FAILURE;
}
}
rco::SystemMessageQueue smq_receive;
if (!smq_receive.bindQueueViaKey(key_receive)) {
// message queue is not exist, try to create it with key
if (smq_receive.getErrorNo() == EACCES) {
cerr << "Bind queue with key failed." << endl;
cerr << "Errno = [" << smq_receive.getErrorNo() << "]" << endl;
return EXIT_FAILURE;
}
if (!smq_receive.createQueue(static_cast<key_t>(key_receive))) {
cerr << "Can not create queue with key." << endl;
cerr << "Errno = [" << smq_receive.getErrorNo() << "]" << endl;
return EXIT_FAILURE;
}
}
rco::SystemMessageQueue smq_send;
if (!smq_send.bindQueueViaKey(key_send)) {
// message queue is not exist, try to create it with key
if (smq_send.getErrorNo() == EACCES) {
cerr << "Bind queue with (key+1) failed." << endl;
cerr << "Errno = [" << smq_send.getErrorNo() << "]" << endl;
return EXIT_FAILURE;
}
if (!smq_send.createQueue(static_cast<key_t>(key_send))) {
cerr << "Can not create queue with (key+1)." << endl;
cerr << "Errno = [" << smq_send.getErrorNo() << "]" << endl;
return EXIT_FAILURE;
}
}
while (true) {
cout << "Wait for receiving message from queue..." << endl;
MessageBuffer receive_data;
receive_data.mtype = 1;
if (smq_receive.receiveFromQueue(&receive_data, sizeof(receive_data.mtext),
0, 0) == static_cast<ssize_t>(-1)) {
cerr << "Receive message from queue failed." << endl;
cerr << "Errno = [" << smq_receive.getErrorNo() << "]" << endl;
continue;
}
cout << "Received message:" << endl;
cout << receive_data.mtext << endl;
auto current_time = time(nullptr);
string timestamp = ctime(¤t_time);
const auto msgsz = timestamp.size() + 1;
MessageBuffer send_buffer;
send_buffer.mtype = 1;
memcpy(send_buffer.mtext, timestamp.c_str(), msgsz);
if (!smq_send.sendToQueue(&send_buffer, msgsz, 0)) {
cerr << "Send message to queue failed." << endl;
cerr << "Errno = [" << smq_send.getErrorNo() << "]" << endl;
continue;
}
}
return EXIT_SUCCESS;
}