Skip to content
Snippets Groups Projects
Commit f0da184e authored by hvanwelbergen's avatar hvanwelbergen
Browse files
parents f22dbc4c 0eaf05b0
No related branches found
No related tags found
No related merge requests found
ipaaca.pb.cc
ipaaca.pb.h
ifeq ($(WBS_ARCH),mac)
LIB_SUFFIX=.dylib
else
LIB_SUFFIX=.so
endif
CONFIG = -DIPAACA_DEBUG_MESSAGES
SOURCES = ipaaca.cc ipaaca.pb.cc ipaaca-test-main.cc
IPAACASOURCES = ipaaca.cc ipaaca.pb.cc
SOURCES = ${IPAACASOURCES} ipaaca-test-main.cc
TEXTSOURCES = ${IPAACASOURCES} textsender.cc
CCFLAGS=-I. -I/usr/local/include -I/opt/local/include ${CONFIG}
LIBFLAGS=-fPIC -shared
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}
COMPILER = gfilt
all: lib
lib:
${COMPILER} ${CCFLAGS} ${IPAACASOURCES} ${LIBS} ${LIBFLAGS} -o libipaaca${LIB_SUFFIX}
receiver:
${COMPILER} ${CCFLAGS} -DMAKE_RECEIVER -o ipaaca-receiver ${SOURCES} ${LIBS}
sender:
${COMPILER} ${CCFLAGS} -DMAKE_SENDER -o ipaaca-sender ${SOURCES} ${LIBS}
main:
${COMPILER} ${CCFLAGS} -o ipaaca-main ${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
rm -f libipaaca${LIB_SUFFIX} ipaaca-main ipaaca-sender ipaaca-receiver ipaaca.pb.h ipaaca.pb.cc
......@@ -5,105 +5,76 @@
//#include <rsc/logging/LoggerFactory.h>
// //rsc::logging::LoggerFactory::getInstance().reconfigure(rsc::logging::Logger::LEVEL_ALL);
#ifdef MAKE_RECEIVER
//boost::mutex mtx;
using namespace ipaaca;
//
// TESTS
//
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;
}
}
};
using namespace ipaaca;
#ifdef MAKE_RECEIVER
void my_first_iu_handler(IUInterface::ptr iu, IUEventType type, bool local)
{
std::cout << "" << iu_event_type_to_str(type) << " " << (local?"(of local IU)":"(of remote IU)") << "" << std::endl;
if (type == IU_LINKSUPDATED) {
std::cout << " setting something in the remote payload" << std::endl;
iu->payload()["new_field"] = "remotely_set";
}
}
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));
try{
//initialize_ipaaca_rsb();
InputBuffer::ptr ib = InputBuffer::create("Tester", "testcategory");
ib->register_handler(my_first_iu_handler);
while (true) {
sleep(1);
}
} catch (ipaaca::Exception& e) {
std::cout << "== IPAACA EXCEPTION == " << e.what() << std::endl;
}
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;
void iu_handler_for_remote_changes(IUInterface::ptr iu, IUEventType type, bool local)
{
std::cout << "" << iu_event_type_to_str(type) << " " << (local?"(of local IU)":"(of remote IU)") << "" << std::endl;
}
#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;
try{
//initialize_ipaaca_rsb();
std::string s = "The string \"" + iu->payload["TEST"].to_str() + "\" is the new value.";
OutputBuffer::ptr ob = OutputBuffer::create("Tester");
ob->register_handler(iu_handler_for_remote_changes);
//std::cout << "Buffer: " << ob->unique_name() << std::endl;
IU::ptr iu = IU::create("testcategory");
ob->add(iu);
std::cout << "Updating in 1 sec" << std::endl;
sleep(1);
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;
//}
iu->add_link("grin", "DUMMY_IU_UID_1234efef1234");
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;
std::cout << "Committing and quitting in 1 sec" << std::endl;
sleep(1);
iu->commit();
} catch (ipaaca::Exception& e) {
std::cout << "== IPAACA EXCEPTION == " << e.what() << std::endl;
}
}
#endif
......
This diff is collapsed.
This diff is collapsed.
ipaaca.pb.cc
ipaaca.pb.h
CONFIG = -DIPAACA_DEBUG_MESSAGES
IPAACASOURCES = ../../src/ipaaca.cc ipaaca.pb.cc
TEXTSOURCES = ${IPAACASOURCES} textsender.cc
CCFLAGS=-I. -I../../src -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
COMPILER = gfilt
all: protoc textsender
textsender:
${COMPILER} ${CCFLAGS} -o textsender ${TEXTSOURCES} ${LIBS}
protoc:
protoc --proto_path=../../../proto ../../../proto/ipaaca.proto --cpp_out=.
clean:
rm -f textsender ipaaca.pb.h ipaaca.pb.cc
#include <ipaaca.h>
#include <typeinfo>
using namespace ipaaca;
const char RECV_CATEGORY[] = "WORD";
const char SEND_CATEGORY[] = "TEXT";
class TextSender {
protected:
OutputBuffer::ptr _ob;
InputBuffer::ptr _ib;
public:
TextSender();
void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
void inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
IUInterface::ptr find_last_iu();
void publish_text_to_print(const std::string& text, const std::string& parent_iu_uid="");
};
TextSender::TextSender() {
_ob = OutputBuffer::create("TextSenderOut");
_ob->register_handler(boost::bind(&TextSender::outbuffer_handle_iu_event, this, _1, _2, _3));
_ib = InputBuffer::create("TextSenderIn", RECV_CATEGORY);
_ib->register_handler(boost::bind(&TextSender::inbuffer_handle_iu_event, this, _1, _2, _3));
}
void TextSender::outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
std::cout << "(own IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
if (event_type == IU_UPDATED) {
std::set<std::string> parent_uids = iu->get_links("GRIN");
if (parent_uids.size() > 0) {
std::string parent_uid = *(parent_uids.begin());
std::cout << "updating parent ..." << std::endl;
std::set<std::string> next_uids = iu->get_links("SUCCESSOR");
if (next_uids.size() > 0) {
std::string next_uid = *(next_uids.begin());
IUInterface::ptr next_iu = _ob->get(next_uid);
std::set<std::string> next_letter_grin_links = next_iu->get_links("GRIN");
if (next_letter_grin_links.count(parent_uid) == 0) {
// next letter belongs to new word
IUInterface::ptr parent_iu = _ib->get(parent_uid);
parent_iu->payload()["STATE"] = "REALIZED";
} else {
IUInterface::ptr parent_iu = _ib->get(parent_uid);
parent_iu->payload()["STATE"] = "STARTED";
}
} else {
// there are no more letters, this is the end of the final word
IUInterface::ptr parent_iu = _ib->get(parent_uid);
parent_iu->payload()["STATE"] = "REALIZED";
}
std::cout << " ... done." << std::endl;
}
} else {
}
}
void TextSender::inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
if (event_type == IU_LINKSUPDATED) {
std::cout << "links updated" << std::endl;
} else if (event_type == IU_ADDED) {
std::string word = iu->payload()["WORD"];
std::cout << "Received new word: " << word << std::endl;
publish_text_to_print(word, iu->uid());
} else if (event_type == IU_RETRACTED) {
std::string retracted_uid = iu->uid();
} else {
std::cout << "(IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
}
}
IUInterface::ptr TextSender::find_last_iu() {
std::set<IUInterface::ptr> ius = _ob->get_ius();
for (std::set<IUInterface::ptr>::iterator it = ius.begin(); it!=ius.end(); ++it) {
if ((*it)->get_links("SUCCESSOR").size() == 0) return *it;
}
return IUInterface::ptr();
}
void TextSender::publish_text_to_print(const std::string& text, const std::string& parent_iu_uid) {
IUInterface::ptr previous_iu = find_last_iu();
if (previous_iu) {
// insert a blank if we already have words in the buffer
IU::ptr iu = IU::create( SEND_CATEGORY );
iu->payload()["CONTENT"] = " ";
_ob->add(iu);
previous_iu->add_link( "SUCCESSOR", iu->uid() );
iu->add_link( "PREDECESSOR", previous_iu->uid() );
if (parent_iu_uid != "") iu->add_link( "GRIN", parent_iu_uid );
previous_iu = iu;
}
for (int i=0; i<text.size(); ++i) {
IU::ptr iu = IU::create( SEND_CATEGORY );
iu->payload()["CONTENT"] = std::string(1, text.at(i));
_ob->add(iu);
if (previous_iu) {
previous_iu->add_link( "SUCCESSOR", iu->uid() );
iu->add_link( "PREDECESSOR", previous_iu->uid() );
if (parent_iu_uid != "") iu->add_link( "GRIN", parent_iu_uid );
}
if (previous_iu) std::cout << "previous IU: " << *previous_iu << std::endl;
previous_iu = iu;
}
}
int main() {
TextSender sender;
sleep(1);
sender.publish_text_to_print("(INIT)");
std::cout << "Press Ctrl-C to cancel..." << std::endl;
while (true) sleep(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