Skip to content

Commit

Permalink
simplesim: Add session id to GET_INFO messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondraceq authored and Jaromel-Tuzerad committed Jan 18, 2024
1 parent 07c1272 commit a03a694
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 53 deletions.
6 changes: 5 additions & 1 deletion softwareComponents/rofiHalSim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

cmake_minimum_required(VERSION 3.11)

set(SRC
rofi_hal.cpp
session_id.cpp
)

add_library(rofi_hal_sim SHARED rofi_hal.cpp)
add_library(rofi_hal_sim SHARED ${SRC})
target_link_libraries(rofi_hal_sim PRIVATE ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES} rofisimMessages atoms)
target_link_libraries(rofi_hal_sim PUBLIC rofi::hal::inc)
target_include_directories(rofi_hal_sim SYSTEM PRIVATE ${GAZEBO_INCLUDE_DIRS})
Expand Down
2 changes: 2 additions & 0 deletions softwareComponents/rofiHalSim/publish_worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "gazebo_node_handler.hpp"
#include "message_logger.hpp"
#include "session_id.hpp"
#include "subscriber_wrapper.hpp"

#include <distributorReq.pb.h>
Expand Down Expand Up @@ -110,6 +111,7 @@ class PublishWorker {

rofi::messages::DistributorReq req;
req.set_reqtype( rofi::messages::DistributorReq::GET_INFO );
req.set_sessionid( SessionId::get().bytes() );
publish( std::move( req ) );

_onRofiTopicsUpdate.wait( lock, [ this, rofiId ] {
Expand Down
50 changes: 1 addition & 49 deletions softwareComponents/rofiHalSim/rofi_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,23 @@
#include <thread>
#include <utility>

#include <boost/uuid/random_generator.hpp>

#include "connector_worker.hpp"
#include "joint_worker.hpp"
#include "publish_worker.hpp"
#include "session_id.hpp"
#include "wait_worker.hpp"

#include <distributorReq.pb.h>
#include <distributorResp.pb.h>
#include <rofiCmd.pb.h>
#include <rofiResp.pb.h>

#ifndef SESSION_ID
#define SESSION_ID boost::uuids::random_generator()()
#endif


namespace
{
using namespace rofi::hal;
namespace msgs = rofi::messages;

class SessionId {
public:
static const SessionId & get()
{
static SessionId instance = SessionId( SESSION_ID );
return instance;
}

const std::string & bytes() const
{
return _bytes;
}

private:
template < typename T, std::enable_if_t< std::is_integral_v< T >, int > = 0 >
constexpr SessionId( T id )
{
_bytes.reserve( sizeof( T ) );

for ( size_t i = 0; i < sizeof( T ); i++ ) {
_bytes.push_back( *( reinterpret_cast< char * >( &id ) + i ) );
}
}

template < typename T,
std::enable_if_t< sizeof( typename T::value_type ) == sizeof( char ), int > = 0 >
constexpr SessionId( T id )
{
_bytes.reserve( id.size() );

for ( auto & c : id ) {
static_assert( sizeof( decltype( c ) ) == sizeof( char ) );
_bytes.push_back( reinterpret_cast< char & >( c ) );
}
}

SessionId( const SessionId & other ) = delete;
SessionId & operator=( const SessionId & other ) = delete;

private:
std::string _bytes;
};

class ConnectorSim;
class JointSim;

Expand Down
16 changes: 16 additions & 0 deletions softwareComponents/rofiHalSim/session_id.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "session_id.hpp"

#include <boost/uuid/random_generator.hpp>


#ifndef SESSION_ID
#define SESSION_ID boost::uuids::random_generator()()
#endif

using rofi::hal::SessionId;

const SessionId & SessionId::get()
{
static SessionId instance = SessionId( SESSION_ID );
return instance;
}
49 changes: 49 additions & 0 deletions softwareComponents/rofiHalSim/session_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <string>
#include <type_traits>


namespace rofi::hal
{

class SessionId {
public:
static const SessionId & get();

const std::string & bytes() const
{
return _bytes;
}

private:
template < typename T, std::enable_if_t< std::is_integral_v< T >, int > = 0 >
constexpr SessionId( T id )
{
_bytes.reserve( sizeof( T ) );

for ( size_t i = 0; i < sizeof( T ); i++ ) {
_bytes.push_back( *( reinterpret_cast< char * >( &id ) + i ) );
}
}

template < typename T,
std::enable_if_t< sizeof( typename T::value_type ) == sizeof( char ), int > = 0 >
constexpr SessionId( T id )
{
_bytes.reserve( id.size() );

for ( auto & c : id ) {
static_assert( sizeof( decltype( c ) ) == sizeof( char ) );
_bytes.push_back( reinterpret_cast< char & >( c ) );
}
}

SessionId( const SessionId & other ) = delete;
SessionId & operator=( const SessionId & other ) = delete;

private:
std::string _bytes;
};

} // namespace rofi::hal
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Distributor {
onRequest( *reqCopy );
}

rofi::messages::DistributorResp onGetInfoReq();
rofi::messages::DistributorResp onGetInfoReq( SessionId sessionId );
rofi::messages::DistributorResp onLockOneReq( SessionId sessionId );
rofi::messages::DistributorResp onTryLockReq( ModuleId moduleId, SessionId sessionId );
rofi::messages::DistributorResp onUnlockReq( ModuleId moduleId, SessionId sessionId );
Expand Down
5 changes: 3 additions & 2 deletions softwareComponents/simplesimLib/src/distributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Distributor::onRequest( const rofi::messages::DistributorReq & req )
if ( req.rofiid() != 0 ) {
std::cerr << "Got GET_INFO distributor request with non-zero id\n";
}
sendResponse( onGetInfoReq() );
sendResponse( onGetInfoReq( req.sessionid() ) );
break;
}
case DistributorReq::LOCK_ONE:
Expand Down Expand Up @@ -64,10 +64,11 @@ void Distributor::onRequest( const rofi::messages::DistributorReq & req )
}
}

rofi::messages::DistributorResp Distributor::onGetInfoReq()
rofi::messages::DistributorResp Distributor::onGetInfoReq( SessionId sessionId )
{
rofi::messages::DistributorResp resp;
resp.set_resptype( rofi::messages::DistributorReq::GET_INFO );
resp.set_sessionid( sessionId );

_modulesCommunication.forEachLockedModule(
[ &resp ]( ModuleId moduleId, const std::string & topic ) {
Expand Down

0 comments on commit a03a694

Please sign in to comment.