Example.cc 3.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// $Id$

/*************************************************************************
 * XDAQ Components for Distributed Data Acquisition                      *
 * Copyright (C) 2000-2019, CERN.                                        *
 * All rights reserved.                                                  *
 * Authors: L. Orsini, D. Simelevicius                                   *
 *                                                                       *
 * For the licensing terms see LICENSE.                                  *
 * For the list of contributors see CREDITS.                             *
 ************************************************************************/

#include "prometheus/Example.h"


#include "cgicc/HTMLClasses.h"
#include "xdata/InfoSpaceFactory.h"
#include "toolbox/task/TimerFactory.h"
#include "xgi/framework/Method.h"


XDAQ_INSTANTIATOR_IMPL(prometheus::Example);


prometheus::Example::Example (xdaq::ApplicationStub* s)
	: xdaq::Application(s), xgi::framework::UIManager(this)
{



	// create a prometheus object ( e.g. xdata::Counter )
	counter_ = new xdata::Counter("my_counter_total", "How many times it ticks?", {{"my_counter_total_label", "value"}});
	counter_->add("my_counter", {{"my_example_label", "value"}});
	
35
36
37
38
	// create a prometheus object ( e.g. xdata::Counter )
		counter2_ = new xdata::Counter("my_counter_total2", "How many times it ticks2?", {{"my_counter_total_label2", "value"}});
		counter2_->add("my_counter2", {{"my_example_label2", "value"}});

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    xgi::framework::deferredbind(this, this, &prometheus::Example::Default, "Default");

	//
	// Bind setting of default parameters
	getApplicationInfoSpace()->addListener(this, "urn:xdaq-event:setDefaultValues");
	getApplicationContext()->addActionListener(this);

}

prometheus::Example::~Example()
{

}
void prometheus::Example::timeExpired(toolbox::task::TimerEvent& e)
{
	LOG4CPLUS_DEBUG (this->getApplicationLogger(), "Timer callback");

	//std::cout << "timer expired " << e.type()  << std::endl;
	if (e.getTimerTask()->name  == "urn:prometheus:watchdog" )
	{
		// user example update
        (*counter_)["my_counter"].Increment();
62
        (*counter2_)["my_counter2"].Increment(2);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		return;
	}
	

}

void prometheus::Example::actionPerformed(toolbox::Event& e)
{
	LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Received event " << e.type());
	if ( e.type() == "urn:xdaq-event:profile-loaded")
	{
			std::cout << "got event " << e.type() << std::endl;
	}
}

void prometheus::Example::actionPerformed( xdata::Event& event)
{
	if (event.type() == "urn:xdaq-event:setDefaultValues")
	{	
		// create a qualified infospace to contain prometheus objects
		toolbox::net::URN urn = this->createQualifiedInfoSpace("prometheus");
		xdata::InfoSpace * is = xdata::getInfoSpaceFactory()->get(urn.toString());
		// publish counter to prometheus
		is->lock();
		is->fireItemAvailable("another_time_running_seconds_total", counter_);
88
89
		is->fireItemAvailable("another_time_running_seconds_total2", counter2_);

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
		is->unlock();

		// activate a timer for user example
		toolbox::task::Timer * timer = 0;
		timer = toolbox::task::getTimerFactory()->createTimer("urn:prometheus:application");

		toolbox::TimeInterval interval;
		toolbox::TimeVal start;
		start = toolbox::TimeVal::gettimeofday();

		interval.fromString("PT3S");
		timer->scheduleAtFixedRate( start, this,interval , 0, "urn:prometheus:watchdog" );


	}
	else
	{
		LOG4CPLUS_ERROR (this->getApplicationLogger(), "Received unsupported event type '" << event.type() << "'");
	}
}

void prometheus::Example::Default(xgi::Input * in, xgi::Output * out )
{

	*out << cgicc::h3("Example prometheus application with counter") << std::endl;

}