Skip to content

Commit

Permalink
Merge pull request #24 from Pesa/code-cleanups
Browse files Browse the repository at this point in the history
Assorted code cleanups
  • Loading branch information
pulsejet authored Dec 8, 2023
2 parents 3e48741 + 3282ce2 commit d31e0df
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 161 deletions.
8 changes: 4 additions & 4 deletions .jenkins.d/10-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ if [[ $JOB_NAME != *"code-coverage" && $JOB_NAME != *"limited-build" ]]; then
# Cleanup
./waf --color=yes distclean

# Build in release mode without tests
./waf --color=yes configure
# Build in release mode with examples
./waf --color=yes configure --with-examples --with-compression
./waf --color=yes build

# Cleanup
./waf --color=yes distclean
fi

# Build in debug mode with tests and examples
./waf --color=yes configure --debug --with-tests --with-examples $ASAN $COVERAGE
# Build in debug mode with tests
./waf --color=yes configure --debug --with-tests $ASAN $COVERAGE
./waf --color=yes build

# (tests will be run against the debug version)
Expand Down
40 changes: 21 additions & 19 deletions examples/chat-pubsub.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012-2022 University of California, Los Angeles
* Copyright (c) 2012-2023 University of California, Los Angeles
*
* This file is part of ndn-svs, synchronization library for distributed realtime
* applications for NDN.
Expand All @@ -14,12 +14,13 @@
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*/

#include <ctime>
#include <functional>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <functional>
#include <ctime>

#include <ndn-svs/svspubsub.hpp>

using namespace ndn::svs;
Expand All @@ -33,7 +34,7 @@ struct Options
class Program
{
public:
Program(const Options &options)
Program(const Options& options)
: m_options(options)
{
// Use HMAC signing for Sync Interests
Expand All @@ -46,8 +47,8 @@ class Program

// Do not fetch publications older than 10 seconds
SVSPubSubOptions opts;
opts.UseTimestamp = true;
opts.MaxPubAge = ndn::time::milliseconds(10000);
opts.useTimestamp = true;
opts.maxPubAge = ndn::time::seconds(10);

// Create the Pub/Sub instance
m_svsps = std::make_shared<SVSPubSub>(
Expand All @@ -58,7 +59,7 @@ class Program
opts,
secOpts);

std::cout << "SVS client starting:" << m_options.m_id << std::endl;
std::cout << "SVS client starting: " << m_options.m_id << std::endl;

// Subscribe to all data packets with prefix /chat (the "topic")
m_svsps->subscribe(ndn::Name("/chat"), [] (const auto& subData)
Expand All @@ -79,23 +80,22 @@ class Program
void
run()
{
// Begin processing face events in a separate thread
std::thread thread_svs([this] { face.processEvents(); });
// Begin processing face events in a separate thread.
std::thread svsThread([this] { face.processEvents(); });

// Announce our presence.
// Note that the SVS-PS instance is thread-safe
std::string init_msg = "User " + m_options.m_id + " has joined the groupchat";
publishMsg(init_msg);
// Note that the SVS-PS instance is thread-safe.
publishMsg("User " + m_options.m_id + " has joined the groupchat");

// Read from stdin and publish messages
std::string userInput = "";
// Read from stdin and publish messages.
std::string userInput;
while (true) {
std::getline(std::cin, userInput);
publishMsg(userInput);
}

// Wait for the SVS-PS thread to finish
thread_svs.join();
// Wait for the SVS-PS thread to finish.
svsThread.join();
}

protected:
Expand Down Expand Up @@ -148,11 +148,12 @@ class Program
ndn::KeyChain m_keyChain;
};

int main(int argc, char **argv)
int
main(int argc, char** argv)
{
if (argc != 2) {
std::cout << "Usage: client <prefix>" << std::endl;
exit(1);
std::cerr << "Usage: " << argv[0] << " <prefix>" << std::endl;
return 1;
}

Options opt;
Expand All @@ -161,5 +162,6 @@ int main(int argc, char **argv)

Program program(opt);
program.run();

return 0;
}
58 changes: 30 additions & 28 deletions examples/chat.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012-2021 University of California, Los Angeles
* Copyright (c) 2012-2023 University of California, Los Angeles
*
* This file is part of ndn-svs, synchronization library for distributed realtime
* applications for NDN.
Expand All @@ -16,6 +16,7 @@

#include <iostream>
#include <string>
#include <string_view>
#include <thread>
#include <vector>

Expand All @@ -30,7 +31,8 @@ struct Options
class Program
{
public:
Program(const Options &options) : m_options(options)
Program(const Options& options)
: m_options(options)
{
// Use HMAC signing for Sync Interests
// Note: this is not generally recommended, but is used here for simplicity
Expand All @@ -39,35 +41,34 @@ class Program

// Create the SVSync instance
m_svs = std::make_shared<ndn::svs::SVSync>(
ndn::Name(m_options.prefix), // Sync prefix, common for all nodes in the group
ndn::Name(m_options.m_id), // Unique data prefix for this node
face, // Shared NDN face
std::bind(&Program::onMissingData, this, _1), // Callback on learning new sequence numbers from SVS
securityOptions); // Security configuration
ndn::Name(m_options.prefix), // Sync prefix, common for all nodes in the group
ndn::Name(m_options.m_id), // Unique data prefix for this node
face, // Shared NDN face
std::bind(&Program::onMissingData, this, _1), // Callback on learning new sequence numbers from SVS
securityOptions); // Security configuration

std::cout << "SVS client starting:" << m_options.m_id << std::endl;
std::cout << "SVS client starting: " << m_options.m_id << std::endl;
}

void
run()
{
// Begin processing face events in a separate thread
std::thread thread_svs([this] { face.processEvents(); });
// Begin processing face events in a separate thread.
std::thread svsThread([this] { face.processEvents(); });

// Announce our presence.
// Note that the SVSync instance is thread-safe
std::string init_msg = "User " + m_options.m_id + " has joined the groupchat";
publishMsg(init_msg);
// Note that the SVSync instance is thread-safe.
publishMsg("User " + m_options.m_id + " has joined the groupchat");

// Read from stdin and publish messages
std::string userInput = "";
// Read from stdin and publish messages.
std::string userInput;
while (true) {
std::getline(std::cin, userInput);
publishMsg(userInput);
}

// Wait for the SVSync thread to finish on exit
thread_svs.join();
// Wait for the SVSync thread to finish on exit.
svsThread.join();
}

protected:
Expand All @@ -85,12 +86,12 @@ class Program
{
// Request a single data packet using the SVSync API
ndn::svs::NodeID nid = v[i].nodeId;
m_svs->fetchData(nid, s, [nid] (const ndn::Data& data)
{
const std::string content(reinterpret_cast<const char*>(data.getContent().value()),
data.getContent().value_size());
std::cout << data.getName() << " : " << content << std::endl;
});
m_svs->fetchData(nid, s, [nid] (const auto& data)
{
std::string content(reinterpret_cast<const char*>(data.getContent().value()),
data.getContent().value_size());
std::cout << data.getName() << " : " << content << std::endl;
});
}
}
}
Expand All @@ -99,11 +100,11 @@ class Program
* Publish a string message to the SVSync group
*/
void
publishMsg(const std::string& msg)
publishMsg(std::string_view msg)
{
// Encode the message into a Content TLV block, which is what the SVSync API expects
auto block = ndn::encoding::makeStringBlock(ndn::tlv::Content, msg);
m_svs->publishData(block, ndn::time::milliseconds(1000));
m_svs->publishData(block, ndn::time::seconds(1));
}

public:
Expand All @@ -114,11 +115,11 @@ class Program
};

int
main(int argc, char **argv)
main(int argc, char** argv)
{
if (argc != 2) {
std::cout << "Usage: client <prefix>" << std::endl;
exit(1);
std::cerr << "Usage: " << argv[0] << " <prefix>" << std::endl;
return 1;
}

Options opt;
Expand All @@ -127,5 +128,6 @@ main(int argc, char **argv)

Program program(opt);
program.run();

return 0;
}
Loading

0 comments on commit d31e0df

Please sign in to comment.