12 #include "cgicc/CgiDefs.h"
13 #include "cgicc/Cgicc.h"
14 #include "cgicc/HTTPHTMLHeader.h"
15 #include "cgicc/HTMLClasses.h"
16 #include "cgicc/HTTPResponseHeader.h"
19 #include "xdaq/ApplicationGroup.h"
20 #include "xdaq/ApplicationRegistry.h"
22 #include "xoap/MessageReference.h"
23 #include "xoap/MessageFactory.h"
24 #include "xoap/SOAPEnvelope.h"
25 #include "xoap/SOAPPart.h"
26 #include "xoap/SOAPBody.h"
27 #include "xoap/SOAPBodyElement.h"
28 #include "xoap/Method.h"
29 #include "xoap/domutils.h"
30 #include "xoap/DOMParser.h"
31 #include "xoap/SOAPHeader.h"
33 #include "xgi/Table.h"
34 #include "xcept/tools.h"
38 #include "toolbox/task/WorkLoopFactory.h"
39 #include "xdata/InfoSpaceFactory.h"
44 #include "toolbox/Runtime.h"
47 #include "xdata/exdr/FixedSizeInputStreamBuffer.h"
48 #include "xdata/Table.h"
49 #include "xdata/exdr/Serializer.h"
50 #include "xdata/exdr/AutoSizeOutputStreamBuffer.h"
51 #include "xdaq/ApplicationDescriptorImpl.h"
53 #include "toolbox/TimeVal.h"
54 #include "toolbox/stl.h"
55 #include "toolbox/regex.h"
63 #include "xoap/Event.h"
65 #include<boost/tokenizer.hpp>
72 : xdaq::Application(
s)
74 getApplicationDescriptor()->setAttribute(
"icon",
"/xmas2dqm/wse/images/Las.png");
76 LOG4CPLUS_DEBUG(this->getApplicationLogger(),
"inside constructor of xmas2dqm::wse::Application");
103 fsm_.setInitialState(
'H');
104 fsm_.setStateName(
'F',
"Failed");
109 state_ = fsm_.getStateName (fsm_.getCurrentState());
110 getApplicationInfoSpace()->fireItemAvailable(
"stateName",&state_);
111 getApplicationInfoSpace()->fireItemAvailable(
"LASurl",&LASurl_);
112 getApplicationInfoSpace()->fireItemAvailable(
"Period",&Period_);
113 getApplicationInfoSpace()->fireItemAvailable(
"LASQueueSize",&LASQueueSize_);
118 getApplicationInfoSpace()->addItemChangedListener (
"stateName",
this);
119 getApplicationInfoSpace()->addItemChangedListener (
"LASurl",
this);
120 getApplicationInfoSpace()->addItemChangedListener (
"Period",
this);
121 getApplicationInfoSpace()->addItemChangedListener (
"LASQueueSize",
this);
122 getApplicationInfoSpace()->addItemChangedListener (
"flashlistMonitor",
this);
123 getApplicationInfoSpace()->addItemChangedListener (
"runNumber",
this);
125 LASurl_ =
"http://srv-c2d04-18.cms:9943/urn:xdaq-application:lid=100/retrieveCollection";
127 LASQueueSize_ =
"100000";
134 curl_global_init(CURL_GLOBAL_ALL);
138 LASReadoutWorkLoop_ = toolbox::task::getWorkLoopFactory()->getWorkLoop(
"LASReadoutWaitingWorkLoop",
"waiting");
141 if (LASReadoutWorkLoop_->isActive() ==
false)
143 LASReadoutWorkLoop_->activate();
149 LASReadoutTimer_ = toolbox::task::getTimerFactory()->createTimer(
"PeriodicLASReadout");
157 LOG4CPLUS_DEBUG(this->getApplicationLogger(),
"finish of Constructor of xmas2dqm::wse::XmasToDQM");
165 static int times = 0;
168 LOG4CPLUS_DEBUG (getApplicationLogger(),
"inside WorkLoop...ready to ask LAS for EXDR data : time " << times <<
"th");
169 LOG4CPLUS_DEBUG (getApplicationLogger(),
"Period = " +
Period_.toString() +
" LASurl = " +
LASurl_.toString());
172 xdata::Table * ptr_table =
new xdata::Table();
176 LOG4CPLUS_DEBUG (getApplicationLogger(),
"return value from getEXDR_LAS = " << ret );
178 if (ret == -1 || ret == -2)
185 LOG4CPLUS_DEBUG (getApplicationLogger(),
"LASWorkLoop freeing xdata::Table * space" );
189 LOG4CPLUS_WARN (getApplicationLogger(),
"getEXDRLAS didn't complete properly, returning from LASREeadoutWorkLoop" );
195 LOG4CPLUS_DEBUG (getApplicationLogger(),
"inside WorkLoop...lock the mutex ");
202 LOG4CPLUS_INFO (getApplicationLogger(),
"inside WorkLoop...check (...and possible wait) if data queue is full");
211 LOG4CPLUS_DEBUG (getApplicationLogger(),
"data queue has available store...proceeding...");
215 std::map<std::string, std::string, std::less<std::string> >::iterator
i;
235 LOG4CPLUS_DEBUG (getApplicationLogger(),
"inside WorkLoop...ready to call XmastoDQM::ToDQM::digest " );
240 LOG4CPLUS_DEBUG (getApplicationLogger(),
"inside WorkLoop...signaling new element added to the data queue ");
245 LOG4CPLUS_DEBUG (getApplicationLogger(),
"inside WorkLoop...release mutex, allow access to the data queue");
257 LOG4CPLUS_DEBUG(this->getApplicationLogger(),
"inside getEXDR_LAS.........");
262 char data[200] =
"fmt=exdr&flash=";
273 curl_handle = curl_easy_init();
275 if (curl_handle ==
NULL)
277 LOG4CPLUS_WARN(getApplicationLogger(),
"Failed to create CURL connection");
282 code = curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data);
283 if (code != CURLE_OK)
285 LOG4CPLUS_WARN (getApplicationLogger(),
"Failed to set post fields");
291 curl_easy_setopt(curl_handle, CURLOPT_URL, LASurl_.toString().c_str());
294 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
299 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (
void *)&chunk);
303 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
"libcurl-agent/1.0");
306 curl_easy_perform(curl_handle);
309 curl_easy_cleanup(curl_handle);
320 LOG4CPLUS_DEBUG (getApplicationLogger(),
"chunk.memory length = " << chunk.
size);
322 xdata::exdr::FixedSizeInputStreamBuffer inBuffer(chunk.
memory,chunk.
size);
327 xdata::exdr::Serializer serializer;
331 serializer.import( rtable, &inBuffer );
336 LOG4CPLUS_ERROR(this->getApplicationLogger(),
"xdata::exdr::Serializer exception occured...");
337 LOG4CPLUS_ERROR(this->getApplicationLogger(),xcept::stdformat_exception_history(e));
359 static int times = 0;
363 LOG4CPLUS_DEBUG (getApplicationLogger(),
"timeExpired was called... : time " << times <<
"th");
364 LASReadoutWorkLoop_->submit(LASReadout_);
374 return realloc(ptr, size);
383 size_t realsize = size * nmemb;
389 memcpy(&(mem->
memory[mem->
size]), ptr, realsize);
390 mem->
size += realsize;
401 LOG4CPLUS_DEBUG(getApplicationLogger(),
"start of actionperformed");
402 LOG4CPLUS_DEBUG(getApplicationLogger(), e.type());
405 if (e.type() ==
"ItemChangedEvent")
407 std::string item =
dynamic_cast<xdata::ItemChangedEvent&
>(
e).itemName();
409 if ( item ==
"Period")
412 LOG4CPLUS_DEBUG(getApplicationLogger(),
"item = " + item);
414 if(fsm_.getStateName (fsm_.getCurrentState()) !=
"Enabled")
421 LASReadoutTimer_->remove(std::string(
"LASReadout"));
423 catch(toolbox::task::exception::NotActive &e)
425 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::NotActive exception occured...");
427 catch(toolbox::task::exception::NoJobs)
429 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::NoJobs exception occured...");
431 catch(toolbox::task::exception::JobNotFound &e)
433 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::JobNotFound exception occured...");
434 toolbox::TimeInterval
interval(atoi(Period_.toString().c_str()),0);
440 toolbox::TimeInterval
interval(atoi(Period_.toString().c_str()),0);
442 startLASReadout_ = toolbox::TimeVal::gettimeofday();
444 LASReadoutTimer_->scheduleAtFixedRate( startLASReadout_,
this, interval, 0, std::string(
"LASReadout") );
448 if ( item ==
"flashlistMonitor")
460 if ( item ==
"runNumber")
468 LOG4CPLUS_DEBUG(getApplicationLogger(),
"end of actionperformed");
493 xoap::SOAPPart
part =
msg->getSOAPPart();
494 xoap::SOAPEnvelope
env = part.getEnvelope();
495 xoap::SOAPBody body = env.getBody();
496 DOMNode*
node = body.getDOMNode();
497 DOMNodeList* bodyList = node->getChildNodes();
498 for (
unsigned int i = 0;
i < bodyList->getLength();
i++)
500 DOMNode*
command = bodyList->item(
i);
502 if (command->getNodeType() == DOMNode::ELEMENT_NODE)
505 std::string commandName = xoap::XMLCh2String (command->getLocalName());
510 toolbox::Event::Reference
e(
new toolbox::Event(commandName,
this));
518 xoap::MessageReference reply = xoap::createMessage();
519 xoap::SOAPEnvelope envelope = reply->getSOAPPart().getEnvelope();
520 xoap::SOAPName responseName = envelope.createName( commandName +
"Response",
"xdaq", XDAQ_NS_URI);
522 (void) envelope.getBody().addBodyElement ( responseName );
533 LOG4CPLUS_DEBUG (getApplicationLogger(),
"New state before reset is: " << fsm_.getStateName (fsm_.getCurrentState()) );
536 state_ = fsm_.getStateName (fsm_.getCurrentState());
538 xoap::MessageReference reply = xoap::createMessage();
539 xoap::SOAPEnvelope envelope = reply->getSOAPPart().getEnvelope();
540 xoap::SOAPName responseName = envelope.createName(
"ResetResponse",
"xdaq", XDAQ_NS_URI);
542 (void) envelope.getBody().addBodyElement ( responseName );
544 LOG4CPLUS_DEBUG (getApplicationLogger(),
"New state after reset is: " << fsm_.getStateName (fsm_.getCurrentState()) );
552 LOG4CPLUS_DEBUG (getApplicationLogger(),
e->type());
557 LASReadoutTimer_->remove(std::string(
"LASReadout"));
559 catch(toolbox::task::exception::NotActive &
e)
561 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::NotActive exception occured...");
563 catch(toolbox::task::exception::NoJobs)
565 LOG4CPLUS_INFO(getApplicationLogger(),
"toolbox::task::exception::NoJobs exception occured...");
567 catch(toolbox::task::exception::JobNotFound &e)
569 LOG4CPLUS_INFO(getApplicationLogger(),
"toolbox::task::exception::JobNotFound exception occured...");
570 toolbox::TimeInterval
interval(atoi(Period_.toString().c_str()),0);
575 toolbox::TimeInterval
interval(atoi(Period_.toString().c_str()),0);
577 startLASReadout_ = toolbox::TimeVal::gettimeofday();
579 LASReadoutTimer_->scheduleAtFixedRate( startLASReadout_,
this, interval, 0, std::string(
"LASReadout") );
586 LOG4CPLUS_DEBUG (getApplicationLogger(),
e->type());
590 LASReadoutTimer_->remove(std::string(
"LASReadout"));
592 catch(toolbox::task::exception::NotActive &
e)
594 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::NotActive exception occured...");
596 catch(toolbox::task::exception::NoJobs)
598 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::NoJobs exception occured...");
600 catch(toolbox::task::exception::JobNotFound &e)
602 LOG4CPLUS_WARN(getApplicationLogger(),
"toolbox::task::exception::JobNotFound exception occured...");
603 toolbox::TimeInterval
interval(atoi(Period_.toString().c_str()),0);
614 state_ = fsm.getStateName (fsm.getCurrentState());
615 LOG4CPLUS_DEBUG (getApplicationLogger(),
"New state is:" << fsm.getStateName (fsm.getCurrentState()) );
620 toolbox::fsm::FailedEvent &
fe =
dynamic_cast<toolbox::fsm::FailedEvent&
>(*e);
621 LOG4CPLUS_ERROR (getApplicationLogger(),
"Failure occurred when performing transition from: " <<
622 fe.getFromState() <<
" to: " << fe.getToState() <<
" exception: " << fe.getException().what() );
static const char runNumber_[]
xoap::MessageReference fireEvent(xoap::MessageReference msg)
void stateChanged(toolbox::fsm::FiniteStateMachine &fsm)
void failedTransition(toolbox::Event::Reference e)
void digest(const std::string &flashListName, const std::string &originator, const std::string &tag, xdata::Table *table)
void actionPerformed(xdata::Event &e)
static void * myrealloc(void *ptr, size_t size)
void HaltAction(toolbox::Event::Reference e)
void EnableAction(toolbox::Event::Reference e)
static ToDqm * instance()
void timeExpired(toolbox::task::TimerEvent &e)
uint16_t mem[nChs][nEvts]
int getEXDR_LAS(xdata::Table *rtable)
bool LASReadoutWorkLoop(toolbox::task::WorkLoop *wl)
xoap::MessageReference reset(xoap::MessageReference msg)
xdata::String LASQueueSize_
char data[epos_bytes_allocation]
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
tuple size
Write out results.