Skip to content

Commit

Permalink
add 2 new event API functions gOpenNamedEvent and gOpenUnnamedEvent
Browse files Browse the repository at this point in the history
gOpenNamedEvent can handle ownership
  • Loading branch information
rex-schilasky committed Oct 23, 2023
1 parent b0b7655 commit 6687670
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 29 deletions.
23 changes: 23 additions & 0 deletions ecal/core/include/ecal/ecal_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,32 @@

#include <ecal/ecal_os.h>
#include <ecal/ecal_eventhandle.h>
#include <ecal/ecal_deprecate.h>

#include <string>

namespace eCAL
{
/**
* @brief Open a named event with ownership.
*
* @param [out] event_ Returned event struct.
* @param event_name_ Event name.
* @param ownership_ Event is owned by the caller and will be destroyed on CloseEvent
*
* @return True if succeeded.
**/
ECAL_API bool gOpenNamedEvent(eCAL::EventHandleT* event_, const std::string& event_name_, bool ownership_);

/**
* @brief Open a named event with ownership.
*
* @param [out] event_ Returned event struct.
*
* @return True if succeeded.
**/
ECAL_API bool gOpenUnnamedEvent(eCAL::EventHandleT* event_);

/**
* @brief Open a named or unnamed event.
*
Expand All @@ -38,6 +60,7 @@ namespace eCAL
*
* @return True if succeeded.
**/
ECAL_DEPRECATE_SINCE_5_13("Use either gOpenNamedEvent or gOpenUnnamedEvent")
ECAL_API bool gOpenEvent(eCAL::EventHandleT* event_, const std::string& event_name_ = "");

/**
Expand Down
73 changes: 65 additions & 8 deletions ecal/core/src/ecal_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/**
* @brief eCAL handle helper class - windows platform
* @brief eCAL handle helper class
**/

#include <ecal/ecal.h>
Expand All @@ -35,12 +35,12 @@

#include "ecal_win_main.h"

namespace eCAL
namespace
{
bool gOpenEvent(EventHandleT* event_, const std::string& event_name_)
bool OpenEvent(eCAL::EventHandleT* event_, const std::string& event_name_)
{
if(event_ == nullptr) return(false);
EventHandleT event;
eCAL::EventHandleT event;
event.name = event_name_;
event.handle = ::CreateEvent(nullptr, false, false, event_name_.c_str());
if(event.handle != nullptr)
Expand All @@ -50,6 +50,25 @@ namespace eCAL
}
return(false);
}
}

namespace eCAL
{
bool gOpenNamedEvent(eCAL::EventHandleT* event_, const std::string& event_name_, bool /*ownership_*/)
{
return OpenEvent(event_, event_name_);
}

bool gOpenUnnamedEvent(eCAL::EventHandleT* event_)
{
return OpenEvent(event_, "");
}

// deprecated
bool gOpenEvent(EventHandleT* event_, const std::string& event_name_)
{
return OpenEvent(event_, event_name_);
}

bool gCloseEvent(const EventHandleT& event_)
{
Expand Down Expand Up @@ -302,9 +321,10 @@ namespace eCAL
class CNamedEvent
{
public:
explicit CNamedEvent(const std::string& name_) :
explicit CNamedEvent(const std::string& name_, bool ownership_) :
m_name(name_ + "_evt"),
m_event(nullptr)
m_event(nullptr),
m_owner(ownership_)
{
m_name = (m_name[0] != '/') ? "/" + m_name : m_name; // make memory file path compatible for all posix systems
m_event = named_event_open(m_name.c_str());
Expand All @@ -318,7 +338,10 @@ namespace eCAL
{
if(m_event == nullptr) return;
named_event_close(m_event);
named_event_destroy(m_name.c_str());
if(m_owner)
{
named_event_destroy(m_name.c_str());
}
}

void set()
Expand Down Expand Up @@ -371,8 +394,42 @@ namespace eCAL

std::string m_name;
named_event_t* m_event;
bool m_owner;
};

bool gOpenNamedEvent(EventHandleT* event_, const std::string& event_name_, bool ownership_)
{
if(event_ == nullptr) return(false);

EventHandleT event;
event.name = event_name_;
event.handle = new CNamedEvent(event.name, ownership_);

if(event.handle != nullptr)
{
*event_ = event;
return true;
}
return false;
}

bool gOpenUnnamedEvent(EventHandleT* event_)
{
if(event_ == nullptr) return(false);

EventHandleT event;
event.name = "";
event.handle = new CEvent();

if(event.handle != nullptr)
{
*event_ = event;
return true;
}
return false;
}

// deprecated
bool gOpenEvent(EventHandleT* event_, const std::string& event_name_)
{
if(event_ == nullptr) return(false);
Expand All @@ -386,7 +443,7 @@ namespace eCAL
}
else
{
event.handle = new CNamedEvent(event.name);
event.handle = new CNamedEvent(event.name, true);
}

if(event.handle != nullptr)
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/ecal_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace eCAL
{
if(m_tdata.is_started) return(0);

gOpenEvent(&m_tdata.event);
gOpenUnnamedEvent(&m_tdata.event);
m_tdata.do_stop = false;
m_tdata.period = period_;
m_tdata.ext_caller = ext_caller_;
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/ecal_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace eCAL
{
const std::string event_name = EVENT_SHUTDOWN_PROC + std::string("_") + std::to_string(process_id_);
EventHandleT event;
if (gOpenEvent(&event, event_name))
if (gOpenNamedEvent(&event, event_name, true))
{
std::cout << "Shutdown local eCAL process " << process_id_ << std::endl;
gSetEvent(event);
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/ecalc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ extern "C"
ECALC_API ECAL_HANDLE eCAL_Event_gOpenEvent(const char* event_name_)
{
eCAL::EventHandleT* event_handle = new eCAL::EventHandleT;
const bool success = eCAL::gOpenEvent(event_handle, event_name_);
const bool success = eCAL::gOpenNamedEvent(event_handle, event_name_, true);
if (success)
{
return(event_handle);
Expand Down
4 changes: 2 additions & 2 deletions ecal/core/src/io/ecal_memfile_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ namespace eCAL
if (m_created) return false;

// open memory file events
gOpenEvent(&m_event_snd, memfile_event_);
gOpenEvent(&m_event_ack, memfile_event_ + "_ack");
gOpenNamedEvent(&m_event_snd, memfile_event_, false);
gOpenNamedEvent(&m_event_ack, memfile_event_ + "_ack", false);

// create memory file access
m_memfile.Create(memfile_name_.c_str(), false);
Expand Down
6 changes: 3 additions & 3 deletions ecal/core/src/io/ecal_memfile_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ namespace eCAL
if (iter == m_event_handle_map.end())
{
SEventHandlePair event_pair;
gOpenEvent(&event_pair.event_snd, event_snd_name);
gOpenEvent(&event_pair.event_ack, event_ack_name);
gOpenNamedEvent(&event_pair.event_snd, event_snd_name, true);
gOpenNamedEvent(&event_pair.event_ack, event_ack_name, true);
m_event_handle_map.insert(std::pair<std::string, SEventHandlePair>(process_id_, event_pair));
return true;
}
Expand All @@ -77,7 +77,7 @@ namespace eCAL
// event was deactivated by a sync timeout in SendSyncEvents
if (!gEventIsValid(iter->second.event_ack))
{
gOpenEvent(&iter->second.event_ack, event_ack_name);
gOpenNamedEvent(&iter->second.event_ack, event_ack_name, true);
}

// Set the ack event to valid again, so we will wait for the subscriber
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/pubsub/ecal_subgate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace eCAL
static const std::string event_name(EVENT_SHUTDOWN_PROC + std::string("_") + std::to_string(Process::GetProcessID()));
if (!gEventIsValid(evt))
{
gOpenEvent(&evt, event_name);
gOpenNamedEvent(&evt, event_name, true);
}
return(evt);
}
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/misc/event_rec/src/event_rec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int /*argc*/, char** /*argv*/)

// create named event
eCAL::EventHandleT event_handle;
eCAL::gOpenEvent(&event_handle, event_name);
eCAL::gOpenNamedEvent(&event_handle, event_name, false);

// timer
auto start_time(std::chrono::steady_clock::now());
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/misc/event_snd/src/event_snd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int /*argc*/, char** /*argv*/)

// create named event
eCAL::EventHandleT event_handle;
eCAL::gOpenEvent(&event_handle, event_name);
eCAL::gOpenNamedEvent(&event_handle, event_name, true);

// timer
auto start_time(std::chrono::steady_clock::now());
Expand Down
9 changes: 6 additions & 3 deletions testing/ecal/event_test/src/event_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ TEST(Event, EventSetGet)

// create named event
eCAL::EventHandleT event_handle;
EXPECT_EQ(true, eCAL::gOpenEvent(&event_handle, event_name));
EXPECT_EQ(true, eCAL::gOpenNamedEvent(&event_handle, event_name, true));

// get none set event
// wait for event, expect false
EXPECT_EQ(false, gWaitForEvent(event_handle, 10));

// set event
EXPECT_EQ(true, gSetEvent(event_handle));

// get set event
// wait for event, expect true now
EXPECT_EQ(true, gWaitForEvent(event_handle, 100));

// close (and unlink/destroy) event
EXPECT_EQ(true, eCAL::gCloseEvent(event_handle));
}
79 changes: 72 additions & 7 deletions testing/ecal/pubsub_test/src/pubsub_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,6 @@ TEST(IO, MultipleSendsUDP)
eCAL::Finalize();
}





#if 0
TEST(IO, ZeroPayloadMessageTCP)
{
Expand Down Expand Up @@ -923,8 +919,6 @@ TEST(IO, ZeroPayloadMessageTCP)
}
#endif

#include <ecal/msg/string/publisher.h>
#include <ecal/msg/string/subscriber.h>
TEST(IO, DestroyInCallback)
{
/* Test setup :
Expand Down Expand Up @@ -988,4 +982,75 @@ TEST(IO, DestroyInCallback)
// finalize eCAL API
// without destroying any pub / sub
eCAL::Finalize();
}
}

TEST(IO, SubscriberReconnection)
{
/* Test setup :
* publisher runs permanently in a thread
* subscriber start reading
* subscriber gets out of scope (destruction)
* subscriber starts again in a new scope
* Test ensures that subscriber is reconnecting and all sync mechanism are working properly again.
*/

// initialize eCAL API
eCAL::Initialize(0, nullptr, "SubscriberReconnection");

// enable loop back communication in the same thread
eCAL::Util::EnableLoopback(true);

// start publishing thread
std::atomic<bool> stop_publishing(false);
eCAL::string::CPublisher<std::string> pub_foo("foo");
std::thread pub_foo_t([&pub_foo, &stop_publishing]() {
while (!stop_publishing)
{
pub_foo.Send("Hello World");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << "Stopped publishing" << std::endl;
});

// scope 1
{
size_t callback_received_count(0);

eCAL::string::CSubscriber<std::string> sub_foo("foo");
auto receive_lambda = [&sub_foo, &callback_received_count](const char* /*topic_*/, const std::string& /*msg*/, long long /*time_*/, long long /*clock_*/, long long /*id_*/) {
std::cout << "Receiving in scope 1" << std::endl;
callback_received_count++;
};
sub_foo.AddReceiveCallback(receive_lambda);

// sleep for 2 seconds, we should receive something
std::this_thread::sleep_for(std::chrono::seconds(2));

EXPECT_TRUE(callback_received_count > 0);
}

// scope 2
{
size_t callback_received_count(0);

eCAL::string::CSubscriber<std::string> sub_foo("foo");
auto receive_lambda = [&sub_foo, &callback_received_count](const char* /*topic_*/, const std::string& /*msg*/, long long /*time_*/, long long /*clock_*/, long long /*id_*/) {
std::cout << "Receiving in scope 2" << std::endl;
callback_received_count++;
};
sub_foo.AddReceiveCallback(receive_lambda);

// sleep for 2 seconds, we should receive something
std::this_thread::sleep_for(std::chrono::seconds(2));

EXPECT_TRUE(callback_received_count > 0);
}

// stop publishing and join thread
stop_publishing = true;
pub_foo_t.join();

// finalize eCAL API
// without destroying any pub / sub
eCAL::Finalize();
}

0 comments on commit 6687670

Please sign in to comment.