Files
vrpmdv-rtservice/src/RTService.cpp
2024-04-17 09:34:52 +02:00

150 lines
4.2 KiB
C++

#include <string>
#include <list>
#include <iostream>
#include <thread>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/server.h>
#include "RTService.pb.h"
#include "../include/RTSMonitoringTask.h"
using namespace boost::interprocess;
using namespace proto_messages;
class RTServiceImpl : public RTService
{
private:
/* data */
RTSMonitoringTask rtsMonTask;
const std::string RTSCOMMANDQUEUE = "RTSSERVICECOMMANDQUEUE";
const int MAXMSGSIZE = 250;
const int MAXMSGCOUNT = 25;
// message_queue shared_ptr<message_queue> rtsMQ;
private:
public:
virtual void createMonitoring(::google::protobuf::RpcController* /* controller_ */, const SCreateIn* request_, ::SResult* response_, ::google::protobuf::Closure* /* done_ */)
{
// print request and
std::cout << "Received request RTService / CreateMonitoring : " << request_->id() << " and " << request_->name() << std::endl << std::endl;
bool res = rtsMonTask.CreateMonitoring(request_->id(), request_->samplerate(), request_->sampleperiod(), request_->downtime(), request_->status());
// create response
response_->set_result(res);
}
// we need all parameters like id, samplerate, sampletime, downtime, later: HW-channels with the configurations
bool createMonitoring(std::string id, int samplerate, int sampleperiod, int downtime, std::string status) {
//TODO ML: add this to the M4Core
if (rtsMonTask.Init()) {
return rtsMonTask.CreateMonitoring(id, samplerate, sampleperiod, downtime, status);
}
return false;
}
bool deleteMonitoring(std::string id) {
//TODO ML: add this to the M4Core
return true;
}
std::string getMonitoringState(std::string id) {
//TODO ask the M4Core for the Monitpring Status
return "Start";
}
std::list<std::string> getMonitoringStates() {
//TODO ask the M4Core for the Monitpring Status
std::list<std::string> list;
return list;
}
bool setMonitoringStatus(std::string id, std::string status) {
//set the Status
return true;
}
void Run() {
BOOST_TRY{
//Erase previous message queue
message_queue::remove(RTSCOMMANDQUEUE.c_str());
//Create a message_queue.
message_queue rtsMQ
(create_only //only create
,RTSCOMMANDQUEUE.c_str() //name
,MAXMSGCOUNT //max message number
,MAXMSGSIZE //max message size
);
bool bCmdQueueRun = true;
unsigned int priority;
message_queue::size_type recvd_size;
boost::property_tree::ptree pt;
while (bCmdQueueRun) {
char rtsCmdbuffer [MAXMSGSIZE];
//mq.receive(buffer, sizeof(buffer), recvd_size, pri);
rtsMQ.receive(rtsCmdbuffer, sizeof(rtsCmdbuffer), recvd_size, priority);
//read json and go to cmd execution
boost::property_tree::read_json(rtsCmdbuffer,pt);
}
// //Send 100 numbers
// for(int i = 0; i < 100; ++i){
// mq.send(&i, sizeof(i), 0);
// }
}
BOOST_CATCH(interprocess_exception &ex){
std::cout << ex.what() << std::endl;
message_queue::remove(RTSCOMMANDQUEUE.c_str());
} BOOST_CATCH_END
}
};
int main(int argc, char **argv) {
// initialize eCAL API
eCAL::Initialize(argc, argv, "RTService");
// create minimal service server
eCAL::CServiceServer rtService("RTSMonService");
// create Math service server
std::shared_ptr<RTService> rt_service = std::make_shared<RTServiceImpl>();
eCAL::protobuf::CServiceServer<RTService> math_server(rt_service);
// idle
while(eCAL::Ok())
{
// sleep 100 ms
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// finalize eCAL API
eCAL::Finalize();
return 0;
}