Skip to content

Commit

Permalink
Add initial files for curl communciator and global state class
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaSBrown committed Jan 22, 2024
1 parent a7529e6 commit 8c227ff
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
134 changes: 134 additions & 0 deletions common/include/common/GlobalStateSingleton.hpp
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
5 changes: 5 additions & 0 deletions common/include/common/ProtocolTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ enum class ProtocolType {
ZQTP // ZeroMQ Transport Layer Protocol
};

enum class ProtocolTechnology {
CURL,
ZEROMQ
};

} // namespace SDMS

#endif // PROTOCOL_TYPES_HPP
62 changes: 62 additions & 0 deletions common/source/GlobalStateSingleton.cpp
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
43 changes: 43 additions & 0 deletions common/source/communicators/CurlCommunicator.cpp
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
68 changes: 68 additions & 0 deletions common/source/communicators/CurlCommunicator.hpp
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

0 comments on commit 8c227ff

Please sign in to comment.