Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UDPServer refactoring #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions include/abb_libegm/egm_udp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,34 @@ class UDPServer
*
* \param io_service for operating boost asio's asynchronous functions.
* \param port_number for the server's UDP socket.
* \param p_interface that processes the received messages.
* \param interface UDP server that processes the received messages.
*
* \throw \a std::exception if an error occurs.
*/
UDPServer(boost::asio::io_service& io_service,
unsigned short port_number,
AbstractUDPServerInterface* p_interface);
AbstractUDPServerInterface& interface);


/**
* \brief A destructor.
* \brief Start UDP server on a specified address and port.
*
* \param io_service for operating boost asio's asynchronous functions.
* \param server_endpoint server's UDP endpoint.
* \param interface UDP server that processes the received messages.
*
* \throw \a std::exception if an error occurs.
*/
~UDPServer();
UDPServer(boost::asio::io_service& io_service,
boost::asio::ip::udp::endpoint const& server_endpoint,
AbstractUDPServerInterface& interface);


/**
* \brief Checks if the server was successfully initialized or not.
*
* \return bool true if and only if the server was initialized correctly.
* \brief A destructor.
*/
bool isInitialized() const;
~UDPServer();


private:
/**
Expand Down Expand Up @@ -155,7 +166,7 @@ class UDPServer
/**
* \brief The server's UDP socket.
*/
boost::shared_ptr<boost::asio::ip::udp::socket> p_socket_;
boost::asio::ip::udp::socket socket_;

/**
* \brief The address of the calling computer (e.g. an ABB robot controller or a virtual controller in RobotStudio).
Expand All @@ -170,17 +181,12 @@ class UDPServer
/**
* \brief Pointer to an object that is derived from AbstractUDPSeverInterface, which processes the received messages.
*/
AbstractUDPServerInterface* p_interface_;
AbstractUDPServerInterface& interface_;

/**
* \brief Container for server data.
*/
UDPServerData server_data_;

/**
* \brief Flag indicating if the server was initialized successfully or not.
*/
bool initialized_;
};

} // end namespace egm
Expand Down
4 changes: 2 additions & 2 deletions src/egm_base_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ EGMBaseInterface::EGMBaseInterface(boost::asio::io_service& io_service,
const unsigned short port_number,
const BaseConfiguration& configuration)
:
udp_server_(io_service, port_number, this),
udp_server_(io_service, port_number, *this),
configuration_(configuration)
{
if (configuration_.active.use_logging)
Expand Down Expand Up @@ -913,7 +913,7 @@ bool EGMBaseInterface::initializeCallback(const UDPServerData& server_data)

bool EGMBaseInterface::isInitialized()
{
return udp_server_.isInitialized();
return true;
}

bool EGMBaseInterface::isConnected()
Expand Down
67 changes: 23 additions & 44 deletions src/egm_udp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,73 +48,52 @@ namespace egm

UDPServer::UDPServer(boost::asio::io_service& io_service,
unsigned short port_number,
AbstractUDPServerInterface* p_interface)
:
initialized_(false),
p_interface_(p_interface)
AbstractUDPServerInterface& interface)
: UDPServer {io_service, boost::asio::ip::udp::endpoint {boost::asio::ip::udp::v4(), port_number}, interface}
{
bool success = true;

try
{
server_data_.port_number = port_number;
p_socket_.reset(new boost::asio::ip::udp::socket(io_service,
boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(),
port_number)));
}
catch (std::exception e)
{
success = false;
}

if (success)
{
initialized_ = true;
startAsynchronousReceive();
}
}

UDPServer::~UDPServer()

UDPServer::UDPServer(boost::asio::io_service& io_service,
boost::asio::ip::udp::endpoint const& server_endpoint,
AbstractUDPServerInterface& interface)
: socket_ {io_service, server_endpoint}
, interface_ {interface}
{
if (p_socket_)
{
p_socket_->close();
p_socket_.reset();
}
server_data_.port_number = server_endpoint.port();
startAsynchronousReceive();
}

bool UDPServer::isInitialized() const

UDPServer::~UDPServer()
{
return initialized_;
socket_.close();
}

void UDPServer::startAsynchronousReceive()
{
if (p_socket_)
{
p_socket_->async_receive_from(boost::asio::buffer(receive_buffer_),
remote_endpoint_,
boost::bind(&UDPServer::receiveCallback,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
socket_.async_receive_from(boost::asio::buffer(receive_buffer_),
remote_endpoint_,
boost::bind(&UDPServer::receiveCallback,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void UDPServer::receiveCallback(const boost::system::error_code& error, const std::size_t bytes_transferred)
{
server_data_.p_data = receive_buffer_;
server_data_.bytes_transferred = (int) bytes_transferred;

if (error == boost::system::errc::success && p_interface_)
if (error == boost::system::errc::success)
{
// Process the received data via the callback method (creates the reply message).
const std::string& reply = p_interface_->callback(server_data_);
const std::string& reply = interface_.callback(server_data_);

if (!reply.empty() && p_socket_)
if (!reply.empty())
{
// Send the response message to the robot controller.
p_socket_->async_send_to(boost::asio::buffer(reply),
socket_.async_send_to(boost::asio::buffer(reply),
remote_endpoint_,
boost::bind(&UDPServer::sendCallback,
this,
Expand Down