Skip to content

Commit

Permalink
Integrate client capability into C++ test node
Browse files Browse the repository at this point in the history
Further changes:
 - user log function takes a context pointer
 - "packetized" flag can be given to Domain::add_channels()
 - TCP backend uses legacy stream protocol instead of packet protocol
  • Loading branch information
samuelsadok committed Apr 16, 2021
1 parent da6ab3a commit 69f8681
Show file tree
Hide file tree
Showing 29 changed files with 428 additions and 349 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- uses: actions/upload-artifact@v2
with:
name: test-server-${{ matrix.target }}
path: test/build/test_server.elf
path: test/build/test_node.elf

test-pyfibre:
needs: [compile]
Expand Down Expand Up @@ -70,12 +70,12 @@ jobs:
python3 --version
chmod +x ./artifacts/test-server-$ARCH/test_server.elf
ls ./artifacts/test-server-$ARCH/test_server.elf
chmod +x ./artifacts/test-server-$ARCH/test_node.elf
ls ./artifacts/test-server-$ARCH/test_node.elf
cp ./artifacts/libfibre-$ARCH/* ./python/fibre/
# Launch test server in background
FIBRE_LOG=5 ./artifacts/test-server-$ARCH/test_server.elf >test_server.log 2>&1 &
FIBRE_LOG=5 ./artifacts/test-server-$ARCH/test_node.elf --server --domain tcp-server:address=localhost,port=14220 >test_server.log 2>&1 &
# TODO: try launching client/server in reverse order
sleep 1
Expand All @@ -91,7 +91,7 @@ jobs:
CLIENT_STATUS="fail"
fi
# Tell test_server.elf politely to finish (if it's still running)
# Tell test_node.elf politely to finish (if it's still running)
echo "terminiating test server"
Expand Down Expand Up @@ -132,7 +132,9 @@ jobs:
- name: Check C++ Formatting
run: |
clang-format --version
# TODO: we run this only on one file for now until we have it properly configured
clang-format -style=file --Werror --dry-run test/test_server.cpp
# TODO: we run this only on a few selected files for now until we have it properly configured
clang-format -style=file --Werror --dry-run test/test_node.cpp
clang-format -style=file --Werror --dry-run test/test_node.hpp
clang-format -style=file --Werror --dry-run sim/sim_main.cpp
# TODO: check if interface_generator outputs the same thing with Python 3.5 and Python 3.8
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ __pycache__
# Test build files
/test/autogen
/test/build
/test/test_server.elf

# Tup database
/.tup
25 changes: 24 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,35 @@
"name": "C++ Test Server",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test/build/test_server.elf",
"program": "${workspaceFolder}/test/build/test_node.elf",
"stopAtEntry": false,
"cwd": "${workspaceFolder}/test",
"environment": [{"name": "FIBRE_LOG", "value": "5"}],
"externalConsole": false,
"MIMode": "gdb",
"args": ["--server", "--domain", "tcp-server:address=localhost,port=14220"],
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"symbolLoadInfo": {
"loadAll": true,
"exceptionList": ""
}
},
{
"name": "Simulation",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/sim/build/fibre_sim",
"stopAtEntry": false,
"cwd": "${workspaceFolder}/sim",
"environment": [{"name": "FIBRE_LOG", "value": "5"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
Expand Down
10 changes: 10 additions & 0 deletions Tuprules.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

function compile(src_file, extra_inputs)
obj_file = 'build/'..tup.file(src_file)..'.o'
tup.frule{
inputs={src_file, extra_inputs=extra_inputs},
command='^co^ '..CXX..' -c %f '..tostring(CFLAGS)..' -o %o',
outputs={obj_file}
}
return obj_file
end
2 changes: 1 addition & 1 deletion cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If your application doesn't use tup, you have to manually check which code files
Currently there's no nice walkthrough for this but here are two applications that you can use as an example:

- The [ODrive Firmware](https://github.com/madcowswe/ODrive/tree/devel/Firmware)
- The [test server](https://github.com/samuelsadok/fibre/blob/devel/test/test_server.cpp)
- The [test node](https://github.com/samuelsadok/fibre/blob/devel/test/test_node.cpp)

## Configuring `libfibre`

Expand Down
93 changes: 0 additions & 93 deletions cpp/endpoints_template.j2

This file was deleted.

33 changes: 26 additions & 7 deletions cpp/fibre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <string>
#endif

#if FIBRE_ENABLE_TEXT_LOGGING
#include <chrono>
#endif

#if FIBRE_ENABLE_EVENT_LOOP
# ifdef __linux__
# include "platform_support/epoll_event_loop.hpp"
Expand Down Expand Up @@ -167,7 +171,7 @@ static std::string get_local_time() {
std::to_string((now.time_since_epoch() - seconds_since_epoch).count());
}

void fibre::log_to_stderr(const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text) {
void fibre::log_to_stderr(void* ctx, const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text) {
switch ((LogLevel)level) {
case LogLevel::kDebug:
//std::cerr << "\x1b[93;1m"; // yellow
Expand All @@ -181,7 +185,7 @@ void fibre::log_to_stderr(const char* file, unsigned line, int level, uintptr_t
std::cerr << get_local_time() << " [" << file << ":" << line << "] " << text << "\x1b[0m" << std::endl;
}
#else
void fibre::log_to_stderr(const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text) {
void fibre::log_to_stderr(void* ctx, const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text) {
// ignore
}
#endif
Expand Down Expand Up @@ -282,13 +286,23 @@ void Domain::add_channels(ChannelDiscoveryResult result) {
}

#if FIBRE_ENABLE_CLIENT || FIBRE_ENABLE_SERVER
// Deleted during on_stopped()
auto protocol = new fibre::LegacyProtocolPacketBased(this, result.rx_channel, result.tx_channel, result.mtu);
if (result.packetized) {
// Deleted during on_stopped_p()
auto protocol = new fibre::LegacyProtocolPacketBased(this, result.rx_channel, result.tx_channel, result.mtu);
#if FIBRE_ENABLE_CLIENT
protocol->start(MEMBER_CB(this, on_found_root_object), MEMBER_CB(this, on_lost_root_object), MEMBER_CB(this, on_stopped));
protocol->start(MEMBER_CB(this, on_found_root_object), MEMBER_CB(this, on_lost_root_object), MEMBER_CB(this, on_stopped_p));
#else
protocol->start(MEMBER_CB(this, on_stopped));
protocol->start(MEMBER_CB(this, on_stopped_p));
#endif
} else {
// Deleted during on_stopped_s()
auto protocol = new fibre::LegacyProtocolStreamBased(this, result.rx_channel, result.tx_channel);
#if FIBRE_ENABLE_CLIENT
protocol->start(MEMBER_CB(this, on_found_root_object), MEMBER_CB(this, on_lost_root_object), MEMBER_CB(this, on_stopped_s));
#else
protocol->start(MEMBER_CB(this, on_stopped_s));
#endif
}
#endif
}

Expand All @@ -308,10 +322,15 @@ void Domain::on_lost_root_object(LegacyObjectClient* obj_client, std::shared_ptr
}
#endif

void Domain::on_stopped(LegacyProtocolPacketBased* protocol, StreamStatus status) {
void Domain::on_stopped_p(LegacyProtocolPacketBased* protocol, StreamStatus status) {
delete protocol;
}

void Domain::on_stopped_s(LegacyProtocolPacketBased* protocol, StreamStatus status) {
size_t offset = (size_t)&((LegacyProtocolStreamBased*)nullptr)->inner_protocol_;
delete (LegacyProtocolStreamBased*)((uintptr_t)protocol - offset);
}

#if FIBRE_ENABLE_SERVER
ServerFunctionDefinition* Domain::get_server_function(ServerFunctionId id) {
if (id < n_static_server_functions) {
Expand Down
40 changes: 0 additions & 40 deletions cpp/function_stubs_template.j2

This file was deleted.

1 change: 1 addition & 0 deletions cpp/include/fibre/channel_discoverer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct ChannelDiscoveryResult {
AsyncStreamSource* rx_channel;
AsyncStreamSink* tx_channel;
size_t mtu;
bool packetized;
};

struct ChannelDiscoveryContext {};
Expand Down
5 changes: 3 additions & 2 deletions cpp/include/fibre/fibre.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ class Domain {
void on_found_root_object(LegacyObjectClient* obj_client, std::shared_ptr<LegacyObject> obj);
void on_lost_root_object(LegacyObjectClient* obj_client, std::shared_ptr<LegacyObject> obj);
#endif
void on_stopped(LegacyProtocolPacketBased* protocol, StreamStatus status);
void on_stopped_p(LegacyProtocolPacketBased* protocol, StreamStatus status);
void on_stopped_s(LegacyProtocolPacketBased* protocol, StreamStatus status);

#if FIBRE_ALLOW_HEAP
std::unordered_map<std::string, fibre::ChannelDiscoveryContext*> channel_discovery_handles;
Expand Down Expand Up @@ -205,7 +206,7 @@ void close(Context*);
* If Fibre is compiled with FIBRE_ENABLE_TEXT_LOGGING=1 this function logs the
* event to stderr. Otherwise it does nothing.
*/
void log_to_stderr(const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text);
void log_to_stderr(void* ctx, const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text);

LogLevel get_log_verbosity();

Expand Down
5 changes: 3 additions & 2 deletions cpp/include/fibre/libfibre.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ struct LibFibreLogger {
int verbosity;

// TODO: document (see fibre.hpp for now)
void(*log)(const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text);
void(*log)(void* ctx, const char* file, unsigned line, int level, uintptr_t info0, uintptr_t info1, const char* text);
void* ctx;
};

/**
Expand Down Expand Up @@ -361,7 +362,7 @@ FIBRE_PUBLIC void libfibre_close_domain(LibFibreDomain* domain);
*
* The channels can be closed with libfibre_close_tx() and libfibre_close_rx().
*/
FIBRE_PUBLIC void libfibre_add_channels(LibFibreDomain* domain, LibFibreRxStream** tx_channel, LibFibreTxStream** rx_channel, size_t mtu);
FIBRE_PUBLIC void libfibre_add_channels(LibFibreDomain* domain, LibFibreRxStream** tx_channel, LibFibreTxStream** rx_channel, size_t mtu, bool packetized);

/**
* @brief Starts looking for Fibre objects that match the specifications.
Expand Down
Loading

0 comments on commit 69f8681

Please sign in to comment.