Commit 5be19956 authored by fmg005's avatar fmg005

adding dummy monitor

parent c69cbbcb
#include "Monitor.h"
using namespace CLA;
/* wrapper for tcp client */
Monitor::Monitor(std::shared_ptr<CLA::LOGGER>& logger) : m_logger(logger),
m_tcp_monitor_client(logger) {
m_tcp_monitor_client.initialize(); /* connection setup to server via tcp socket */
}
Monitor::Monitor(std::shared_ptr<CLA::LOGGER>& logger, const string& topic, const
string& type) : m_logger(logger), m_tcp_monitor_client(logger, topic, type) {
m_tcp_monitor_client.initialize(); /* connection setup to server via tcp socket */
}
void Monitor::send_patient_data (double data) {
m_tcp_monitor_client.send_patient_data(data);
}
Monitor::~Monitor() {
}
#ifndef Monitor_H_
#define Monitor_H_
#include "tcp_monitor_client.h"
namespace CLA {
class Monitor {
/* wrapper for monitor tcp client */
private:
CLA::tcp_monitor_client m_tcp_monitor_client;
std::shared_ptr<CLA::LOGGER> m_logger;
public:
Monitor( std::shared_ptr<CLA::LOGGER>&);
Monitor( std::shared_ptr<CLA::LOGGER>&, const string&, const string&);
~Monitor();
void send_patient_data(double);
};
}
#endif
#ifndef TCP_MONITOR_CLIENT_H
#define TCP_MONITOR_CLIENT_H
#include <sys/types.h>
#include <sys/socket.h> /* socket functions */
#include <sys/time.h> /* FD_SET, FD_ISSET, FD_ZERO, timeval, fd_set */
#include <sys/select.h>/* select() */
#include <stdio.h>
#include <stdlib.h> /* exit, EXIT_FAILURE */
#include <string.h> /* memcpy */
#include <unistd.h>
#include <netdb.h> /* gethostbyname */
#include <netinet/in.h>/* sockaddr_in */
#include <iostream>
#include <memory>
#include "CLA_Logger.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace rapidjson; /* third party library for parsing json data */
using namespace std;
namespace CLA {
class tcp_monitor_client {
/* impementation of a non-blocking tcp client to talk
* to the rosbridge udp server
*/
private:
struct hostent *hp; /* host information */
struct sockaddr_in servaddr; /* server address information */
struct timeval timeout;
string json_message;
string m_topic;
string m_type;
const char* m_host;
int port;
int fd, rec_value,recvlen;
int SOCKET_TIMEOUT_SEC;
int SOCKET_TIMEOUT_uSEC;
socklen_t addrlen;
Document document; /* to parse json data */
StringBuffer strbuf;
fd_set set; /* descriptor read set*/
std::shared_ptr<CLA::LOGGER> m_logger;
public:
tcp_monitor_client(std::shared_ptr<CLA::LOGGER>&);
tcp_monitor_client(std::shared_ptr<CLA::LOGGER>&, const string&, const string&);
~tcp_monitor_client();
void initialize();
void send_data_to_server(double);
void send_patient_data(double);
void waiting_to_send();
void clear();
};
}
#endif
#include "tcp_monitor_client.h"
using namespace CLA;
tcp_monitor_client::tcp_monitor_client(std::shared_ptr<CLA::LOGGER>& logger) : m_logger(logger) {
/* we can only talk to rosbrige server using json strings; here we want to
* 'subscribe' to 'chatter topic' publishing messages of type 'std_msgs/String'
*/
json_message = "{\"op\":\"advertise\", \"topic\":\"/monitor\", \"type\":\"std_msgs/Float64\"}";
m_host = "localhost"; /* rosbridge server runs on localhost */
port = 9090; /* rosbridge server uses this port number */
SOCKET_TIMEOUT_SEC = 1;
SOCKET_TIMEOUT_uSEC = 0;
}
tcp_monitor_client::tcp_monitor_client(std::shared_ptr<CLA::LOGGER>& logger,
const string& topic, const string& type): m_topic(move(topic)), m_type(move(type)),
m_logger(logger) {
json_message = "{\"op\":\"advertise\", \"topic\": \""+m_topic+"\", \"type\": \""+m_type+"\"}";
m_host = "localhost"; /* rosbridge server runs on localhost */
port = 9090; /* rosbridge server uses this port number */
SOCKET_TIMEOUT_SEC = 1;
SOCKET_TIMEOUT_uSEC = 0;
}
void tcp_monitor_client::initialize() {
/* create a UDP socket */
fd = socket(AF_INET, SOCK_STREAM, 0);
m_logger->info("Connecting to rosbridge server ...");
if (fd < 0) {
std::cerr <<"can not create socket"<<std::endl;
m_logger->error("Failed to create socket");
exit(EXIT_FAILURE);
}
/* look up the address of the server given its name */
hp = gethostbyname(m_host);
if(!hp) {
std::cerr << "could not obtain address "<< m_host << std::endl;
m_logger->error("Culd not obtiain address");
exit(EXIT_FAILURE);
}
/* initialize sockaddr_in servaddr*/
memset((char*)&servaddr, 0, sizeof(servaddr));
/* connect to remote host (rosbridge_server in our case)*/
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port); /* convert to network byte order */
/* put host's address into th server address struct */
memcpy((void*)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
addrlen = sizeof(servaddr);
/*connect to server */
if (connect(fd, (struct sockaddr*)&servaddr, addrlen) < 0) {
cout << "Connection Failed \n";
m_logger->error("Connection Failed");
exit(EXIT_FAILURE);
}
m_logger->info("Connection established");
/* send json message to server */
int n = write(fd, json_message.c_str(), strlen(json_message.c_str()));
if (n < 0) {
cerr <<"Can not write to socket\n";
m_logger->error("Can not write to socket");
}
m_logger->info("Advertise request sent to rosbridge server");
}
void tcp_monitor_client::send_patient_data(double p_data) {
timeout.tv_sec = SOCKET_TIMEOUT_SEC;
timeout.tv_usec = SOCKET_TIMEOUT_uSEC;
FD_ZERO(&set); /* initialize set */
FD_SET(fd, &set);/* add descriptor to the read set */
rec_value = select(FD_SETSIZE,NULL, &set, NULL, &timeout);
if (rec_value == -1 ) {
cerr << "socket error\n";
m_logger->error("socket error");
exit(EXIT_FAILURE);
}
else if (rec_value == 0) {
/* on timeout -> keep calling the waiting_to_send function
*/
waiting_to_send();
}
else {
send_data_to_server(p_data);
}
}
void tcp_monitor_client::send_data_to_server(double p_data) {
document.SetObject();
Document::AllocatorType& allocator = document.GetAllocator();
Value object(kObjectType);
document.AddMember("op", "publish", allocator);
document.AddMember("topic", "/monitor", allocator);
object.AddMember("data", p_data, allocator);
document.AddMember("msg", object, allocator);
Writer<StringBuffer> writer(strbuf);
document.Accept(writer);
string json_msg = strbuf.GetString();
int i = send(fd, json_msg.c_str(), strlen(json_msg.c_str()), 0);
if (i < 0) {
cerr <<"Can not write MAP to socket\n";
m_logger->error("Can not write MAP to socket");
}
strbuf.Clear();
m_logger->debug("patient data sent to rosbridge server");
}
void tcp_monitor_client::waiting_to_send() {
m_logger->info("waiting for new values to send");
}
void tcp_monitor_client::clear() {
hp = nullptr;
}
tcp_monitor_client::~tcp_monitor_client() {
m_logger->debug("Shutting down socket connection");
close(fd);
clear();
delete hp;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment