Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.cache
build
.vscode
.private.md
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ project(COMMUNICATION_TEACHING LANGUAGES C CXX)

set(EXPORT_COMPILE_COMMANDS ON)

include_directories(include)

find_package(Boost REQUIRED CONFIG COMPONENTS system)
find_package(OpenSSL REQUIRED)
find_package(Threads REQUIRED)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(
Utils
Expand Down
77 changes: 75 additions & 2 deletions src/Serial/boost_serial_console.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,84 @@
#include <atomic>
#include <boost/asio.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/serial_port.hpp>
#include <iostream>
#include <string>
#include <thread>

int main(int argc, char* argv[])
class SerialConsole
{
public:
SerialConsole(boost::asio::io_context& ioc, std::string port_name)
{
m_serial_port = std::make_shared<boost::asio::serial_port>(
boost::asio::serial_port(ioc, port_name));
}

~SerialConsole() { m_serial_port->close(); }

void start()
{
while (true)
{
std::cout << "$ ";
std::cin >> m_command;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
break;
}
if (m_command == "exit")
{
std::cout << "Exit..." << std::endl;
break;
}
if (m_command != "encrypt" && m_command != "decrypt" &&
m_command != "help")
{
std::cerr << "Wrong command usgae" << std::endl;
m_info.clear();
m_request = "help";
}
else if (m_command != "help")
{
std::cin >> m_info;
m_request = m_command + " " + m_info;
}
else
{
m_info.clear();
m_request = m_command;
}
/* backend parse the info to be encrypted or decryped started from
the index 8, which means you should add a space or any other
character to occupy this pos.*/
m_serial_port->write_some(boost::asio::buffer(m_request));
auto read_size =
m_serial_port->read_some(boost::asio::buffer(m_buffer));
std::string response = std::string(m_buffer.data(), read_size);
std::cout << response << std::endl;
}
}

private:
std::shared_ptr<boost::asio::serial_port> m_serial_port;
std::string m_command;
std::string m_info;
std::string m_request;
std::array<char, 1024> m_buffer;
};

int main()
{
std::string port_name = "/dev/ttyUSB1";

boost::asio::io_context io_context;
boost::asio::serial_port serial_port(io_context, port_name);

SerialConsole console = SerialConsole(io_context, port_name);
console.start();

return 0;
}
18 changes: 14 additions & 4 deletions src/Socket/boost_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
#include <boost/asio/read.hpp>
#include <iostream>

int main()
int main(int argc, char* argv[])
{
boost::asio::io_context io_context;
boost::asio::ip::tcp::socket socket(io_context);
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::connect(socket, resolver.resolve("localhost", "12345"));

std::string message = "time";
socket.write_some(boost::asio::buffer(message));
std::string command = argv[1];
std::string request;

if (command == "echo")
{
std::string message = argv[2];
request += " " + message;
}
socket.write_some(boost::asio::buffer(request));
std::string response;
response.resize(1024);
socket.read_some(boost::asio::buffer(response));
if (command != "exit")
socket.read_some(boost::asio::buffer(response));
else
response = "Session closed, client closed...";
std::cout << "Response: " << response << std::endl;

return 0;
Expand Down
31 changes: 30 additions & 1 deletion src/Socket/boost_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,30 @@ class Session : public std::enable_shared_from_this<Session>
// Modify Code here
read_buffer[length] = '\0';
std::cout << "Received: " << read_buffer << std::endl;
if (strcmp(read_buffer, "time") == 0)
std::string command = std::string(read_buffer, 4);
if (command == "time")
{
std::string time = get_current_time();
std::copy(time.begin(), time.end(), write_buffer);
do_write(time.size());
}
else if (command == "echo")
{
std::copy(&read_buffer[5], &read_buffer[length],
write_buffer);
do_write(length - 5);
}
else if (command == "exit")
{
this->close();
}
else if (command == "help")
{
std::string help_msg = show_usage();
std::copy(help_msg.begin(), help_msg.end(),
write_buffer);
do_write(help_msg.size());
}
else
{
std::copy(read_buffer, read_buffer + length,
Expand All @@ -53,6 +71,17 @@ class Session : public std::enable_shared_from_this<Session>
});
}

std::string show_usage()
{
std::stringstream ss;
ss << "Usage:\n";
ss << " help - Display this help message\n";
ss << " time - Return current time\n";
ss << " echo <your script> - Return the srcipt you sended\n";
ss << " exit - Close connection and exit\n";
return ss.str();
}

void do_write(std::size_t length)
{
auto self(shared_from_this());
Expand Down