Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • scs/ipaaca
  • ramin.yaghoubzadeh/ipaaca
2 results
Show changes
Showing
with 3880 additions and 27 deletions
cmake_minimum_required (VERSION 2.6)
# project name
project (ipaaca)
project (ipaaca_cpp_example_component)
## use the following line to enable console debug messages in ipaaca
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIPAACA_DEBUG_MESSAGES")
......@@ -9,19 +9,23 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIPAACA_DEBUG_MESSAGES")
# find cmake modules locally too
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
set(BOOSTLIBS boost_regex-mt boost_date_time-mt boost_program_options-mt boost_thread-mt boost_filesystem-mt boost_signals-mt boost_system-mt)
set(PROTOLIBS protobuf)
set(RSBLIBS rsc rsbcore)
find_package(Boost COMPONENTS system filesystem thread regex REQUIRED)
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${LIBS} ${BOOSTLIBS} ${PROTOLIBS} ${RSBLIBS})
find_package(Protobuf REQUIRED)
link_directories(${PROTOBUF_LIBRARY_DIRS})
include_directories(${PROTOBUF_INCLUDE_DIRS})
#set(RSBLIBS rsc rsbcore)
set(LIBS ${LIBS} ipaaca )
set(LIBS ${LIBS} ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES})
#${RSBLIBS})
# enhance the default search paths (headers, libs ...)
set(CMAKE_PREFIX_PATH ${PROJECT_SOURCE_DIR}:/opt/local:${CMAKE_PREFIX_PATH})
## Ace2 uses deprecated style (non-template friend functions and non-const char*s)
## We just hide the warnings here
#set(CXX_OLD_CODE_CONVENIENCE_FLAGS "-Wno-non-template-friend -Wno-write-strings")
# Compiler defines copied from the old build system
set(CXX_DEFINES "-D_BSD_SOURCE -DUSE_AV -DMGC_USE_DOUBLE -DLEDA_PREFIX -D__NO_CAST_TO_LOCAL_TYPE__ -DDBGLVL=0")
if (DEFINED APPLE)
......@@ -34,25 +38,20 @@ endif(DEFINED APPLE)
# Combine the extra compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_OLD_CODE_CONVENIENCE_FLAGS} ${CXX_DEFINES}")
# add include dir for auto-generated headers placed in build/
include_directories( ${PROJECT_SOURCE_DIR}/build )
# add local include directory
include_directories( ${PROJECT_SOURCE_DIR}/include )
# add lib and include directory from pulled dependencies
include_directories( ${PROJECT_SOURCE_DIR}/lib/include )
link_directories( ${PROJECT_SOURCE_DIR}/lib )
include_directories( ${PROJECT_SOURCE_DIR}/../../../dist/include ${PROJECT_SOURCE_DIR}/../../../deps/include )
link_directories( ${PROJECT_SOURCE_DIR}/../../../dist/lib ${PROJECT_SOURCE_DIR}/../../../deps/lib )
# specify source files for ipaaca (auto-generated ones are in build/ )
set (SOURCE
src/ipaaca.cc
build/ipaaca.pb.cc
src/example-component.cc
)
# compile all files to "ipaaca" shared library
add_library(ipaaca SHARED ${SOURCE})
# and link all the required external libs (found above using find_package etc.)
target_link_libraries(ipaaca ${LIBS})
add_executable(example-component ${SOURCE})
target_link_libraries(example-component ${LIBS})
set(DEFAULT_BIN_SUBDIR bin)
set(DEFAULT_LIB_SUBDIR lib)
......@@ -60,15 +59,9 @@ set(DEFAULT_DATA_SUBDIR share/data)
set(DEFAULT_INCLUDE_SUBDIR include)
set(CMAKE_INSTALL_PREFIX "")
install (
TARGETS ipaaca
TARGETS example-component
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
install(
DIRECTORY include
DESTINATION /
FILES_MATCHING PATTERN "*.h" PATTERN "*.hh" PATTERN "*.hpp" PATTERN "*.inl"
)
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 Sociable Agents Group
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
//
// Ipaaca 2 (ipaaca-rsb)
//
// *****************************
// *** ***
// *** C++ usage example ***
// *** ***
// *****************************
//
// Example class highlighting the use of the C++ interface
// to ipaaca2. This class uses a setup similar to an ipaaca1
// 'Component' (the concept does not exist anymore in ipaaca2).
//
#include <ipaaca/ipaaca.h>
#include <typeinfo>
using namespace ipaaca;
/// Test component for ipaaca2
/// The interface is much more flexible than in ipaaca1.
/// This example class provides an interface similar to ipaaca1.
class LegacyComponent {
protected:
/// Make a buffer pair, as in ipaaca1.
/// NOTE1: This is no longer a restriction. You can have
/// multiple buffers, no input buffer, etc.
/// NOTE2: Most objects are referred to using smart pointers
/// using the type name <className>::ptr - don't use '*'
OutputBuffer::ptr _out_buf;
InputBuffer::ptr _in_buf;
public:
/// Constructor to set up the component
LegacyComponent();
/// IU event handler function. Can be a member function,
/// a static function, anything. Use boost::bind on registration.
/// NOTE1: you can register any number of handlers on any Buffer.
/// NOTE2: this example function has the appropriate signature.
void handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
/// example publishing function to produce a 'grounded' IU
void publish_reply_iu(const std::string& text, const std::string& received_iu_uid);
void publish_reply_message(const std::string& text, const std::string& received_iu_uid);
void publish_hello_world();
};
LegacyComponent::LegacyComponent() {
/// First create the buffer pair
/// Create an output buffer
_out_buf = OutputBuffer::create("MyOutputBuffer");
/// Create an input buffer with category interest
/// NOTE: You can pass up to four categories as strings
// to the constructor, or use an std::vector<std::string>
_in_buf = InputBuffer::create("MyInputBuffer", "myCategoryInterest");
/// Now register the IU handler on both buffers.
/// NOTE1: we could register separate handlers instead.
/// NOTE2: boost::bind enables use of simple closures:
/// You could specify constants for the handler function, as long as the
/// remaining open arguments form the correct signature for IU handlers.
/// ** If you simply want to use a class member function, use it as below **
/// NOTE3: the Buffers are 'live' immediately on creation. As soon as
/// you connect a handler, it can be triggered (no backend connect etc.).
/// If this is not what you want, you should set a flag when you are
/// ready to actually start, and have the handlers honor that flag.
_out_buf->register_handler(boost::bind(&LegacyComponent::handle_iu_event, this, _1, _2, _3));
_in_buf->register_handler(boost::bind(&LegacyComponent::handle_iu_event, this, _1, _2, _3));
}
void LegacyComponent::handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
if (local) {
std::cout << "[Received update of self-owned IU]" << std::endl;
// could do processing here to cope with remote change of own IU
} else {
// event on a remote IU
if (event_type == IU_MESSAGE) {
std::cout << "[Received new Message!]" << std::endl;
std::string description = iu->payload()["description"];
std::cout << "[ Current description: " << description << "]" << std::endl;
/// let's also react by emitting an IU ourselves (function below)
publish_reply_iu("important-result", iu->uid());
} else if (event_type == IU_ADDED) {
std::cout << "[Received new IU!]" << std::endl;
/// new Payload class enables dynamic typing to some degree (numeric default 0)
std::string description = iu->payload()["description"];
double fraction = iu->payload()["fraction"];
/// let's also get the grounded-in links
std::set<std::string> grin_links = iu->get_links("GRIN");
std::cout << "[ Current description: " << description << "]" << std::endl;
/// let's also react by emitting an IU ourselves (function below)
publish_reply_message("important-result", iu->uid());
} else if (event_type == IU_UPDATED) {
std::cout << "[Received IU payload update for IU " << iu->uid() << "]" << std::endl;
std::string description = iu->payload()["description"];
std::cout << "[ Current description: " << description << "]" << std::endl;
} else if (event_type == IU_LINKSUPDATED) {
std::cout << "[IU links updated.]" << std::endl;
} else if (event_type == IU_COMMITTED) {
std::cout << "[IU " << iu->uid() << " has been committed to.]" << std::endl;
} else if (event_type == IU_RETRACTED) {
std::cout << "[IU " << iu->uid() << " has been retracted.]" << std::endl;
} else if (event_type == IU_DELETED) {
std::cout << "[IU " << iu->uid() << " has been deleted.]" << std::endl;
} else {
// Possible to stringify the type:
std::cout << "[(IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")]" << std::endl;
}
}
}
void LegacyComponent::publish_reply_iu(const std::string& text, const std::string& received_iu_uid) {
/// create a new IU
IU::ptr iu = IU::create( "myResultCategory" );
/// Add something to the payload
iu->payload()["description"] = "SomeResult";
/// Now add a grounded-in link pointing to the IU received before
/// There are no limitations to the link group names.
/// "GRIN" is a convention for the "grounded-in" function of the GAM.
iu->add_link("GRIN", received_iu_uid);
/// add to output buffer ( = "publish")
_out_buf->add(iu);
}
void LegacyComponent::publish_reply_message(const std::string& text, const std::string& received_iu_uid) {
IU::ptr iu = Message::create( "myResultCategory" );
iu->payload()["description"] = "SomeResult";
iu->add_link("GRIN", received_iu_uid);
_out_buf->add(iu);
}
void LegacyComponent::publish_hello_world() {
IU::ptr iu = Message::create( "myCategoryInterest"); //helloWorld" );
iu->payload()["description"] = "Hello world";
_out_buf->add(iu);
}
int main() {
std::cout << "Creating buffers..." << std::endl;
LegacyComponent compo;
sleep(1);
std::cout << "Publishing an initial IU..." << std::endl;
compo.publish_hello_world();
std::cout << "*** Running main loop, press Ctrl-C to cancel... ***" << std::endl;
/// NOTE: custom main loop no longer required.
while (true) sleep(1);
}
/**
* `b64.h' - b64
*
* copyright (c) 2014 joseph werle
*/
/*
From https://github.com/littlstar/b64.c/blob/master/LICENSE :
The MIT License (MIT)
Copyright (c) 2014 Little Star Media, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef B64_H
#define B64_H 1
// RYT start
#include <string>
std::string base64_encode(const std::string& bin);
std::string base64_decode(const std::string& b64);
// RYT end
/**
* Memory allocation functions to use. You can define b64_malloc and
* b64_realloc to custom functions if you want.
*/
#ifndef b64_malloc
# define b64_malloc(ptr) malloc(ptr)
#endif
#ifndef b64_realloc
# define b64_realloc(ptr, size) realloc(ptr, size)
#endif
/**
* Base64 index table.
*/
static const char b64_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
#ifdef __cplusplus
extern "C" {
#endif
/**
* Encode `unsigned char *' source with `size_t' size.
* Returns a `char *' base64 encoded string.
*/
char *
b64_encode (const unsigned char *, size_t);
/**
* Dencode `char *' source with `size_t' size.
* Returns a `unsigned char *' base64 decoded string.
*/
unsigned char *
b64_decode (const char *, size_t);
/**
* Dencode `char *' source with `size_t' size.
* Returns a `unsigned char *' base64 decoded string + size of decoded string.
*/
unsigned char *
b64_decode_ex (const char *, size_t, size_t *);
#ifdef __cplusplus
}
#endif
#endif
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
/**
* \file ipaaca-backend.h
*
* \brief Header file for abstract backend participant implementation
* (used in the core library and as a base to derive specific backends).
*
* Users should not include this file directly, but use ipaaca.h
*
* \b Note: This file is only included during compilation of ipaaca,
* for regular use, the full internal API is not exposed.
* Users generally need never touch the internal transport layer.
*
* \author Ramin Yaghoubzadeh Torky (ryaghoubzadeh@uni-bielefeld.de)
* \date January, 2019
*/
#ifndef __ipaaca_backend_mqtt_h_INCLUDED__
#define __ipaaca_backend_mqtt_h_INCLUDED__
#ifndef __ipaaca_h_INCLUDED__
#error "Please do not include this file directly, use ipaaca.h instead"
#endif
}
// Backend-specific include[s]
#include <mosquittopp.h>
namespace ipaaca {
#define _MQTT_REMOTE_SERVER_MAX_QUEUED_REQUESTS 0
namespace backend {
namespace mqtt {
//
// START of backend-specific implementation
//
// here: MQTT
// helper to encapsulate a wait-until-live mechanism
// you can take this for other backends and adopt the friend class name
class ParticipantCore {
friend class MQTTBackEnd;
protected:
IPAACA_MEMBER_VAR_EXPORT std::condition_variable _condvar;
IPAACA_MEMBER_VAR_EXPORT std::mutex _condvar_mutex;
IPAACA_MEMBER_VAR_EXPORT bool _running;
IPAACA_MEMBER_VAR_EXPORT bool _live;
protected:
IPAACA_HEADER_EXPORT ParticipantCore();
IPAACA_HEADER_EXPORT void signal_live();
IPAACA_HEADER_EXPORT bool wait_live(long timeout_milliseconds = 15000);
};
class MQTTParticipant: public ParticipantCore, public mosqpp::mosquittopp {
public:
typedef std::shared_ptr<MQTTParticipant> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT std::string _scope;
IPAACA_MEMBER_VAR_EXPORT std::string _client_id;
IPAACA_MEMBER_VAR_EXPORT std::string host;
IPAACA_MEMBER_VAR_EXPORT int port;
IPAACA_MEMBER_VAR_EXPORT int keepalive;
public:
IPAACA_HEADER_EXPORT MQTTParticipant(const MQTTParticipant& orig) = delete; // forbid copy-construction for backend
IPAACA_HEADER_EXPORT inline virtual ~MQTTParticipant() { }
IPAACA_HEADER_EXPORT MQTTParticipant(const std::string& client_id, const std::string& scope, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT void connect_and_background();
IPAACA_HEADER_EXPORT virtual void on_error();
IPAACA_HEADER_EXPORT virtual void on_disconnect(int rc);
IPAACA_HEADER_EXPORT static int get_next_mid();
/* // available mosquittopp callbacks:
virtual void on_connect(int rc) {return;}
virtual void on_disconnect(int rc) {return;}
virtual void on_publish(int mid) {return;}
virtual void on_message(const struct mosquitto_message * message) {return;}
virtual void on_subscribe(int mid, int qos_count, const int * granted_qos) {return;}
virtual void on_unsubscribe(int mid) {return;}
virtual void on_log(int level, const char * str) {return;}
virtual void on_error() {return;}
*/
};
class MQTTInformer: public MQTTParticipant, public Informer {
public:
typedef std::shared_ptr<MQTTInformer> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT std::string _client_id;
public:
IPAACA_HEADER_EXPORT MQTTInformer(const std::string& client_id, const std::string& scope, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT virtual void on_connect(int rc);
IPAACA_HEADER_EXPORT virtual bool internal_publish(const std::string& wire);
};
class MQTTListener: public MQTTParticipant, public Listener {
public:
typedef std::shared_ptr<MQTTListener> ptr;
public:
IPAACA_HEADER_EXPORT MQTTListener(const std::string& client_id, const std::string& scope, InputBuffer* buffer_ptr, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT virtual void on_connect(int rc);
IPAACA_HEADER_EXPORT virtual void on_subscribe(int mid, int qos_count, const int * granted_qos);
IPAACA_HEADER_EXPORT virtual void on_message(const struct mosquitto_message * message);
};
class MQTTLocalServer: public MQTTParticipant, public LocalServer {
public:
typedef std::shared_ptr<MQTTLocalServer> ptr;
protected:
IPAACA_HEADER_EXPORT void send_result_for_request(const std::string& request_endpoint, const std::string& request_uid, int64_t result);
public:
IPAACA_HEADER_EXPORT MQTTLocalServer(const std::string& client_id, const std::string& scope, ipaaca::OutputBuffer* buffer_ptr, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT virtual void on_connect(int rc);
IPAACA_HEADER_EXPORT virtual void on_subscribe(int mid, int qos_count, const int * granted_qos);
IPAACA_HEADER_EXPORT virtual void on_message(const struct mosquitto_message * message);
};
class MQTTRemoteServer: public MQTTParticipant, public RemoteServer {
public:
typedef std::shared_ptr<RemoteServer> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT std::map<std::string, PendingRequest::ptr> _pending_requests;
IPAACA_MEMBER_VAR_EXPORT ipaaca::Lock _pending_requests_lock;
IPAACA_MEMBER_VAR_EXPORT std::string _remote_end_scope; // this is the actual important scope,
// MQTTParticipant::_scope is repurposed here for receiving replies
IPAACA_MEMBER_VAR_EXPORT std::string _name; // using this (unique) auto-generated name
public:
IPAACA_HEADER_EXPORT MQTTRemoteServer(const std::string& client_id, const std::string& scope, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT int64_t request_remote_payload_update(std::shared_ptr<IUPayloadUpdate> update);
IPAACA_HEADER_EXPORT int64_t request_remote_link_update(std::shared_ptr<IULinkUpdate> update);
IPAACA_HEADER_EXPORT int64_t request_remote_commission(std::shared_ptr<protobuf::IUCommission> update);
IPAACA_HEADER_EXPORT int64_t request_remote_resend_request(std::shared_ptr<protobuf::IUResendRequest> update);
IPAACA_HEADER_EXPORT virtual void on_connect(int rc);
IPAACA_HEADER_EXPORT virtual void on_subscribe(int mid, int qos_count, const int * granted_qos);
IPAACA_HEADER_EXPORT virtual void on_message(const struct mosquitto_message * message);
IPAACA_HEADER_EXPORT PendingRequest::ptr queue_pending_request(Event::ptr request);
IPAACA_HEADER_EXPORT int64_t blocking_call(Event::ptr request);
};
class MQTTBackEnd: public BackEnd
{
public:
typedef std::shared_ptr<MQTTBackEnd> ptr;
friend class BackEndLibrary;
protected:
IPAACA_HEADER_EXPORT MQTTBackEnd();
public:
IPAACA_HEADER_EXPORT static BackEnd::ptr get();
IPAACA_HEADER_EXPORT void teardown();
IPAACA_HEADER_EXPORT Informer::ptr createInformer(const std::string& scope);
IPAACA_HEADER_EXPORT Listener::ptr createListener(const std::string& scope, InputBuffer* buf);
IPAACA_HEADER_EXPORT LocalServer::ptr createLocalServer(const std::string& scope, OutputBuffer* buf);
IPAACA_HEADER_EXPORT RemoteServer::ptr createRemoteServer(const std::string& scope);
IPAACA_HEADER_EXPORT inline std::string make_valid_scope(const std::string& scope) override { return scope; }
};
} // of namespace mqtt
} // of namespace backend
#endif // of __ipaaca_backend_mqtt_h_INCLUDED__
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
/**
* \file ipaaca-backend.h
*
* \brief Header file for abstract backend participant implementation
* (used in the core library and as a base to derive specific backends).
*
* Users should not include this file directly, but use ipaaca.h
*
* \b Note: This file is only included during compilation of ipaaca,
* for regular use, the full internal API is not exposed.
* Users generally need never touch the internal transport layer.
*
* \author Ramin Yaghoubzadeh Torky (ryaghoubzadeh@uni-bielefeld.de)
* \date January, 2019
*/
#ifndef __ipaaca_backend_ros_h_INCLUDED__
#define __ipaaca_backend_ros_h_INCLUDED__
#ifndef __ipaaca_h_INCLUDED__
#error "Please do not include this file directly, use ipaaca.h instead"
#endif
}
// Backend-specific include[s]
#include "ros/ros.h"
#include "std_msgs/String.h"
namespace ipaaca {
#define _ROS_REMOTE_SERVER_MAX_QUEUED_REQUESTS 0
namespace backend {
namespace ros {
//
// START of backend-specific implementation
//
// here: ROS
// helper to encapsulate a wait-until-live mechanism
// you can take this for other backends and adopt the friend class name
class ParticipantCore {
friend class ROSBackEnd;
protected:
IPAACA_MEMBER_VAR_EXPORT std::condition_variable _condvar;
IPAACA_MEMBER_VAR_EXPORT std::mutex _condvar_mutex;
IPAACA_MEMBER_VAR_EXPORT bool _running;
IPAACA_MEMBER_VAR_EXPORT bool _live;
protected:
IPAACA_HEADER_EXPORT ParticipantCore();
IPAACA_HEADER_EXPORT void signal_live();
IPAACA_HEADER_EXPORT bool wait_live(long timeout_milliseconds = 15000);
};
class ROSParticipant: public ParticipantCore {
public:
typedef std::shared_ptr<ROSParticipant> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT std::string _scope;
IPAACA_MEMBER_VAR_EXPORT std::string host;
IPAACA_MEMBER_VAR_EXPORT int port;
IPAACA_MEMBER_VAR_EXPORT int keepalive;
protected:
// a pointer; odd, but see the ROSBackEnd constructor & destructor
::ros::NodeHandle* _node_handle;
public:
IPAACA_HEADER_EXPORT ROSParticipant(const ROSParticipant& orig) = delete; // forbid copy-construction for backend
IPAACA_HEADER_EXPORT inline virtual ~ROSParticipant() { }
IPAACA_HEADER_EXPORT ROSParticipant(::ros::NodeHandle* node, const std::string& scope, Config::ptr config = nullptr);
};
class ROSInformer: public ROSParticipant, public Informer {
public:
typedef std::shared_ptr<ROSInformer> ptr;
protected:
::ros::Publisher _ros_pub;
public:
IPAACA_HEADER_EXPORT ROSInformer(::ros::NodeHandle* node, const std::string& scope, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT virtual bool internal_publish(const std::string& wire);
};
class ROSListener: public ROSParticipant, public Listener {
public:
typedef std::shared_ptr<ROSListener> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT ::ros::Subscriber _ros_sub;
public:
IPAACA_HEADER_EXPORT ROSListener(::ros::NodeHandle* node, const std::string& scope, InputBuffer* buffer_ptr, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT virtual void on_message(const std_msgs::String::ConstPtr& msg);
};
class ROSLocalServer: public ROSParticipant, public LocalServer {
public:
typedef std::shared_ptr<ROSLocalServer> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT ::ros::Subscriber _ros_sub;
IPAACA_MEMBER_VAR_EXPORT std::map<std::string, ::ros::Publisher> _ros_pubs;
protected:
IPAACA_HEADER_EXPORT void send_result_for_request(const std::string& request_endpoint, const std::string& request_uid, int64_t result);
IPAACA_HEADER_EXPORT ::ros::Publisher get_publisher(const std::string& endpoint);
public:
IPAACA_HEADER_EXPORT ROSLocalServer(::ros::NodeHandle* node, const std::string& scope, ipaaca::OutputBuffer* buffer_ptr, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT virtual void on_message(const std_msgs::String::ConstPtr& msg);
};
class ROSRemoteServer: public ROSParticipant, public RemoteServer {
public:
typedef std::shared_ptr<RemoteServer> ptr;
protected:
IPAACA_MEMBER_VAR_EXPORT std::map<std::string, PendingRequest::ptr> _pending_requests;
IPAACA_MEMBER_VAR_EXPORT ipaaca::Lock _pending_requests_lock;
IPAACA_MEMBER_VAR_EXPORT std::string _remote_end_scope; // this is the actual important scope,
// ROSParticipant::_scope is repurposed here for receiving replies
IPAACA_MEMBER_VAR_EXPORT std::string _name; // using this (unique) auto-generated name
IPAACA_MEMBER_VAR_EXPORT ::ros::Subscriber _ros_sub;
IPAACA_MEMBER_VAR_EXPORT ::ros::Publisher _ros_pub;
public:
IPAACA_HEADER_EXPORT ROSRemoteServer(::ros::NodeHandle* node, const std::string& scope, Config::ptr config = nullptr);
IPAACA_HEADER_EXPORT int64_t request_remote_payload_update(std::shared_ptr<IUPayloadUpdate> update);
IPAACA_HEADER_EXPORT int64_t request_remote_link_update(std::shared_ptr<IULinkUpdate> update);
IPAACA_HEADER_EXPORT int64_t request_remote_commission(std::shared_ptr<protobuf::IUCommission> update);
IPAACA_HEADER_EXPORT int64_t request_remote_resend_request(std::shared_ptr<protobuf::IUResendRequest> update);
IPAACA_HEADER_EXPORT PendingRequest::ptr queue_pending_request(Event::ptr request);
IPAACA_HEADER_EXPORT int64_t blocking_call(Event::ptr request);
IPAACA_HEADER_EXPORT virtual void on_message(const std_msgs::String::ConstPtr& msg);
};
class ROSBackEnd: public BackEnd
{
public:
typedef std::shared_ptr<ROSBackEnd> ptr;
friend class BackEndLibrary;
protected:
bool _need_init;
char* _cfakename;
::ros::NodeHandle* _node_handle;
::ros::AsyncSpinner* _spinner;
protected:
IPAACA_HEADER_EXPORT ROSBackEnd();
public:
IPAACA_HEADER_EXPORT static BackEnd::ptr get();
IPAACA_HEADER_EXPORT void teardown();
IPAACA_HEADER_EXPORT void init_once();
IPAACA_HEADER_EXPORT Informer::ptr createInformer(const std::string& scope);
IPAACA_HEADER_EXPORT Listener::ptr createListener(const std::string& scope, InputBuffer* buf);
IPAACA_HEADER_EXPORT LocalServer::ptr createLocalServer(const std::string& scope, OutputBuffer* buf);
IPAACA_HEADER_EXPORT RemoteServer::ptr createRemoteServer(const std::string& scope);
IPAACA_HEADER_EXPORT inline std::string make_valid_scope(const std::string& scope) override {
// strip the leading slash for the ROS node name (removing this
// extra rule would lead to a global (non-movable) namespace
if (scope.length() && (scope[0] == '/')) return scope.substr(1);
else return scope;
}
};
} // of namespace ros
} // of namespace backend
#endif // of __ipaaca_backend_ros_h_INCLUDED__
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.