CMsgRootProducer.cc
From GlueXWiki
// Example shows how to serialize root object and publish it via cMsg // // Elliott Wolin, JLab, 26-jan-2009 // include files #include <iostream> #include <iomanip> #include <string> #include <stdlib.h> // for cMsg #include "cMsg.hxx" // root include files #include "TH1F.h" #include "TMessage.h" using namespace std; using namespace cmsg; // misc variables */ static string url = "cMsg://localhost/cMsg"; static int hcount = 0; static int debug = 0; static int done = 0; // prototypes */ void decode_command_line(int argc, char **argv); //---------------------------------------------------------------- //---------------------------------------------------------------- int main (int argc, char **argv) { // decode command line decode_command_line(argc,argv); try { // create and fill a root hist object TH1F *hist = new TH1F("fhist","This is the TH1F hist title",100,0.,100.); for(int i=0; i<10000; i++) hist->Fill(float(1+int(100.0*(rand()/(RAND_MAX+1.0))))); // create TMessage object that contains serialized version of hist object TMessage *tm = new TMessage(kMESS_OBJECT); tm->WriteObject(hist); // connect to cMsg server cMsg c(url,"hproducer","histogram producer"); c.connect(); // create and fill cMsg message // byte array filled with contents of TMessage object internal buffer, // which contains serialized root object cMsgMessage m; m.setSubject("root"); m.setType("hist"); m.setByteArrayNoCopy(tm->Buffer(),tm->Length()); // send message while(!done) { c.send(m); hcount++; cout << "sent message " << hcount << endl; done=hcount>=10; sleep(1); } // done c.disconnect(); } catch(cMsgException &e) { cout << "?cMsg exception: " << e.what() << endl; exit(EXIT_FAILURE); } catch(...) { cout << "?unknown exception" << endl; exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } //---------------------------------------------------------------- void decode_command_line(int argc, char**argv) { const char *help = "\nusage:\n\n hproducer [-url url] [-debug]\n"; int i; // loop over arguments */ i=1; while (i<argc) { if (strncasecmp(argv[i],"-h",2)==0) { printf("%s\n",help); exit(EXIT_SUCCESS); } else if (strncasecmp(argv[i],"-url",4)==0) { url=string(argv[i+1]); i=i+2; } else if (strncasecmp(argv[i],"-debug",6)==0) { debug=1; i=i+1; } else if (strncasecmp(argv[i],"-",1)==0) { printf("\n ?unknown command line arg: %s\n\n",argv[i]); exit(EXIT_FAILURE); } else { break; } } return; } //----------------------------------------------------------------