Skip to content

Commit

Permalink
Implement tox_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansa89 committed Apr 1, 2017
1 parent 200ee1c commit 5ff0997
Show file tree
Hide file tree
Showing 21 changed files with 852 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ addons:
packages:
- check
- libcv-dev # For av_test.
- libev-dev # For tox_loop.
- libevent-dev # For tox_loop.
- libhighgui-dev # For av_test.
- libopencv-contrib-dev # For av_test.
- libsndfile1-dev # For av_test.
Expand Down
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ if (BUILD_TOXAV)
endif()
endif()

if(LIBEV_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LIBEV")
elseif(LIBEVENT_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LIBEVENT")
endif()

################################################################################
#
# :: Tox Core Library
Expand Down Expand Up @@ -234,6 +240,18 @@ if(RT_LIBRARIES)
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} "-lrt")
endif()

if(LIBEV_FOUND)
target_link_modules(toxnetwork ${LIBEV_LIBRARIES})
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} "-lev")
elseif(LIBEVENT_FOUND)
target_link_modules(toxnetwork ${LIBEVENT_LIBRARIES})
if(NOT WIN32)
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} "-levent -levent_pthreads")
else()
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} "-levent")
endif()
endif()

if(WIN32)
target_link_modules(toxnetwork ws2_32 iphlpapi)
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} "-lws2_32 -liphlpapi")
Expand Down Expand Up @@ -425,7 +443,7 @@ auto_test(resource_leak)
auto_test(save_friend)
auto_test(skeleton)
auto_test(tox)
auto_test(tox_loop_test)
auto_test(tox_loop)
auto_test(tox_many)
auto_test(tox_many_tcp)
auto_test(tox_one)
Expand Down
11 changes: 9 additions & 2 deletions auto_tests/Makefile.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if BUILD_TESTS

TESTS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest tox_strncasecmp_test
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest tox_strncasecmp_test
TESTS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_loop_test tox_test dht_autotest tox_strncasecmp_test
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_loop_test tox_test dht_autotest tox_strncasecmp_test

AUTOTEST_CFLAGS = \
$(LIBSODIUM_CFLAGS) \
Expand Down Expand Up @@ -61,6 +61,13 @@ TCP_test_CFLAGS = $(AUTOTEST_CFLAGS)
TCP_test_LDADD = $(AUTOTEST_LDADD)


tox_loop_test_SOURCES = ../auto_tests/tox_loop_test.c

tox_loop_test_CFLAGS = $(AUTOTEST_CFLAGS)

tox_loop_test_LDADD = $(AUTOTEST_LDADD)


tox_test_SOURCES = ../auto_tests/tox_test.c

tox_test_CFLAGS = $(AUTOTEST_CFLAGS)
Expand Down
61 changes: 29 additions & 32 deletions auto_tests/tox_loop_test.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@


#include "helpers.h"

#include "../toxcore/tox.h"

#include <check.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "../toxcore/tox.h"

#include "helpers.h"

#define TCP_RELAY_PORT 33448
/* The Travis-CI container responds poorly to ::1 as a localhost address
* You're encouraged to -D FORCE_TESTS_IPV6 on a local test */
Expand All @@ -19,82 +17,81 @@
#define TOX_LOCALHOST "127.0.0.1"
#endif

struct loop_test {
typedef struct {
int start_count, stop_count;
pthread_mutex_t mutex;
Tox *tox;
};
} loop_test;

void tox_loop_cb_start(Tox *tox, void *user_data)
{
struct loop_test *userdata = user_data;
loop_test *userdata = (loop_test *) user_data;
pthread_mutex_lock(&userdata->mutex);
userdata->start_count++;
}

void tox_loop_cb_stop(Tox *tox, void *user_data)
{
struct loop_test *userdata = user_data;
loop_test *userdata = (loop_test *) user_data;
userdata->stop_count++;
pthread_mutex_unlock(&userdata->mutex);
}

void *tox_loop_worker(void *data)
{
struct loop_test *userdata = data;
int retval = tox_loop(userdata->tox, data);
return (void *)retval;
loop_test *userdata = (loop_test *) data;
tox_loop(userdata->tox, data, NULL);
return NULL;
}

START_TEST(test_tox_loop)
{
pthread_t worker1, worker2;
struct Tox_Options opts;
struct loop_test userdata;
pthread_t worker, worker_tcp;
struct Tox_Options *opts = tox_options_new(NULL);
loop_test userdata;
uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
int retval;

userdata.start_count = 0;
userdata.stop_count = 0;
pthread_mutex_init(&userdata.mutex, NULL);

tox_options_default(&opts);
opts.tcp_port = TCP_RELAY_PORT;
userdata.tox = tox_new(&opts, 0);
tox_options_set_tcp_port(opts, TCP_RELAY_PORT);
userdata.tox = tox_new(opts, NULL);
tox_callback_loop_begin(userdata.tox, tox_loop_cb_start);
tox_callback_loop_end(userdata.tox, tox_loop_cb_stop);
pthread_create(&worker1, NULL, tox_loop_worker, &userdata);
pthread_create(&worker, NULL, tox_loop_worker, &userdata);

tox_self_get_dht_id(userdata.tox, dpk);

tox_options_default(&opts);
struct loop_test userdata_tcp;
tox_options_default(opts);
loop_test userdata_tcp;
userdata_tcp.start_count = 0;
userdata_tcp.stop_count = 0;
pthread_mutex_init(&userdata_tcp.mutex, NULL);
userdata_tcp.tox = tox_new(&opts, 0);
userdata_tcp.tox = tox_new(opts, NULL);
tox_callback_loop_begin(userdata_tcp.tox, tox_loop_cb_start);
tox_callback_loop_end(userdata_tcp.tox, tox_loop_cb_stop);
pthread_create(&worker2, NULL, tox_loop_worker, &userdata_tcp);
pthread_create(&worker_tcp, NULL, tox_loop_worker, &userdata_tcp);

pthread_mutex_lock(&userdata_tcp.mutex);
TOX_ERR_BOOTSTRAP error = 0;
ck_assert_msg(tox_add_tcp_relay(userdata_tcp.tox, TOX_LOCALHOST, TCP_RELAY_PORT, dpk, &error), "add relay error, %i",
TOX_ERR_BOOTSTRAP error;
ck_assert_msg(tox_add_tcp_relay(userdata_tcp.tox, TOX_LOCALHOST, TCP_RELAY_PORT, dpk, &error), "Add relay error, %i",
error);
ck_assert_msg(tox_bootstrap(userdata_tcp.tox, TOX_LOCALHOST, 33445, dpk, 0), "Bootstrap error");
ck_assert_msg(tox_bootstrap(userdata_tcp.tox, TOX_LOCALHOST, 33445, dpk, &error), "Bootstrap error, %i", error);
pthread_mutex_unlock(&userdata_tcp.mutex);

sleep(10);

tox_loop_stop(userdata.tox);
pthread_join(worker1, (void **)&retval);
pthread_join(worker, (void **)&retval);
ck_assert_msg(retval == 0, "tox_loop didn't return 0");

tox_kill(userdata.tox);
ck_assert_msg(userdata.start_count == userdata.stop_count, "start and stop must match");

tox_loop_stop(userdata_tcp.tox);
pthread_join(worker2, (void **)&retval);
pthread_join(worker_tcp, (void **)&retval);
ck_assert_msg(retval == 0, "tox_loop didn't return 0");

tox_kill(userdata_tcp.tox);
Expand All @@ -103,9 +100,9 @@ START_TEST(test_tox_loop)
END_TEST

#ifdef TRAVIS_ENV
uint8_t timeout_mux = 20;
static uint8_t timeout_mux = 20;
#else
uint8_t timeout_mux = 10;
static uint8_t timeout_mux = 10;
#endif

static Suite *tox_suite(void)
Expand Down
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
- sudo apt-get install clang
- sudo apt-get install build-essential libtool autotools-dev automake checkinstall check git yasm
- sudo apt-get install libopus-dev libvpx-dev pkg-config
- sudo apt-get install libev-dev libevent-dev

# ------------ network_test requires that "localhost" resolves to ::1 ------------
- sudo bash -c "echo '::1 localhost ipv6-localhost ipv6-loopback' >> /etc/hosts" # ipv6 localhost entry
Expand Down
32 changes: 20 additions & 12 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,38 @@ include(ModulePackage)

find_package(Threads REQUIRED)

find_library(NCURSES_LIBRARIES ncurses )
find_library(UTIL_LIBRARIES util )
find_library(RT_LIBRARIES rt )
find_library(NCURSES_LIBRARIES ncurses )
find_library(UTIL_LIBRARIES util )
find_library(RT_LIBRARIES rt )

# For toxcore.
pkg_use_module(LIBSODIUM libsodium )
pkg_use_module(LIBSODIUM libsodium )
pkg_use_module(LIBEV ev )
if(NOT LIBEV_FOUND)
if(NOT WIN32)
pkg_use_module(LIBEVENT libevent_pthreads )
else()
pkg_use_module(LIBEVENT libevent )
endif()
endif()

# For toxav.
pkg_use_module(OPUS opus )
pkg_use_module(VPX vpx )
pkg_use_module(OPUS opus )
pkg_use_module(VPX vpx )

# For tox-bootstrapd.
pkg_use_module(LIBCONFIG libconfig )
pkg_use_module(LIBCONFIG libconfig )

# For auto tests.
pkg_use_module(CHECK check )
pkg_use_module(CHECK check )

# For tox-spectest.
pkg_use_module(MSGPACK msgpack )
pkg_use_module(MSGPACK msgpack )

# For av_test.
pkg_use_module(OPENCV opencv )
pkg_use_module(PORTAUDIO portaudio-2.0)
pkg_use_module(SNDFILE sndfile )
pkg_use_module(OPENCV opencv )
pkg_use_module(PORTAUDIO portaudio-2.0 )
pkg_use_module(SNDFILE sndfile )

###############################################################################
#
Expand Down
27 changes: 27 additions & 0 deletions cmake/ModulePackage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ option(ENABLE_SHARED "Build shared (dynamic) libraries for all modules" ON)
option(ENABLE_STATIC "Build static libraries for all modules" ON)
option(COMPILE_AS_CXX "Compile all C code as C++ code" OFF)

include(FindPackageHandleStandardArgs)

if(NOT ENABLE_SHARED AND NOT ENABLE_STATIC)
message(WARNING
"Both static and shared libraries are disabled; "
Expand Down Expand Up @@ -48,6 +50,31 @@ function(pkg_use_module mod pkg)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem ${dir}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${dir}" PARENT_SCOPE)
endforeach()
else()
set(${mod}_DEFINITIONS ${${mod}_CFLAGS_OTHER})
find_path(${mod}_INCLUDE_DIR NAMES ${ARGV1}.h
HINTS ${${mod}_INCLUDEDIR} ${${mod}_INCLUDE_DIRS}
PATH_SUFFIXES ${ARGV1})
find_library(${mod}_LIBRARY NAMES ${ARGV1} lib${ARGV1}
HINTS ${${mod}_LIBDIR} ${${mod}_LIBRARY_DIRS})
find_package_handle_standard_args(${mod} DEFAULT_MSG
${mod}_LIBRARY ${mod}_INCLUDE_DIR)

if(${mod}_FOUND)
mark_as_advanced(${mod}_INCLUDE_DIR ${mod}_LIBRARY)
set(${mod}_LIBRARIES ${${mod}_LIBRARY} PARENT_SCOPE)
set(${mod}_INCLUDE_DIRS ${${mod}_INCLUDE_DIR} PARENT_SCOPE)
set(${mod}_FOUND TRUE PARENT_SCOPE)
link_directories(${${mod}_LIBRARY_DIRS})
include_directories(${${mod}_INCLUDE_DIRS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${${mod}_CFLAGS_OTHER}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${${mod}_CFLAGS_OTHER}" PARENT_SCOPE)

foreach(dir ${${mod}_INCLUDE_DIRS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem ${dir}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${dir}" PARENT_SCOPE)
endforeach()
endif()
endif()
endfunction()

Expand Down
Loading

0 comments on commit 5ff0997

Please sign in to comment.