Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_janaded.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_janaded.cc
4 // Created: Fri 20 Jul 2012 10:03:57 AM EDT
5 // Creator: garmon
6 //
7 
8 #include <iostream>
9 #include <fstream>
10 #include <queue>
11 using namespace std;
12 
13 #include <JANA/JApplication.h>
15 
16 #include <pthread.h>
17 
18 using namespace jana;
19 
20 static pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
21 static pthread_cond_t cond;
22 static queue<string> commands;
23 
24 
25 
26 // Routine used to allow us to register our JEventSourceGenerator
27 extern "C"{
28 void InitPlugin(JApplication *app){
29  InitJANAPlugin(app);
30  app->AddProcessor(new JEventProcessor_janaded());
31 }
32 } // "C"
33 
34 // Tokenize a string
35 static inline void Tokenize(string str, vector<string> &tokens, const char delim=' ')
36 {
37  tokens.clear();
38  unsigned int cutAt;
39  while( (cutAt = str.find(delim)) != (unsigned int)str.npos ){
40  if(cutAt > 0)tokens.push_back(str.substr(0,cutAt));
41  str = str.substr(cutAt+1);
42  }
43  if(str.length() > 0)tokens.push_back(str);
44 }
45 
46 //-----------------------------------------
47 // JEventProcessor_janaded (constructor)
48 //-----------------------------------------
50 {
51  string myName = "JEventProcessor_janaded";
52  string myDescription = "janaded";
53  string UDL = "cMsg:cMsg://localhost/cMsg/test";
54 
55  cMsg* conn = new cMsg(UDL, myName, myDescription);
56  conn->connect();
57  conn->subscribe("janaded", "test", this, NULL);
58  conn->start();
59 
60  japp->monitor_heartbeat=false;
61 
62 }
63 
64 //------------------
65 // init
66 //------------------
68 {
69  JANADED_VERBOSE=0;
70  app->GetJParameterManager()->SetDefaultParameter("JANADED_VERBOSE", JANADED_VERBOSE);
71 
72  return NOERROR;
73 }
74 
75 //------------------
76 // brun
77 //------------------
78 jerror_t JEventProcessor_janaded::brun(JEventLoop *eventLoop, int32_t runnumber)
79 {
80 
81  return NOERROR;
82 }
83 
84 //------------------
85 // evnt
86 //------------------
87 jerror_t JEventProcessor_janaded::evnt(JEventLoop *loop, uint64_t eventnumber)
88 {
89  while(true) {
90  pthread_mutex_lock(&mutex1);
91  pthread_cond_wait( &cond, &mutex1 );
92  string command = commands.front();
93  commands.pop();
94  if(command=="NEXT_EVENT") {
95  cout << "Next Event Command" <<endl;
96  return NOERROR;
97  }
98  else if (command=="Whatever") {
99  //do whatever
100  }
101  else {
102  cout << "Command was" << command <<endl;
103  }
104 
105  pthread_mutex_unlock(&mutex1);
106  }
107 
108 }
109 
110 //------------------
111 // erun
112 //------------------
114 {
115  return NOERROR;
116 }
117 
118 //------------------
119 // fini
120 //------------------
122 {
123 
124  return NOERROR;
125 }
126 
127 void JEventProcessor_janaded::callback(cMsgMessage *msg, void *arg) {
128  pthread_mutex_lock(&mutex1);
129  commands.push(msg->getText());
130  pthread_mutex_unlock(&mutex1);
131  //pthread_cond_broadcast( &cond );
132  pthread_cond_signal( &cond );
133 
134  delete msg;
135 }
136 
137 
138 
char str[256]
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
jerror_t fini(void)
Called after last event of last event source has been processed.
static queue< string > commands
jerror_t init(void)
Called once at program start.
static void Tokenize(string str, vector< string > &tokens, const char delim=' ')
JApplication * japp
static pthread_cond_t cond
InitPlugin_t InitPlugin
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
void callback(cMsgMessage *msg, void *arg)
Callback method.
static pthread_mutex_t mutex1