Skip to content

Commit

Permalink
Merge pull request #4 from janekbaraniewski/basic-functionality
Browse files Browse the repository at this point in the history
Basic functionality
  • Loading branch information
janekbaraniewski authored Apr 29, 2024
2 parents dbbde0d + 38a7db9 commit dca98bf
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 234 deletions.
38 changes: 0 additions & 38 deletions .github/workflows/docker.yml

This file was deleted.

20 changes: 7 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,15 @@ jobs:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_ARCHITECTURE_ID=${{ matrix.config.arch }}
cmake --build build --config Release
- name: Rename binaries
- name: Rename binary
run: |
mv build/serial_client build/serial-client-${{ matrix.config.os }}-${{ matrix.config.arch }}
mv build/serial_server build/serial-server-${{ matrix.config.os }}-${{ matrix.config.arch }}
mv build/ser2net2ser build/ser2net2ser-${{ github.ref }}-${{ matrix.config.os }}-${{ matrix.config.arch }}
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: artifacts-${{ matrix.config.os }}-${{ matrix.config.arch }}
path: |
build/serial-client-${{ matrix.config.os }}-${{ matrix.config.arch }}
build/serial-server-${{ matrix.config.os }}-${{ matrix.config.arch }}
name: ser2net2ser-${{ matrix.config.os }}-${{ matrix.config.arch }}
path: build/ser2net2ser-${{ github.ref }}-${{ matrix.config.os }}-${{ matrix.config.arch }}

create-and-upload-release:
needs: build-and-release
Expand Down Expand Up @@ -110,9 +107,6 @@ jobs:
container-image:
runs-on: ubuntu-latest
strategy:
matrix:
component: [server, client]
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -133,16 +127,16 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build container image
run: make build-${{ matrix.component }}-image
run: make build-images
env:
COMMIT_HASH: ${{ env.COMMIT_HASH }}

- name: Tag container image
run: make tag-${{ matrix.component }}-image
run: make tag-images
env:
COMMIT_HASH: ${{ env.COMMIT_HASH }}

- name: Push container images
run: make push-${{ matrix.component }}-images
run: make push-images
env:
COMMIT_HASH: ${{ env.COMMIT_HASH }}
18 changes: 7 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
matrix:
config:
# Define combinations of operating systems and architectures
- {os: ubuntu-latest, arch: x64, server-artifact: serial_server_linux_x64, client-artifact: serial_client_linux_x64}
- {os: ubuntu-latest, arch: arm64, server-artifact: serial_server_linux_arm64, client-artifact: serial_client_linux_arm64}
# - {os: windows-latest, arch: x64, server-artifact: serial_server_windows_x64.exe, client-artifact: serial_client_windows_x64.exe}
# - {os: windows-latest, arch: arm64, server-artifact: serial_server_windows_arm64.exe, client-artifact: serial_client_windows_arm64.exe}
- {os: macos-latest, arch: x64, server-artifact: serial_server_mac_x64, client-artifact: serial_client_mac_x64}
- {os: macos-latest, arch: arm64, server-artifact: serial_server_mac_arm64, client-artifact: serial_client_mac_arm64}
- {os: ubuntu-latest, arch: x64}
- {os: ubuntu-latest, arch: arm64}
# - {os: windows-latest, arch: x64}
# - {os: windows-latest, arch: arm64}
- {os: macos-latest, arch: x64}
- {os: macos-latest, arch: arm64}

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -54,10 +54,6 @@ jobs:
container-image:
runs-on: ubuntu-latest
strategy:
matrix:
component: [server, client]

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -73,4 +69,4 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build container image
run: make build-${{ matrix.component }}-image
run: make build-images
23 changes: 6 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ target_link_libraries(logging PUBLIC
Boost::date_time
)

add_library(serial_server_lib src/logging.cpp src/SerialServer.cpp src/VirtualSerialPort.cpp)
target_include_directories(serial_server_lib PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(serial_server_lib PRIVATE
add_library(core_lib src/logging.cpp src/SerialServer.cpp src/SerialClient.cpp src/VirtualSerialPort.cpp)
target_include_directories(core_lib PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(core_lib PRIVATE
Boost::system
Boost::program_options
Boost::log
Expand All @@ -43,20 +43,9 @@ target_link_libraries(serial_server_lib PRIVATE
pthread
)

add_executable(serial_server src/SerialServer.cpp)
target_link_libraries(serial_server PRIVATE
serial_server_lib
logging
Boost::system
Boost::program_options
Boost::log
Boost::log_setup
Boost::date_time
)

add_executable(serial_client src/SerialClient.cpp)
target_link_libraries(serial_client PRIVATE
serial_server_lib
add_executable(ser2net2ser src/main.cpp)
target_link_libraries(ser2net2ser PRIVATE
core_lib
logging
Boost::system
Boost::program_options
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.server → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RUN apt-get update && apt-get install -y \
rm -rf /var/lib/apt/lists/*

# Copy binaries from the builder stage
COPY --from=builder /app/serial_server /app/serial_server
COPY --from=builder /app/ser2net2ser /usr/local/bin/ser2net2ser

# Command to run the application
CMD ["./serial_server"]
CMD ["ser2net2ser"]
38 changes: 0 additions & 38 deletions Dockerfile.client

This file was deleted.

27 changes: 4 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,14 @@ test: build
@cd build && ctest

build-images: ## Build container images
build-images: build-server-image build-client-image

build-server-image:
@docker build . -f Dockerfile.server -t $(REPO)-server:$(COMMIT_HASH)

build-client-image:
@docker build . -f Dockerfile.client -t $(REPO)-client:$(COMMIT_HASH)
@docker build . -f Dockerfile -t $(REPO):$(COMMIT_HASH)

tag-images: ## Tag container images
tag-images: tag-server-image tag-client-image

tag-server-image:
@docker tag $(REPO)-server:$(COMMIT_HASH) $(REPO)-server:$(VERSION)

tag-client-image:
@docker tag $(REPO)-client:$(COMMIT_HASH) $(REPO)-client:$(VERSION)
@docker tag $(REPO):$(COMMIT_HASH) $(REPO):$(VERSION)


push-images: ## Push container images to registry
push-images: tag-images push-server-images push-client-images

push-server-images:
@docker push $(REPO)-server:$(COMMIT_HASH)
@docker push $(REPO)-server:$(VERSION)

push-client-images:
@docker push $(REPO)-client:$(COMMIT_HASH)
@docker push $(REPO)-client:$(VERSION)
@docker push $(REPO):$(COMMIT_HASH)
@docker push $(REPO):$(VERSION)

.PHONY: clean build test build-images build-server-image build-client-image tag-images tag-client-image tag-server-image push-images push-client-images push-server-images help
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This will compile both the server and client applications.
The server needs to be connected to a serial device. It can be started with the following command:

```bash
./build/serial_server \
ser2net2ser serve \
--device /dev/ttyUSB0 \
--baud 9600 \
--port 12345
Expand All @@ -38,14 +38,14 @@ The server needs to be connected to a serial device. It can be started with the
--port: TCP port on which the server will listen for incoming connections.
```

![server](docs/server.png)
![serve](docs/serve.png)

## Running the Client

The client should be run on the machine where you want the virtual serial port to be created:

```bash
sudo ./build/serial_client \
ser2net2ser connect \
--server 192.168.1.100 \
--port 12345 \
--vsp "tty.usbserial-666"
Expand All @@ -57,7 +57,7 @@ sudo ./build/serial_client \
--vsp: Name of the virtual serial port to be created.
```

![client](docs/client.png)
![connect](docs/connect.png)

## Docker Containers

Expand Down
Binary file removed docs/client.png
Binary file not shown.
Binary file added docs/connect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/serve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/server.png
Binary file not shown.
14 changes: 6 additions & 8 deletions include/SerialClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ using ip::tcp;
using std::string;

class SerialClient {
public:
SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name);
void run();

private:
io_service io_service_;
tcp::socket socket_;
boost::asio::io_service& io_service_;
boost::asio::ip::tcp::socket socket_;
std::array<char, 256> buffer_;
VirtualSerialPort vsp_;
VirtualSerialPort vsp_; // Declare the VirtualSerialPort object

public:
SerialClient(boost::asio::io_service& io_service, const std::string& server_ip, unsigned short server_port, const std::string& vsp_name);
void run();
void do_read_write();
};


#endif // CLIENT_H
41 changes: 4 additions & 37 deletions src/SerialClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ using std::cout;
using std::cerr;
using std::endl;

SerialClient::SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name)
: socket_(io_service_), vsp_(vsp_name) {
SerialClient::SerialClient(boost::asio::io_service& io_service, const std::string& server_ip, unsigned short server_port, const std::string& vsp_name)
: io_service_(io_service), socket_(io_service), vsp_(vsp_name) {
BOOST_LOG_TRIVIAL(info) << "Initializing client...";
tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver resolver(io_service);
auto endpoint_iterator = resolver.resolve({server_ip, std::to_string(server_port)});
BOOST_LOG_TRIVIAL(info) << "Connecting to server at " << server_ip << ":" << server_port;
connect(socket_, endpoint_iterator);
Expand All @@ -28,7 +28,7 @@ void SerialClient::run() {
void SerialClient::do_read_write() {
socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) {
if (!ec) {
string data(buffer_.data(), length);
std::string data(buffer_.begin(), buffer_.begin() + length);
BOOST_LOG_TRIVIAL(info) << "Received data: " << data;
if (vsp_.write(data)) {
BOOST_LOG_TRIVIAL(info) << "Data written to virtual serial port.";
Expand All @@ -41,36 +41,3 @@ void SerialClient::do_read_write() {
}
});
}

int main(int argc, char* argv[]) {
init_logging();

try {
options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("server,s", value<string>()->default_value("127.0.0.1"), "Server IP address")
("port,p", value<unsigned short>()->default_value(12345), "Server port")
("vsp,v", value<string>()->required(), "Virtual serial port name");

variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);

if (vm.count("help")) {
cout << desc << endl;
return 0;
}

string server_ip = vm["server"].as<string>();
unsigned short server_port = vm["port"].as<unsigned short>();
string vsp_name = vm["vsp"].as<string>();

SerialClient client(server_ip, server_port, vsp_name);
client.run();
} catch (const std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "Exception: " << e.what();
cerr << "Exception: " << e.what() << endl;
}
return 0;
}
Loading

0 comments on commit dca98bf

Please sign in to comment.