Skip to content

Commit

Permalink
clkmgr: hide ClientState class from the public API.
Browse files Browse the repository at this point in the history
Use the Pimpl idiom to hide the implementation details of ClientState class
from the public API, providing a clean and maintainable interface.

Signed-off-by: Song Yoong Siang <yoong.siang.song@intel.com>
  • Loading branch information
yoongsiang2 authored and erezgeva committed Feb 18, 2025
1 parent 486e21a commit 1d637e7
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 53 deletions.
2 changes: 1 addition & 1 deletion clkmgr/client/client_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

#include "pub/clkmgr/client_state.h"
#include "client/client_state.hpp"
#include "client/msgq_tport.hpp"
#include "common/clkmgrtypes.hpp"
#include "common/message.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*
*/

#ifndef CLKMGR_CLIENT_STATE_H
#define CLKMGR_CLIENT_STATE_H
#ifndef CLIENT_CLIENT_STATE_H
#define CLIENT_CLIENT_STATE_H

#include "pub/clkmgr/subscription.h"

Expand Down Expand Up @@ -170,4 +170,4 @@ class ClientState

__CLKMGR_NAMESPACE_END

#endif /* CLKMGR_CLIENT_STATE_H */
#endif /* CLIENT_CLIENT_STATE_H */
1 change: 0 additions & 1 deletion clkmgr/client/clkmgr.i
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@

/* library code */
%include "pub/clockmanager.h"
%include "pub/clkmgr/client_state.h"
%include "pub/clkmgr/subscription.h"
%include "pub/clkmgr/utility.h"
60 changes: 30 additions & 30 deletions clkmgr/client/clockmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include "pub/clockmanager.h"
#include "client/client_state.hpp"
#include "client/connect_msg.hpp"
#include "client/msgq_tport.hpp"
#include "client/notification_msg.hpp"
Expand All @@ -36,6 +37,10 @@ rtpi::condition_variable ClientConnectMessage::cv;
rtpi::mutex ClientSubscribeMessage::cv_mtx;
rtpi::condition_variable ClientSubscribeMessage::cv;

ClockManager::ClockManager() : implClientState(new ClientState()) {}

ClockManager::~ClockManager() = default;

ClockManager &ClockManager::FetchSingle()
{
static ClockManager m_single;
Expand All @@ -47,18 +52,13 @@ bool ClockManager::init()
return true;
}

const ClientState &ClockManager::getClientState()
{
return appClientState;
}

bool ClockManager::clkmgr_connect()
{
unsigned int timeout_sec = (unsigned int) DEFAULT_CONNECT_TIME_OUT;
Message0 connectMsg(new ClientConnectMessage());
TransportClientId newClientID;
ClientConnectMessage *cmsg = dynamic_cast<decltype(cmsg)>(connectMsg.get());
cmsg->setClientState(&appClientState);
cmsg->setClientState(implClientState.get());
if(!ClientMessage::init()) {
PrintError("Client Message Init Failed");
return false;
Expand All @@ -72,10 +72,10 @@ bool ClockManager::clkmgr_connect()
/* Wait for connection result */
auto endTime = system_clock::now() + seconds(timeout_sec);
unique_lock<rtpi::mutex> lck(ClientConnectMessage::cv_mtx);
while(appClientState.get_connected() == false) {
while(implClientState->get_connected() == false) {
auto res = ClientConnectMessage::cv.wait_until(lck, endTime);
if(res == cv_status::timeout) {
if(appClientState.get_connected() == false) {
if(implClientState->get_connected() == false) {
PrintDebug("[CONNECT] Connect reply from proxy - "
"timeout failure!!");
return false;
Expand All @@ -85,7 +85,7 @@ bool ClockManager::clkmgr_connect()
}
if((cmsg != nullptr) && !(cmsg->getClientId().empty())) {
strcpy((char *)newClientID.data(), (char *)cmsg->getClientId().data());
appClientState.set_clientID(newClientID);
implClientState->set_clientID(newClientID);
}
return true;
}
Expand All @@ -102,30 +102,30 @@ bool ClockManager::clkmgr_subscribe(const ClkMgrSubscription &newSub,
return false;
} else
PrintDebug("[clkmgr::subscribe] subscribeMsgcreation is OK !!");
cmsg->setClientState(&appClientState);
cmsg->setClientState(implClientState.get());
/* Write the current event subscription */
appClientState.set_eventSub(newSub);
implClientState->set_eventSub(newSub);
/* Copy the event Mask */
cmsg->getSubscription().set_event_mask(newSub.get_event_mask());
strcpy((char *)cmsg->getClientId().data(),
(char *)appClientState.get_clientID().data());
cmsg->set_sessionId(appClientState.get_sessionId());
(char *)implClientState->get_clientID().data());
cmsg->set_sessionId(implClientState->get_sessionId());
ClientMessageQueue::writeTransportClientId(subscribeMsg.get());
ClientMessageQueue::sendMessage(subscribeMsg.get());
/* Wait for subscription result */
auto endTime = system_clock::now() + seconds(timeout_sec);
unique_lock<rtpi::mutex> lck(ClientSubscribeMessage::cv_mtx);
while(appClientState.get_subscribed() == false) {
while(implClientState->get_subscribed() == false) {
auto res = ClientSubscribeMessage::cv.wait_until(lck, endTime);
if(res == cv_status::timeout) {
if(appClientState.get_subscribed() == false) {
if(implClientState->get_subscribed() == false) {
PrintDebug("[SUBSCRIBE] No reply from proxy - timeout failure!!");
return false;
}
} else
PrintDebug("[SUBSCRIBE] SUBSCRIBE reply received.");
}
Event_state clkmgrCurrentState = appClientState.get_eventState();
Event_state clkmgrCurrentState = implClientState->get_eventState();
currentState = clkmgrCurrentState;
return true;
}
Expand All @@ -144,9 +144,9 @@ bool ClockManager::clkmgr_disconnect()
}
/* Delete the ClientPtpEvent inside Subscription */
ClientSubscribeMessage::deleteClientPtpEventStruct(
appClientState.get_sessionId());
implClientState->get_sessionId());
/* Delete the ClientState reference inside ClientNotificationMessage class */
ClientNotificationMessage::deleteClientState(&appClientState);
ClientNotificationMessage::deleteClientState(implClientState.get());
retVal = true;
done:
if(!retVal)
Expand All @@ -163,14 +163,14 @@ int64_t timespec_delta(const timespec &last_notification_time,
return delta_in_ms;
}

bool check_proxy_liveness(ClientState &appClientState)
bool check_proxy_liveness(ClientState &implClientState)
{
timespec current_time;
if(clock_gettime(CLOCK_REALTIME, &current_time) == -1)
PrintDebug("[CONNECT] Failed to get current time.");
else {
int64_t timeout =
timespec_delta(appClientState.get_last_notification_time(),
timespec_delta(implClientState.get_last_notification_time(),
current_time);
if(timeout < DEFAULT_LIVENESS_TIMEOUT_IN_MS)
return true;
Expand All @@ -181,19 +181,19 @@ bool check_proxy_liveness(ClientState &appClientState)
PrintDebug("[CONNECT] Failed to cast to ClientConnectMessage");
return false;
}
appClientState.set_connected(false);
cmsg->setClientState(&appClientState);
cmsg->set_sessionId(appClientState.get_sessionId());
implClientState.set_connected(false);
cmsg->setClientState(&implClientState);
cmsg->set_sessionId(implClientState.get_sessionId());
ClientMessageQueue::writeTransportClientId(connectMsg.get());
ClientMessageQueue::sendMessage(connectMsg.get());
/* Wait for connection result */
auto endTime = system_clock::now() +
milliseconds(DEFAULT_LIVENESS_TIMEOUT_IN_MS);
unique_lock<rtpi::mutex> lck(ClientConnectMessage::cv_mtx);
while(appClientState.get_connected() == false) {
while(implClientState.get_connected() == false) {
auto res = ClientConnectMessage::cv.wait_until(lck, endTime);
if(res == cv_status::timeout) {
if(appClientState.get_connected() == false) {
if(implClientState.get_connected() == false) {
PrintDebug("[CONNECT] Connect reply timeout!!");
return false;
}
Expand All @@ -214,11 +214,11 @@ int ClockManager::clkmgr_status_wait(int timeout,
Event_state eventState;
do {
/* Check the liveness of the Proxy's message queue */
if(!check_proxy_liveness(appClientState))
if(!check_proxy_liveness(*implClientState))
return -1;
/* Get the event state and event count from the API */
eventCount = appClientState.get_eventStateCount();
eventState = appClientState.get_eventState();
eventCount = implClientState->get_eventStateCount();
eventState = implClientState->get_eventState();
/* Check if any member of eventCount is non-zero */
if(eventCount.offset_in_range_event_count ||
eventCount.as_capable_event_count ||
Expand All @@ -239,7 +239,7 @@ int ClockManager::clkmgr_status_wait(int timeout,
return 0;
/* Reset the atomic count by reducing the corresponding eventCount */
ClientSubscribeMessage::resetClientPtpEventStruct(
appClientState.get_sessionId(), eventCount);
appClientState.set_eventStateCount(eventCount);
implClientState->get_sessionId(), eventCount);
implClientState->set_eventStateCount(eventCount);
return 1;
}
2 changes: 1 addition & 1 deletion clkmgr/client/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef CLIENT_MESSAGE_HPP
#define CLIENT_MESSAGE_HPP

#include "pub/clkmgr/client_state.h"
#include "client/client_state.hpp"
#include "common/message.hpp"

#include <cstdint>
Expand Down
2 changes: 1 addition & 1 deletion clkmgr/client/notification_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

#include "pub/clkmgr/client_state.h"
#include "client/client_state.hpp"
#include "client/notification_msg.hpp"
#include "client/subscribe_msg.hpp"
#include "common/message.hpp"
Expand Down
2 changes: 1 addition & 1 deletion clkmgr/client/subscribe_msg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef CLIENT_SUBSCRIBE_MSG_HPP
#define CLIENT_SUBSCRIBE_MSG_HPP

#include "pub/clkmgr/client_state.h"
#include "client/client_state.hpp"
#include "client/message.hpp"
#include "common/subscribe_msg.hpp"

Expand Down
24 changes: 13 additions & 11 deletions clkmgr/pub/clockmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

#ifdef __cplusplus

#include "pub/clkmgr/client_state.h"
#include "pub/clkmgr/subscription.h"
#include <memory>

__CLKMGR_NAMESPACE_BEGIN

class ClientState; /* Forward declaration */

/**
* Class to provide APIs to set up and manage the client-runtime.
* @note the class is singelton
Expand All @@ -26,14 +29,19 @@ class ClockManager
{
private:
/**
* State of current client-runtime.
* Implementation pointer to current state of client-runtime.
*/
std::unique_ptr<ClientState> implClientState;

/**
* Constructor
*/
ClientState appClientState;
ClockManager();

/**
* Default constructor
* Destructor
*/
ClockManager() = default;
~ClockManager();

public:

Expand All @@ -49,12 +57,6 @@ class ClockManager
*/
bool init();

/**
* Get the client state
* @return Reference to the client state
*/
const ClientState &getClientState();

/**
* Establish connection between Client and Proxy
* @return true on success, false on failure
Expand Down
4 changes: 0 additions & 4 deletions clkmgr/sample/clkmgr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ int main(int argc, char *argv[])
std::cout << "[clkmgr] failure in connecting !!!\n";
ret = EXIT_FAILURE;
goto do_exit;
} else {
const ClientState &myState = cm.getClientState();
std::cout << "[clkmgr] Connected. Session ID : " <<
myState.get_sessionId() << "\n";
}

sleep(1);
Expand Down

0 comments on commit 1d637e7

Please sign in to comment.