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 int filedescriptor)
45 {
46  URCL_LOG_DEBUG("New client connected at FD %d.", filedescriptor);
47 }
48 
49 void ScriptSender::disconnectionCallback(const int filedescriptor)
50 {
51  URCL_LOG_DEBUG("Client at FD %d disconnected.", filedescriptor);
52 }
53 
54 void ScriptSender::messageCallback(const int 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 int 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
void setDisconnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients disconnecting from the server.
Definition: tcp_server.h:83
#define URCL_LOG_ERROR(...)
Definition: log.h:26
void disconnectionCallback(const int filedescriptor)
const std::string PROGRAM_REQUEST_
Definition: script_sender.h:64
void setConnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients connecting to the server.
Definition: tcp_server.h:72
void start()
Start event handling.
Definition: tcp_server.cpp:308
void messageCallback(const int filedescriptor, char *buffer)
void connectionCallback(const int filedescriptor)
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
void sendProgram(const int filedescriptor)
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:315
void setMessageCallback(std::function< void(const int, char *, int)> func)
This callback will be triggered on messages received on the socket.
Definition: tcp_server.h:94
#define URCL_LOG_INFO(...)
Definition: log.h:25


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47