-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial files for curl communciator and global state class
- Loading branch information
1 parent
a7529e6
commit 8c227ff
Showing
5 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#ifndef GLOBAL_STATE_SINGLETON_HPP | ||
#define GLOBAL_STATE_SINGLETON_HPP | ||
#pragma once | ||
|
||
// Standard includes | ||
#include <atomic> | ||
#include <mutex> | ||
#include <unordered_set> | ||
|
||
namespace SDMS { | ||
|
||
/** | ||
* The purpose of this Class is to handle all Global init and cleanup that is: | ||
* 1. Thread safe | ||
* 2. Should only be called once. | ||
**/ | ||
class GlobalStateSingleton { | ||
|
||
public: | ||
using InitFunctions = void (*)(void); | ||
using CleanupFunctions = void (*)(void); | ||
|
||
/** | ||
* Make functions hashable | ||
**/ | ||
struct InitFunctionsHash { | ||
std::size_t operator()(const InitFunctions& func) const; | ||
}; | ||
struct InitFunctionsEqual { | ||
bool operator()(const InitFunctions& lhs, const InitFunctions& rhs) const; | ||
}; | ||
|
||
struct CleanupFunctionsHash { | ||
std::size_t operator()(const CleanupFunctions& func) const; | ||
}; | ||
|
||
struct CleanupFunctionsEqual { | ||
bool operator()(const CleanupFunctions& lhs, const CleanupFunctions& rhs) const; | ||
}; | ||
|
||
private: | ||
static std::unordered_set< | ||
InitFunctions, | ||
InitFunctionsHash, | ||
InitFunctionsEqual> | ||
m_init_functions; | ||
|
||
static std::unordered_set< | ||
CleanupFunctions, | ||
CleanupFunctionsHash, | ||
CleanupFunctionsEqual> | ||
m_cleanup_functions; | ||
|
||
static GlobalStateSingleton * m_instance; | ||
static std::once_flag m_onceFlag; | ||
|
||
GlobalStateSingleton() {}; | ||
|
||
mutable std::mutex m_init_mutex; | ||
bool m_initialized = false; | ||
mutable std::mutex m_cleanup_mutex; | ||
bool m_cleanup = false; | ||
public: | ||
|
||
static GlobalStateSingleton* getInstance() { | ||
std::call_once(m_onceFlag, [] { | ||
m_instance = new GlobalStateSingleton(); | ||
}); | ||
return m_instance; | ||
} | ||
/** | ||
* Disable copy constructor | ||
**/ | ||
GlobalStateSingleton(const GlobalStateSingleton&) = delete; | ||
|
||
/** | ||
* Disable assignment operator | ||
**/ | ||
GlobalStateSingleton& operator=(const GlobalStateSingleton&) = delete; | ||
|
||
/** | ||
* To statically register init and cleanup functions you need to run the | ||
* register method | ||
* | ||
* | ||
* ```C++ | ||
* GlobalStateSingleton global_state; | ||
* gloabl_state.register<RouterBookKeepingOperator,OperatorType::RouterBookKeeping>(); | ||
* ``` | ||
**/ | ||
template <class T> static bool registerInitFunction(T func) { | ||
if (m_init_functions.count(func) > 0) { | ||
return false; | ||
} else { | ||
m_init_functions.insert(func); | ||
} | ||
return true; | ||
} | ||
|
||
template <class T> static bool registerCleanupFunction(T func) { | ||
if (m_cleanup_functions.count(func) > 0) { | ||
return false; | ||
} else { | ||
m_cleanup_functions.insert(func); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Initialize will only execute all functions once. | ||
**/ | ||
void init(); | ||
|
||
/** | ||
* Cleanup will only call all cleanup functions the first time it is called. | ||
**/ | ||
void cleanup(); | ||
|
||
/** | ||
* Have the globals been initialized | ||
**/ | ||
bool initialized() const; | ||
|
||
/** | ||
* Have the globals been cleaned up | ||
**/ | ||
bool cleaned() const; | ||
|
||
}; | ||
|
||
} // namespace SDMS | ||
|
||
|
||
#endif // GLOBAL_STATE_SINGLETON_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Public includes | ||
#include "GlobalStateSingleton.hpp" | ||
|
||
namespace SDMS { | ||
|
||
|
||
|
||
std::size_t GlobalStateSingleton::InitFunctionsHash::operator()( | ||
const InitFunctions& func) const { | ||
// Custom hash implementation based on the function's target | ||
return std::hash<InitFunctions>{}(func); | ||
} | ||
|
||
bool GlobalStateSingleton::InitFunctionsEqual::operator()( | ||
const InitFunctions& lhs, | ||
const InitFunctions& rhs) const { | ||
// Custom equality comparison based on the function's target | ||
return lhs == rhs; | ||
} | ||
|
||
std::size_t GlobalStateSingleton::CleanupFunctionsHash::operator()( | ||
const CleanupFunctions& func) const { | ||
// Custom hash implementation based on the function's target | ||
return std::hash<CleanupFunctions>{}(func); | ||
} | ||
|
||
bool GlobalStateSingleton::CleanupFunctionsEqual::operator()( | ||
const CleanupFunctions& lhs, | ||
const CleanupFunctions& rhs) const { | ||
// Custom equality comparison based on the function's target | ||
return lhs == rhs; | ||
} | ||
|
||
void GlobalStateSingleton::init() { | ||
std::lock_guard<std::mutex> lock(m_init_mutex); | ||
if( m_initialized ) return; | ||
for( auto & init_func : this->m_init_functions ) { | ||
init_func(); | ||
} | ||
m_initialized = true; | ||
} | ||
|
||
void GlobalStateSingleton::cleanup() { | ||
std::lock_guard<std::mutex> lock(m_cleanup_mutex); | ||
if( m_cleanup ) return; | ||
for( auto & cleanup_func : this->m_cleanup_functions ) { | ||
cleanup_func(); | ||
} | ||
m_cleanup = true; | ||
} | ||
|
||
bool GlobalStateSingleton::initialized() const { | ||
std::lock_guard<std::mutex> lock(m_init_mutex); | ||
return m_initialized; | ||
} | ||
|
||
bool GlobalStateSingleton::initialized() const { | ||
std::lock_guard<std::mutex> lock(m_cleanup_mutex); | ||
return m_cleanup; | ||
} | ||
|
||
} // namespace SDMS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Local private includes | ||
#include "CurlCommunicator.hpp" | ||
|
||
namespace SDMS { | ||
|
||
CurlCommunicator::CurlCommunicator(const SocketOptions &socket_options, | ||
const ICredentials &credentials, | ||
uint32_t timeout_on_receive_milliseconds, | ||
long timeout_on_poll_milliseconds, | ||
const LogContext &log_context) { | ||
|
||
} | ||
|
||
CurlCommunicator::~CurlCommunicator() { | ||
|
||
} | ||
/** | ||
* Poll for incoming messages at the sockets | ||
* | ||
* Return true if a message(s) has been provided | ||
* Return false if timeout and or no message | ||
**/ | ||
ICommunicator::Response CurlCommunicator::poll(const MessageType) { | ||
|
||
} | ||
|
||
void CurlCommunicator::send(IMessage &message) { | ||
|
||
} | ||
|
||
ICommunicator::Response CurlCommunicator::receive(const MessageType) { | ||
} | ||
|
||
const std::string CurlCommunicator::id() const { | ||
|
||
} | ||
const std::string CurlCommunicator::address() const { | ||
|
||
} | ||
|
||
} // namespace SDMS | ||
|
||
#endif // CURL_COMMUNICATOR_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#ifndef CURL_COMMUNICATOR_HPP | ||
#define CURL_COMMUNICATOR_HPP | ||
#pragma once | ||
|
||
// Local private includes | ||
#include "../Buffer.hpp" | ||
|
||
// Local public includes | ||
#include "../ProtoBufFactory.hpp" | ||
#include "common/DynaLog.hpp" | ||
#include "common/ICommunicator.hpp" | ||
#include "common/IMessage.hpp" | ||
#include "common/ISocket.hpp" | ||
#include "common/MessageFactory.hpp" | ||
|
||
// Third party includes | ||
#include <curl/curl.h> | ||
|
||
// Standard includes | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace SDMS { | ||
|
||
class CurlCommunicator : public ICommunicator { | ||
protected: | ||
std::unique_ptr<ISocket> m_socket; | ||
|
||
/// Optional timeout in milliseconds (0 = wait forever) | ||
uint32_t m_timeout_on_receive_milliseconds = 0; | ||
long m_timeout_on_poll_milliseconds = 10; | ||
MessageFactory m_msg_factory; | ||
Buffer m_buffer; | ||
ProtoBufFactory m_protocol_factory; | ||
ICommunicator::Response m_poll(uint32_t timeout_milliseconds); | ||
|
||
LogContext m_log_context; | ||
|
||
public: | ||
/** To be used by children*/ | ||
CurlCommunicator(const LogContext &log_context) | ||
: m_log_context(log_context){}; | ||
|
||
CurlCommunicator(const SocketOptions &socket_options, | ||
const ICredentials &credentials, | ||
uint32_t timeout_on_receive_milliseconds, | ||
long timeout_on_poll_milliseconds, | ||
const LogContext &log_context); | ||
|
||
virtual ~CurlCommunicator(); | ||
/** | ||
* Poll for incoming messages at the sockets | ||
* | ||
* Return true if a message(s) has been provided | ||
* Return false if timeout and or no message | ||
**/ | ||
virtual ICommunicator::Response poll(const MessageType) final; | ||
|
||
virtual void send(IMessage &message) final; | ||
virtual ICommunicator::Response receive(const MessageType) final; | ||
|
||
virtual const std::string id() const noexcept final; | ||
virtual const std::string address() const noexcept final; | ||
}; | ||
|
||
} // namespace SDMS | ||
|
||
#endif // CURL_COMMUNICATOR_HPP |