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 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
|
static const char *RcsId = "$Id: dbapi_cache.cpp 15556 2011-02-11 08:25:58Z taurel $";
//+============================================================================
//
// file : Deviceclass.cpp
//
// description : C++ source code for the DbServerCache class
//
// project : TANGO
//
// author(s) : E.Taurel
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2010,2011
// 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: 15556 $
//
// $Log$
// Revision 3.14 2010/09/09 13:43:38 taurel
// - Add year 2010 in Copyright notice
//
// Revision 3.13 2009/03/30 15:02:58 taurel
// - Fix last bugs before Tango 7 ??
//
// Revision 3.12 2009/03/13 09:32:27 taurel
// - Small changes to fix Windows VC8 warnings in Warning level 3
//
// Revision 3.11 2009/02/23 14:27:53 taurel
// - Added a DeviceProxy::get_property_list() method
//
// Revision 3.10 2009/01/21 12:45:15 taurel
// - Change CopyRights for 2009
//
// Revision 3.9 2008/10/06 15:02:16 taurel
// - Changed the licensing info from GPL to LGPL
//
// Revision 3.8 2008/10/02 16:09:25 taurel
// - Add some licensing information in each files...
//
// Revision 3.7 2008/06/14 11:28:07 taurel
// - DevEncoded attribute data type implementation work going on
//
// Revision 3.6 2008/05/20 12:42:29 taurel
// - Commit after merge with release 7 branch
//
// Revision 3.5 2008/03/11 14:36:44 taurel
// - Apply patches from Frederic Picca about compilation with gcc 4.2
// Revision 3.2.2.1 2008/02/07 15:56:58 taurel
// - First implementation of the Controlled Access done
//
// Revision 3.4 2008/01/25 15:45:58 taurel
// - Some changes in the Db cache
// - A lighter system to shutdown DS in case of dynamic attribute
//
// Revision 3.3 2008/01/08 14:33:52 taurel
// - strcasecmp() is not named like this on Windows !!
//
// Revision 3.2 2007/10/17 13:42:04 taurel
// - Bug found in Db server cache
//
// Revision 3.1 2007/10/16 08:25:47 taurel
// - Add management of the TC connection establishment timeout for DB access
// - Add DB server cache in DS used during DS startup sequence
// - Comment out the sleep time during DS startup sequence
//
//
//-============================================================================
#if HAVE_CONFIG_H
#include <ac_config.h>
#endif
#include <tango.h>
using namespace CORBA;
namespace Tango
{
//-----------------------------------------------------------------------------
//
// DbServerCache::DbServerCache() - constructor of the DbServerCache class
//
// This class manage a data cache used during the device server
// process startup phase. The 2 ctor parameters are
//
// in : db : The database object
// ds_name : The device server name (exec_name/inst_name)
// host : The host name
//
//-----------------------------------------------------------------------------
DbServerCache::DbServerCache(Database *db,string &ds_name,string &host)
{
//
// Get all data from database
//
try
{
received = db->fill_server_cache(ds_name,host);
}
catch (Tango::DevFailed &)
{
cout3 << "Got an exception while getting cache data from database !!" << endl;
throw;
}
received.inout() >>= data_list;
n_data = data_list->length();
//
// Extract the different blocks from the big list
// First, the device server admin device parameters
//
int start_idx = 0;
int stop_idx = 0;
if (n_data == 2)
{
imp_adm.first_idx = start_idx;
imp_adm.last_idx = 1;
return;
}
stop_idx = start_idx + 7;
imp_adm.first_idx = start_idx;
imp_adm.last_idx = stop_idx;
//
// Event factory
//
start_idx = stop_idx + 1;
if (::strcmp((*data_list)[start_idx + 1],"Not Found") == 0)
{
stop_idx = stop_idx + 2;
}
else
{
stop_idx = stop_idx + 6;
}
imp_notifd_event.first_idx = start_idx;
imp_notifd_event.last_idx = stop_idx;
//
// Server event
//
start_idx = stop_idx + 1;
if (::strcmp((*data_list)[start_idx + 1],"Not Found") == 0)
{
stop_idx = stop_idx + 2;
}
else
{
stop_idx = stop_idx + 6;
}
imp_adm_event.first_idx = start_idx;
imp_adm_event.last_idx = stop_idx;
//
// DServer class prop
//
prop_indexes(start_idx,stop_idx,DServer_class_prop,data_list);
//
// Default prop
//
prop_indexes(start_idx,stop_idx,Default_prop,data_list);
//
// Admin device prop
//
prop_indexes(start_idx,stop_idx,adm_dev_prop,data_list);
//
// Embedded classes number
//
start_idx = stop_idx + 1;
class_nb = ::atoi((*data_list)[start_idx + 1]);
stop_idx = stop_idx + 2;
classes_idx = new ClassEltIdx [class_nb];
for (int cl_loop = 0;cl_loop < class_nb;cl_loop++)
{
//
// Embedded class prop
//
prop_indexes(start_idx,stop_idx,classes_idx[cl_loop].class_prop,data_list);
//
// Embedded class attribute prop
//
prop_att_indexes(start_idx,stop_idx,classes_idx[cl_loop].class_att_prop,data_list);
//
// Device list
//
start_idx = stop_idx + 1;
int nb_dev = ::atoi((*data_list)[start_idx + 1]);
stop_idx = stop_idx + nb_dev + 2;
classes_idx[cl_loop].dev_list.first_idx = start_idx;
classes_idx[cl_loop].dev_list.last_idx = stop_idx;
classes_idx[cl_loop].dev_nb = nb_dev;
classes_idx[cl_loop].devs_idx = new DevEltIdx [nb_dev];
for (int loop = 0;loop < nb_dev;loop++)
{
//
// Device properties
//
prop_indexes(start_idx,stop_idx,classes_idx[cl_loop].devs_idx[loop].dev_prop,data_list);
//
// Device attribute properties
//
prop_att_indexes(start_idx,stop_idx,classes_idx[cl_loop].devs_idx[loop].dev_att_prop,data_list);
}
}
//
// Control System Service(s) property
//
prop_indexes(start_idx,stop_idx,ctrl_serv_prop,data_list);
}
//-----------------------------------------------------------------------------
//
// DbServerCache::import_adm_dev()
//
// This method returns to the caller of the info necessary to import the
// DS admin device. The returned data are the same than the one returned by
// the classical API
//
// This method returns a pointer to a DevVarLongStringArray initilised
// with the adm device import data
//-----------------------------------------------------------------------------
const DevVarLongStringArray *DbServerCache::import_adm_dev()
{
if (imp_adm.last_idx == 1)
{
Tango::Except::throw_exception("aaa","bbb","ccc");
}
imp_adm_data.lvalue.length(2);
imp_adm_data.svalue.length(5);
imp_adm_data.lvalue[0] = ::atoi((*data_list)[imp_adm.first_idx + 5]);
imp_adm_data.lvalue[1] = ::atoi((*data_list)[imp_adm.last_idx]);
for (int loop = 0;loop < 5;loop++)
imp_adm_data.svalue[loop] = CORBA::string_dup((*data_list)[imp_adm.first_idx + loop]);
return &imp_adm_data;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::import_notifd_event()
//
// This method returns to the caller of the info necessary to import the
// event associated to the notifd running on the DS host.
// The returned data are the same than the one returned by
// the classical API
//
// This method returns a pointer to a DevVarLongStringArray initilised
// with the notifd event import data
//-----------------------------------------------------------------------------
const DevVarLongStringArray *DbServerCache::import_notifd_event()
{
if (imp_notifd_event.last_idx == imp_notifd_event.first_idx + 1)
{
Tango::Except::throw_exception("aaa","bbb","ccc");
}
imp_notifd_event_data.lvalue.length(2);
imp_notifd_event_data.svalue.length(5);
imp_notifd_event_data.lvalue[0] = ::atoi((*data_list)[imp_notifd_event.first_idx + 4]);
imp_notifd_event_data.lvalue[1] = ::atoi((*data_list)[imp_notifd_event.last_idx]);
for (int loop = 0;loop < 4;loop++)
imp_notifd_event_data.svalue[loop] = CORBA::string_dup((*data_list)[imp_notifd_event.first_idx + loop]);
return &imp_notifd_event_data;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::import_adm_event()
//
// This method returns to the caller of the info necessary to import the
// event associated to the DS server.
// The returned data are the same than the one returned by
// the classical API
//
// This method returns a pointer to a DevVarLongStringArray initilised
// with the DS event import data
//-----------------------------------------------------------------------------
const DevVarLongStringArray *DbServerCache::import_adm_event()
{
if (imp_adm_event.last_idx == imp_adm_event.first_idx + 1)
{
Tango::Except::throw_exception("aaa","bbb","ccc");
}
imp_adm_event_data.lvalue.length(2);
imp_adm_event_data.svalue.length(5);
imp_adm_event_data.lvalue[0] = ::atoi((*data_list)[imp_adm_event.first_idx + 4]);
imp_adm_event_data.lvalue[1] = ::atoi((*data_list)[imp_notifd_event.last_idx]);
for (int loop = 0;loop < 4;loop++)
imp_adm_event_data.svalue[loop] = CORBA::string_dup((*data_list)[imp_adm_event.first_idx + loop]);
return &imp_adm_event_data;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_class_property()
//
// This method returns to the caller the wanted class properties
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The class name followed by the wanted
// property names
//
// This method returns a pointer to a DevVarStringArray initilised
// with the class properties
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_class_property(DevVarStringArray *in_param)
{
int ret_length = 2;
if (TG_strcasecmp((*in_param)[0],"DServer") == 0)
{
ret_obj_prop.length(ret_length);
ret_obj_prop[0] = CORBA::string_dup("DServer");
get_obj_prop(in_param,DServer_class_prop);
}
else if (TG_strcasecmp((*in_param)[0],"Default") == 0)
{
ret_obj_prop.length(ret_length);
ret_obj_prop[0] = CORBA::string_dup("Default");
get_obj_prop(in_param,Default_prop);
}
else
{
ret_obj_prop.length(ret_length);
ret_obj_prop[0] = CORBA::string_dup((*in_param)[0]);
int cl_idx = find_class((*in_param)[0]);
if (cl_idx != -1)
{
get_obj_prop(in_param,classes_idx[cl_idx].class_prop);
}
else
{
TangoSys_OMemStream o;
o << "Class " << (*in_param)[0] << " not found in DB cache" << ends;
Tango::Except::throw_exception((const char *)"DB_ClassNotFoundInCache",o.str(),
(const char *)"DbServerCache::get_dev_property");
}
}
return &ret_obj_prop;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_obj_prop()
//
// This method searches the index(es) of the object wanted property within
// all the data returne by the DB server. A structure will be initialised
// with the indexes
//
// in : in_param : The object name followed by the wanted
// property names
// dev_prop : Boolean set to true in case of device property.
// For device property, a undefined prop is returned with
// a value set to " " !
// out : obj : Reference to the structure which will be initialsed by this
// method
//
//-----------------------------------------------------------------------------
void DbServerCache::get_obj_prop(DevVarStringArray *in_param,PropEltIdx &obj,bool dev_prop)
{
int ret_length = 2;
int found_prop = 0;
char n_prop_str[256];
int nb_wanted_prop = in_param->length() - 1;
int obj_prop = obj.prop_nb;
for (int loop = 0;loop < nb_wanted_prop;loop++)
{
int lo;
for (lo = 0;lo < obj_prop * 2;lo = lo + 2)
{
if (TG_strcasecmp((*in_param)[loop + 1],(*data_list)[obj.props_idx[lo]]) == 0)
{
int old_ret_length = ret_length;
int nb_elt = obj.props_idx[lo + 1];
ret_length = ret_length + 2 + nb_elt;
ret_obj_prop.length(ret_length);
ret_obj_prop[old_ret_length] = CORBA::string_dup((*in_param)[loop + 1]);
ret_obj_prop[old_ret_length + 1] = CORBA::string_dup((*data_list)[obj.props_idx[lo] + 1]);
for (int k = 0;k < nb_elt;k++)
{
ret_obj_prop[old_ret_length + 2 + k] = CORBA::string_dup((*data_list)[obj.props_idx[lo] + 2 + k]);
}
found_prop++;
break;
}
}
if (lo >= (obj_prop * 2))
{
int old_length = ret_length;
ret_length = ret_length + 2;
if (dev_prop == true)
ret_length++;
ret_obj_prop.length(ret_length);
ret_obj_prop[old_length] = CORBA::string_dup((*in_param)[loop + 1]);
ret_obj_prop[old_length + 1] = CORBA::string_dup("0");
if (dev_prop == true)
ret_obj_prop[old_length + 2] = CORBA::string_dup(" ");
found_prop++;
}
}
::sprintf(n_prop_str,"%d",found_prop);
ret_obj_prop[1] = CORBA::string_dup(n_prop_str);
cout4 << "DbCache --> Data returned for a get_obj_property for object " << (*in_param)[0] << endl;
for (unsigned int ll=0;ll< ret_obj_prop.length();ll++)
cout4 << " DbCache --> Returned string = " << ret_obj_prop[ll] << endl;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_dev_property()
//
// This method returns to the caller the wanted device properties
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The device name followed by the wanted
// property names
//
// This method returns a pointer to a DevVarStringArray initilised
// with the device properties
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_dev_property(DevVarStringArray *in_param)
{
int ret_length = 2;
//
// There is a special case for the dserver admin device
//
if (TG_strncasecmp((*in_param)[0],"dserver/",8) == 0)
{
ret_obj_prop.length(ret_length);
ret_obj_prop[0] = CORBA::string_dup((*in_param)[0]);
get_obj_prop(in_param,adm_dev_prop,true);
}
else
{
int class_ind,dev_ind;
int res = find_dev_att((*in_param)[0],class_ind,dev_ind);
if (res == -1)
{
TangoSys_OMemStream o;
o << "Device " << (*in_param)[0] << " not found in DB cache" << ends;
Tango::Except::throw_exception((const char *)"DB_DeviceNotFoundInCache",o.str(),
(const char *)"DbServerCache::get_dev_property");
}
else
{
ret_obj_prop.length(ret_length);
ret_obj_prop[0] = CORBA::string_dup((*in_param)[0]);
get_obj_prop(in_param,classes_idx[class_ind].devs_idx[dev_ind].dev_prop,true);
}
}
return &ret_obj_prop;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_dev_list()
//
// This method returns to the list of devices for a specified class embedded
// within the device server process
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The device server name followed by the class name
//
// This method returns a pointer to a DevVarStringArray initilised
// with the device list
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_dev_list(DevVarStringArray *in_param)
{
int cl_idx = find_class((*in_param)[1]);
if (cl_idx != -1)
{
ret_dev_list.length(classes_idx[cl_idx].dev_nb);
for (int loop = 0;loop < classes_idx[cl_idx].dev_nb;loop++)
ret_dev_list[loop] = CORBA::string_dup((*data_list)[classes_idx[cl_idx].dev_list.first_idx + 2 + loop]);
}
else
ret_dev_list.length(0);
return &ret_dev_list;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::find_class()
//
// This method returns the index of the class with name passed as parameter
// into the array of classes info
//
// in : cl_name : The class name
//
// This method returns the index in the classes info array of the wanted
// class
//-----------------------------------------------------------------------------
int DbServerCache::find_class(DevString cl_name)
{
for (int loop = 0;loop < class_nb;loop++)
{
if (TG_strcasecmp((*data_list)[classes_idx[loop].class_prop.first_idx],cl_name) == 0)
return loop;
}
return -1;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_class_att_property()
//
// This method returns to the caller the class attribute properties for the
// class and attributes specified in the in parameters
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The class name followed by the wanted
// attribute names
//
// This method returns a pointer to a DevVarStringArray initilised
// with the class attribute properties
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_class_att_property(DevVarStringArray *in_param)
{
int found_att = 0;
char n_att_str[256];
ret_obj_att_prop.length(2);
ret_obj_att_prop[0] = CORBA::string_dup((*in_param)[0]);
int cl_idx = find_class((*in_param)[0]);
if (cl_idx != -1)
{
//
// The class is found
//
int wanted_att_nb = in_param->length() - 1;
int class_att_nb = classes_idx[cl_idx].class_att_prop.att_nb;
for (int loop = 0;loop < wanted_att_nb;loop++)
{
int ll;
for (ll = 0;ll < class_att_nb;ll++)
{
if (TG_strcasecmp((*in_param)[loop + 1],(*data_list)[classes_idx[cl_idx].class_att_prop.atts_idx[ll]]) == 0)
{
//
// The attribute is found, copy all its properties
//
int att_index = classes_idx[cl_idx].class_att_prop.atts_idx[ll];
int nb_prop = ::atoi((*data_list)[att_index + 1]);
int nb_elt = 0;
int nb_to_copy = 0;
int tmp_idx = att_index + 2;
nb_to_copy = 2;
for (int k = 0;k < nb_prop;k++)
{
nb_elt = ::atoi((*data_list)[tmp_idx + 1]);
tmp_idx = tmp_idx + nb_elt + 2;
nb_to_copy = nb_to_copy + 2 + nb_elt;
}
int old_length = ret_obj_att_prop.length();
ret_obj_att_prop.length(old_length + nb_to_copy);
for (int j = 0;j < nb_to_copy;j++)
ret_obj_att_prop[old_length + j] = CORBA::string_dup((*data_list)[att_index + j]);
found_att++;
break;
}
}
if (ll == class_att_nb)
{
found_att++;
int old_length = ret_obj_att_prop.length();
ret_obj_att_prop.length(old_length + 2);
ret_obj_att_prop[old_length] = CORBA::string_dup((*in_param)[loop + 1]);
ret_obj_att_prop[old_length + 1] = CORBA::string_dup("0");
}
}
::sprintf(n_att_str,"%d",found_att);
ret_obj_att_prop[1] = CORBA::string_dup(n_att_str);
}
else
{
TangoSys_OMemStream o;
o << "Class " << (*in_param)[0] << " not found in DB cache" << ends;
Tango::Except::throw_exception((const char *)"DB_ClassNotFoundInCache",o.str(),
(const char *)"DbServerCache::get_dev_property");
}
cout4 << "DbCache --> Returned data for a get_class_att_property for class " << (*in_param)[0] << endl;
for (unsigned int ll=0;ll< ret_obj_att_prop.length();ll++)
cout4 << " DbCache --> Returned object att prop = " << ret_obj_att_prop[ll] << endl;
return &ret_obj_att_prop;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_dev_att_property()
//
// This method returns to the caller the device attribute properties for the
// device and attributes specified in the in parameters
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The device name followed by the wanted
// attribute names
//
// This method returns a pointer to a DevVarStringArray initialised
// with the device attribute properties
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_dev_att_property(DevVarStringArray *in_param)
{
int found_att = 0;
char n_att_str[256];
ret_obj_att_prop.length(2);
ret_obj_att_prop[0] = CORBA::string_dup((*in_param)[0]);
int class_ind,dev_ind;
int ret_value = find_dev_att((*in_param)[0],class_ind,dev_ind);
if (ret_value != -1)
{
int wanted_att_nb = in_param->length() - 1;
int dev_att_nb = classes_idx[class_ind].devs_idx[dev_ind].dev_att_prop.att_nb;
for (int loop = 0;loop < wanted_att_nb;loop++)
{
int ll;
for (ll = 0;ll < dev_att_nb;ll++)
{
if (TG_strcasecmp((*in_param)[loop + 1],(*data_list)[classes_idx[class_ind].devs_idx[dev_ind].dev_att_prop.atts_idx[ll]]) == 0)
{
int att_index = classes_idx[class_ind].devs_idx[dev_ind].dev_att_prop.atts_idx[ll];
int nb_prop = ::atoi((*data_list)[att_index + 1]);
int nb_elt = 0;
int nb_to_copy = 0;
int tmp_idx = att_index + 2;
nb_to_copy = 2;
for (int k = 0;k < nb_prop;k++)
{
nb_elt = ::atoi((*data_list)[tmp_idx + 1]);
tmp_idx = tmp_idx + nb_elt + 2;
nb_to_copy = nb_to_copy + 2 + nb_elt;
}
int old_length = ret_obj_att_prop.length();
ret_obj_att_prop.length(old_length + nb_to_copy);
for (int j = 0;j < nb_to_copy;j++)
ret_obj_att_prop[old_length + j] = CORBA::string_dup((*data_list)[att_index + j]);
found_att++;
break;
}
}
if (ll == dev_att_nb)
{
found_att++;
int old_length = ret_obj_att_prop.length();
ret_obj_att_prop.length(old_length + 2);
ret_obj_att_prop[old_length] = CORBA::string_dup((*in_param)[loop + 1]);
ret_obj_att_prop[old_length + 1] = CORBA::string_dup("0");
}
}
::sprintf(n_att_str,"%d",found_att);
ret_obj_att_prop[1] = CORBA::string_dup(n_att_str);
}
else
{
TangoSys_OMemStream o;
o << "Device " << (*in_param)[0] << " not found in DB cache" << ends;
Tango::Except::throw_exception((const char *)"DB_DeviceNotFoundInCache",o.str(),
(const char *)"DbServerCache::get_dev_att_property");
}
cout4 << "DbCache --> Returned data for a get_dev_att_property for device " << (*in_param)[0] << endl;
for (unsigned int ll=0;ll< ret_obj_att_prop.length();ll++)
cout4 << " DbCache --> Returned object att prop = " << ret_obj_att_prop[ll] << endl;
return &ret_obj_att_prop;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::find_dev_att()
//
// This method sets the class and device index for the device with name
// passed in the in parameter. These indexes are indexes in the local
// structure arrays
//
// in : dev_name : The device name
// out : class_ind : The index of the data related to the device class
// dev_ind : The index of the data related to the device itself
//
// This method returns 0 if everything OK or -1 if the device is not found
//-----------------------------------------------------------------------------
int DbServerCache::find_dev_att(DevString dev_name,int &class_ind,int &dev_ind)
{
for (int loop = 0;loop < class_nb;loop++)
{
for (int ll = 0;ll < classes_idx[loop].dev_nb;ll++)
{
if (TG_strcasecmp((*data_list)[classes_idx[loop].dev_list.first_idx + 2 + ll],dev_name) == 0)
{
class_ind = loop;
dev_ind = ll;
return 0;
}
}
}
return -1;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::find_obj()
//
// This method sets the class and device index for the device with name
// passed in the in parameter. These indexes are indexes in the local
// structure arrays
//
// in : obj_name : The object name
// out : obj_ind : The index of the data related to the object
//
// This method returns 0 if everything OK or -1 if the device is not found
//-----------------------------------------------------------------------------
int DbServerCache::find_obj(DevString obj_name,int &obj_ind)
{
if (TG_strcasecmp((*data_list)[ctrl_serv_prop.first_idx],obj_name) == 0)
{
obj_ind = ctrl_serv_prop.first_idx;
return 0;
}
return -1;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::~DbServerCache() - destructor of the DbServerCache class
//
//-----------------------------------------------------------------------------
DbServerCache::~DbServerCache()
{
if (DServer_class_prop.props_idx != NULL)
delete [] DServer_class_prop.props_idx;
if (Default_prop.props_idx != NULL)
delete [] Default_prop.props_idx;
if (adm_dev_prop.props_idx != NULL)
delete [] adm_dev_prop.props_idx;
if (ctrl_serv_prop.props_idx != NULL)
delete [] ctrl_serv_prop.props_idx;
for (int cl_loop = 0;cl_loop < class_nb;cl_loop++)
{
if (classes_idx[cl_loop].class_prop.props_idx != NULL)
delete [] classes_idx[cl_loop].class_prop.props_idx;
if (classes_idx[cl_loop].class_att_prop.atts_idx != NULL)
delete [] classes_idx[cl_loop].class_att_prop.atts_idx;
for (int dev_loop = 0;dev_loop < classes_idx[cl_loop].dev_nb;dev_loop++)
{
if (classes_idx[cl_loop].devs_idx[dev_loop].dev_prop.props_idx != NULL)
delete [] classes_idx[cl_loop].devs_idx[dev_loop].dev_prop.props_idx;
if (classes_idx[cl_loop].devs_idx[dev_loop].dev_att_prop.atts_idx != NULL)
delete [] classes_idx[cl_loop].devs_idx[dev_loop].dev_att_prop.atts_idx;
}
delete [] classes_idx[cl_loop].devs_idx;
}
delete [] classes_idx;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::prop_indexes()
//
// This method computes where is the last data related to the object
// in the big array coming from the Db server
//
// args (in) : start : The index in the array of the first data related
// to the object
// list : The string array coming from Db server
//
// args (out) : stop : The index in the array of the last data related
// to the object
// obj : Structure where all object parameters must be stored
//-----------------------------------------------------------------------------
void DbServerCache::prop_indexes(int &start,int &stop,PropEltIdx &obj,const DevVarStringArray *list)
{
start = stop + 1;
int nb_prop = atoi((*list)[start + 1]);
if (nb_prop == 0)
{
stop = start + 1;
obj.last_idx = stop;
obj.first_idx = start;
obj.prop_nb = 0;
obj.props_idx = NULL;
return;
}
stop = stop + 2;
int id = 0;
obj.props_idx = new int[nb_prop * 2];
for (int loop = 0;loop < nb_prop;loop++)
{
obj.props_idx[id++] = stop + 1;
int nb_elt = atoi((*list)[stop + 2]);
obj.props_idx[id++] = nb_elt;
stop = stop + nb_elt + 2;
}
obj.last_idx = stop;
obj.first_idx = start;
obj.prop_nb = nb_prop;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::prop_att_indexes()
//
// This method computes where is the last data related to the object
// (for attribute related object) in the big array coming from the Db server
//
// args (in) : start : The index in the array of the first data related
// to the object
// list : The string array coming from Db server
//
// args (out) : stop : The index in the array of the last data related
// to the object
// obj : Structure where all object parameters must be stored
//-----------------------------------------------------------------------------
void DbServerCache::prop_att_indexes(int &start,int &stop,AttPropEltIdx &obj,const DevVarStringArray *list)
{
start = stop + 1;
int nb_att = atoi((*list)[start + 1]);
if (nb_att == 0)
{
stop = start + 1;
obj.last_idx = stop;
obj.first_idx = start;
obj.att_nb = 0;
obj.atts_idx = NULL;
return;
}
int id = 0;
obj.att_nb = nb_att;
obj.atts_idx = new int[nb_att];
stop = start + 2;
for (int ll = 0;ll < nb_att;ll++)
{
obj.atts_idx[id++] = stop;
int nb_prop = atoi((*list)[stop + 1]);
stop = stop + 2;
for (int loop = 0;loop < nb_prop;loop++)
{
int nb_elt = atoi((*list)[stop + 1]);
stop = stop + nb_elt + 2;
}
if (ll == (nb_att - 1))
stop--;
}
obj.last_idx = stop;
obj.first_idx = start;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_obj_property()
//
// This method returns to the caller the wanted object properties
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The object name followed by the wanted
// property names
//
// This method returns a pointer to a DevVarStringArray initilised
// with the object properties
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_obj_property(DevVarStringArray *in_param)
{
int ret_length = 2;
int obj_ind;
int res = find_obj((*in_param)[0],obj_ind);
if (res == -1)
{
TangoSys_OMemStream o;
o << "Object " << (*in_param)[0] << " not found in DB cache" << ends;
Tango::Except::throw_exception((const char *)"DB_DeviceNotFoundInCache",o.str(),
(const char *)"DbServerCache::get_obj_property");
}
else
{
ret_obj_prop.length(ret_length);
ret_obj_prop[0] = CORBA::string_dup((*in_param)[0]);
get_obj_prop(in_param,ctrl_serv_prop,true);
}
return &ret_obj_prop;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_device_property_list()
//
// This method returns to the caller the device property names
// The returned data are the same than the one returned by
// the classical API
//
// in : in_param : The device name followed by a wildcard
//
// This method returns a pointer to a DevVarStringArray initilised
// with the device property names
//-----------------------------------------------------------------------------
const DevVarStringArray *DbServerCache::get_device_property_list(DevVarStringArray *in_param)
{
//
// There is a special case for the dserver admin device
//
if (TG_strncasecmp((*in_param)[0],"dserver/",8) == 0)
{
ret_prop_list.length(0);
get_obj_prop_list(in_param,adm_dev_prop);
}
else
{
int class_ind,dev_ind;
int res = find_dev_att((*in_param)[0],class_ind,dev_ind);
if (res == -1)
{
TangoSys_OMemStream o;
o << "Device " << (*in_param)[0] << " not found in DB cache" << ends;
Tango::Except::throw_exception((const char *)"DB_DeviceNotFoundInCache",o.str(),
(const char *)"DbServerCache::get_device_property_list");
}
else
{
ret_prop_list.length(0);
get_obj_prop_list(in_param,classes_idx[class_ind].devs_idx[dev_ind].dev_prop);
}
}
return &ret_prop_list;
}
//-----------------------------------------------------------------------------
//
// DbServerCache::get_obj_prop_list()
//
// This method retrieves device property list for a device
//
// in : in_param : The device name followed by a wildcard
// obj : The device object in the cache
//
// This method returns a pointer to a DevVarStringArray initialised
// with the device property names
//-----------------------------------------------------------------------------
void DbServerCache::get_obj_prop_list(DevVarStringArray *in_param,PropEltIdx &obj)
{
//
// First analyse the wildcard
//
bool before,after,wildcard_used;
string before_str;
string after_str;
string wildcard((*in_param)[1]);
transform(wildcard.begin(),wildcard.end(),wildcard.begin(),::tolower);
string::size_type pos = wildcard.find('*');
if (pos != string::npos)
{
wildcard_used = true;
if (pos == 0)
{
if (wildcard.size() == 1)
{
before = false;
after = false;
}
else
{
before = false;
after = true;
after_str = wildcard.substr(pos + 1);
}
}
else
{
before = true;
before_str = wildcard.substr(0,pos);
if (pos == wildcard.size() - 1)
after = false;
else
{
after = true;
after_str = wildcard.substr(pos + 1);
}
}
}
else
wildcard_used = false;
int ret_length = 1;
int obj_prop = obj.prop_nb;
int lo;
string::size_type pos_after,pos_before;
//
// A loop on each prop.
//
for (lo = 0;lo < obj_prop * 2;lo = lo + 2)
{
//
// Check according to the user wildcard, if we have to returned this property name
// Note: The property name is case independant
//
bool store = false;
if (wildcard_used == true)
{
if (before == false)
{
if (after == false)
store = true;
else
{
string tmp_name((*data_list)[obj.props_idx[lo]]);
transform(tmp_name.begin(),tmp_name.end(),tmp_name.begin(),::tolower);
pos_after = tmp_name.rfind(after_str);
if ((pos_after != string::npos) && (pos_after == (tmp_name.size() - after_str.size())))
store = true;
}
}
else
{
string tmp_name((*data_list)[obj.props_idx[lo]]);
transform(tmp_name.begin(),tmp_name.end(),tmp_name.begin(),::tolower);
if (after == false)
{
pos_before = tmp_name.find(before_str);
if ((pos_before != string::npos) && (pos_before == 0))
store = true;
}
else
{
pos_before = tmp_name.find(before_str);
pos_after = tmp_name.rfind(after_str);
if ((pos_before != string::npos) && (pos_before == 0) &&
(pos_after != string::npos) && (pos_after == (tmp_name.size() - after_str.size())))
store = true;
}
}
}
else
{
string tmp_name((*data_list)[obj.props_idx[lo]]);
transform(tmp_name.begin(),tmp_name.end(),tmp_name.begin(),::tolower);
if (tmp_name == wildcard)
store = true;
}
//
// Store the property name if decided
//
if (store == true)
{
ret_prop_list.length(ret_length);
ret_prop_list[ret_length - 1] = CORBA::string_dup((*data_list)[obj.props_idx[lo]]);
ret_length = ret_length + 1;
}
}
}
} // End of Tango namespace
|