-
Notifications
You must be signed in to change notification settings - Fork 0
/
satterm_client.cpp
133 lines (109 loc) · 5.34 KB
/
satterm_client.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
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
/*
satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate terminal emulator instances.
Copyright © 2021 Dr Seb N.F. Sikora.
satellite_terminal is distributed under the terms of the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
seb.nf.sikora@protonmail.com
*/
#include <string> // std::string.
#include <vector> // std::vector.
#include <iostream> // std::cout, std::cerr, std::endl.
#include <signal.h> // SIGPIPE, SIG_IGN.
#include <unistd.h> // write(), read(), close().
#include "satellite_terminal.h"
// Class constructor and member function definitions for derived Client class.
// Construct a client by parsing argv from the stipulated argument index (inclusive).
SatTerm_Client::SatTerm_Client(std::string const& identifier, int argc, char* argv[], bool display_messages, char end_char) {
m_identifier = identifier;
m_display_messages = display_messages;
signal(SIGPIPE, SIG_IGN); // If the reader at the other end of the pipe closes prematurely, when we try and write() to the pipe
// a SIGPIPE signal is generated and this process terminates.
// We call signal() here to prevent the signal from being raised as-per https://stackoverflow.com/a/9036323
// After each write() call we need to check the return value and if -1 check for the EPIPE error code
// before/if writing again.
m_component_type = "Client";
m_end_char = end_char;
size_t argv_start_index = ParseVarargs(argc, argv);
if (argv_start_index != 0) {
m_working_path = std::string(argv[argv_start_index]);
m_stop_fifo_index = std::stoi(std::string(argv[argv_start_index + 1]));
m_stop_message = std::string(argv[argv_start_index + 2]);
size_t tx_fifo_count = std::stoi(std::string(argv[argv_start_index + 3]));
size_t rx_fifo_count = std::stoi(std::string(argv[argv_start_index + 4]));
m_tx_fifo_paths = ParseFifoPaths(argv_start_index + 5, tx_fifo_count, argv);
m_rx_fifo_paths = ParseFifoPaths(argv_start_index + 5 + tx_fifo_count, rx_fifo_count, argv);
if (m_display_messages) {
std::string message = "Fifo working path is " + m_working_path;
std::cerr << message << std::endl;
}
unsigned long timeout_seconds = 5;
bool success = OpenFifos(timeout_seconds);
if (success) {
if (m_display_messages) {
std::string message = "Client " + m_identifier + " initialised successfully.";
std::cerr << message << std::endl;
}
m_connected = true;
} else {
if (m_display_messages) {
std::string message = "Client " + m_identifier + " unable to intialise connection.";
std::cerr << message << std::endl;
}
m_connected = false;
}
} else {
m_error_code = {-1, "ParseVarargs()_no_client_args"};
if (m_display_messages) {
std::string error_message = "ParseVarargs() found no client configuration arguments.";
std::cerr << error_message << std::endl;
}
m_connected = false;
}
}
SatTerm_Client::~SatTerm_Client() {
if (IsConnected()) {
// Some terminal emulators perform more than one fork() while starting the binary, and as such it becomes very difficult to obtain the
// pid of the (grand)child process and waitpid() for the client to finish. Instead the server will loop on GetMessage() on the zeroth fifo
// until GetErrorCode() returns {-1, "read()_EOF"}. We trigger that here by closing the write end of that fifo
close(m_tx_fifo_descriptors[0]);
}
}
size_t SatTerm_Client::ParseVarargs(int argc, char* argv[]) {
size_t argument_index = 1;
bool failed = false;
while (std::string(argv[argument_index]) != "client_args") {
argument_index ++;
if (argument_index == (size_t)(argc)) {
failed = true;
break;
}
}
if (failed) {
return 0;
} else {
return argument_index + 1;
}
}
std::vector<std::string> SatTerm_Client::ParseFifoPaths(size_t argv_start_index, size_t argv_count, char* argv[]) {
std::vector<std::string> paths_container = {};
for (size_t i = argv_start_index; i < argv_start_index + argv_count; i ++) {
paths_container.emplace_back(std::string(argv[i]));
}
return paths_container;
}
bool SatTerm_Client::OpenFifos(unsigned long timeout_seconds) {
bool success = false;
success = OpenTxFifos(timeout_seconds);
if (success) {
success = OpenRxFifos(timeout_seconds);
}
return success;
}