dashboard_client.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2019 FZI Forschungszentrum Informatik
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // -- END LICENSE BLOCK ------------------------------------------------
18 
19 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 
28 #include <regex>
29 #include <ur_client_library/log.h>
32 
33 namespace urcl
34 {
35 DashboardClient::DashboardClient(const std::string& host) : host_(host), port_(DASHBOARD_SERVER_PORT)
36 {
37 }
38 
40 {
42  {
43  URCL_LOG_ERROR("%s", "Socket is already connected. Refusing to reconnect.");
44  return false;
45  }
46  bool ret_val = false;
47  if (TCPSocket::setup(host_, port_))
48  {
49  URCL_LOG_INFO("%s", read().c_str());
50  ret_val = true;
51  }
52  return ret_val;
53 }
54 
56 {
57  URCL_LOG_INFO("Disconnecting from Dashboard server on %s:%d", host_.c_str(), port_);
58  TCPSocket::close();
59 }
60 
61 bool DashboardClient::send(const std::string& text)
62 {
63  size_t len = text.size();
64  const uint8_t* data = reinterpret_cast<const uint8_t*>(text.c_str());
65  size_t written;
66  return TCPSocket::write(data, len, written);
67 }
68 
69 std::string DashboardClient::read()
70 {
71  std::stringstream result;
72  char character;
73  size_t read_chars = 99;
74  while (read_chars > 0)
75  {
76  if (!TCPSocket::read((uint8_t*)&character, 1, read_chars))
77  {
78  disconnect();
79  throw TimeoutException("Did not receive answer from dashboard server in time. Disconnecting from dashboard "
80  "server.",
81  *recv_timeout_);
82  }
83  result << character;
84  if (character == '\n')
85  {
86  break;
87  }
88  }
89  return result.str();
90 }
91 
92 std::string DashboardClient::sendAndReceive(const std::string& text)
93 {
94  std::string response = "ERROR";
95  std::lock_guard<std::mutex> lock(write_mutex_);
96  if (send(text))
97  {
98  response = read();
99  }
100  else
101  {
102  throw UrException("Failed to send request to dashboard server. Are you connected to the Dashboard Server?");
103  }
104  rtrim(response);
105 
106  return response;
107 }
108 
109 } // namespace urcl
#define URCL_LOG_ERROR(...)
Definition: log.h:37
bool send(const std::string &text)
std::unique_ptr< timeval > recv_timeout_
Definition: tcp_socket.h:63
void rtrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
std::string sendAndReceive(const std::string &command)
Sends a command through the socket and waits for an answer.
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:77
bool connect()
Opens a connection to the dasboard server on the host as specified in the constructor.
A specialized exception representing that communication to the tool is not possible.
Definition: exceptions.h:108
Socket is connected and ready to use.
void disconnect()
Makes sure no connection to the dashboard server is held inside the object.
#define URCL_LOG_INFO(...)
Definition: log.h:36
Our base class for exceptions. Specialized exceptions should inherit from those.
Definition: exceptions.h:40


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26