script_sender.h
Go to the documentation of this file.
1 
2 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
3 
4 // -- BEGIN LICENSE BLOCK ----------------------------------------------
5 // Copyright 2019 FZI Forschungszentrum Informatik
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
29 #ifndef UR_CLIENT_LIBRARY_SCRIPT_SENDER_H_INCLUDED
30 #define UR_CLIENT_LIBRARY_SCRIPT_SENDER_H_INCLUDED
31 
32 #include <thread>
33 
35 #include "ur_client_library/log.h"
36 
37 namespace urcl
38 {
39 namespace comm
40 {
46 {
47 public:
48  ScriptSender() = delete;
55  ScriptSender(uint32_t port, const std::string& program) : server_(port), script_thread_(), program_(program)
56  {
58  std::bind(&ScriptSender::messageCallback, this, std::placeholders::_1, std::placeholders::_2));
59  server_.setConnectCallback(std::bind(&ScriptSender::connectionCallback, this, std::placeholders::_1));
60  server_.setDisconnectCallback(std::bind(&ScriptSender::disconnectionCallback, this, std::placeholders::_1));
61  server_.start();
62  }
63 
64 private:
66  std::thread script_thread_;
67  std::string program_;
68 
69  const std::string PROGRAM_REQUEST_ = std::string("request_program\n");
70 
71  void connectionCallback(const int filedescriptor)
72  {
73  URCL_LOG_DEBUG("New client connected at FD %d.", filedescriptor);
74  }
75 
76  void disconnectionCallback(const int filedescriptor)
77  {
78  URCL_LOG_DEBUG("Client at FD %d disconnected.", filedescriptor);
79  }
80 
81  void messageCallback(const int filedescriptor, char* buffer)
82  {
83  if (std::string(buffer) == PROGRAM_REQUEST_)
84  {
85  URCL_LOG_INFO("Robot requested program");
86  sendProgram(filedescriptor);
87  }
88  }
89 
90  void sendProgram(const int filedescriptor)
91  {
92  size_t len = program_.size();
93  const uint8_t* data = reinterpret_cast<const uint8_t*>(program_.c_str());
94  size_t written;
95 
96  if (server_.write(filedescriptor, data, len, written))
97  {
98  URCL_LOG_INFO("Sent program to robot");
99  }
100  else
101  {
102  URCL_LOG_ERROR("Could not send program to robot");
103  }
104  }
105 };
106 
107 } // namespace comm
108 } // namespace urcl
109 
110 #endif // UR_CLIENT_LIBRARY_SCRIPT_SENDER_H_INCLUDED
void setDisconnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients disconnecting from the server.
Definition: tcp_server.h:83
ScriptSender(uint32_t port, const std::string &program)
Creates a ScriptSender object, including a new TCPServer.
Definition: script_sender.h:55
#define URCL_LOG_ERROR(...)
Definition: log.h:37
void messageCallback(const int filedescriptor, char *buffer)
Definition: script_sender.h:81
void connectionCallback(const int filedescriptor)
Definition: script_sender.h:71
void setConnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients connecting to the server.
Definition: tcp_server.h:72
Wrapper class for a TCP socket server.
Definition: tcp_server.h:59
void start()
Start event handling.
Definition: tcp_server.cpp:307
#define URCL_LOG_DEBUG(...)
Definition: log.h:34
const std::string PROGRAM_REQUEST_
Definition: script_sender.h:69
bool write(const int fd, const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to a client.
Definition: tcp_server.cpp:314
void disconnectionCallback(const int filedescriptor)
Definition: script_sender.h:76
The ScriptSender class starts a TCPServer for a robot to connect to and waits for a request to receiv...
Definition: script_sender.h:45
#define URCL_LOG_INFO(...)
Definition: log.h:36
void setMessageCallback(std::function< void(const int, char *)> func)
This callback will be triggered on messages received on the socket.
Definition: tcp_server.h:94
void sendProgram(const int filedescriptor)
Definition: script_sender.h:90


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