Skip to content

Commit

Permalink
programs: pingclient
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jun 20, 2020
1 parent 4fa7957 commit 603ab0e
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 16 deletions.
18 changes: 9 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ install:
script:
- cd $TRAVIS_BUILD_DIR && mk/update-list.sh
- meson build.coverage -Db_coverage=true -Dwerror=true -Dunittest=enabled
- meson build.coverage -Db_coverage=true -Dwerror=true -Dunittest=enabled -Dprograms=disabled
- ninja -C build.coverage test
- |
travis_fold start "build.coverage/testlog"
travis_fold start "coverage-testlog"
cat build.coverage/meson-logs/testlog.txt
travis_fold end "build.coverage/testlog"
- meson build.sanitize -Db_sanitize=address,undefined -Dunittest=enabled
travis_fold end "coverage-testlog"
- meson build.sanitize -Db_sanitize=address,undefined -Dunittest=enabled -Dprograms=enabled
- ninja -C build.sanitize test
- |
travis_fold start "build.sanitize/testlog"
travis_fold start "sanitize-testlog"
cat build.sanitize/meson-logs/testlog.txt
travis_fold end "build.sanitize/testlog"
- LDFLAGS='-fuse-ld=lld -L/usr/local/lib' meson build.llvm --native-file mk/native-llvm.txt --buildtype debugoptimized -Dunittest=enabled
travis_fold end "sanitize-testlog"
- LDFLAGS='-fuse-ld=lld -L/usr/local/lib' meson build.llvm --native-file mk/native-llvm.txt --buildtype debugoptimized -Dunittest=enabled -Dprograms=enabled
- ninja -C build.llvm test
- |
travis_fold start "build.llvm/testlog"
travis_fold start "llvm-testlog"
cat build.llvm/meson-logs/testlog.txt
travis_fold end "build.llvm/testlog"
travis_fold end "llvm-testlog"
- build_platform esp8266
- build_platform esp32
- build_platform nrf52840
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

**NDNph** provides [Named Data Networking](https://named-data.net/) packet encoding and more in a header-only C++11 library. It is part of [esp8266ndn](https://github.com/yoursunny/esp8266ndn) that supports microcontroller programming in Arduino IDE. NDNph can also work independently on Linux and other platforms.

* [Doxygen documentation](https://esp8266ndn.netlify.com/) together with esp8266ndn
* [Doxygen documentation](https://esp8266ndn.ndn.today/) together with esp8266ndn
* [#NDNph on Twitter](https://twitter.com/hashtag/NDNph) for announcements
* [ndn-lib mailing list](https://www.lists.cs.ucla.edu/mailman/listinfo/ndn-lib) for best-effort support

Expand Down
7 changes: 5 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# NDNph Examples

This examples directory is to allow Arduino IDE to locate the unit tests.
For examples on how to use NDNph, please see [esp8266ndn](https://github.com/yoursunny/esp8266ndn) library.
This examples directory is to allow running unit tests in Arduino IDE.
Examples on how to use NDNph in a program can be found in:

* [../programs](programs) directory: NDNph on Linux
* [esp8266ndn](https://github.com/yoursunny/esp8266ndn) library: NDNph on microcontrollers
9 changes: 9 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ project('NDNph', 'cpp',
])
subdir('src')
subdir('tests')

programs_option = get_option('programs')
if not programs_option.disabled()
if has_all_linux_deps
subdir('programs')
elif programs_option.enabled()
error('programs enabled but is missing dependency')
endif
endif
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
option('unittest', type: 'feature')
option('programs', type: 'feature')
3 changes: 3 additions & 0 deletions programs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NDNph Linux Examples

This directory contains several self-contained programs using NDNph.
1 change: 1 addition & 0 deletions programs/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
executable('ndnph-pingclient', 'pingclient.cpp', dependencies: [lib_dep])
63 changes: 63 additions & 0 deletions programs/pingclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <NDNph-config.h>
#include <NDNph.h>

#include <cinttypes>
#include <cstdio>
#include <unistd.h>

ndnph::UdpUnicastTransport transport;
ndnph::Face face(transport);

std::unique_ptr<ndnph::PingClient> client;

static bool
parseArgs(int argc, char** argv)
{
int interval = 1000;

int c;
while ((c = getopt(argc, argv, "i:")) != -1) {
switch (c) {
case 'i': {
interval = atoi(optarg);
if (interval <= 0 || interval > 60000) {
return false;
}
break;
}
}
}

if (argc - optind != 1) {
return false;
}
const char* prefix = argv[optind];

static ndnph::StaticRegion<1024> prefixRegion;
client.reset(new ndnph::PingClient(ndnph::Name::parse(prefixRegion, prefix), face, interval));
return true;
}

int
main(int argc, char** argv)
{
if (!parseArgs(argc, argv)) {
fprintf(stderr, "ndnph-pingclient [-i INTERVAL] PREFIX\n"
" PREFIX should have 'ping' suffix to interact with ndn-tools ndnpingserver\n"
" INTERVAL must be between 1 and 60000 milliseconds\n"
" INTERVAL should be no less than RTT, or all requests will timeout\n");
return 2;
}

transport.beginTunnel({ 127, 0, 0, 1 });
for (;;) {
ndnph::port::Clock::sleep(1);
face.loop();

static uint16_t i = 0;
if (++i % 1024 == 0) {
auto cnt = client->readCounters();
printf("%" PRIu32 "I %" PRIu32 "D\n", cnt.nTxInterests, cnt.nRxData);
}
}
}
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ if not have_this_thread
conf.set('NDNPH_PORT_CHRONO_BUSY_SLEEP', '')
endif

has_all_linux_deps = boost.found() and mbedcrypto.found() and have_urandom and have_socket_h

config_h = configure_file(output: 'NDNph-config.h', configuration: conf)
lib_dep = declare_dependency(
include_directories: include_directories('.'),
Expand Down
10 changes: 8 additions & 2 deletions src/ndnph/app/ping-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

namespace ndnph {

/** @brief Periodically transmit Interests to test reachability. */
/**
* @brief Periodically transmit Interests to test reachability.
*
* This is a simple ping client implementation that can only keep one pending Interest.
* After sending a probe Interest, responses to previous Interests are no longer accepted.
* Therefore, interval must be greater than RTT, otherwise this client cannot receive any Data.
*/
class PingClient : public PacketHandler
{
public:
/**
* @brief Constructor.
* @param prefix name prefix to request.
* @param prefix name prefix to request. It should have 'ping' suffix.
* @param face face for communication.
* @param interval Interest interval in milliseconds.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ndnph/app/ping-server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PingServer : public PacketHandler
public:
/**
* @brief Constructor.
* @param prefix name prefix to serve.
* @param prefix name prefix to serve. It should have 'ping' suffix.
* @param face face for communication.
*/
explicit PingServer(Name prefix, Face& face)
Expand Down
2 changes: 1 addition & 1 deletion tests/unittest/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
gmock = dependency('gmock', required: false)
gtest = dependency('gtest', main: true, required: false)
if gmock.found() and gtest.found() and boost.found() and mbedcrypto.found() and have_urandom and have_socket_h
if gmock.found() and gtest.found() and has_all_linux_deps
unittest_exe = executable('unittest',
unittest_files,
dependencies: [lib_dep, gmock, gtest],
Expand Down

0 comments on commit 603ab0e

Please sign in to comment.