-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from chandleg/dev
Stage Commit
- Loading branch information
Showing
21 changed files
with
1,874 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: SocketCluster CI | ||
|
||
on: | ||
push: | ||
branches: [ stage, prod ] | ||
pull_request: | ||
branches: [ stage, prod ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Install Boost | ||
run: sudo apt-get install libboost-all-dev | ||
- name: Install dependencies | ||
run: sudo apt-get update && sudo apt-get install -y build-essential cmake | ||
- name: Configure CMake | ||
run: cmake -S . -B build | ||
- name: Build | ||
run: cmake --build build | ||
- name: Run tests | ||
run: ./build/bin/tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
build/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"files.associations": { | ||
"__bit_reference": "cpp", | ||
"__config": "cpp", | ||
"__debug": "cpp", | ||
"__errc": "cpp", | ||
"__hash_table": "cpp", | ||
"__locale": "cpp", | ||
"__mutex_base": "cpp", | ||
"__node_handle": "cpp", | ||
"__split_buffer": "cpp", | ||
"__threading_support": "cpp", | ||
"__tree": "cpp", | ||
"__verbose_abort": "cpp", | ||
"array": "cpp", | ||
"atomic": "cpp", | ||
"bitset": "cpp", | ||
"cctype": "cpp", | ||
"charconv": "cpp", | ||
"clocale": "cpp", | ||
"cmath": "cpp", | ||
"complex": "cpp", | ||
"condition_variable": "cpp", | ||
"cstdarg": "cpp", | ||
"cstddef": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cstring": "cpp", | ||
"ctime": "cpp", | ||
"cwchar": "cpp", | ||
"cwctype": "cpp", | ||
"deque": "cpp", | ||
"exception": "cpp", | ||
"fstream": "cpp", | ||
"future": "cpp", | ||
"initializer_list": "cpp", | ||
"iomanip": "cpp", | ||
"ios": "cpp", | ||
"iosfwd": "cpp", | ||
"iostream": "cpp", | ||
"istream": "cpp", | ||
"limits": "cpp", | ||
"list": "cpp", | ||
"locale": "cpp", | ||
"map": "cpp", | ||
"mutex": "cpp", | ||
"new": "cpp", | ||
"optional": "cpp", | ||
"ostream": "cpp", | ||
"queue": "cpp", | ||
"ratio": "cpp", | ||
"regex": "cpp", | ||
"set": "cpp", | ||
"sstream": "cpp", | ||
"stack": "cpp", | ||
"stdexcept": "cpp", | ||
"streambuf": "cpp", | ||
"string": "cpp", | ||
"string_view": "cpp", | ||
"system_error": "cpp", | ||
"thread": "cpp", | ||
"tuple": "cpp", | ||
"typeinfo": "cpp", | ||
"unordered_map": "cpp", | ||
"variant": "cpp", | ||
"vector": "cpp", | ||
"algorithm": "cpp", | ||
"any": "cpp", | ||
"cinttypes": "cpp", | ||
"codecvt": "cpp", | ||
"csignal": "cpp", | ||
"coroutine": "cpp", | ||
"source_location": "cpp", | ||
"strstream": "cpp", | ||
"*.ipp": "cpp", | ||
"functional": "cpp", | ||
"bit": "cpp", | ||
"*.tcc": "cpp", | ||
"chrono": "cpp", | ||
"compare": "cpp", | ||
"concepts": "cpp", | ||
"iterator": "cpp", | ||
"memory": "cpp", | ||
"memory_resource": "cpp", | ||
"numeric": "cpp", | ||
"random": "cpp", | ||
"type_traits": "cpp", | ||
"utility": "cpp", | ||
"format": "cpp", | ||
"numbers": "cpp", | ||
"semaphore": "cpp", | ||
"span": "cpp", | ||
"stop_token": "cpp", | ||
"cfenv": "cpp", | ||
"typeindex": "cpp", | ||
"forward_list": "cpp", | ||
"unordered_set": "cpp", | ||
"csetjmp": "cpp", | ||
"barrier": "cpp", | ||
"slist": "cpp", | ||
"latch": "cpp", | ||
"ranges": "cpp", | ||
"shared_mutex": "cpp", | ||
"syncstream": "cpp", | ||
"valarray": "cpp" | ||
}, | ||
"cmake.configureOnOpen": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# API Manual | ||
|
||
## Installation | ||
|
||
The library requires the Boost Beast and Jsoncpp libraries to handle WebSocket communications and JSON data manipulation respectively. Install these dependencies using CMake if they are not already present on your system. | ||
|
||
### Required Libraries | ||
- **Boost Beast** (version >= 1.76.0): Used for WebSocket implementation. Available at [boost.org](https://www.boost.org/). | ||
- **Jsoncpp**: Used for JSON parsing and serialization. Known for its performance and ease of use in C++ environments. | ||
|
||
## Examples | ||
Two simple examples are provided demonstrating the Library in a simple raw C and a C++ classed implementation. Compile the examples using: | ||
```bash | ||
- mkdir build && cd build | ||
- cmake .. | ||
- make | ||
- ./example_client_simple or ./example_client_classed | ||
``` | ||
|
||
## Classes | ||
### Connection | ||
The `Connection` class manages WebSocket connections using Boost Beast. It handles asynchronous read, write, and connection management activities necessary for real-time communication. | ||
|
||
#### Constructor | ||
- `Connection(net::io_context& ioc)`: Initializes a new connection with a given I/O context. This context is used to handle all I/O operations for the WebSocket. | ||
|
||
#### Methods | ||
- `std::thread launch_socket(const char *host, const char *port)`: Starts the WebSocket connection on a separate thread. | ||
- `void stop()`: Stops the WebSocket connection and cleans up resources. | ||
- `void subscribe(std::string channel, socketCallback callback)`: Subscribes to a specific channel with a callback to handle incoming messages. | ||
- `void unsubscribe(std::string channel)`: Unsubscribes from a specific channel. | ||
- `void publish(std::string channel, std::string data)`: Publishes data to a specific channel. | ||
- `void message_processing()`: Handles the internal message processing in its thread. | ||
|
||
#### Callbacks | ||
- `socketCallback`: A function type that handles incoming messages. Takes an event as `std::string` and data as `Json::Value`. | ||
|
||
#### Members | ||
- `websocket::stream<beast::tcp_stream> ws_`: WebSocket stream for the connection. | ||
- `beast::flat_buffer buffer_`: Buffer used for reading WebSocket messages. | ||
|
||
### SocketClusterClient | ||
|
||
The `SocketClusterClient` class manages multiple WebSocket connections and provides methods to create and retrieve these connections. | ||
|
||
#### Constructor | ||
- `SocketClusterClient()`: Initializes a new client capable of handling WebSocket connections. | ||
|
||
#### Methods | ||
- `std::shared_ptr<Connection> createConnection(const char *url, const char *port)`: Creates and returns a new connection to the specified URL and port. | ||
- `std::list<std::shared_ptr<Connection>>& getConnections()`: Returns a list of all active connections. | ||
|
||
#### Members | ||
- `std::list<std::shared_ptr<Connection>> m_connections`: List storing all managed connections. | ||
|
||
|
||
## Errors & Exceptions | ||
- `1000 - Normal Closure` : Connection closed successfully. | ||
- `1001 - Going Away` : Server or client is shutting down. | ||
- `1002 - Protocol Error` : Protocol violation detected. | ||
- `1003 - Unsupported Data` : The endpoint received data of a type it cannot accept. | ||
- `1006 - Abnormal Closure` : Connection closed abnormally without a status code. | ||
|
||
|
||
## Example Usage of SocketClusterClient Library | ||
|
||
This example demonstrates how to use the `SocketClusterClient` and `Connection` classes to connect to a WebSocket server, subscribe to a channel, and handle incoming messages. | ||
|
||
```cpp | ||
#include <iostream> | ||
#include "SocketClusterClient.h" | ||
|
||
// Define a callback function to handle messages received on the WebSocket | ||
void handleMessage(std::string event, Json::Value data) { | ||
std::cout << "Event: " << event << "\nMessage received: " << data.toStyledString() << std::endl; | ||
} | ||
|
||
int main() { | ||
// Create a client instance | ||
SocketClusterClient client; | ||
|
||
// Create a new WebSocket connection to the desired host and port | ||
auto connection = client.createConnection("ws://example.com", "80"); | ||
|
||
// Subscribe to a channel with the defined callback | ||
connection->subscribe("exampleChannel", handleMessage); | ||
|
||
// Publish to a channel | ||
connection->publish("exampleChannel", "Hello World!"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,110 @@ | ||
cmake_minimum_required(VERSION 3.10) # Example minimum version | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) | ||
project(SocketClusterClientCPP) | ||
|
||
project(socketcluster_client) | ||
include(FetchContent) | ||
|
||
# === JsonCPP === | ||
FetchContent_Declare(JsonCpp | ||
GIT_REPOSITORY https://github.com/open-source-parsers/jsoncpp.git | ||
GIT_TAG 1.9.5 # Or your desired version | ||
) | ||
FetchContent_MakeAvailable(JsonCpp) | ||
# === === | ||
|
||
# random bullshit | ||
# Default to C99 | ||
if(NOT CMAKE_C_STANDARD) | ||
set(CMAKE_C_STANDARD 99) | ||
endif() | ||
# set(BOOST_INCLUDE_LIBRARIES thread filesystem system program_options) | ||
set(BOOST_ENABLE_CMAKE ON) | ||
include(FetchContent) | ||
FetchContent_Declare( | ||
Boost | ||
GIT_REPOSITORY https://github.com/boostorg/boost.git | ||
GIT_TAG boost-1.74.0 | ||
GIT_SHALLOW TRUE | ||
) | ||
FetchContent_MakeAvailable(Boost) | ||
|
||
find_package(Boost 1.74.0 REQUIRED) | ||
|
||
# Find source files | ||
file(GLOB_RECURSE LIBRARY_SOURCES src/*.cpp) | ||
add_library(SocketClusterClientCPP SHARED ${LIBRARY_SOURCES}) | ||
target_link_libraries(SocketClusterClientCPP PUBLIC jsoncpp_lib) | ||
|
||
# Update include directories to find json-c headers | ||
target_include_directories(SocketClusterClientCPP PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<BUILD_INTERFACE:${jsoncpp_SOURCE_DIR}/include> | ||
$<BUILD_INTERFACE:${websocketpp_SOURCE_DIR}/include> | ||
) | ||
|
||
target_include_directories(SocketClusterClientCPP PUBLIC "${Boost_INCLUDE_DIRS}") | ||
|
||
# Install targets and headers | ||
install(TARGETS SocketClusterClientCPP | ||
ARCHIVE DESTINATION lib | ||
LIBRARY DESTINATION lib | ||
RUNTIME DESTINATION bin) | ||
|
||
install(DIRECTORY src/ | ||
DESTINATION include) | ||
|
||
add_executable(simple_example test/examples/client_classed.cpp) | ||
target_link_libraries(simple_example PUBLIC SocketClusterClientCPP) | ||
|
||
# Default to C++14 | ||
if(NOT CMAKE_CXX_STANDARD) | ||
set(CMAKE_CXX_STANDARD 14) | ||
endif() | ||
|
||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") | ||
add_compile_options(-Wall -Wextra -Wpedantic) | ||
# Coverage | ||
option(BUILD_TESTING "Builds only the test executable." OFF) | ||
option(CODE_COVERAGE "Collect coverage from test library" OFF) | ||
|
||
if(BUILD_TESTING) | ||
enable_testing() | ||
add_subdirectory(test) | ||
add_test(NAME project_tests COMMAND ./bin/tests) | ||
|
||
if(CODE_COVERAGE AND CMAKE_BUILD_TYPE MATCHES Debug) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -O0") | ||
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O0") | ||
# Set the coverage report output directory | ||
set(COVERAGE_DIR ${CMAKE_BINARY_DIR}/coverage) | ||
|
||
# Clean the coverage directory | ||
add_custom_target(coverage_clean | ||
COMMAND ${CMAKE_COMMAND} -E rm -rf ${COVERAGE_DIR} | ||
) | ||
|
||
# Add a target to run the tests and collect coverage information | ||
add_custom_target(coverage | ||
COMMAND ${CMAKE_COMMAND} -E make_directory ${COVERAGE_DIR} | ||
COMMAND ${CMAKE_COMMAND} -E env LCOV_OPTS="--rc lcov_branch_coverage=1 geninfo_unexecuted_blocks=1" $<TARGET_FILE:tests> | ||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | ||
DEPENDS tests coverage_clean | ||
COMMENT "Running tests and collecting coverage data..." | ||
) | ||
|
||
# Add a target to generate the coverage report | ||
# Add a target to generate the coverage report | ||
# Add a target to generate the coverage report | ||
add_custom_target(coverage_report | ||
COMMAND lcov --capture --directory . --output-file ${COVERAGE_DIR}/coverage.info --ignore-errors source,unused | ||
# Remove coverage data for external libraries, including Catch2 | ||
COMMAND lcov --remove ${COVERAGE_DIR}/coverage.info "/usr/*" "${CMAKE_BINARY_DIR}/_deps/*" --output-file ${COVERAGE_DIR}/filtered.info --ignore-errors source,unused | ||
# Optional: Add more --remove lines as needed to exclude other directories or files | ||
COMMAND lcov --list ${COVERAGE_DIR}/filtered.info | ||
COMMAND genhtml -o ${COVERAGE_DIR}/html ${COVERAGE_DIR}/filtered.info | ||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | ||
DEPENDS coverage | ||
COMMENT "Generating coverage report..." | ||
) | ||
|
||
|
||
endif() | ||
endif() | ||
# end random bs | ||
|
||
# find dependencies | ||
find_package(jsonc REQUIRED) | ||
find_package(boostbeast REQUIRED) | ||
|
||
set(SOURCES) | ||
set(dependencies | ||
jsonc | ||
boostbeast | ||
) | ||
|
||
add_executable(main src/main.cpp dependencies) | ||
|
||
|
||
install( | ||
TARGETS maininstall(DIRECTORY | ||
config | ||
DESTINATION share/${PROJECT_NAME} | ||
) | ||
) | ||
|
||
|
Oops, something went wrong.