From 2c7d7c40d69cc9aa4dead162ea0634213292a9bc Mon Sep 17 00:00:00 2001 From: Patrick Debois Date: Thu, 19 Apr 2012 10:02:42 +0200 Subject: [PATCH] basic shared lib structure --- .gitignore | 4 + src/Makefile | 27 +++++++ src/agent/base.cpp | 4 + src/agent/base.h | 9 +++ src/agent/discovery.cpp | 138 +++++++++++++++++++++++++++++++++++ src/agent/discovery.h | 0 src/agent/puppetd.cpp | 0 src/agent/puppetd.h | 0 src/main.cpp | 6 ++ src/mcollective_listener.cpp | 45 ++++++++++++ src/mcollective_listener.h | 16 ++++ 11 files changed, 249 insertions(+) create mode 100644 .gitignore create mode 100644 src/Makefile create mode 100644 src/agent/base.cpp create mode 100644 src/agent/base.h create mode 100644 src/agent/discovery.cpp create mode 100644 src/agent/discovery.h create mode 100644 src/agent/puppetd.cpp create mode 100644 src/agent/puppetd.h create mode 100644 src/main.cpp create mode 100644 src/mcollective_listener.cpp create mode 100644 src/mcollective_listener.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57a663a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +.*\.swp +mcpp +*.so diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..026c89a --- /dev/null +++ b/src/Makefile @@ -0,0 +1,27 @@ +#------------------------------------------------------------------------------ +# --soname vs macosx +# http://www.corpit.ru/pipermail/udns/2011q1/000124.html + +SOURCE=mc-discovery.cpp +#SOURCE=x1.cpp +PROGRAM=mcpp +MYINCLUDES=-I/opt/include/activemq-cpp-3.4.1 + +MYLIBRARIES= +#MYLIBRARIES=-L/opt/lib -lactivemq-cpp -lyaml-cpp -lapr-1 -laprutil-1 -lpthread +MYLIBRARIES=-L/opt/lib -lactivemq-cpp -lyaml-cpp -lapr-1 -laprutil-1 -lcrypto -lssl -lpthread +#MYLIBRARIES=-L/opt/lib -Wl,-Bstatic -lssl -lcrypto -lapr-1 -laprutil-1 -lactivemq-cpp -lyaml-cpp -Wl,-Bdynamic -lpthread -lssl -lcrypto +#MYLIBRARIES=-L/usr/lib64 -Wl,-Bstatic -lssl -lcrypto -L/opt/lib -Wl,-Bdynamic -lapr-1 -laprutil-1 -lactivemq-cpp -lyaml-cpp -lpthread +CXX=g++ + +#------------------------------------------------------------------------------ + +main: libmcollective.so main.o + $(CXX) -o $(PROGRAM) main.o -L. -lmcollective + +libmcollective.so: mcollective_listener.cpp + $(CXX) -fPIC -c mcollective_listener.cpp -o mcollective.o + $(CXX) -shared -Wl -o libmcollective.so mcollective.o + +clean: + rm *.o *.so $(PROGRAM) diff --git a/src/agent/base.cpp b/src/agent/base.cpp new file mode 100644 index 0000000..6460e94 --- /dev/null +++ b/src/agent/base.cpp @@ -0,0 +1,4 @@ +#include "base.h" + +void agentbase::setx(int newx) { myx = newx; } +int agentbase::getx() { return myx; } diff --git a/src/agent/base.h b/src/agent/base.h new file mode 100644 index 0000000..5445d19 --- /dev/null +++ b/src/agent/base.h @@ -0,0 +1,9 @@ +class agentbase { + int myx; + + public: + + agentbase() { myx=0; } + void setx(int newx); + int getx(); +}; diff --git a/src/agent/discovery.cpp b/src/agent/discovery.cpp new file mode 100644 index 0000000..081a4cc --- /dev/null +++ b/src/agent/discovery.cpp @@ -0,0 +1,138 @@ +//Constructor passes the session + +// // Create the destination (Topic or Queue) +// destination = session->createTopic ("mcollective.discovery.command"); + + +// // Create a MessageConsumer from the Session to the Topic or Queue +// consumer = session->createConsumer (destination); + +// Set listener (a class that has onMessage(const Message * message) throw() +// consumer->setMessageListener (this); +// +// +// + + +// Called from the consumer since this class is a registered MessageListener +// virtual void +// onMessage (const Message * message) +// throw () + +// extract message to yaml and pass it to handle + + // const BytesMessage * + // bytesMessage = dynamic_cast < const + // BytesMessage * >(message); + +// if (bytesMessage != NULL) +// { +// //std::cout << bytesMessage->getBodyBytes(); +// //bytesMessage->reset(); +// +// size_t i = bytesMessage->getBodyLength (); +// printf ("%lu", i); +// ofstream ofs ("message.yaml", ofstream::out); +// for (int x = 1; x <= i; x++) +// { +// ofs << bytesMessage->readByte (); +// } +// ofs.flush (); +// ofs.close (); +// +// try +// { +// std::ifstream fin ("message.yaml"); +// //std::stringstream fin(std::string(bytesMessage->getBodyBytes())); +// YAML::Parser parser (fin); +// YAML::Node doc; +// // We assume only the first doc, need to check with doc.size +// parser.GetNextDocument (doc); + + +/////////////////////// String to YAML +// std::stringstream bodystream (body); +// YAML::Parser bodyparser (bodystream); +// YAML::Node bodydoc; +// std::string action; +// bodyparser.GetNextDocument (bodydoc); +// bodydoc >> action; +// std::cout << action; + +/////////////// Construct body +// // Construct YAML body +// YAML::Emitter reply_message_body_yaml; +// reply_message_body_yaml << "pong"; + +///////////// MD5 sign body +// std::string reply_message_body = reply_message_body_yaml.c_str(); +// std::cout << reply_message_body << std::endl; +// // Append PSK to it +// std::string psk = "unset"; +// std::string body_psk = reply_message_body; +// body_psk.append(psk); +// std::stringstream md5sumstream; + +// // MD5 - https://gist.github.com/2389719 +// // but needs a real string +// // http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/e1774395-ba99-4fe6-98eb-2224a67984b9 +// unsigned char md5_result[MD5_DIGEST_LENGTH]; +// const unsigned char * constStr = reinterpret_cast (body_psk.c_str()); +// MD5(constStr, body_psk.length() , md5_result); +// for (i=0; i < MD5_DIGEST_LENGTH; i++) +// { +// printf("%02x", md5_result[i]); +// char digit[2]; +// sprintf(digit,"%02x", md5_result[i]); +// md5sumstream << digit; +// } +// printf("\\n"); +// + + +// Construct answer + +// YAML::Emitter reply_message_yaml; +// +// reply_message_yaml << YAML::BeginMap; +// reply_message_yaml << YAML::Key << ":msgtime"; +// reply_message_yaml << YAML::Value << 1010101; +// reply_message_yaml << YAML::Key << ":requestid"; +// reply_message_yaml << YAML::Value << requestid; +// reply_message_yaml << YAML::Key << ":body"; +// reply_message_yaml << YAML::Value << reply_message_body; +// reply_message_yaml << YAML::Key << ":senderid"; +// reply_message_yaml << YAML::Value << "mcpp"; +// reply_message_yaml << YAML::Key << ":senderagent"; +// reply_message_yaml << YAML::Value << "discovery"; +// reply_message_yaml << YAML::Key << ":msgtarget"; +// reply_message_yaml << YAML::Value << "/topic/mcollective.discovery.reply"; +// +// reply_message_yaml << YAML::Key << ":hash"; +// reply_message_yaml << YAML::Value << hash; +// reply_message_yaml << YAML::EndMap; +// +// +// // Put it in a string +// std::string reply_message = reply_message_yaml.c_str(); +// std::cout << reply_message << std::endl; + + // // Create the destination (Topic or Queue) + // destination = session->createTopic( "mcollective.discovery.reply" ); + + // // Create a MessageProducer from the Session to the Topic or Queue + // producer = session->createProducer( destination ); + // producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT ); + + // // Create a messages + // BytesMessage* reply = session->createBytesMessage(); + + // reply->writeString(reply_message.c_str()); + // producer->send( reply ); + // printf("reply send \\n"); + + // delete reply; + + // }catch ( CMSException& e ) { + // e.printStackTrace(); + // } diff --git a/src/agent/discovery.h b/src/agent/discovery.h new file mode 100644 index 0000000..e69de29 diff --git a/src/agent/puppetd.cpp b/src/agent/puppetd.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/agent/puppetd.h b/src/agent/puppetd.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c71ccdb --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,6 @@ +#include "mcollective_listener.h" +int main(int argc, char *argv[]) +{ + McollectiveListener a; + a.DoSomething(); +} diff --git a/src/mcollective_listener.cpp b/src/mcollective_listener.cpp new file mode 100644 index 0000000..02ff913 --- /dev/null +++ b/src/mcollective_listener.cpp @@ -0,0 +1,45 @@ +#include "agent/base.h" +#include "mcollective_listener.h" + +#include +#include +#include +#include + +using namespace +std; + +McollectiveListener::McollectiveListener() { +} + +void McollectiveListener::DoSomething() { + + // Initialize the library + //activemq::library::ActiveMQCPP::initializeLibrary (); + + //Set the connection string + std::string brokerURI = "tcp://127.0.0.1:6163" "?wireFormat=stomp"; + + std::cout << "=====================================================\n"; + std::cout << "Starting Listener:" << std::endl; + std::cout << "-----------------------------------------------------\n"; + + // Create a ConnectionFactory + // auto_ptr < ConnectionFactory > + // connectionFactory (ConnectionFactory::createCMSConnectionFactory + // (brokerURI)); + // + + // Create a Connection + // connection = + // connectionFactory->createConnection ("mcollective", "marionette"); + // connection->start (); + // connection->setExceptionListener (this); + + // Create a Session + //session = connection->createSession (Session::AUTO_ACKNOWLEDGE); + + //DiscoveryAgent discoveryAgent(&session); + //PuppetAgent puppetAgent(&session); + +} diff --git a/src/mcollective_listener.h b/src/mcollective_listener.h new file mode 100644 index 0000000..2cc09b9 --- /dev/null +++ b/src/mcollective_listener.h @@ -0,0 +1,16 @@ +#ifndef __MCOLLECTIVELISTENER__ +#define __MCOLLECTIVELISTENER__ + +class McollectiveListener +{ + public: + McollectiveListener(); + + /* use virtual otherwise linker will try to perform static linkage */ + virtual void DoSomething(); + + private: + int x; +}; + +#endif