Commit a85a5858 authored by fmg005's avatar fmg005

clean up

parent b647581d
......@@ -3,11 +3,6 @@
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.setup_connection(); /* 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.setup_connection(); /* connection setup to server via tcp socket */
......
......@@ -45,7 +45,6 @@ namespace CLA {
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 setup_connection();
......
......@@ -2,25 +2,17 @@
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 = 0;
SOCKET_TIMEOUT_uSEC = 5000000;
}
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) {
/* we can only talk to rosbrige server using json strings; here we want to
* `publish` to `monitor topic` -> messages of type `std_msgs/String`
*/
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 = 0;
SOCKET_TIMEOUT_uSEC = 500000;
SOCKET_TIMEOUT_uSEC = 500000;/* block for 0.5s */
}
void tcp_monitor_client::setup_connection() {
......@@ -28,15 +20,13 @@ void tcp_monitor_client::setup_connection() {
fd = socket(AF_INET, SOCK_STREAM, 0);
m_logger->debug("Connecting to rosbridge server for monitor data ...");
if (fd < 0) {
std::cerr <<"can not create socket"<<std::endl;
m_logger->error("Failed to create socket");
m_logger->error("Failed to create socket for publishing monitor data");
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");
m_logger->error("Could not obtiain address");
exit(EXIT_FAILURE);
}
/* initialize sockaddr_in servaddr*/
......@@ -49,16 +39,14 @@ void tcp_monitor_client::setup_connection() {
addrlen = sizeof(servaddr);
/*connect to server */
if (connect(fd, (struct sockaddr*)&servaddr, addrlen) < 0) {
cout << "Connection Failed \n";
m_logger->error("Connection Failed");
m_logger->error("Connection to monitor Failed");
exit(EXIT_FAILURE);
}
m_logger->info("Connection established");
m_logger->info("Connection to monitor established 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->error("Can not write monitor data to socket");
}
m_logger->debug("Advertise request to "+m_topic+" sent to rosbridge server");
}
......@@ -67,11 +55,10 @@ void tcp_monitor_client::publish_patient_data(const string& 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);
FD_SET(fd, &set);/* add descriptor to the write set */
rec_value = select(FD_SETSIZE, NULL, &set, NULL, &timeout);
if (rec_value == -1 ) {
cerr << "socket error\n";
m_logger->error("socket error");
m_logger->error("socket error while connecting to "+m_topic);
exit(EXIT_FAILURE);
}
else if (rec_value == 0) {
......@@ -101,15 +88,14 @@ void tcp_monitor_client::send_data_to_server(const string& p_data) {
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");
m_logger->error("Can not write to socket to publish "+m_topic+" data");
}
strbuf.Clear();
m_logger->debug("publishing patient data to "+m_topic+" topic " );
m_logger->debug("Publishing patient data to "+m_topic+" topic " );
}
void tcp_monitor_client::waiting_to_send() {
m_logger->info("waiting for new values to send");
m_logger->info("Waiting for new values to send ...");
}
void tcp_monitor_client::clear() {
......@@ -117,7 +103,7 @@ void tcp_monitor_client::clear() {
}
tcp_monitor_client::~tcp_monitor_client() {
m_logger->debug("Shutting down socket connection");
m_logger->debug("Shutting down monitor 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