Skip to content

Commit 951c769

Browse files
Merge pull request #1 from janekbaraniewski/init
Init
2 parents 1ede24b + b8aeac6 commit 951c769

18 files changed

+715
-123
lines changed

.github/workflows/docker.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build and Push Docker images
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- 'src/**'
9+
- 'Dockerfile.server'
10+
- 'Dockerfile.client'
11+
12+
jobs:
13+
build-and-push:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
20+
- name: Set up Docker Buildx
21+
uses: docker/setup-buildx-action@v1
22+
23+
- name: Login to GitHub Container Registry
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Build and tag the Docker image
31+
run: make build-images
32+
33+
- name: tag images
34+
run: make tag-images
35+
36+
- name: Push Docker images to GitHub Packages
37+
run: make push-images

.github/workflows/release.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
build-and-release:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Set up Python
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: '3.x'
18+
- name: Install dependencies
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y libboost-all-dev
22+
- name: Build
23+
run: |
24+
cmake -S . -B build
25+
cmake --build build
26+
- name: Archive Production Artifacts
27+
uses: actions/upload-artifact@v3
28+
with:
29+
name: binaries
30+
path: build/
31+
- name: Create Release
32+
id: create_release
33+
uses: actions/create-release@v1
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
with:
37+
tag_name: ${{ github.ref }}
38+
release_name: Release ${{ github.ref }}
39+
draft: false
40+
prerelease: false
41+
- name: Upload Release Asset
42+
uses: actions/upload-release-asset@v2
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
with:
46+
upload_url: ${{ steps.create_release.outputs.upload_url }}
47+
asset_path: ./build/serial_server
48+
asset_name: serial_server
49+
asset_content_type: application/octet-stream

.github/workflows/test.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Install dependencies
16+
run: |
17+
sudo apt-get update
18+
sudo apt-get install -y libboost-all-dev
19+
- name: Run tests
20+
run: make test
21+
22+
docker:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v2
28+
29+
- name: Set up Docker Buildx
30+
uses: docker/setup-buildx-action@v1
31+
32+
- name: Build and tag the Docker image
33+
run: make build-images

CMakeLists.txt

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ set(CMAKE_CXX_STANDARD 14)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
# Find Boost libraries
8-
find_package(Boost 1.65 REQUIRED COMPONENTS system)
8+
find_package(Boost 1.65 REQUIRED COMPONENTS system program_options log_setup log date_time)
99

1010
include(FetchContent)
1111
FetchContent_Declare(
1212
googletest
1313
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
1414
)
15-
# For Windows: Prevent overriding the parent project's compiler/linker settings
1615
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
1716
FetchContent_MakeAvailable(googletest)
1817

@@ -22,31 +21,61 @@ else()
2221
message(FATAL_ERROR "GTest::gtest_main not found")
2322
endif()
2423

24+
include_directories(${CMAKE_SOURCE_DIR}/include)
25+
26+
add_library(logging src/logging.cpp)
27+
target_include_directories(logging PUBLIC ${CMAKE_SOURCE_DIR}/include)
28+
target_link_libraries(logging PUBLIC
29+
Boost::log
30+
Boost::log_setup
31+
Boost::date_time
32+
)
33+
34+
add_library(serial_server_lib src/logging.cpp src/server.cpp src/VirtualSerialPort.cpp) # Include VirtualSerialPort.cpp
35+
target_include_directories(serial_server_lib PUBLIC ${CMAKE_SOURCE_DIR}/include)
36+
target_link_libraries(serial_server_lib PRIVATE
37+
Boost::system
38+
Boost::program_options
39+
Boost::log
40+
Boost::log_setup
41+
Boost::date_time
42+
logging
43+
pthread
44+
)
2545

26-
# Define the executable for the server
2746
add_executable(serial_server src/server.cpp)
28-
target_include_directories(serial_server PRIVATE ${Boost_INCLUDE_DIRS})
2947
target_link_libraries(serial_server PRIVATE
48+
serial_server_lib
49+
logging
3050
Boost::system
31-
pthread
51+
Boost::program_options
52+
Boost::log
53+
Boost::log_setup
54+
Boost::date_time
3255
)
3356

34-
# Define the executable for the client
3557
add_executable(serial_client src/client.cpp)
36-
target_include_directories(serial_client PRIVATE ${Boost_INCLUDE_DIRS})
3758
target_link_libraries(serial_client PRIVATE
59+
serial_server_lib
60+
logging
3861
Boost::system
39-
pthread
62+
Boost::program_options
63+
Boost::log
64+
Boost::log_setup
65+
Boost::date_time
4066
)
4167

42-
# Enable testing
43-
enable_testing()
44-
45-
# Add a test executable
46-
add_executable(test_app tests/test_server.cpp)
47-
target_link_libraries(test_app PRIVATE
48-
GTest::gtest_main
49-
)
68+
# # TODO: REENABLE TESTS
69+
# enable_testing()
70+
# add_executable(test_app tests/test_server.cpp)
71+
# target_compile_definitions(test_app PRIVATE UNIT_TEST)
72+
# target_link_libraries(test_app PRIVATE
73+
# serial_server_lib # this includes server.cpp
74+
# Boost::system
75+
# GTest::gtest_main
76+
# GTest::gtest
77+
# GTest::gmock
78+
# )
5079

51-
include(GoogleTest)
52-
gtest_discover_tests(test_app)
80+
# include(GoogleTest)
81+
# gtest_discover_tests(test_app EXTRA_ARGS --gtest_color=yes)

Dockerfile.client

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Build stage
2+
FROM ubuntu:20.04 as builder
3+
4+
WORKDIR /app
5+
6+
# Install build dependencies
7+
RUN apt-get update && apt-get install -y \
8+
g++ \
9+
cmake \
10+
libboost-system-dev \
11+
libboost-program-options-dev \
12+
libboost-log-dev \
13+
libboost-date-time-dev
14+
15+
# Copy source code
16+
COPY . /app
17+
18+
# Build the application
19+
RUN cmake . && make
20+
21+
# Final stage
22+
FROM ubuntu:20.04
23+
24+
WORKDIR /app
25+
26+
# Install runtime dependencies only
27+
RUN apt-get update && apt-get install -y \
28+
libboost-system1.71.0 \
29+
libboost-program-options1.71.0 \
30+
libboost-log1.71.0 \
31+
libboost-date-time1.71.0 && \
32+
rm -rf /var/lib/apt/lists/*
33+
34+
# Copy binaries from the builder stage
35+
COPY --from=builder /app/serial_client /app/serial_client
36+
37+
# Command to run the application
38+
CMD ["./serial_client", "--server", "server_ip", "--port", "12345"]

Dockerfile.server

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Build stage
2+
FROM ubuntu:20.04 as builder
3+
4+
WORKDIR /app
5+
6+
# Install build dependencies
7+
RUN apt-get update && apt-get install -y \
8+
g++ \
9+
cmake \
10+
libboost-system-dev \
11+
libboost-program-options-dev \
12+
libboost-log-dev \
13+
libboost-date-time-dev
14+
15+
# Copy source code
16+
COPY . /app
17+
18+
# Build the application
19+
RUN cmake . && make
20+
21+
# Final stage
22+
FROM ubuntu:20.04
23+
24+
WORKDIR /app
25+
26+
# Install runtime dependencies only
27+
RUN apt-get update && apt-get install -y \
28+
libboost-system1.71.0 \
29+
libboost-program-options1.71.0 \
30+
libboost-log1.71.0 \
31+
libboost-date-time1.71.0 && \
32+
rm -rf /var/lib/apt/lists/*
33+
34+
# Copy binaries from the builder stage
35+
COPY --from=builder /app/serial_server /app/serial_server
36+
37+
# Command to run the application
38+
CMD ["./serial_server"]

Makefile

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
BUILD_DIR = build
2+
REPO = ghcr.io/janekbaraniewski/ser2net2ser
3+
VERSION ?= latest
4+
COMMIT_HASH ?= $(shell git rev-parse --short HEAD)
25

36
clean:
47
@rm -rf ${BUILD_DIR}/*
58

6-
build:
9+
build: clean
710
@cmake -S . -B build
811
@cmake --build build
912

1013
test: build
1114
@cd build && ctest
1215

13-
.PHONY: clean build test
16+
build-images:
17+
@docker build . -f Dockerfile.server -t $(REPO)/server:$(COMMIT_HASH)
18+
@docker build . -f Dockerfile.client -t $(REPO)/client:$(COMMIT_HASH)
19+
20+
tag-images:
21+
@docker tag $(REPO)/server:$(COMMIT_HASH) $(REPO)/server:$(VERSION)
22+
@docker tag $(REPO)/client:$(COMMIT_HASH) $(REPO)/client:$(VERSION)
23+
24+
push-images: tag-images
25+
@docker push $(REPO)/server:$(COMMIT_HASH)
26+
@docker push $(REPO)/server:$(VERSION)
27+
@docker push $(REPO)/client:$(COMMIT_HASH)
28+
@docker push $(REPO)/client:$(VERSION)
29+
30+
help:
31+
@echo "Makefile commands:"
32+
@echo " clean - Remove build directory."
33+
@echo " build - Build the project using cmake."
34+
@echo " test - Run tests after building."
35+
@echo " build-images - Build Docker images for both server and client."
36+
@echo " tag-images - Tag Docker images with the latest or specified version."
37+
@echo " push-images - Push Docker images to the registry."
38+
@echo " help - Show this help message."
39+
40+
.PHONY: clean build test build-images tag-images push-images help

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ser2net2ser

include/ISerialPort.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef ISERIALPORT_H
2+
#define ISERIALPORT_H
3+
4+
#include <string>
5+
#include <boost/asio.hpp>
6+
7+
using namespace boost::asio;
8+
using std::string;
9+
10+
class ISerialPort {
11+
public:
12+
virtual void open(const string& device) = 0;
13+
virtual void set_option(const serial_port_base::baud_rate& option) = 0;
14+
virtual void async_read_some(const boost::asio::mutable_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0;
15+
virtual void async_write(const boost::asio::const_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0;
16+
virtual ~ISerialPort() = default;
17+
};
18+
19+
#endif // ISERIALPORT_H

include/RealSerialPort.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef REALSERIALPORT_H
2+
#define REALSERIALPORT_H
3+
4+
#include "ISerialPort.h"
5+
#include <boost/asio.hpp>
6+
7+
class RealSerialPort : public ISerialPort {
8+
public:
9+
explicit RealSerialPort(io_service& io) : port(io) {}
10+
11+
void open(const string& device) override {
12+
port.open(device);
13+
}
14+
15+
void set_option(const serial_port_base::baud_rate& option) override {
16+
port.set_option(option);
17+
}
18+
19+
void async_read_some(const boost::asio::mutable_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) override {
20+
port.async_read_some(buffer, handler);
21+
}
22+
23+
void async_write(const boost::asio::const_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) override {
24+
boost::asio::async_write(port, buffer, handler);
25+
}
26+
27+
private:
28+
boost::asio::serial_port port;
29+
};
30+
31+
#endif // REALSERIALPORT_H

0 commit comments

Comments
 (0)