File: dev_poll.cpp

package info (click to toggle)
tango 8.1.2c%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,964 kB
  • ctags: 14,286
  • sloc: cpp: 133,954; sh: 14,704; ansic: 1,083; makefile: 944; java: 215; python: 55
file content (686 lines) | stat: -rw-r--r-- 18,494 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
88
89
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
static const char *RcsId = "$Id$";

//+============================================================================
//
// file :		dev_poll.cpp
//
// description :	C++ source code for the DeviceImpl
//			class. This class
//			is the root class for all derived Device classes.
//			It is an abstract class. The DeviceImpl class is the
//			CORBA servant which is "exported" onto the network and
//			accessed by the client.
//
// project :		TANGO
//
// author(s) :		E.Taurel
//
// Copyright (C) :      2011,2012
//						European Synchrotron Radiation Facility
//                      BP 220, Grenoble 38043
//                      FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Tango.  If not, see <http://www.gnu.org/licenses/>.
//
// $Revision$
//
//-============================================================================

#if HAVE_CONFIG_H
#include <ac_config.h>
#endif

#include <tango.h>

namespace Tango
{

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::init_poll_no_db
//
// description : 	Init polling info for device running without DB
//                  In such a case, polling is available only for
//                  object with polling defined in code.
//                  Fill in string vectors which are in case of DS using
//                  database initialised from the db.
//
//--------------------------------------------------------------------------

void DeviceImpl::init_poll_no_db()
{
    bool old_set = false;

//
// A loop for all device attribute
//

    vector<Attribute *> &att_list = dev_attr->get_attribute_list();
    vector<Attribute *>::iterator ite;
    for (ite = att_list.begin();ite != att_list.end();++ite)
    {
        long poll_period = (*ite)->get_polling_period();
        if (poll_period != 0)
        {
            vector<string> &polled_attr_list = get_polled_attr();
            polled_attr_list.push_back((*ite)->get_name());
            stringstream ss;
            ss << poll_period;
            polled_attr_list.push_back(ss.str());

            if (old_set == false)
            {
               set_poll_old_factor(DEFAULT_POLL_OLD_FACTOR);
               old_set = true;
            }
        }
    }

//
// A loop for all device commands
//

    vector<Command *> &cmd_list = device_class->get_command_list();
    vector<Command *>::iterator ite_cmd;
    for (ite_cmd = cmd_list.begin();ite_cmd != cmd_list.end();++ite_cmd)
    {
        long poll_period = (*ite_cmd)->get_polling_period();
        if (poll_period != 0)
        {
            vector<string> &polled_cmd_list = get_polled_cmd();
            polled_cmd_list.push_back((*ite_cmd)->get_name());
            stringstream ss;
            ss << poll_period;
            polled_cmd_list.push_back(ss.str());

            if (old_set == false)
            {
               set_poll_old_factor(DEFAULT_POLL_OLD_FACTOR);
               old_set = true;
            }
        }
    }
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::is_attribute_polled
//
// description : 	Returns true if the attribute is polled
//
// argument: att_name : The attribute name
//
//--------------------------------------------------------------------------

bool DeviceImpl::is_attribute_polled(const string &att_name)
{
	string att = att_name;
	transform(att.begin(),att.end(),att.begin(),::tolower);

	vector<string> &att_list = get_polled_attr();
	for (unsigned int i = 0;i < att_list.size();i = i+2)
	{

//
//	Convert to lower case before comparison
//

		string name_lowercase(att_list[i]);
		transform(name_lowercase.begin(),name_lowercase.end(),name_lowercase.begin(),::tolower);
		if ( att == name_lowercase )
		{

//
// when the polling buffer is externally filled (polling period == 0)
// mark the attribute as not polled! No events can be send by the polling thread!
//

			if ( att_list[i+1] == "0" )
			{
				return false;
			}
			else
				return true;
		}
	}

//
// now check wether a polling period is set (for example by pogo)
//

    Tango::Attribute &the_att = dev_attr->get_attr_by_name(att_name.c_str());
	if ( the_att.get_polling_period() > 0 )
	{

//
// check the list of non_auto_polled attributes to verify wether
// the polling was disabled
//

		vector<string> &napa = get_non_auto_polled_attr();
		for (unsigned int j = 0;j < napa.size();j++)
		{
#ifdef _TG_WINDOWS_
			if (_stricmp(napa[j].c_str(), att_name.c_str()) == 0)
#else
			if (strcasecmp(napa[j].c_str(), att_name.c_str()) == 0)
#endif
			{
				return false;
			}
		}

		return true;
	}

	return false;
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::is_command_polled
//
// description : 	Returns true if the command is polled
//
// argument: cmd_name : The command name
//
//--------------------------------------------------------------------------

bool DeviceImpl::is_command_polled(const string &cmd_name)
{
	string cmd = cmd_name;
	transform(cmd.begin(),cmd.end(),cmd.begin(),::tolower);

	vector<string> &cmd_list = get_polled_cmd();
	for (unsigned int i = 0;i < cmd_list.size();i = i+2)
	{

//
//	Convert to lower case before comparison
//

		string name_lowercase(cmd_list[i]);
		transform(name_lowercase.begin(),name_lowercase.end(),name_lowercase.begin(),::tolower);
		if ( cmd == name_lowercase )
		{

//
// when the polling buffer is externally filled (polling period == 0)
// mark the attribute as not polled! No events can be send by the polling thread!
//

			if ( cmd_list[i+1] == "0" )
			{
				return false;
			}
			else
				return true;
		}
	}

//
// now check wether a polling period is set (for example by pogo)
//

    Tango::Command &the_cmd = device_class->get_cmd_by_name(cmd_name);
	if ( the_cmd.get_polling_period() > 0 )
	{

//
// check the list of non_auto_polled attributes to verify wether
// the polling was disabled
//

		vector<string> &napa = get_non_auto_polled_cmd();
		for (unsigned int j = 0;j < napa.size();j++)
		{
#ifdef _TG_WINDOWS_
			if (_stricmp(napa[j].c_str(), cmd_name.c_str()) == 0)
#else
			if (strcasecmp(napa[j].c_str(), cmd_name.c_str()) == 0)
#endif
			{
				return false;
			}
		}

		return true;
	}

	return false;
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::get_attribute_poll_period
//
// description : 	Get the attribute polling period in mS (O if not polled)
//
// argument: att_name : The attribute name
//
//--------------------------------------------------------------------------

int DeviceImpl::get_attribute_poll_period(const string &att_name)
{
    int per = 0;

	string att = att_name;
	transform(att.begin(),att.end(),att.begin(),::tolower);

    bool found = false;
	vector<string> &att_list = get_polled_attr();
	for (unsigned int i = 0;i < att_list.size();i = i+2)
	{

//
//	Convert to lower case before comparison
//

		string name_lowercase(att_list[i]);
		transform(name_lowercase.begin(),name_lowercase.end(),name_lowercase.begin(),::tolower);
		if ( att == name_lowercase )
		{
            stringstream ss;
            ss << att_list[i + 1];
            ss >> per;
            found = true;

            break;
		}
	}

//
// now check wether a polling period is set (for example by pogo)
//

    if (found == false)
    {
    	Tango::Attribute &the_att = dev_attr->get_attr_by_name(att_name.c_str());
    	per = the_att.get_polling_period();
	}

	return per;
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::get_command_poll_period
//
// description : 	Get the command polling period in mS (0 if not polled)
//
// argument: cmd_name : The command name
//
//--------------------------------------------------------------------------

int DeviceImpl::get_command_poll_period(const string &cmd_name)
{
    int per = 0;

	string cmd = cmd_name;
	transform(cmd.begin(),cmd.end(),cmd.begin(),::tolower);

    bool found = false;
	vector<string> &cmd_list = get_polled_cmd();
	for (unsigned int i = 0;i < cmd_list.size();i = i+2)
	{

//
//	Convert to lower case before comparison
//

		string name_lowercase(cmd_list[i]);
		transform(name_lowercase.begin(),name_lowercase.end(),name_lowercase.begin(),::tolower);
		if ( cmd == name_lowercase )
		{
            stringstream ss;
            ss << cmd_list[i + 1];
            ss >> per;
            found = true;

            break;
		}
	}

//
// now check wether a polling period is set (for example by pogo)
//

    if (found == false)
    {
        Tango::Command &the_cmd = device_class->get_cmd_by_name(cmd_name);
        per = the_cmd.get_polling_period();
	}

	return per;
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::poll_attribute
//
// description : 	Poll one attribute. If the attribute is already polled,
//                  update its polling period to the new value
//
// argument: att_name : The attribute name
//           period : The polling period
//
//--------------------------------------------------------------------------

void DeviceImpl::poll_attribute(const string &att_name,int period)
{
    poll_object(att_name,period,POLL_ATTR);
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::poll_command
//
// description : 	Poll one command. If the command is already polled,
//                  update its polling period to the new value
//
// argument: cmd_name : The command name
//           period : The polling period
//
//--------------------------------------------------------------------------

void DeviceImpl::poll_command(const string &cmd_name,int period)
{
    poll_object(cmd_name,period,POLL_CMD);
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::poll_object
//
// description : 	Poll one object. If the object is already polled,
//                  update its polling period to the new value
//
// argument: cmd_name : The object name
//           period : The polling period
//           type : Command or attribute
//
//--------------------------------------------------------------------------

void DeviceImpl::poll_object(const string &obj_name,int period,PollObjType type)
{
    Tango::Util *tg = Tango::Util::instance();

	if (tg->is_svr_shutting_down() == true)
	{
		Except::throw_exception((const char *)API_NotSupported,
								(const char *)"It's not supported to start polling on any device cmd/attr while the device is shutting down",
								(const char *)"DeviceImpl::poll_object");
	}

    if (tg->is_svr_starting() == true)
    {

//
// If server is starting, we rely on the Util::polling_configure method to effectively
// start the polling
// Nevertheless, some tests are coded before doing the job
//

        if (period < MIN_POLL_PERIOD)
        {
            TangoSys_OMemStream o;
            o << period << " is below the min authorized period (" << MIN_POLL_PERIOD << " mS)" << ends;
            Except::throw_exception((const char *)API_NotSupported,o.str(),
                        (const char *)"DeviceImpl::poll_object");
        }

//
// Just to be sure that the command/attribute exist
// Also init ptr to the command/attribute polled list
//

        vector<string> *poll_obj;

        if (type == POLL_CMD)
        {
            device_class->get_cmd_by_name(obj_name);
            vector<string> &po = get_polled_cmd();
            poll_obj = &po;
        }
        else
        {
            dev_attr->get_attr_by_name(obj_name.c_str());
            vector<string> &po = get_polled_attr();
            poll_obj = &po;
        }

//
// Check if the command is not already in the polled command
// If yes, only update polling period in vector
// Otherwise, add cmd name and polling period in vector
//
// Util::polling_configure will ask dserver polling command
// to store info in db only if polling period is negative.
//

        bool found = false;
        string obj_name_lower(obj_name);
        transform(obj_name_lower.begin(),obj_name_lower.end(),obj_name_lower.begin(),::tolower);

        for (unsigned int i = 0;i < poll_obj->size();i = i + 2)
        {
            string tmp_name((*poll_obj)[i]);
            transform(tmp_name.begin(),tmp_name.end(),tmp_name.begin(),::tolower);
            if (tmp_name == obj_name_lower)
            {
                found = true;
                stringstream ss;
                string period_str;
                ss << period;
                ss >> period_str;
                if (ss)
                    (*poll_obj)[i + 1] = period_str;
            }
        }

        if (found == false)
        {
            stringstream ss;
            ss << -period;

            if (ss)
            {
                poll_obj->push_back(obj_name);
                poll_obj->push_back(ss.str());
            }
        }
    }
    else
    {

//
// Ask the admin device to do the work (simulating the classical
// way to tune polling)
// If the attribute is already polled, it's an update polling period
// Otherwise, it's a add object polling command
//

        DServer *ds = tg->get_dserver_device();
        CORBA::Any the_any;

        DevVarLongStringArray *send = new DevVarLongStringArray();
        send->lvalue.length(1);
        send->svalue.length(3);

        send->svalue[0] = CORBA::string_dup(get_name().c_str());
        if (type == POLL_ATTR)
            send->svalue[1] = CORBA::string_dup("attribute");
        else
            send->svalue[1] = CORBA::string_dup("command");
        send->svalue[2] = CORBA::string_dup(obj_name.c_str());
        send->lvalue[0] = period;

        the_any <<= send;

        CORBA::Any *received_any = NULL;

        if (type == POLL_CMD)
        {
            if (is_command_polled(obj_name) == true)
            {
                if (get_command_poll_period(obj_name) != period)
                    received_any = ds->command_inout("UpdObjPollingPeriod",the_any);
            }
            else
                received_any = ds->command_inout("AddObjPolling",the_any);
        }
        else
        {
            if (is_attribute_polled(obj_name) == true)
            {
                if (get_attribute_poll_period(obj_name) != period)
                    received_any = ds->command_inout("UpdObjPollingPeriod",the_any);
            }
            else
                received_any = ds->command_inout("AddObjPolling",the_any);
        }

        delete received_any;
    }
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::stop_poll_attribute
//
// description : 	Stop polling one attribute
//                  Does nothing if the attribute is not polled
//
// argument: att_name : The attribute name
//
//--------------------------------------------------------------------------

void DeviceImpl::stop_poll_attribute(const string &att_name)
{
    stop_poll_object(att_name,POLL_ATTR);
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::stop_poll_command
//
// description : 	Stop polling one command
//                  Does nothing if the command is not polled
//
// argument: cmd_name : The command name
//
//--------------------------------------------------------------------------

void DeviceImpl::stop_poll_command(const string &cmd_name)
{
    stop_poll_object(cmd_name,POLL_CMD);
}

//+-------------------------------------------------------------------------
//
// method : 		DeviceImpl::stop_poll_object
//
// description : 	Stop polling one object
//                  Does nothing if the object is not polled
//
// argument: obj_name : The object name
//
//--------------------------------------------------------------------------

void DeviceImpl::stop_poll_object(const string &obj_name,PollObjType type)
{
    Tango::Util *tg = Tango::Util::instance();

    if (tg->is_svr_starting() == true)
    {

//
// Just to be sure that the attribute/command exist
//

        vector<string> *poll_obj;

        if (type == POLL_CMD)
        {
            device_class->get_cmd_by_name(obj_name);
            vector<string> &po = get_polled_cmd();
            poll_obj = &po;
        }
        else
        {
            dev_attr->get_attr_by_name(obj_name.c_str());
            vector<string> &po = get_polled_attr();
            poll_obj = &po;
        }

//
// Remove object info in vector of polled attributes/commands
//

        string obj_name_lower(obj_name);
        transform(obj_name_lower.begin(),obj_name_lower.end(),obj_name_lower.begin(),::tolower);

        vector<string>::iterator ite;
        for (ite = poll_obj->begin();ite != poll_obj->end();ite = ite + 2)
        {
            string tmp_name(*ite);
            transform(tmp_name.begin(),tmp_name.end(),tmp_name.begin(),::tolower);
            if (tmp_name == obj_name_lower)
            {
                ite = poll_obj->erase(ite,ite+2);
                if (ite == poll_obj->end())
                    break;
                else
                    ite = ite - 2;
            }
        }
    }
    else
    {

		if (tg->is_device_restarting(device_name) == false)
		{

//
// Ask the admin device to do the work (simulating the classical
// way to tune polling)
//

			DServer *ds = tg->get_dserver_device();
			CORBA::Any the_any;

			DevVarStringArray *send = new DevVarStringArray();
			send->length(3);

			(*send)[0] = CORBA::string_dup(get_name().c_str());
			if (type == POLL_CMD)
				(*send)[1] = CORBA::string_dup("command");
			else
				(*send)[1] = CORBA::string_dup("attribute");
			(*send)[2] = CORBA::string_dup(obj_name.c_str());

			the_any <<= send;

			CORBA::Any *received_any;

			received_any = ds->command_inout("RemObjPolling",the_any);
			delete received_any;
		}
    }
}


} // End of Tango namespace