src/control/script_sender.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 2021 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 
29 
30 namespace urcl
31 {
32 namespace control
33 {
34 ScriptSender::ScriptSender(uint32_t port, const std::string& program)
35  : server_(port), script_thread_(), program_(program)
36 {
38  std::bind(&ScriptSender::messageCallback, this, std::placeholders::_1, std::placeholders::_2));
39  server_.setConnectCallback(std::bind(&ScriptSender::connectionCallback, this, std::placeholders::_1));
40  server_.setDisconnectCallback(std::bind(&ScriptSender::disconnectionCallback, this, std::placeholders::_1));
41  server_.start();
42 }
43 
44 void ScriptSender::connectionCallback(const socket_t filedescriptor)
45 {
46  URCL_LOG_DEBUG("New client connected at FD %d.", filedescriptor);
47 }
48 
50 {
51  URCL_LOG_DEBUG("Client at FD %d disconnected.", filedescriptor);
52 }
53 
54 void ScriptSender::messageCallback(const socket_t filedescriptor, char* buffer)
55 {
56  if (std::string(buffer) == PROGRAM_REQUEST_)
57  {
58  URCL_LOG_INFO("Robot requested program");
59  sendProgram(filedescriptor);
60  }
61 }
62 
63 void ScriptSender::sendProgram(const socket_t filedescriptor)
64 {
65  size_t len = program_.size();
66  const uint8_t* data = reinterpret_cast<const uint8_t*>(program_.c_str());
67  size_t written;
68 
69  if (server_.write(filedescriptor, data, len, written))
70  {
71  URCL_LOG_INFO("Sent program to robot");
72  }
73  else
74  {
75  URCL_LOG_ERROR("Could not send program to robot");
76  }
77 }
78 
79 } // namespace control
80 } // namespace urcl
urcl::control::ScriptSender::sendProgram
void sendProgram(const socket_t filedescriptor)
Definition: src/control/script_sender.cpp:63
urcl::control::ScriptSender::PROGRAM_REQUEST_
const std::string PROGRAM_REQUEST_
Definition: script_sender.h:64
urcl::control::ScriptSender::messageCallback
void messageCallback(const socket_t filedescriptor, char *buffer)
Definition: src/control/script_sender.cpp:54
socket_t
int socket_t
Definition: socket_t.h:57
urcl::comm::TCPServer::start
void start()
Start event handling.
Definition: tcp_server.cpp:302
urcl
Definition: bin_parser.h:36
urcl::control::ScriptSender::connectionCallback
void connectionCallback(const socket_t filedescriptor)
Definition: src/control/script_sender.cpp:44
URCL_LOG_ERROR
#define URCL_LOG_ERROR(...)
Definition: log.h:26
urcl::control::ScriptSender::disconnectionCallback
void disconnectionCallback(const socket_t filedescriptor)
Definition: src/control/script_sender.cpp:49
URCL_LOG_DEBUG
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
urcl::comm::TCPServer::setDisconnectCallback
void setDisconnectCallback(std::function< void(const socket_t)> func)
This callback will be triggered on clients disconnecting from the server.
Definition: tcp_server.h:92
urcl::control::ScriptSender::ScriptSender
ScriptSender()=delete
urcl::comm::TCPServer::setMessageCallback
void setMessageCallback(std::function< void(const socket_t, char *, int)> func)
This callback will be triggered on messages received on the socket.
Definition: tcp_server.h:103
urcl::comm::TCPServer::write
bool write(const socket_t fd, const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to a client.
Definition: tcp_server.cpp:309
URCL_LOG_INFO
#define URCL_LOG_INFO(...)
Definition: log.h:25
urcl::control::ScriptSender::program_
std::string program_
Definition: script_sender.h:62
script_sender.h
urcl::control::ScriptSender::server_
comm::TCPServer server_
Definition: script_sender.h:60
urcl::comm::TCPServer::setConnectCallback
void setConnectCallback(std::function< void(const socket_t)> func)
This callback will be triggered on clients connecting to the server.
Definition: tcp_server.h:81


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58