Skip to content
Snippets Groups Projects
Commit 4b56e39c authored by Ramin Yaghoubzadeh's avatar Ramin Yaghoubzadeh
Browse files

1. ATTENTION: protobuf namespace changed!!

    ipaaca -> ipaaca.protobuf
2. C++ version, early development stage (committing as a backup)
parent abebfd51
No related branches found
No related tags found
No related merge requests found
language=cpp
resolve.status=beta
resource.path=${shared.resources}/Shared3DModels/resource;${shared.resources}/DefaultShaders/resource;${shared.resources}/LogbackConfigs/resource;${shared.resources}/HmiHumanoidBodyControl/resource;${shared.resources}/HmiHumanoidEmbodiments/resource;
rebuild.list=
<?xml version="1.0" encoding="UTF-8"?>
<project name="IpaacaCpp" default="run">
<import file="../../soashared/ant/build.xml" />
<target name="-pre-compilation">
<echo message="Compiling protobuf file" />
<exec executable="protoc">
<arg value="--proto_path=../proto" />
<arg value="../proto/ipaaca.proto" />
<arg value="--cpp_out=build/" />
</exec>
</target>
</project>
CONFIG = -DIPAACA_DEBUG_MESSAGES
SOURCES = ipaaca.cc ipaaca.pb.cc ipaaca-test-main.cc
CCFLAGS=-I. -I/usr/local/include -I/opt/local/include ${CONFIG}
BOOSTLIBS = -L/opt/local/lib -lboost_regex-mt -lboost_date_time-mt -lboost_program_options-mt -lboost_thread-mt -lboost_filesystem-mt -lboost_signals-mt -lboost_system-mt
PROTOLIBS = -L/opt/local/lib -lprotobuf
LIBS = ${BOOSTLIBS} ${PROTOLIBS} -L/usr/local/lib -lrsc -lrsbcore
all: protoc
#g++ ${CCFLAGS} -o ipaaca-test ${SOURCES} ${LIBS}
g++ ${CCFLAGS} -DMAKE_RECEIVER -o ipaaca-receiver ${SOURCES} ${LIBS}
g++ ${CCFLAGS} -DMAKE_SENDER -o ipaaca-sender ${SOURCES} ${LIBS}
protoc:
protoc --proto_path=../../proto ../../proto/ipaaca.proto --cpp_out=.
clean:
rm -f ipaaca-test ipaaca-sender ipaaca-receiver ipaaca.pb.h ipaaca.pb.cc
#include <ipaaca.h>
#include <typeinfo>
//#include <rsc/logging/Logger.h>
//#include <rsc/logging/LoggerFactory.h>
// //rsc::logging::LoggerFactory::getInstance().reconfigure(rsc::logging::Logger::LEVEL_ALL);
#ifdef MAKE_RECEIVER
//boost::mutex mtx;
using namespace ipaaca;
class Testo {
protected:
std::string _name;
public:
inline Testo(const std::string& name="Testo"): _name(name) { }
inline void handleIUEvent(EventPtr event)
{
std::cout << _name << " received a ";
std::string type = event->getType();
if (type == "ipaaca::IUPayloadUpdate") {
std::cout << *boost::static_pointer_cast<IUPayloadUpdate>(event->getData()) << std::endl;
} else if (type == "ipaaca::IULinkUpdate") {
std::cout << *boost::static_pointer_cast<IULinkUpdate>(event->getData()) << std::endl;
} else {
std::cout << type << " (Unhandled type!)" << std::endl;
}
}
};
int main() {
initialize_ipaaca_rsb();
ListenerPtr listener = Factory::getInstance().createListener( Scope("/tutorial/converter"));
Testo t("TESTO");
HandlerPtr event_handler = HandlerPtr(new EventFunctionHandler(boost::bind(&Testo::handleIUEvent, boost::ref(t), _1)));
listener->addHandler( event_handler );
while(true) {
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
}
return EXIT_SUCCESS;
}
#else
#ifdef MAKE_SENDER
using namespace ipaaca;
int main() {
initialize_ipaaca_rsb();
//Informer<ipaaca::IUPayloadUpdate>::Ptr pinformer = Factory::getInstance().createInformer<ipaaca::IUPayloadUpdate> ( Scope("/tutorial/converter"));
//Informer<ipaaca::IULinkUpdate>::Ptr linformer = Factory::getInstance().createInformer<ipaaca::IULinkUpdate> ( Scope("/tutorial/converter"));
Informer<AnyType>::Ptr informer = Factory::getInstance().createInformer<AnyType> ( Scope("/tutorial/converter"));
IUPayloadUpdate* pup = new ipaaca::IUPayloadUpdate();
Informer<ipaaca::IUPayloadUpdate>::DataPtr pdata(pup);
pup->uid = "2000";
pup->revision = 3;
pup->writer_name = "Comp1_OB";
pup->is_delta = true;
pup->new_items["new_key"] = "new_value";
pup->new_items["another_key"] = "some_info";
pup->keys_to_remove.push_back("old_key");
informer->publish(pdata);
IULinkUpdate* lup = new ipaaca::IULinkUpdate();
Informer<ipaaca::IULinkUpdate>::DataPtr ldata(lup);
lup->uid = "2001";
lup->revision = 4;
lup->writer_name = "Comp2_IB";
lup->is_delta = true;
lup->new_links["SLL"].insert("2000");
lup->new_links["grin"].insert("1002");
lup->links_to_remove["grin"].insert("1001");
informer->publish(ldata);
std::cout << "Done." << std::endl;
return EXIT_SUCCESS;
}
#else
//
// TESTS
//
using namespace ipaaca;
int main() {
//try{
initialize_ipaaca_rsb();
IU::ref iu = IU::create();
std::cout << "payload.get(\"TEST\") = \"" << iu->payload.get("TEST") << "\"" << std::endl;
std::cout << "payload[\"TEST\"] = \"" << (std::string) iu->payload["TEST"] << "\"" << std::endl;
iu->payload["TEST"] = "123.5-WAS-SET";
std::cout << "payload[\"TEST\"] = \"" << (std::string) iu->payload["TEST"] << "\"" << std::endl;
std::string s = "The string \"" + iu->payload["TEST"].to_str() + "\" is the new value.";
std::cout << "Concatenation test: " << s << std::endl;
std::cout << "Interpreted as long value: " << iu->payload["TEST"].to_int() << std::endl;
std::cout << "Interpreted as double value: " << iu->payload["TEST"].to_float() << std::endl;
//} catch (std::exception& e) {
// std::cout << e.what() << std::endl;
//}
}
#endif
#endif
#include <ipaaca.h>
#include <cstdlib>
namespace ipaaca {
/*
*/
void initialize_ipaaca_rsb()
{
ParticipantConfig config = ParticipantConfig::fromConfiguration();
Factory::getInstance().setDefaultParticipantConfig(config);
boost::shared_ptr<IUPayloadUpdateConverter> payload_update_converter(new IUPayloadUpdateConverter());
boost::shared_ptr<IULinkUpdateConverter> link_update_converter(new IULinkUpdateConverter());
stringConverterRepository()->registerConverter(payload_update_converter);
stringConverterRepository()->registerConverter(link_update_converter);
//IPAACA_TODO("initialize all converters")
}
std::ostream& operator<<(std::ostream& os, const IUPayloadUpdate& obj)
{
os << "PayloadUpdate(uid=" << obj.uid << ", revision=" << obj.revision;
os << ", writer_name=" << obj.writer_name << ", is_delta=" << (obj.is_delta?"True":"False");
os << ", new_items = {";
bool first = true;
for (std::map<std::string, std::string>::const_iterator it=obj.new_items.begin(); it!=obj.new_items.end(); ++it) {
if (first) { first=false; } else { os << ", "; }
os << "'" << it->first << "':'" << it->second << "'";
}
os << "}, keys_to_remove = [";
first = true;
for (std::vector<std::string>::const_iterator it=obj.keys_to_remove.begin(); it!=obj.keys_to_remove.end(); ++it) {
if (first) { first=false; } else { os << ", "; }
os << "'" << *it << "'";
}
os << "])";
return os;
}
std::ostream& operator<<(std::ostream& os, const IULinkUpdate& obj)
{
os << "LinkUpdate(uid=" << obj.uid << ", revision=" << obj.revision;
os << ", writer_name=" << obj.writer_name << ", is_delta=" << (obj.is_delta?"True":"False");
os << ", new_links = {";
bool first = true;
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj.new_links.begin(); it!=obj.new_links.end(); ++it) {
if (first) { first=false; } else { os << ", "; }
os << "'" << it->first << "': [";
bool ffirst = true;
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
if (ffirst) { ffirst=false; } else { os << ", "; }
os << "'" << *it2 << "'";
}
os << "]";
}
os << "}, links_to_remove = {";
first = true;
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj.links_to_remove.begin(); it!=obj.links_to_remove.end(); ++it) {
if (first) { first=false; } else { os << ", "; }
os << "'" << it->first << "': [";
bool ffirst = true;
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
if (ffirst) { ffirst=false; } else { os << ", "; }
os << "'" << *it2 << "'";
}
os << "]";
}
os << "})";
return os;
}
/*
void init_inprocess_too() {
//ParticipantConfig config = Factory::getInstance().getDefaultParticipantConfig();
ParticipantConfig config = ParticipantConfig::fromFile("rsb.cfg");
//ParticipantConfig::Transport inprocess = config.getTransport("inprocess");
//inprocess.setEnabled(true);
//config.addTransport(inprocess);
Factory::getInstance().setDefaultParticipantConfig(config);
}
*/
IU::ref IU::create(/* params */)
{
IU::ref iu = IU::ref(new IU(/* params */));
iu->payload.initialize(iu);
return iu;
}
// PayloadEntryProxy//{{{
PayloadEntryProxy::PayloadEntryProxy(Payload* payload, const std::string& key)
: _payload(payload), _key(key)
{
}
PayloadEntryProxy& PayloadEntryProxy::operator=(const std::string& value)
{
_payload->set(_key, value);
return *this;
}
PayloadEntryProxy::operator std::string()
{
return _payload->get(_key);
}
PayloadEntryProxy::operator long()
{
return atol(operator std::string().c_str());
}
PayloadEntryProxy::operator double()
{
return atof(operator std::string().c_str());
}
//}}}
// Payload//{{{
Payload::Payload()
{
}
void Payload::initialize(boost::shared_ptr<IUInterface> iu)
{
_iu = iu;
}
PayloadEntryProxy Payload::operator[](const std::string& key)
{
//boost::shared_ptr<PayloadEntryProxy> p(new PayloadEntryProxy(this, key));
return PayloadEntryProxy(this, key);
}
inline void Payload::set(const std::string& k, const std::string& v) {
//self._iu._modify_payload(self, isdelta=true, newitm={k:v}, keystorm=[], writer_name=None );
_store[k] = v;
}
inline void Payload::remove(const std::string& k) {
//self._iu._modify_payload(self, isdelta=true, newitm={}, keystorm=[k], writer_name=None );
_store.erase(k);
}
inline std::string Payload::get(const std::string& k) {
if (_store.count(k)>0) return _store[k];
else return IPAACA_PAYLOAD_DEFAULT_STRING_VALUE;
}
//}}}
/*
// IUConverter//{{{
IUConverter::IUConverter()
: Converter<std::string> ("ipaaca::IU", "ipaaca-iu", true)
{
}
std::string IUConverter::serialize(const AnnotatedData& data, std::string& wire)
{
// Ensure that DATA actually holds a datum of the data-type we expect.
assert(data.first == getDataType()); // "ipaaca::IU"
// NOTE: a dynamic_pointer_cast cannot be used from void*
boost::shared_ptr<const IU> obj = boost::static_pointer_cast<const IU> (data.second);
boost::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
for (std::map<std::string, std::string>::const_iterator it=obj->new_items.begin(); it!=obj->new_items.end(); ++it) {
protobuf::PayloadItem* item = pbo->add_new_items();
item->set_key(it->first);
item->set_value(it->second);
}
for (std::vector<std::string>::const_iterator it=obj->keys_to_remove.begin(); it!=obj->keys_to_remove.end(); ++it) {
pbo->add_keys_to_remove(*it);
}
pbo->SerializeToString(&wire);
return getWireSchema();
}
AnnotatedData IUConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "ipaaca-iu-payload-update"
boost::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
pbo->ParseFromString(wire);
boost::shared_ptr<IU> obj(new IU());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
for (int i=0; i<pbo->new_items_size(); i++) {
const protobuf::PayloadItem& it = pbo->new_items(i);
obj->new_items[it.key()] = it.value();
}
for (int i=0; i<pbo->keys_to_remove_size(); i++) {
obj->keys_to_remove.push_back(pbo->keys_to_remove(i));
}
return std::make_pair(getDataType(), obj);
}
//}}}
*/
// IUPayloadUpdateConverter//{{{
IUPayloadUpdateConverter::IUPayloadUpdateConverter()
: Converter<std::string> ("ipaaca::IUPayloadUpdate", "ipaaca-iu-payload-update", true)
{
}
std::string IUPayloadUpdateConverter::serialize(const AnnotatedData& data, std::string& wire)
{
assert(data.first == getDataType()); // "ipaaca::IUPayloadUpdate"
boost::shared_ptr<const IUPayloadUpdate> obj = boost::static_pointer_cast<const IUPayloadUpdate> (data.second);
boost::shared_ptr<protobuf::IUPayloadUpdate> pbo(new protobuf::IUPayloadUpdate());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
for (std::map<std::string, std::string>::const_iterator it=obj->new_items.begin(); it!=obj->new_items.end(); ++it) {
protobuf::PayloadItem* item = pbo->add_new_items();
item->set_key(it->first);
item->set_value(it->second);
item->set_type("str"); // FIXME other types than str (later)
}
for (std::vector<std::string>::const_iterator it=obj->keys_to_remove.begin(); it!=obj->keys_to_remove.end(); ++it) {
pbo->add_keys_to_remove(*it);
}
pbo->SerializeToString(&wire);
return getWireSchema();
}
AnnotatedData IUPayloadUpdateConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "ipaaca-iu-payload-update"
boost::shared_ptr<protobuf::IUPayloadUpdate> pbo(new protobuf::IUPayloadUpdate());
pbo->ParseFromString(wire);
boost::shared_ptr<IUPayloadUpdate> obj(new IUPayloadUpdate());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
for (int i=0; i<pbo->new_items_size(); i++) {
const protobuf::PayloadItem& it = pbo->new_items(i);
obj->new_items[it.key()] = it.value();
}
for (int i=0; i<pbo->keys_to_remove_size(); i++) {
obj->keys_to_remove.push_back(pbo->keys_to_remove(i));
}
return std::make_pair(getDataType(), obj);
}
//}}}
// IULinkUpdateConverter//{{{
IULinkUpdateConverter::IULinkUpdateConverter()
: Converter<std::string> ("ipaaca::IULinkUpdate", "ipaaca-iu-link-update", true)
{
}
std::string IULinkUpdateConverter::serialize(const AnnotatedData& data, std::string& wire)
{
assert(data.first == getDataType()); // "ipaaca::IULinkUpdate"
boost::shared_ptr<const IULinkUpdate> obj = boost::static_pointer_cast<const IULinkUpdate> (data.second);
boost::shared_ptr<protobuf::IULinkUpdate> pbo(new protobuf::IULinkUpdate());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj->new_links.begin(); it!=obj->new_links.end(); ++it) {
protobuf::LinkSet* links = pbo->add_new_links();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj->links_to_remove.begin(); it!=obj->links_to_remove.end(); ++it) {
protobuf::LinkSet* links = pbo->add_links_to_remove();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
pbo->SerializeToString(&wire);
return getWireSchema();
}
AnnotatedData IULinkUpdateConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "ipaaca-iu-link-update"
boost::shared_ptr<protobuf::IULinkUpdate> pbo(new protobuf::IULinkUpdate());
pbo->ParseFromString(wire);
boost::shared_ptr<IULinkUpdate> obj(new IULinkUpdate());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
for (int i=0; i<pbo->new_links_size(); ++i) {
const protobuf::LinkSet& it = pbo->new_links(i);
for (int j=0; j<it.targets_size(); ++j) {
obj->new_links[it.type()].insert(it.targets(j)); // = vec;
}
}
for (int i=0; i<pbo->links_to_remove_size(); ++i) {
const protobuf::LinkSet& it = pbo->links_to_remove(i);
for (int j=0; j<it.targets_size(); ++j) {
obj->links_to_remove[it.type()].insert(it.targets(j));
}
}
return std::make_pair(getDataType(), obj);
}
//}}}
} // of namespace ipaaca
#ifndef __IPAACA_H__
#define __IPAACA_H_
#ifdef IPAACA_DEBUG_MESSAGES
#define IPAACA_INFO(i) std::cout << __FILE__ << ":" << __LINE__ << ": " << __func__ << "() -- " << i << std::endl;
#define IPAACA_IMPLEMENT_ME(i) std::cout << __FILE__ << ":" << __LINE__ << ": " << __func__ << "() -- IMPLEMENT ME" << std::endl;
#define IPAACA_TODO(i) std::cout << __FILE__ << ":" << __LINE__ << ": " << __func__ << "() -- TODO: " << i << std::endl;
#else
#define IPAACA_INFO(i) ;
#define IPAACA_IMPLEMENT_ME(i) ;
#define IPAACA_TODO(i) ;
#endif
#define IPAACA_PAYLOAD_DEFAULT_STRING_VALUE ""
#include <iostream>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/pointer_cast.hpp>
#include <rsc/runtime/TypeStringTools.h>
#include <rsb/Factory.h>
#include <rsb/Handler.h>
#include <rsb/Event.h>
#include <rsb/converter/Repository.h>
#include <rsb/converter/ProtocolBufferConverter.h>
#include <rsb/converter/Converter.h>
#include <rsb/rsbexports.h>
#include <ipaaca.pb.h>
//using namespace boost;
using namespace rsb;
using namespace rsb::converter;
namespace ipaaca {
enum IUEventType {
ADDED,
COMMITTED,
DELETED,
RETRACTED,
UPDATED,
LINKSUPDATED
};
enum IUAccessMode {
PUSH,
REMOTE,
MESSAGE
};
class Payload;
class IUInterface;
class IU;
class RemotePushIU;
// class IULinkUpdate
// class IULinkUpdateConverter;
class IUStore;
class FrozenIUStore;
class IUEventHandler;
class Buffer {
};
class InputBuffer: public Buffer {
};
class OutputBuffer: public Buffer {
};
/*
class IUEventFunctionHandler: public rsb::EventFunctionHandler {
protected:
Buffer* _buffer;
public:
inline IUEventFunctionHandler(Buffer* buffer, const EventFunction& function, const std::string& method="")
: EventFunctionHandler(function, method), _buffer(buffer) { }
};
*/
class IUPayloadUpdate {
public:
std::string uid;
uint32_t revision;
std::string writer_name;
bool is_delta;
std::map<std::string, std::string> new_items;
std::vector<std::string> keys_to_remove;
friend std::ostream& operator<<(std::ostream& os, const IUPayloadUpdate& obj);
};
class IUPayloadUpdateConverter: public rsb::converter::Converter<std::string> {
public:
IUPayloadUpdateConverter();
std::string serialize(const rsb::AnnotatedData& data, std::string& wire);
rsb::AnnotatedData deserialize(const std::string& wireSchema, const std::string& wire);
};
class IULinkUpdate {
public:
std::string uid;
uint32_t revision;
std::string writer_name;
bool is_delta;
std::map<std::string, std::set<std::string> > new_links;
std::map<std::string, std::set<std::string> > links_to_remove;
friend std::ostream& operator<<(std::ostream& os, const IULinkUpdate& obj);
};
class IULinkUpdateConverter: public rsb::converter::Converter<std::string> {
public:
IULinkUpdateConverter();
std::string serialize(const rsb::AnnotatedData& data, std::string& wire);
rsb::AnnotatedData deserialize(const std::string& wireSchema, const std::string& wire);
};
void initialize_ipaaca_rsb();
class PayloadEntryProxy
{
protected:
Payload* _payload;
std::string _key;
public:
PayloadEntryProxy(Payload* payload, const std::string& key);
PayloadEntryProxy& operator=(const std::string& value);
operator std::string();
operator long();
operator double();
inline std::string to_str() { return operator std::string(); }
inline long to_int() { return operator long(); }
inline double to_float() { return operator double(); }
};
class Payload
{
protected:
std::map<std::string, std::string> _store;
boost::shared_ptr<IUInterface> _iu;
protected:
friend class IU;
friend class RemotePushIU;
Payload();
void initialize(boost::shared_ptr<IUInterface> iu);
public:
PayloadEntryProxy operator[](const std::string& key);
void set(const std::string& k, const std::string& v);
void remove(const std::string& k);
std::string get(const std::string& k);
};
class IUInterface {
public:
inline virtual ~IUInterface() { }
};
class IU: public IUInterface {
public:
Payload payload;
protected:
inline IU() { }
public:
inline ~IU() { }
static boost::shared_ptr<IU> create();
typedef boost::shared_ptr<IU> ref;
};
class RemotePushIU: public IUInterface {
public:
inline ~RemotePushIU() { }
};
class IUPublishedError: public std::exception
{
protected:
std::string _description;
public:
inline ~IUPublishedError() throw() { }
inline IUPublishedError() { //boost::shared_ptr<IU> iu) {
_description = "IUPublishedError";
}
const char* what() const throw() {
return _description.c_str();
}
};
} // of namespace ipaaca
#endif
[transport.spread]
host = localhost
port = 4803
enabled = 1
package ipaaca;
package ipaaca.protobuf;
message IntMessage {
required sint32 value = 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment