Skip to content

Commit

Permalink
Merge pull request #23 from denismakogon/more-api
Browse files Browse the repository at this point in the history
adding before and after request methods
  • Loading branch information
denismakogon authored Jun 14, 2024
2 parents 0d20dc0 + 00822d8 commit b4cdd0f
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .github/conan2/macos_default
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ os=Macos
[buildenv]
CC=/usr/bin/clang
CXX=/usr/bin/clang++

[options]
boost/*:with_stacktrace_backtrace=False
boost/*:without_locale=True
25 changes: 13 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 1

- name: caching
uses: actions/cache@v3
uses: actions/cache@v4
id: cmakelists
with:
path: |
Expand All @@ -32,8 +32,9 @@ jobs:
- name: install prerequisites
shell: bash
run: |
brew install [email protected]
brew install gcc
pip3 install conan
pip3 install conan --break-system-packages
- name: conan-profile
shell: bash
Expand All @@ -48,7 +49,7 @@ jobs:
- name: compile x86_64
shell: bash
run: |
conan install . --output-folder=build --build=missing
conan install . --output-folder=build --build=missing
cd build
cp ../toolchains/macos-x86_64.cmake conan_toolchain.cmake
LIB_SUFFIX="-macos-x86_64" cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
Expand All @@ -70,7 +71,7 @@ jobs:
rm -fr *
- name: artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: libjudy-macos
path: libs/libjudy*
Expand All @@ -84,7 +85,7 @@ jobs:
fetch-depth: 1

- name: caching
uses: actions/cache@v3
uses: actions/cache@v4
id: cmakelists
with:
path: |
Expand Down Expand Up @@ -126,7 +127,7 @@ jobs:
rm -fr CMakeFiles/
- name: artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: libjudy-linux
path: libs/libjudy*
Expand All @@ -149,7 +150,7 @@ jobs:
fetch-depth: 1

- name: caching
uses: actions/cache@v3
uses: actions/cache@v4
id: cmakelists
with:
path: |
Expand All @@ -162,7 +163,7 @@ jobs:
uses: oracle-actions/setup-java@v1
with:
install: false
uri: https://download.java.net/java/early_access/jextract/1/openjdk-21-jextract+1-2_${{ matrix.TARGET }}-x64_bin.tar.gz
uri: https://download.java.net/java/early_access/jextract/22/5/openjdk-22-jextract+5-33_${{matrix.TARGET}}-${{matrix.PLATFORM}}_bin.tar.gz

- name: 'Extract jextract'
shell: sh
Expand All @@ -172,11 +173,11 @@ jobs:
export PATH=${PATH}:/tmp/jextract/bin
- name: 'Install OracleJDK GA'
id: jdk_21
id: jdk
uses: oracle-actions/setup-java@v1
with:
website: oracle.com
release: 21
release: 22

- name: prepare-project
shell: bash
Expand Down Expand Up @@ -227,13 +228,13 @@ jobs:
mkdir src/main/resources
- name: libjudy-linux
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: libjudy-linux
path: build/src/main/resources/lib

- name: libjudy-macos
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: libjudy-macos
path: build/src/main/resources/lib
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ make jar
#include "server.h"
#include "structs.h"

void printRequest(char*, long bytesTransferred, char* host, int port) {
printf("a new packet from %s:%d of %ld bytes received!\n", host, port, bytesTransferred);
void printRequest(struct udp_request* request) {
printf("%s [INFO] packet is %ld bytes long\n", currentDateTime().c_str(), request->bytesReceived);
std::flush(std::cout);
}


int main(int argc, char const *argv[]) {
return startNativeServer(printRequest, 20777, 2048, 1000);
}
Expand All @@ -75,8 +76,8 @@ see [main.cc](src/main.cc) for more details.
#include "server.h"
void printRequest(char*, long bytesTransferred, char* host, int port) {
printf("a new packet from %s:%d of %ld bytes received!\n", host, port, bytesTransferred);
void printRequest(struct udp_request* request) {
printf("%s [INFO] packet is %ld bytes long\n", currentDateTime().c_str(), request->bytesReceived);
std::flush(std::cout);
}
Expand Down
7 changes: 5 additions & 2 deletions include/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#ifdef __cplusplus
extern "C" {
#endif
int nativeServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount);
void boostServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount);
int nativeServer(void (*handler)(char* /* data */, long /* bytesReceived*/, char* /* host */, int /* port */), int port, int bufferSize, int threadCount);
void boostServer(void (*handler)(struct udp_request* request),
void (*before_handler)(char* /* data */, char* /* errorCategory */, int /* errorCode */, long /* bytesReceived */, char* /* client */, int /* port */),
void (*after_handler)(char* /* data */, long /* bytesReceived*/, char* /* host */, int /* port */),
int port, int bufferSize, int threadCount);
#ifdef __cplusplus
}
#endif
7 changes: 6 additions & 1 deletion include/server.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef server_h
#define server_h

#include "structs.h"

int startNativeServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount);
void startBoostServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount);
void startBoostServer(void (*handler)(struct udp_request* request),
void (*before_handler)(char* /* data */, char* /* errorCategory */, int /* errorCode */, long /* bytesReceived */, char* /* client */, int /* port */),
void (*after_handler)(char* /* data */, long /* bytesReceived*/, char* /* host */, int /* port */),
int port, int bufferSize, int threadCount);
#endif /* server_h */
7 changes: 7 additions & 0 deletions include/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ struct udpRequest {
struct sockaddr_in clientaddr;
};

struct udp_request {
size_t bytesReceived;
char* data;
char* host;
int port;
};

#endif /* structs_h */
2 changes: 1 addition & 1 deletion jextract_dump.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#### Extracted from: include/structs.h

--source -t judy.server
-t judy.server
-I include/
--include-function nativeServer
--include-function boostServer
Expand Down
2 changes: 1 addition & 1 deletion jitpack.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
before_install:
- sdk install java 21-open
- sdk install java 22-open
install:
- PROJECT=judy
- version=$(date +'%Y.%m.%d')
Expand Down
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<properties>
<src.dir>build/src</src.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
</properties>

<distributionManagement>
Expand All @@ -44,10 +44,11 @@
${project.build.directory}
</outputDirectory>
</configuration>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.3.1</version>
<version>3.3.2</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
Expand All @@ -63,7 +64,7 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.13.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
Expand All @@ -74,7 +75,7 @@
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.1</version>
<configuration>
<archive>
<manifest>
Expand All @@ -85,13 +86,13 @@
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<configuration>
</configuration>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.5</version>
<version>3.5.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down
34 changes: 27 additions & 7 deletions src/asio_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <boost/thread.hpp>

#include "timestamp.h"
#include "structs.h"

using namespace boost::asio;

Expand All @@ -16,9 +17,11 @@ namespace {
}

struct UdpServer {
explicit UdpServer(ip::udp::socket socket, void (*handler_)(char*, long, char*, int), int bufferSize, int threadPoolSize)
explicit UdpServer(ip::udp::socket socket, void (*handler_)(struct udp_request* request), void (*before_handler_)(char*, char*, int, long, char*, int), void (*after_handler_)(char*, long, char*, int), int bufferSize, int threadPoolSize)
: socket_(std::move(socket)), pool(threadPoolSize) {
this->handler = handler_;
this->before_handler = before_handler_;
this->after_handler = after_handler_;
this->bufferSize = bufferSize;
this->data_ = new char[bufferSize];
read();
Expand All @@ -34,10 +37,11 @@ struct UdpServer {
}

void handleRequest(char* data_, std::size_t bytes_transferred, const char* clientAddress, int clientPort) {
udp_request req = {
bytes_transferred, data_, (char*) clientAddress, clientPort
};
boost::asio::post(this->pool, boost::bind(
this->handler, data_, bytes_transferred,
(char*) clientAddress,
clientPort
this->handler, &req
));

printf("%s [INFO] passing UDP request to judy.server.udp[%s:%d] foreign handler | payload size: %lu bytes\n",
Expand All @@ -47,9 +51,20 @@ struct UdpServer {
bytes_transferred
);
std::flush(std::cout);

boost::asio::post(this->pool, boost::bind(
this->after_handler, data_, bytes_transferred,
(char*) clientAddress,
clientPort
));
}

void handle_receive(const boost::system::error_code& error, char* data_, std::size_t bytes_transferred) {
boost::asio::post(this->pool, boost::bind(
this->before_handler,
data_, (char*) error.category().name(), error.value(),
bytes_transferred, (char*) remote_endpoint_.address().to_string().c_str(), remote_endpoint_.port()
));
if (error || strcmp(data_, "\n") == 0) {
printf("%s [ERROR] error occured: %s, %d\n", currentDateTime().c_str(), error.category().name(), error.value());
printData(data_, bytes_transferred);
Expand Down Expand Up @@ -86,10 +101,15 @@ struct UdpServer {
boost::asio::thread_pool pool;
ip::udp::socket socket_;
ip::udp::endpoint remote_endpoint_;
void (*handler)(char*, long, char*, int);
void (*handler)(struct udp_request* request);
void (*before_handler)(char*, char*, int, long, char*, int);
void (*after_handler)(char*, long, char*, int);
};

void startBoostServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount) {
void startBoostServer(void (*handler)(struct udp_request* request),
void (*before_handler)(char*, char*, int, long, char*, int),
void (*after_handler)(char*, long, char*, int),
int port, int bufferSize, int threadCount) {
try {
io_context ctx;
ip::udp::endpoint endpoint(ip::udp::v4(), port);
Expand All @@ -101,7 +121,7 @@ void startBoostServer(void (*handler)(char*, long, char*, int), int port, int bu
exit(0);
};

UdpServer server(std::move(socket), handler, 2048, threadCount);
UdpServer server(std::move(socket), handler, before_handler, after_handler, 2048, threadCount);
std::vector<std::thread> threadGroup;
for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) {
std::thread thread([&ctx]() { ctx.run(); });
Expand Down
9 changes: 6 additions & 3 deletions src/c_api.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "server.h"

#include "structs.h"

extern "C" int nativeServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount) {
return startNativeServer(handler, port, bufferSize, threadCount);
}

extern "C" void boostServer(void (*handler)(char*, long, char*, int), int port, int bufferSize, int threadCount) {
startBoostServer(handler, port, bufferSize, threadCount);
extern "C" void boostServer(void (*handler)(struct udp_request* request),
void (*before_handler)(char* /* data */, char* /* errorCategory */, int /* errorCode */, long /* bytesReceived */, char* /* client */, int /* port */),
void (*after_handler)(char* /* data */, long /* bytesReceived*/, char* /* host */, int /* port */),
int port, int bufferSize, int threadCount) {
startBoostServer(handler, before_handler, after_handler, port, bufferSize, threadCount);
}
17 changes: 14 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
#include <thread>

#include "server.h"
#include "structs.h"
#include "timestamp.h"

void printRequest(char*, long bytesTransferred, char* host, int port) {
printf("%s [INFO] a new packet from %s:%d of %ld bytes received!\n", currentDateTime().c_str(), host, port, bytesTransferred);
void printRequest(struct udp_request* request) {
printf("%s [INFO] packet is %ld bytes long\n", currentDateTime().c_str(), request->bytesReceived);
std::flush(std::cout);
}

void beforeRequest(char* /* data */, char* /* errorCategory */, int /*errorCode*/, long bytesTransferred, char* host, int port) {
printf("%s [INFO] before request executed\n", currentDateTime().c_str());
printf("%s [INFO] a new packet from %s:%d received\n", currentDateTime().c_str(), host, port);
std::flush(std::cout);
}

void afterRequest(char* /* data */, long /* bytesTransferred */, char* /* host */, int /* port */) {
printf("%s [INFO] after request\n", currentDateTime().c_str());
}

int main() {
startBoostServer(&printRequest, 20777, 2048, std::thread::hardware_concurrency());
startBoostServer(&printRequest, &beforeRequest, &afterRequest, 20777, 2048, std::thread::hardware_concurrency());
}

0 comments on commit b4cdd0f

Please sign in to comment.