Skip to content

Commit

Permalink
Import and remake of the aware project
Browse files Browse the repository at this point in the history
  • Loading branch information
breese committed Dec 29, 2017
0 parents commit 4842404
Show file tree
Hide file tree
Showing 65 changed files with 4,864 additions and 0 deletions.
66 changes: 66 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
###############################################################################
#
# Copyright (C) 2017 Bjorn Reese <[email protected]>
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
###############################################################################

cmake_minimum_required(VERSION 3.0)
project(trial.aware)

option(WITH_DNSSD "Build with DNS-SD" TRUE)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")

set(TRIAL_AWARE_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set(TRIAL_AWARE_BUILD_DIR ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${TRIAL_AWARE_BUILD_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${TRIAL_AWARE_BUILD_DIR}/bin)

set(CMAKE_CXX_STANDARD 11)

if (MSVC)
set(TRIAL_AWARE_WARNING_FLAGS /W4)
else()
set(TRIAL_AWARE_WARNING_FLAGS -Wall -Wextra -pedantic)
endif()

###############################################################################
# Boost package
###############################################################################

find_package(Boost 1.55.0 REQUIRED QUIET COMPONENTS system)

###############################################################################
# DNS-SD package
###############################################################################

if (WITH_DNSSD)
find_package(DNSSD REQUIRED)
if (TARGET DNSSD::Avahi)
message(STATUS "DNS-SD backend: Avahi")
elseif (TARGET DNSSD::mDNSResponder)
message(STATUS "DNS-SD backend: mDNSResponder")
else()
message(FATAL_ERROR "DNS-SD backend: Not found")
endif()
endif()

###############################################################################
# Trial.Aware package
###############################################################################

include_directories(${TRIAL_AWARE_ROOT}/include)
add_subdirectory(src)

###############################################################################
# Examples
###############################################################################

if (WITH_DNSSD)
add_subdirectory(example/dnssd EXCLUDE_FROM_ALL)
add_custom_target(example DEPENDS dnssd-example)
endif()
23 changes: 23 additions & 0 deletions LICENSE_1_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions cmake/FindAvahi.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# FindAvahi
#
# Find Avahi
#
# ::
# Avahi_INCLUDE_DIR
# Avahi_LIBRARY
# Avahi_FOUND
#
# Defines DNSSD::Avahi imported target

find_package(PkgConfig QUIET REQUIRED)
pkg_check_modules(Avahi QUIET avahi-client)

if (Avahi_FOUND)
find_library(Avahi_COMMON_LIBRARY NAMES avahi-common PATHS ${Avahi_LIBDIR})
find_library(Avahi_CLIENT_LIBRARY NAMES avahi-client PATHS ${Avahi_LIBDIR})

if (NOT TARGET DNSSD::AvahiCommon)
add_library(DNSSD::AvahiCommon SHARED IMPORTED GLOBAL)
set_target_properties(DNSSD::AvahiCommon PROPERTIES
IMPORTED_LOCATION "${Avahi_COMMON_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${Avahi_CFLAGS}"
INTERFACE_INCLUDE_DIRECTORIES "${Avahi_INCLUDE_DIR}"
)
endif()
if (NOT TARGET DNSSD::Avahi)
add_library(DNSSD::Avahi SHARED IMPORTED GLOBAL)
set_target_properties(DNSSD::Avahi PROPERTIES
IMPORTED_LOCATION "${Avahi_CLIENT_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${Avahi_CFLAGS}"
INTERFACE_INCLUDE_DIRECTORIES "${Avahi_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES DNSSD::AvahiCommon
)
endif()

elseif(Avahi_FIND_REQUIRED)
if (NOT Avahi_FIND_QUIETLY)
message(FATAL_ERROR "Avahi package not found")
endif()
endif()
27 changes: 27 additions & 0 deletions cmake/FindDNSSD.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# FindDNSSD
#
# Find DNS Service Discovery backend

option(DNSSD_WITH_AVAHI "Check for Avahi" ON)
set(DNSSD_WITH_MDNSRESPONDER "" CACHE PATH "Check for mDNSResponder using build directory")

if (APPLE)
# Included in libSystem
set(DNSSD_FOUND TRUE)
# FIXME: Create library alias instead

elseif(WIN32)
# FIXME

else() # Posix
if (DNSSD_WITH_AVAHI)
find_package(Avahi QUIET)
endif()

if (NOT DNSSD_FOUND AND DNSSD_WITH_MDNSRESPONDER)
if (NOT DNSSD_WITH_MDNSRESPONDER STREQUAL "")
set(mDNSResponder_BUILD_DIR ${DNSSD_WITH_MDNSRESPONDER})
endif()
find_package(mDNSResponder QUIET)
endif()
endif()
80 changes: 80 additions & 0 deletions cmake/FindmDNSResponder.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# FindmDNSResponder
#
# Find Apple mDNSResponder
#
# ::
# mDNSResponder_INCLUDE_DIR
# mDNSResponder_LIBRARY
# mDNSResponder_FOUND
#
# mDNSResponder_LIBRARIES
# mDNSResponder_INCLUDE_DIRS
#
# Defines DNSSD::mDNSResponder imported target

if (mDNSResponder_BUILD_DIR)
set(mDNSResponder_INCDIR "${mDNSResponder_BUILD_DIR}/mDNSShared")
set(mDNSResponder_LIBDIR "${mDNSResponder_BUILD_DIR}/mDNSPosix/build/prod")
endif()

find_path(mDNSResponder_INCLUDE_DIR
NAMES dns_sd.h
PATHS "${mDNSResponder_INCDIR}")

find_library(mDNSResponder_LIBRARY
NAMES dns_sd
PATHS "${mDNSResponder_LIBDIR}"
)

set(mDNSResponder_FOUND FALSE CACHE BOOL "" FORCE)
if (mDNSResponder_INCLUDE_DIR AND mDNSResponder_LIBRARY)
if (EXISTS ${mDNSResponder_INCLUDE_DIR}/dns_sd.h)
file(STRINGS ${mDNSResponder_INCLUDE_DIR}/dns_sd.h HEADER_FILE NEWLINE_CONSUME)
string(REGEX MATCH "#define _DNS_SD_H[^\n]*" VERSION_LINE ${HEADER_FILE})
string(REGEX REPLACE
"#define _DNS_SD_H[ \t]*([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9]).*"
"\\1.\\2.\\3"
mDNSResponder_VERSION
${VERSION_LINE})
if ("${mDNSResponder_VERSION}" STREQUAL "${VERSION_LINE}")
set(mDNSResponder_VERSION 0)
else()
set(mDNSResponder_FOUND TRUE CACHE BOOL "" FORCE)
endif()
endif()
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(mDNSResponder
FOUND_VAR mDNSResponder_FOUND
REQUIRED_VARS mDNSResponder_LIBRARY mDNSResponder_INCLUDE_DIR
VERSION_VAR mDNSResponder_VERSION
)

if(mDNSResponder_FOUND)
set(mDNSResponder_LIBRARIES ${mDNSResponder_LIBRARY})
set(mDNSResponder_INCLUDE_DIRS ${mDNSResponder_INCLUDE_DIR})
set(mDNSResponder_DEFINITIONS "")

if (NOT TARGET DNSSD::mDNSResponder)
add_library(DNSSD::mDNSResponder SHARED IMPORTED GLOBAL)
set_target_properties(DNSSD::mDNSResponder PROPERTIES
IMPORTED_LOCATION "${mDNSResponder_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${mDNSResponder_DEFINITIONS}"
INTERFACE_INCLUDE_DIRECTORIES "${mDNSResponder_INCLUDE_DIR}"
)
endif()

elseif(mDNSResponder_FIND_REQUIRED)
if (NOT mDNSResponder_FIND_QUIETLY)
message(FATAL_ERROR "mDNSResponder package not found")
endif()
endif()

mark_as_advanced(
mDNSResponder_FOUND
mDNSResponder_VERSION
mDNSResponder_INCLUDE_DIR
mDNSResponder_LIBRARY
mDNSResponder_INCDIR
mDNSResponder_LIBDIR)
11 changes: 11 additions & 0 deletions example/dnssd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_executable(zannounce
zannounce.cpp
)
target_link_libraries(zannounce aware)

add_executable(zmonitor
zmonitor.cpp
)
target_link_libraries(zmonitor aware)

add_custom_target(dnssd-example DEPENDS zannounce zmonitor)
49 changes: 49 additions & 0 deletions example/dnssd/zannounce.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018 Bjorn Reese <[email protected]>
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
///////////////////////////////////////////////////////////////////////////////

#include <string>
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <trial/aware/contact.hpp>
#include <trial/aware/dnssd/factory.hpp>

int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cerr << "Usage: " << argv[0] << " <type> <name>" << std::endl;
return 1;
}

trial::aware::contact::properties_type properties;
properties["key"] = "value";
auto contact = trial::aware::contact(argv[1])
.name(argv[2])
.address(boost::asio::ip::address())
.port(3834)
.properties(properties);

boost::asio::io_service io;
trial::aware::dnssd::factory factory;
auto announcer = factory.make_announce(io);
announcer->async_announce(
contact,
[&contact] (const boost::system::error_code& error)
{
std::cout << "Announced: " << error.message() << std::endl;
if (!error)
{
std::cout << contact.type() << " = " << contact.name() << std::endl;
}
});
io.run();
return 0;
}
93 changes: 93 additions & 0 deletions example/dnssd/zmonitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018 Bjorn Reese <[email protected]>
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
///////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <functional>
#include <utility>
#include <boost/asio.hpp>
#include <trial/aware/contact.hpp>
#include <trial/aware/monitor_socket.hpp>
#include <trial/aware/dnssd/factory.hpp>

class my_monitor
{
public:
my_monitor(boost::asio::io_service& io,
trial::aware::factory& factory)
: socket(factory.make_monitor(io))
{}

void async_monitor(trial::aware::contact& contact)
{
socket->async_monitor(contact,
std::bind(&my_monitor::process_monitor,
this,
std::placeholders::_1,
std::ref(contact)));
}

private:
void process_monitor(const boost::system::error_code& error,
trial::aware::contact& contact)
{
if (!error)
{
if (contact.empty())
{
std::cout << "Removed:" << std::endl;
std::cout << " type = " << contact.type() << std::endl;
std::cout << " name = " << contact.name() << std::endl;
}
else
{
std::cout << "Added:" << std::endl;
std::cout << " type = " << contact.type() << std::endl;
std::cout << " name = " << contact.name() << std::endl;
std::cout << " endpoint = " << contact.address().to_string() << ":" << contact.port() << std::endl;
auto properties = contact.properties();
for (const auto& property : properties)
{
std::cout << " " << property.first << " = " << property.second << std::endl;
}
}
// Launch the next monitor operation
async_monitor(contact);
}
else if (error == boost::asio::error::operation_aborted)
{
// Ignore
}
else
{
std::cout << "Error = " << error << std::endl;
}
}

private:
std::unique_ptr<trial::aware::monitor_socket> socket;
};

int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <type>" << std::endl;
return 1;
}
boost::asio::io_service io;
trial::aware::dnssd::factory factory;
my_monitor monitor(io, factory);

trial::aware::contact contact(argv[1]);
monitor.async_monitor(contact);
io.run();

return 0;
}
Loading

0 comments on commit 4842404

Please sign in to comment.