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 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
|
//+==================================================================================================================
//
// file : Pipe.cpp
//
// description : C++ source code for the Pipe class. The Pipe class is the root class for all derived
// Pipe classes.
//
// project : TANGO
//
// author(s) : E.Taurel
//
// Copyright (C) : 2014,2015
// 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>
#include <eventsupplier.h>
namespace Tango
{
//+-----------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::Pipe
//
// description :
// Constructor for abstract class Pipe
//
//-----------------------------------------------------------------------------------------------------------------
Pipe::Pipe(const string &_name,Tango::DispLevel _level,PipeWriteType _pwt)
:name(_name),disp_level(_level),writable(_pwt),ext(new PipeExt)
{
lower_name = name;
transform(lower_name.begin(),lower_name.end(),lower_name.begin(),::tolower);
pipe_serial_model = PIPE_BY_KERNEL;
event_subscription = 0;
//
// Set lib default value for pipe label and desc
//
label = name;
desc = DescNotSpec;
}
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::set_default_properties
//
// description :
// This method set the default user properties in the Pipe object.
//
// arguments :
// in :
// - prop_list : The user property list
//
//-------------------------------------------------------------------------------------------------------------------
void Pipe::set_default_properties(UserDefaultPipeProp &prop_list)
{
//
// Init value in Pipe instance
//
if ((prop_list.label.empty() == false) &&
(TG_strcasecmp(prop_list.label.c_str(),AlrmValueNotSpec) != 0) &&
(TG_strcasecmp(prop_list.label.c_str(),NotANumber) != 0))
label = prop_list.label;
if (prop_list.description.empty() == false &&
(TG_strcasecmp(prop_list.description.c_str(),AlrmValueNotSpec) != 0) &&
(TG_strcasecmp(prop_list.description.c_str(),NotANumber) != 0))
desc = prop_list.description;
//
// Memorize user default (if any) for case of user requesting a "return to user default"
//
user_def_prop.clear();
if (prop_list.label.empty() == false)
{
PipeProperty pp("label",prop_list.label);
user_def_prop.push_back(pp);
}
if (prop_list.description.empty() == false)
{
PipeProperty pp("description",prop_list.description);
user_def_prop.push_back(pp);
}
}
//+-------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::set_upd_properties
//
// description :
// Update pipe properties
//
// arguments :
// in :
// - new_conf : The pipe new configuration
// - dev : The device
//
//--------------------------------------------------------------------------------------------------------------------
void Pipe::set_upd_properties(const PipeConfig &new_conf,DeviceImpl *dev)
{
//
// Backup current configuration (only label and description)
//
PipeConfig old_conf = new_conf;
old_conf.label = Tango::string_dup(label.c_str());
old_conf.description = Tango::string_dup(desc.c_str());
try
{
//
// Set properties locally. In case of exception bring the backed-up values
//
vector<Attribute::AttPropDb> v_db;
set_properties(new_conf,dev,v_db);
//
// Update database
//
try
{
upd_database(v_db,dev->get_name());
}
catch(DevFailed &)
{
//
// In case of exception, try to store old properties in the database and inform the user about the error
//
try
{
v_db.clear();
set_properties(old_conf,dev,v_db);
upd_database(v_db,dev->get_name());
}
catch(DevFailed &)
{
//
// If the old values could not be restored, notify the user about possible database corruption
//
TangoSys_OMemStream o;
o << "Device " << dev->get_name() << "-> Pipe : " << name;
o << "\nDatabase error occurred whilst setting pipe properties. The database may be corrupted." << ends;
Except::throw_exception(API_CorruptedDatabase,o.str(),"Pipe::set_upd_properties()");
}
throw;
}
}
catch(DevFailed &)
{
vector<Attribute::AttPropDb> v_db;
set_properties(old_conf,dev,v_db);
throw;
}
}
//+--------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::upd_database
//
// description :
// Update database according to the info received in the v_db parameter. We update or delete info in db for
// pipe configuration.
//
// Arguments:
// in :
// - v_db : The vector of info with what has to be done in DB
// - dev_name : The device name
//
//---------------------------------------------------------------------------------------------------------------------
void Pipe::upd_database(vector<Attribute::AttPropDb> &v_db,string &dev_name)
{
//
// Build info needed for the method upddating DB
//
long prop_to_update = 0;
long prop_to_delete = 0;
Tango::DbData db_d;
Tango::DbData db_del;
db_d.push_back(DbDatum(name));
db_del.push_back(DbDatum(name));
vector<Attribute::AttPropDb>::iterator ite;
//
// A loop for each db action
//
for (ite = v_db.begin();ite != v_db.end();++ite)
{
switch (ite->dba)
{
case Attribute::UPD:
{
DbDatum desc(ite->name);
desc << ite->db_value;
db_d.push_back(desc);
prop_to_update++;
}
break;
case Attribute::UPD_FROM_DB:
{
DbDatum desc(ite->name);
desc << ite->db_value_db;
db_d.push_back(desc);
prop_to_update++;
}
break;
case Attribute::UPD_FROM_VECT_STR:
{
DbDatum desc(ite->name);
desc << ite->db_value_v_str;
db_d.push_back(desc);
prop_to_update++;
}
break;
case Attribute::DEL:
{
DbDatum desc(ite->name);
db_del.push_back(desc);
prop_to_delete++;
}
break;
}
}
//
// Update database
//
struct Attribute::CheckOneStrProp cosp;
cosp.prop_to_delete = &prop_to_delete;
cosp.prop_to_update = &prop_to_update;
cosp.db_d = &db_d;
cosp.db_del = &db_del;
//
// Update db only if needed
//
if (*cosp.prop_to_update != 0)
{
cout4 << *cosp.prop_to_update << " properties to update in db" << endl;
(*cosp.db_d)[0] << *cosp.prop_to_update;
//for (const auto &elem: *cosp.db_d)
// cout << "prop_to_update name = " << elem.name << endl;
Tango::Util *tg = Tango::Util::instance();
//
// Implement a reconnection schema. The first exception received if the db server is down is a COMM_FAILURE exception.
// Following exception received from following calls are TRANSIENT exception
//
bool retry = true;
while (retry == true)
{
try
{
tg->get_database()->put_device_pipe_property(dev_name,*cosp.db_d);
retry = false;
}
catch (CORBA::COMM_FAILURE &)
{
tg->get_database()->reconnect(true);
}
}
}
if (*cosp.prop_to_delete != 0)
{
cout4 << *cosp.prop_to_delete << " properties to delete in db" << endl;
(*cosp.db_del)[0] << *cosp.prop_to_delete;
//for (const auto &elem: *cosp.db_del)
// cout << "prop_to_delete name = " << elem.name << endl;
Tango::Util *tg = Tango::Util::instance();
//
// Implement a reconnection schema. The first exception received if the db server is down is a COMM_FAILURE exception.
// Following exception received from following calls are TRANSIENT exception
//
bool retry = true;
while (retry == true)
{
try
{
tg->get_database()->delete_device_pipe_property(dev_name,*cosp.db_del);
retry = false;
}
catch (CORBA::COMM_FAILURE &)
{
tg->get_database()->reconnect(true);
}
}
}
}
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::set_properties
//
// description :
// Set the pipe properties value for PipeConfig
//
// argument :
// in :
// - conf : The new properties sent by client
// - dev : The device
// out :
// - v_db : Vector of data used for database update/delete
//
//--------------------------------------------------------------------------------------------------------------------
void Pipe::set_properties(const Tango::PipeConfig &conf,DeviceImpl *dev,vector<Attribute::AttPropDb> &v_db)
{
//
// Check if the caller try to change "hard coded"properties. Throw exception in case of
//
string user_pipe_name(conf.name.in());
transform(user_pipe_name.begin(),user_pipe_name.end(),user_pipe_name.begin(),::tolower);
if (user_pipe_name != lower_name)
{
Except::throw_exception(API_AttrNotAllowed,"Pipe name is not changeable at run time","Pipe::set_properties()");
}
if (conf.writable != writable)
{
Except::throw_exception(API_AttrNotAllowed,"Pipe writable property is not changeable at run time","Pipe::set_properties()");
}
//
// Copy only a sub-set of the new properties
// For each "string" property, an empty string means returns to its default value which could be the library default
// value or the user defined default value
//
Tango::DeviceClass *dev_class = dev->get_device_class();
vector<PipeProperty> &def_user_prop = get_user_default_properties();
vector<PipeProperty> def_class_prop;
try
{
def_class_prop = dev_class->get_class_pipe()->get_prop_list(name);
}
catch (DevFailed &e) {}
//
// First the string properties
//
set_one_str_prop("description",conf.description,desc,v_db,def_user_prop,def_class_prop,DescNotSpec);
set_one_str_prop("label",conf.label,label,v_db,def_user_prop,def_class_prop,name.c_str());
}
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::set_one_str_prop
//
// description :
// Analyse one of the string properties. String properties are description and label
// A similar method exist in Attribute class. It's difficult to make it static and use it in this Pipe class
//
// argument :
// in :
// - prop_name : The property name
// - conf_val : The new property value
// - def_user_prop : The set of user defined default values
// - def_class_prop : The set of class defined default values
// - lib_def : The property library default value
// out :
// - pipe_conf : The new property in Pipe object
// - v_db : Vector of data used for database update/delete
//
//--------------------------------------------------------------------------------------------------------------------
void Pipe::set_one_str_prop(const char *prop_name,const CORBA::String_member &conf_val,
string &pipe_conf,vector<Attribute::AttPropDb> &v_db,vector<PipeProperty> &def_user_prop,
vector<PipeProperty> &def_class_prop,const char *lib_def)
{
Attribute::AttPropDb apd;
apd.name = prop_name;
bool user_defaults, class_defaults;
string usr_def_val, class_def_val;
size_t nb_user = def_user_prop.size();
size_t nb_class = def_class_prop.size();
user_defaults = prop_in_list(prop_name,usr_def_val,nb_user,def_user_prop);
class_defaults = prop_in_list(prop_name,class_def_val,nb_class,def_class_prop);
if (TG_strcasecmp(conf_val,AlrmValueNotSpec) == 0)
{
//
// Return to lib default. If something defined as user default or class default, put entry in DB to overwrite
// these defaults
//
string old_val = pipe_conf;
pipe_conf = lib_def;
if (old_val != pipe_conf)
{
if (user_defaults == true || class_defaults == true)
{
apd.dba = Attribute::UPD;
apd.db_value = pipe_conf;
v_db.push_back(apd);
}
else
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
}
}
else if (strlen(conf_val) == 0)
{
//
// Return to user default or lib default. If something defined as class default, put entry in DB in order to
// overwrite this default value.
//
string old_val = pipe_conf;
if (user_defaults == true)
pipe_conf = usr_def_val;
else
{
pipe_conf = lib_def;
}
if (old_val != pipe_conf)
{
if (class_defaults == true)
{
apd.dba = Attribute::UPD;
apd.db_value = pipe_conf;
v_db.push_back(apd);
}
else
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
}
}
else if (TG_strcasecmp(conf_val,NotANumber) == 0)
{
//
// Return to class default or user default or lib default
//
string old_val = pipe_conf;
if (class_defaults == true)
pipe_conf = class_def_val;
else if (user_defaults == true)
pipe_conf = usr_def_val;
else
{
pipe_conf = lib_def;
}
if (old_val != pipe_conf)
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
}
else
{
//
// Set property
//
string old_val = pipe_conf;
pipe_conf = conf_val;
if (user_defaults == true && pipe_conf == usr_def_val)
{
//
// Property value is the same than the user default value
//
if (old_val != pipe_conf)
{
if (class_defaults == true)
{
apd.dba = Attribute::UPD;
apd.db_value = pipe_conf;
v_db.push_back(apd);
}
else
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
}
}
else if (class_defaults == true && pipe_conf == class_def_val)
{
//
// Property value is the same than the class default value
//
if (old_val != pipe_conf)
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
}
else if (class_defaults == false && TG_strcasecmp(pipe_conf.c_str(),lib_def) == 0)
{
//
// Property value is the same than the lib default value
//
if (old_val != pipe_conf)
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
}
else if (class_defaults == false && strcmp(prop_name,"label") == 0)
{
//
// Property Label: Property value is the same than the lib default value
//
if (old_val != pipe_conf)
{
if (TG_strcasecmp(pipe_conf.c_str(),LabelNotSpec) == 0)
{
apd.dba = Attribute::DEL;
v_db.push_back(apd);
}
else
{
apd.dba = Attribute::UPD;
apd.db_value = pipe_conf;
v_db.push_back(apd);
}
}
}
else
{
if (old_val != pipe_conf)
{
apd.dba = Attribute::UPD;
apd.db_value = pipe_conf;
v_db.push_back(apd);
}
}
}
}
//+-------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::set_time
//
// description :
// Set the date in the pipe object
//
//--------------------------------------------------------------------------------------------------------------------
void Pipe::set_time()
{
#ifdef _TG_WINDOWS_
struct _timeb t;
_ftime(&t);
when.tv_sec = (CORBA::Long)t.time;
when.tv_usec = (CORBA::Long)(t.millitm * 1000);
when.tv_nsec = 0;
#else
struct timezone tz;
struct timeval tv;
gettimeofday(&tv,&tz);
when.tv_sec = (CORBA::Long)tv.tv_sec;
when.tv_usec = (CORBA::Long)tv.tv_usec;
when.tv_nsec = 0;
#endif
}
//+-------------------------------------------------------------------------
//
// method : Pipe::set_pipe_serial_method
//
// description : Set pipe serialization method
//
//--------------------------------------------------------------------------
void Pipe::set_pipe_serial_model(PipeSerialModel ser_model)
{
if (ser_model == Tango::PIPE_BY_USER)
{
Tango::Util *tg = Tango::Util::instance();
if (tg->get_serial_model() != Tango::BY_DEVICE)
{
Except::throw_exception(API_PipeNotAllowed,
"Pipe serial model by user is not allowed when the process is not in BY_DEVICE serialization model",
"Pipe::set_pipe_serial_model");
}
}
pipe_serial_model=ser_model;
}
Pipe &Pipe::operator[](const string &_na)
{
the_blob.operator[](_na);
return *this;
}
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Pipe::fire_event
//
// description :
// Fire a pipe event
//
// arguments:
// in :
// - dev : Device pointer
// - except : Pointer to the DevFailed exception
//
//---------------------------------------------------------------------------------------------------------------------
void Pipe::fire_event(DeviceImpl *dev,DevFailed *except)
{
cout4 << "Pipe::fire_event() entering ..." << endl;
//
// Check if it is needed to send an event
//
time_t now;
time_t delta_subscription;
now = time(NULL);
delta_subscription = now - event_subscription;
if (delta_subscription >= EVENT_RESUBSCRIBE_PERIOD)
return;
//
// Get the event supplier, and simply return if not created
//
ZmqEventSupplier *event_supplier_zmq = NULL;
Tango::Util *tg = Util::instance();
event_supplier_zmq = tg->get_zmq_event_supplier();
if (event_supplier_zmq == NULL)
{
return;
}
//
// Make sure the severity field is initialized (this field is never used!!)
//
size_t err_nb = except->errors.length();
for (size_t loop = 0;loop < err_nb;loop++)
(except->errors)[loop].severity = Tango::ERR;
//
// Create the structure used to send data to event system
//
EventSupplier::SuppliedEventData ad;
::memset(&ad,0,sizeof(ad));
//
// Fire event
//
vector<string> f_names;
vector<double> f_data;
vector<string> f_names_lg;
vector<long> f_data_lg;
string event_type("pipe");
event_supplier_zmq->push_event(dev, event_type, f_names, f_data, f_names_lg, f_data_lg, ad, name, except, true);
}
#ifdef _TG_WINDOWS_
void Pipe::fire_event(DeviceImpl *_dev,DevicePipeBlob *_dat,bool reuse_it)
{
struct _timeb now_win;
struct timeval now;
_ftime(&now_win);
now.tv_sec = (unsigned long)now_win.time;
now.tv_usec = (long)now_win.millitm * 1000;
fire_event(_dev,_dat,now,reuse_it);
}
#endif // _TG_WINDOWS_
void Pipe::fire_event(DeviceImpl *dev,DevicePipeBlob *p_data,struct timeval &t,bool reuse_it)
{
cout4 << "Pipe::fire_event() entering ..." << endl;
//
// Check if it is needed to send an event
//
time_t now;
time_t delta_subscription;
now = time(NULL);
delta_subscription = now - event_subscription;
if (delta_subscription >= EVENT_RESUBSCRIBE_PERIOD)
{
p_data->reset_insert_ctr();
DevVarPipeDataEltArray *tmp_ptr = p_data->get_insert_data();
delete tmp_ptr;
return;
}
//
// Get the event supplier, and simply return if not created
//
ZmqEventSupplier *event_supplier_zmq = NULL;
Tango::Util *tg = Util::instance();
event_supplier_zmq = tg->get_zmq_event_supplier();
if (event_supplier_zmq == NULL)
{
p_data->reset_insert_ctr();
DevVarPipeDataEltArray *tmp_ptr = p_data->get_insert_data();
delete tmp_ptr;
return;
}
//
// Create the structure used to send data to event system
//
EventSupplier::SuppliedEventData ad;
::memset(&ad,0,sizeof(ad));
//
// Init the structure sent with the event
//
ad.pipe_val = new DevPipeData();
ad.pipe_val->name = Tango::string_dup(name.c_str());
::memset(&(ad.pipe_val->time),0,sizeof(ad.pipe_val->time));
ad.pipe_val->time.tv_sec = t.tv_sec;
ad.pipe_val->time.tv_usec = t.tv_usec;
const string &bl_name = p_data->get_name();
if (bl_name.size() != 0)
ad.pipe_val->data_blob.name = bl_name.c_str();
DevVarPipeDataEltArray *tmp_ptr = p_data->get_insert_data();
if (tmp_ptr == Tango_nullptr)
{
Except::throw_exception(API_PipeNoDataElement,"No data in DevicePipeBlob!","Pipe::fire_event()");
}
CORBA::ULong max,len;
max = tmp_ptr->maximum();
len = tmp_ptr->length();
ad.pipe_val->data_blob.blob_data.replace(max,len,tmp_ptr->get_buffer((CORBA::Boolean)true),true);
//
// Fire event
//
vector<string> f_names;
vector<double> f_data;
vector<string> f_names_lg;
vector<long> f_data_lg;
string event_type("pipe");
event_supplier_zmq->push_event(dev, event_type, f_names, f_data, f_names_lg, f_data_lg, ad, name, NULL, true);
if (reuse_it == false)
{
p_data->reset_insert_ctr();
delete tmp_ptr;
}
delete ad.pipe_val;
}
} // End of Tango namespace
|