Commit dc70671f authored by fmg005's avatar fmg005

pump socket client to talk to rosbridge server

parent 5be19956
#ifndef UDP_CLIENT_H
#define UDP_CLIENT_H
#ifndef TCP_PUMP_CLIENT_H
#define TCP_PUMP_CLIENT_H
#include <sys/types.h>
#include <sys/socket.h> /* socket functions */
......@@ -12,6 +12,7 @@
#include <netdb.h> /* gethostbyname */
#include <netinet/in.h>/* sockaddr_in */
#include <iostream>
#include <memory>
#include "CLA_Logger.h"
#include "rapidjson/document.h"
......@@ -45,10 +46,10 @@ namespace CLA {
socklen_t addrlen;
Document document; /* to parse json data */
fd_set set; /* descriptor read set*/
CLA::LOGGER m_logger;
std::shared_ptr<CLA::LOGGER> m_logger;
public:
tcp_pump_client();
tcp_pump_client(const string&, const string&);
tcp_pump_client(std::shared_ptr<CLA::LOGGER>&);
tcp_pump_client(const string&, const string&, std::shared_ptr<CLA::LOGGER>&);
~tcp_pump_client();
void initialize();
double getCurrentInfusionRate(const string&);
......
......@@ -2,7 +2,7 @@
using namespace CLA;
tcp_pump_client::tcp_pump_client() : m_logger() {
tcp_pump_client::tcp_pump_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'
*/
......@@ -10,20 +10,21 @@ tcp_pump_client::tcp_pump_client() : m_logger() {
m_host = "localhost"; /* rosbridge server runs on localhost */
port = 9090; /* rosbridge server uses this port number */
SOCKET_TIMEOUT_SEC = 1;
SOCKET_TIMEOUT_uSEC = 500;
SOCKET_TIMEOUT_uSEC = 0;
m_bpdrug_last_sent_rate = 0;
m_last_sent_rate = 0;
m_new_rate = 0;
m_current_rate = 0;
}
tcp_pump_client::tcp_pump_client(const string& topic, const string& type): m_topic(move(topic)),
m_type(move(type)), m_logger() {
tcp_pump_client::tcp_pump_client(const string& topic, const string& type,
std::shared_ptr<CLA::LOGGER>& logger): m_topic(move(topic)), m_type(move(type)),
m_logger(logger) {
json_message = "{\"op\":\"subscribe\", \"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 = 500;
SOCKET_TIMEOUT_uSEC = 0;
m_bpdrug_last_sent_rate = 0;
m_last_sent_rate = 0;
m_new_rate = 0;
......@@ -33,15 +34,16 @@ m_type(move(type)), m_logger() {
void tcp_pump_client::initialize() {
/* create a UDP socket */
fd = socket(AF_INET, SOCK_STREAM, 0);
m_logger->debug("Connecting to rosbridge server ...");
if (fd < 0) {
fprintf(stderr, "can not create socket\n");
m_logger.error("Failed to create socket");
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) {
fprintf(stderr, "could not obtain address %s\n", m_host);
std::cerr << "could not obtain address " << m_host << std::endl;
exit(EXIT_FAILURE);
}
/* initialize sockaddr_in servaddr*/
......@@ -54,20 +56,21 @@ void tcp_pump_client::initialize() {
addrlen = sizeof(servaddr);
/* initialize buffer s*/
memset(buff, 0 , sizeof(buff));
/*connect to server */
if (connect(fd, (struct sockaddr*)&servaddr, addrlen) < 0) {
/*connect to server */
cout << "Connection Failed \n";
m_logger.error("TCP Connection Failed");
m_logger->error("Connection Failed");
exit(EXIT_FAILURE);
}
m_logger->debug("Connection established");
/* send json message to server */
int n = write(fd, json_message.c_str(), strlen(json_message.c_str()));
if (n < 0) {
cout <<"Can not write to socket\n";
m_logger.error("Can not write to socket");
exit(EXIT_FAILURE);
cerr <<"Can not write to socket\n";
m_logger->error("Can not write to socket");
}
m_logger->debug("subscription request sent to rosbridge server");
}
double tcp_pump_client::getCurrentInfusionRate(const string& substance) {
......@@ -77,7 +80,8 @@ double tcp_pump_client::getCurrentInfusionRate(const string& substance) {
FD_SET(fd, &set);/* add descriptor to the read set */
rec_value = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
if (rec_value == -1 ) {
cout << "socket error\n";
cerr << "socket error\n";
m_logger->error("socket error");
exit(EXIT_FAILURE);
}
else if (rec_value == 0) {
......@@ -100,12 +104,21 @@ double tcp_pump_client::get_new_rate(const string& substance) {
if (substance == "Norepinephrine") {
m_new_rate = document["msg"]["data"].GetDouble();
m_bpdrug_last_sent_rate = m_new_rate;
if (m_new_rate)
m_logger->debug("New infusion rate for Norepinephrine received: "+to_string(int(m_new_rate))+" mL/hr");
}
else if (substance == "Saline") {
m_new_rate = document["msg"]["data"].GetDouble();
m_saline_last_sent_rate = m_new_rate;
if (m_new_rate > 0)
m_logger->info("New infusion rate for Saline received "+to_string(int(m_new_rate))+" mL/hr");
}
}
else {
m_logger->error("Socket connection to rosbridge server lost");
exit(EXIT_FAILURE);
}
return m_new_rate;
}
......@@ -114,9 +127,11 @@ double tcp_pump_client::waiting_for_new_rate(const string& substance) {
* returning the last sent rate value
*/
if (substance == "Norepinephrine") {
m_logger->debug(substance+": waiting for new infusion rate ...");
m_last_sent_rate = m_bpdrug_last_sent_rate;
}
else if (substance == "Saline") {
m_logger->debug(substance +": waiting for new infusion rate ...");
m_last_sent_rate = m_saline_last_sent_rate;
}
return m_last_sent_rate;
......@@ -127,6 +142,7 @@ void tcp_pump_client::clear() {
}
tcp_pump_client::~tcp_pump_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