Skip to content

Commit

Permalink
MAJOR overhaul and re-write. Spun the lower-level fifo handling code …
Browse files Browse the repository at this point in the history
…off into a class (Port). The Server and Client base Agent class holds seperate in and out maps of unique_ptr to Ports. Get and Send calls to the Agent make corresponding Port Get and Send calls via it's interface.
  • Loading branch information
Sebastien Sikora committed Dec 2, 2021
1 parent b3192c4 commit 6d04cfe
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 41 deletions.
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ int main (void) {
std::string outbound_message = "Message number " + std::to_string(i) + " from server.";
sts.SendMessage(outbound_message);
}

unsigned long timeout_seconds = 5;
unsigned long start_time = time(0);

Expand All @@ -161,15 +161,13 @@ int main (void) {
}
usleep(1000);
}
if (sts.GetErrorCode().err_no != 0) {
std::cerr << sts.GetErrorCode().err_no << " " << sts.GetErrorCode().function << std::endl;
}

std::cerr << "On termination error code = " << sts.GetErrorCode().err_no << " Error detail = " << sts.GetErrorCode().detail << std::endl;

} else {
if (sts.GetErrorCode().err_no != 0) {
std::cerr << sts.GetErrorCode().err_no << " " << sts.GetErrorCode().function << std::endl;
}
sleep(5);

std::cerr << "On termination error code = " << sts.GetErrorCode().err_no << " Error detail = " << sts.GetErrorCode().detail << std::endl;

}
return 0;
}
Expand All @@ -194,27 +192,27 @@ int main(int argc, char *argv[]) {
SatTerm_Client stc("test_client", argc, argv);
if (stc.IsConnected()) {
while (stc.GetErrorCode().err_no == 0) {
std::string inbound_message = stc.GetMessage();
if (inbound_message != "") {
std::cout << inbound_message << std::endl;
if (inbound_message != stc.GetStopMessage()) {
stc.SendMessage(inbound_message);
} else {
break;
}
}
usleep(1000);
}
if (stc.GetErrorCode().err_no != 0) {
std::cerr << stc.GetErrorCode().err_no << " " << stc.GetErrorCode().function << std::endl;
}
sleep(5);
std::cerr << "On termination error code = " << stc.GetErrorCode().err_no << " Error detail = " << stc.GetErrorCode().detail << std::endl;
sleep(5); // Delay to read the message before terminal emulator window closes.
} else {
if (stc.GetErrorCode().err_no != 0) {
std::cerr << stc.GetErrorCode().err_no << " " << stc.GetErrorCode().function << std::endl;
}
sleep(5);
std::cerr << "On termination error code = " << stc.GetErrorCode().err_no << " Error detail = " << stc.GetErrorCode().detail << std::endl;
sleep(5); // Delay to read the message before terminal emulator window closes.
}
return 0;
}
Expand All @@ -225,17 +223,17 @@ Compile `server_demo.cpp` and `client_demo.cpp`, then execute `server_demo` from
<br />

```
user@home:~/Documents/cpp_projects/satellite_terminal$ g++ -Wall -g -O3 -I src/ src/satterm_client.cpp src/satterm_server.cpp src/satterm_component.cpp demos/server_demo.cpp -o server_demo
user@home:~/Documents/cpp_projects/satellite_terminal$ g++ -Wall -g -O3 -I src/ src/satterm_client.cpp src/satterm_server.cpp src/satterm_component.cpp demos/client_demo.cpp -o client_demo
user@home:~/Documents/cpp_projects/satellite_terminal$ g++ -Wall -g -O3 -I src/ src/satterm_agent.cpp src/satterm_client.cpp src/satterm_server.cpp src/satterm_port.cpp demos/server_demo.cpp -o server_demo
user@home:~/Documents/cpp_projects/satellite_terminal$ g++ -Wall -g -O3 -I src/ src/satterm_agent.cpp src/satterm_client.cpp src/satterm_server.cpp src/satterm_port.cpp demos/client_demo.cpp -o client_demo
user@home:~/Documents/cpp_projects/satellite_terminal$ ./server_demo
Fifo working path is /home/user/Documents/cpp_projects/satellite_terminal/
Server working path is /home/user/Documents/cpp_projects/satellite_terminal/
Client process started.
Client process attempting to execute via terminal emulator '-e':
./client_demo client_args /home/user/Documents/cpp_projects/satellite_terminal/ 0 3 q 1 1 test_server_fifo_cs_0 test_server_fifo_sc_0
./client_demo client_args /home/user/Documents/cpp_projects/satellite_terminal/ 3 q 1 1 server_rx server_tx
Trying /usr/bin/x-terminal-emulator
Server test_server opened fifo /home/user/Documents/cpp_projects/satellite_terminal/test_server_fifo_cs_0 for reading on descriptor 3
Server test_server opened fifo /home/user/Documents/cpp_projects/satellite_terminal/test_server_fifo_sc_0 for writing on descriptor 4
Server test_server initialised successfully.
In Port server_rx opened fifo /home/user/Documents/cpp_projects/satellite_terminal/server_rx for reading on descriptor 3
Out Port server_tx opened fifo /home/user/Documents/cpp_projects/satellite_terminal/server_tx for writing on descriptor 4
Server test_server successfully initialised connection.
Message "Message number 0 from server." returned by client.
Message "Message number 1 from server." returned by client.
Message "Message number 2 from server." returned by client.
Expand All @@ -246,8 +244,9 @@ Message "Message number 6 from server." returned by client.
Message "Message number 7 from server." returned by client.
Message "Message number 8 from server." returned by client.
Message "Message number 9 from server." returned by client.
On termination error code = 0 Error detail =
Waiting for client process to terminate...
EOF on read() to fifo index 0 suggests counterpart terminated.
EOF error on GetMessage() for In Port server_rx suggests counterpart terminated.
user@home:~/Documents/cpp_projects/satellite_terminal$
```
<br />
Expand All @@ -256,10 +255,10 @@ Output in child terminal emulator instance:
<br />

```
Fifo working path is /home/user/Documents/cpp_projects/satellite_terminal/
Client test_client opened fifo /home/user/Documents/cpp_projects/satellite_terminal/test_server_fifo_cs_0 for writing on descriptor 3
Client test_client opened fifo /home/user/Documents/cpp_projects/satellite_terminal/test_server_fifo_sc_0 for reading on descriptor 4
Client test_client initialised successfully.
Client working path is /home/user/Documents/cpp_projects/satellite_terminal/
Out Port server_rx opened fifo /home/user/Documents/cpp_projects/satellite_terminal/server_rx for writing on descriptor 3
In Port server_tx opened fifo /home/user/Documents/cpp_projects/satellite_terminal/server_tx for reading on descriptor 4
Client test_client successfully initialised connection.
Message number 0 from server.
Message number 1 from server.
Message number 2 from server.
Expand All @@ -271,6 +270,7 @@ Message number 7 from server.
Message number 8 from server.
Message number 9 from server.
q
On termination error code = 0 Error detail =
```
<br />

Expand Down
5 changes: 0 additions & 5 deletions demos/client_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ int main(int argc, char *argv[]) {

if (inbound_message != "") {
std::cout << inbound_message << std::endl;

if (inbound_message != stc.GetStopMessage()) {
stc.SendMessage(inbound_message);
} else {
Expand All @@ -27,15 +26,11 @@ int main(int argc, char *argv[]) {
}
usleep(1000);
}

std::cerr << "On termination error code = " << stc.GetErrorCode().err_no << " Error detail = " << stc.GetErrorCode().detail << std::endl;
sleep(5); // Delay to read the message before terminal emulator window closes.

} else {

std::cerr << "On termination error code = " << stc.GetErrorCode().err_no << " Error detail = " << stc.GetErrorCode().detail << std::endl;
sleep(5); // Delay to read the message before terminal emulator window closes.

}
return 0;
}
12 changes: 12 additions & 0 deletions src/satellite_terminal.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// -----------------------------------------------------------------------------------------------------
// satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate
// terminal emulator instances.
// -----------------------------------------------------------------------------------------------------
// seb.nf.sikora@protonmail.com
//
// Copyright © 2021 Dr Seb N.F. Sikora.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
// -----------------------------------------------------------------------------------------------------

#include <string> // std::string.
#include <vector> // std::vector.
#include <map>
Expand Down
12 changes: 12 additions & 0 deletions src/satterm_agent.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// -----------------------------------------------------------------------------------------------------
// satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate
// terminal emulator instances.
// -----------------------------------------------------------------------------------------------------
// seb.nf.sikora@protonmail.com
//
// Copyright © 2021 Dr Seb N.F. Sikora.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
// -----------------------------------------------------------------------------------------------------

#include <iostream>
#include <fstream> // std::ifstream.
#include <stdexcept>
Expand Down
12 changes: 12 additions & 0 deletions src/satterm_client.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// -----------------------------------------------------------------------------------------------------
// satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate
// terminal emulator instances.
// -----------------------------------------------------------------------------------------------------
// seb.nf.sikora@protonmail.com
//
// Copyright © 2021 Dr Seb N.F. Sikora.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
// -----------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <map>
Expand Down
21 changes: 18 additions & 3 deletions src/satterm_port.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// -----------------------------------------------------------------------------------------------------
// satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate
// terminal emulator instances.
// -----------------------------------------------------------------------------------------------------
// seb.nf.sikora@protonmail.com
//
// Copyright © 2021 Dr Seb N.F. Sikora.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
// -----------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <ctime> // time().

#include <stdio.h> // perror().
#include <fcntl.h> // open() and O_RDONLY, O_WRONLY, etc.
Expand Down Expand Up @@ -189,9 +202,11 @@ std::string Port::SendMessage(std::string const& message, unsigned long timeout_

size_t bytes_sent = SendBytes(working_message.c_str(), working_message_length, timeout_seconds);

if (bytes_sent == working_message_length) {
if (bytes_sent == working_message_length) { // Sent whole working_message including m_end_char.
return "";
} else {
} else if (bytes_sent == (working_message_length - 1)) { // Sent whole message but did not send m_end_char.
return "";
} else { // Sent part of message.
return message.substr(bytes_sent, std::string::npos);
}
}
Expand Down Expand Up @@ -299,7 +314,7 @@ std::string Port::GetMessage(bool capture_end_char, unsigned long timeout_second
switch (errno) { // See under errors here - https://pubs.opengroup.org/onlinepubs/009604599/functions/read.html
case EAGAIN: // Non-blocking read on empty fifo with connected writer will return -1 with error EAGAIN,
finished = ((time(0) - start_time) > timeout_seconds); // so we continue to poll unless timeout.
if (finished && (timeout_seconds > 0)) {
if (finished && (timeout_seconds > 0)) { // Only set m_error_code to EAGAIN if we have been waiting on a timeout.
m_error_code = {errno, "GetMessage()_tx_conn_timeout"};
}
break;
Expand Down
14 changes: 13 additions & 1 deletion src/satterm_port.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// -----------------------------------------------------------------------------------------------------
// satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate
// terminal emulator instances.
// -----------------------------------------------------------------------------------------------------
// seb.nf.sikora@protonmail.com
//
// Copyright © 2021 Dr Seb N.F. Sikora.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
// -----------------------------------------------------------------------------------------------------

#include <string> // std::string.
#include <vector> // std::vector.

Expand Down Expand Up @@ -59,7 +71,7 @@ class Port {

std::string m_identifier = "";
std::string m_working_path = "";
int m_fifo_descriptor = 0;
char m_end_char = 0;
int m_fifo_descriptor = 0;
std::string m_current_message = "";
};
20 changes: 16 additions & 4 deletions src/satterm_server.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// -----------------------------------------------------------------------------------------------------
// satellite_terminal - Easily spawn and communicate bidirectionally with client processes in separate
// terminal emulator instances.
// -----------------------------------------------------------------------------------------------------
// seb.nf.sikora@protonmail.com
//
// Copyright © 2021 Dr Seb N.F. Sikora.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
// -----------------------------------------------------------------------------------------------------

#include <iostream>
#include <fstream> // std::ifstream.
#include <stdexcept>
Expand All @@ -22,10 +34,10 @@ SatTerm_Server::SatTerm_Server(std::string const& identifier, std::string const&
m_stop_message = stop_message;

if (out_port_identifiers.size() == 0) {
out_port_identifiers.push_back("tx");
out_port_identifiers.push_back("server_tx");
}
if (in_port_identifiers.size() == 0) {
in_port_identifiers.push_back("rx");
in_port_identifiers.push_back("server_rx");
}
m_default_port.tx = out_port_identifiers[0];
m_default_port.rx = in_port_identifiers[0];
Expand All @@ -37,10 +49,10 @@ SatTerm_Server::SatTerm_Server(std::string const& identifier, std::string const&
m_working_path = GetWorkingPath();

if (m_working_path != "") {
bool success = CreatePorts(true, false, m_working_path, out_port_identifiers, m_display_messages, m_end_char, m_out_ports);

bool success = CreatePorts(true, true, m_working_path, in_port_identifiers, m_display_messages, m_end_char, m_in_ports);
if (success) {
success = CreatePorts(true, true, m_working_path, in_port_identifiers, m_display_messages, m_end_char, m_in_ports);
success = CreatePorts(true, false, m_working_path, out_port_identifiers, m_display_messages, m_end_char, m_out_ports);
}

if (success) {
Expand Down

0 comments on commit 6d04cfe

Please sign in to comment.