File size: 42,550 Bytes
68ce2fc | 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 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
/*
Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet, Min Yang Jung
Created on: 2004-04-30
(C) Copyright 2004-2026 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/
#include <cisstMultiTask/mtsComponent.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsInterfaceOutput.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstMultiTask/mtsInterfaceInput.h>
#include <cisstMultiTask/mtsManagerComponentBase.h>
#include <cisstMultiTask/mtsManagerComponentServices.h>
#include <cisstMultiTask/mtsManagerLocal.h>
#include <cisstMultiTask/mtsParameterTypes.h>
#include <cisstOSAbstraction/osaGetTime.h>
#include <cisstOSAbstraction/osaSleep.h>
mtsComponent::mtsComponent(const std::string & componentName):
Name(componentName),
InterfacesProvided("InterfacesProvided"),
InterfacesOutput("InterfacesOutput"),
InterfacesRequired("InterfacesRequired"),
InterfacesInput("InterfacesInput"),
StateTables("StateTables")
{
Initialize();
}
mtsComponent::mtsComponent(void):
InterfacesProvided("InterfacesProvided"),
InterfacesOutput("InterfacesOutput"),
InterfacesRequired("InterfacesRequired"),
InterfacesInput("InterfacesInput"),
StateTables("StateTables")
{
Initialize();
}
void mtsComponent::Initialize(void)
{
UseSeparateLogFileFlag = false;
LoDMultiplexerStreambuf = 0;
LogFile = 0;
ManagerComponentServices = 0;
InterfacesProvided.SetOwner(*this);
InterfacesRequired.SetOwner(*this);
InterfacesOutput.SetOwner(*this);
InterfacesInput.SetOwner(*this);
this->StateTables.SetOwner(*this);
InterfaceProvidedToManager = 0;
ReplayMode = false;
}
mtsComponent::mtsComponent(const mtsComponent & other):
cmnGenericObject(other)
{
cmnThrow("Class mtsComponent: copy constructor for mtsComponent should never be called");
}
mtsComponent::~mtsComponent()
{
KillSeparateLogFile();
if (ManagerComponentServices) {
delete ManagerComponentServices;
}
}
#if CISST_HAS_JSON
void mtsComponent::ConfigureJSON(const Json::Value & configuration)
{
const Json::Value jsonLog = configuration["log"];
if (!jsonLog.empty()) {
const Json::Value jsonAllow = jsonLog["allow"];
if (!jsonAllow.empty()) {
const std::string allow = jsonAllow.asString();
const std::string className = Services()->GetName();
if (allow == "none") {
cmnLogger::SetMaskClass(className, CMN_LOG_ALLOW_NONE);
} else if (allow == "errors") {
cmnLogger::SetMaskClass(className, CMN_LOG_ALLOW_ERRORS);
} else if (allow == "errors-and-warnings") {
cmnLogger::SetMaskClass(className, CMN_LOG_ALLOW_ERRORS_AND_WARNINGS);
} else if (allow == "verbose") {
cmnLogger::SetMaskClass(className, CMN_LOG_ALLOW_VERBOSE);
} else if (allow == "debug") {
cmnLogger::SetMaskClass(className, CMN_LOG_ALLOW_DEBUG);
} else if (allow == "all") {
cmnLogger::SetMaskClass(className, CMN_LOG_ALLOW_ALL);
} else {
CMN_LOG_CLASS_INIT_ERROR << "ConfigureJSON: failed to configure \""
<< this->GetName()
<< "\", the \"log\":\"allow\" value must be one of: "
<< "none, errors, errors-and-warnings, verbose, debug, all. We found: \""
<< allow << "\"" << std::endl;
}
}
const Json::Value jsonSeparateFile = jsonLog["separate-file"];
if (!jsonSeparateFile.empty()) {
if (jsonSeparateFile.isBool()) {
if (jsonSeparateFile.asBool()) {
UseSeparateLogFileDefaultWithDate();
}
} else if (jsonSeparateFile.isString()) {
UseSeparateLogFile(jsonSeparateFile.asString());
}
}
}
}
#endif
const std::string & mtsComponent::GetName(void) const
{
return this->Name;
}
void mtsComponent::GetName(std::string & placeHolder) const
{
placeHolder = this->Name;
}
void mtsComponent::AddTag(const std::string & tag)
{
if (mtsManagerLocal::GetInstance()->IsValidComponentTag(tag)) {
this->mTags.insert(tag);
} else {
CMN_LOG_CLASS_INIT_ERROR << "AddTag: " << tag << " is not a valid component tag for component \"" << this->GetName() << "\". "
<< "Use mtsManagerLocal::AddValidComponentTag to register it." << std::endl;
}
}
const std::set<std::string> & mtsComponent::GetTags(void) const
{
return this->mTags;
}
void mtsComponent::SetName(const std::string & componentName)
{
this->Name = componentName;
}
void mtsComponent::Create(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Create: default create method for component \""
<< this->GetName() << "\"" << std::endl;
this->State = mtsComponentState::READY;
}
bool mtsComponent::CreateAndWait(double timeoutInSeconds)
{
this->Create();
return this->WaitForState(mtsComponentState::READY, timeoutInSeconds);
}
void mtsComponent::Start(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Start: default start method for component \""
<< this->GetName() << "\"" << std::endl;
this->Startup();
this->State = mtsComponentState::ACTIVE;
}
bool mtsComponent::StartAndWait(double timeoutInSeconds)
{
this->Start();
return this->WaitForState(mtsComponentState::ACTIVE, timeoutInSeconds);
}
void mtsComponent::Suspend(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Suspend: default suspend method for component \""
<< this->GetName() << "\"" << std::endl;
this->State = mtsComponentState::READY;
}
void mtsComponent::Kill(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Kill: default kill method for component \""
<< this->GetName() << "\"" << std::endl;
this->Cleanup();
this->State = mtsComponentState::FINISHED;
}
bool mtsComponent::KillAndWait(double timeoutInSeconds)
{
this->Kill();
return this->WaitForState(mtsComponentState::FINISHED, timeoutInSeconds);
}
void mtsComponent::Configure(const std::string & CMN_UNUSED(filename))
{
CMN_LOG_CLASS_INIT_VERBOSE << "Configure: default start method for component \""
<< this->GetName() << "\"" << std::endl;
}
std::vector<std::string> mtsComponent::GetNamesOfInterfacesProvidedOrOutput(void) const
{
std::vector<std::string> all = InterfacesProvided.GetNames();
std::vector<std::string> added = InterfacesOutput.GetNames();
all.insert(all.end(), added.begin(), added.end());
return all;
}
std::vector<std::string> mtsComponent::GetNamesOfInterfacesProvided(void) const
{
return InterfacesProvided.GetNames();
}
std::vector<std::string> mtsComponent::GetNamesOfInterfacesOutput(void) const
{
return InterfacesOutput.GetNames();
}
bool mtsComponent::InterfaceExists(const std::string & interfaceName, cmnLogLevel lod) const
{
return (InterfaceProvidedOrOutputExists(interfaceName, lod)
|| InterfaceRequiredOrInputExists(interfaceName, lod));
}
bool mtsComponent::InterfaceProvidedOrOutputExists(const std::string & interfaceName, cmnLogLevel lod) const
{
if (this->InterfacesProvided.FindItem(interfaceName)) {
CMN_LOG_CLASS(lod) << "InterfaceExists: found a provided interface with name \"" << interfaceName << "\"" << std::endl;
return true;
}
if (this->InterfacesOutput.FindItem(interfaceName)) {
CMN_LOG_CLASS(lod) << "InterfaceExists: found an output interface with name \"" << interfaceName << "\"" << std::endl;
return true;
}
return false;
}
bool mtsComponent::InterfaceRequiredOrInputExists(const std::string & interfaceName, cmnLogLevel lod) const
{
if (this->InterfacesRequired.FindItem(interfaceName)) {
CMN_LOG_CLASS(lod) << "InterfaceExists: found a required interface with name \"" << interfaceName << "\"" << std::endl;
return true;
}
if (this->InterfacesInput.FindItem(interfaceName)) {
CMN_LOG_CLASS(lod) << "InterfaceExists: found an input interface with name \"" << interfaceName << "\"" << std::endl;
return true;
}
return false;
}
mtsInterfaceProvided * mtsComponent::AddInterfaceProvided(const std::string & interfaceProvidedName,
mtsInterfaceQueueingPolicy queueingPolicy)
{
mtsInterfaceProvided * interfaceProvided =
this->AddInterfaceProvidedWithoutSystemEvents(interfaceProvidedName,
queueingPolicy);
if (interfaceProvided) {
interfaceProvided->AddSystemEvents();
}
return interfaceProvided;
}
mtsInterfaceProvided * mtsComponent::AddInterfaceProvidedWithoutSystemEvents(const std::string & interfaceProvidedName,
mtsInterfaceQueueingPolicy queueingPolicy)
{
if (this->InterfaceProvidedOrOutputExists(interfaceProvidedName, CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceProvided: component " << this->GetName() << " already has an interface named \""
<< interfaceProvidedName << "\"" << std::endl;
return 0;
}
mtsInterfaceProvided * interfaceProvided;
if ((queueingPolicy == MTS_COMPONENT_POLICY)
|| (queueingPolicy == MTS_COMMANDS_SHOULD_NOT_BE_QUEUED)) {
interfaceProvided = new mtsInterfaceProvided(interfaceProvidedName, this, MTS_COMMANDS_SHOULD_NOT_BE_QUEUED, 0);
} else {
CMN_LOG_CLASS_INIT_WARNING << "AddInterfaceProvided: adding provided interface \"" << interfaceProvidedName
<< "\" with policy MTS_COMMANDS_SHOULD_BE_QUEUED to component \""
<< this->GetName() << "\", make sure you call ProcessQueuedCommands to empty the queues" << std::endl;
interfaceProvided = new mtsInterfaceProvided(interfaceProvidedName, this, MTS_COMMANDS_SHOULD_BE_QUEUED, 0);
}
if (interfaceProvided) {
if (InterfacesProvided.AddItem(interfaceProvidedName, interfaceProvided, CMN_LOG_LEVEL_INIT_ERROR)) {
return interfaceProvided;
}
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceProvided: component " << this->GetName() << " unable to add interface \""
<< interfaceProvidedName << "\"" << std::endl;
delete interfaceProvided;
return 0;
}
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceProvided: component " << this->GetName() << " unable to create interface \""
<< interfaceProvidedName << "\"" << std::endl;
return 0;
}
mtsInterfaceOutput * mtsComponent::AddInterfaceOutput(const std::string & interfaceOutputName)
{
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceOutput: adding output interface \"" << interfaceOutputName
<< "\" to component \"" << this->GetName()
<< "\" can't be performed from mtsComponent, this method must be derived." << std::endl;
return 0;
}
mtsInterfaceProvided *
mtsComponent::GetInterfaceProvided(const std::string & interfaceProvidedName) const
{
return InterfacesProvided.GetItem(interfaceProvidedName, CMN_LOG_LEVEL_RUN_VERBOSE);
}
mtsInterfaceOutput *
mtsComponent::GetInterfaceOutput(const std::string & interfaceOutputName) const
{
return InterfacesOutput.GetItem(interfaceOutputName, CMN_LOG_LEVEL_RUN_VERBOSE);
}
size_t mtsComponent::GetNumberOfInterfacesProvided(void) const
{
return InterfacesProvided.size();
}
size_t mtsComponent::GetNumberOfInterfacesOutput(void) const
{
return InterfacesOutput.size();
}
bool mtsComponent::RemoveInterfaceProvided(const std::string & interfaceProvidedName)
{
mtsInterfaceProvided * interfaceProvided = GetInterfaceProvided(interfaceProvidedName);
if (!interfaceProvided) {
CMN_LOG_CLASS_RUN_ERROR << "RemoveInterfaceProvided: no provided interface found: \""
<< interfaceProvidedName << "\"" << std::endl;
return false;
}
int userCount = interfaceProvided->GetNumberOfEndUsers();
if (userCount > 0) {
CMN_LOG_CLASS_RUN_ERROR << "RemoveInterfaceProvided: cannot remove interface because it is connected to "
<< userCount << " required interfaces" << std::endl;
// Could call interfaceProvided->GetListOfUserNames() to display more information
return false;
}
if (!InterfacesProvided.RemoveItem(interfaceProvidedName)) {
CMN_LOG_CLASS_RUN_ERROR << "RemoveInterfaceProvided: failed to remove interface \""
<< interfaceProvidedName << "\"" << std::endl;
return false;
}
delete interfaceProvided;
CMN_LOG_CLASS_RUN_VERBOSE << "RemoveInterfaceProvided: removed provided interface \""
<< interfaceProvidedName << "\"" << std::endl;
return true;
}
mtsInterfaceRequired *
mtsComponent::GetInterfaceRequired(const std::string & interfaceRequiredName)
{
return InterfacesRequired.GetItem(interfaceRequiredName, CMN_LOG_LEVEL_RUN_VERBOSE);
}
mtsInterfaceInput *
mtsComponent::GetInterfaceInput(const std::string & interfaceInputName) const
{
return InterfacesInput.GetItem(interfaceInputName, CMN_LOG_LEVEL_RUN_VERBOSE);
}
size_t mtsComponent::GetNumberOfInterfacesRequired(void) const
{
return InterfacesRequired.size();
}
size_t mtsComponent::GetNumberOfInterfacesInput(void) const
{
return InterfacesInput.size();
}
bool mtsComponent::RemoveInterfaceRequired(const std::string & interfaceRequiredName)
{
mtsInterfaceRequired * interfaceRequired = GetInterfaceRequired(interfaceRequiredName);
if (!interfaceRequired) {
CMN_LOG_CLASS_RUN_ERROR << "RemoveInterfaceRequired: no required interface found: \""
<< interfaceRequiredName << "\"" << std::endl;
return false;
}
const mtsInterfaceProvided * connectedInterface = interfaceRequired->GetConnectedInterface();
if (connectedInterface) {
CMN_LOG_CLASS_RUN_ERROR << "RemoveInterfaceRequired: cannot remove interface " << interfaceRequiredName
<< " because it is connected to " << connectedInterface->GetFullName() << std::endl;
return false;
}
if (!InterfacesRequired.RemoveItem(interfaceRequiredName)) {
CMN_LOG_CLASS_RUN_WARNING << "RemoveInterfaceRequired: failed to remove interface "
<< interfaceRequiredName << std::endl;
return false;
}
delete interfaceRequired;
CMN_LOG_CLASS_RUN_VERBOSE << "RemoveInterfaceRequired: removed required interface \""
<< interfaceRequiredName << "\"" << std::endl;
return true;
}
mtsInterfaceRequired * mtsComponent::AddInterfaceRequiredExisting(const std::string & interfaceRequiredName,
mtsInterfaceRequired * interfaceRequired)
{
if (this->InterfaceRequiredOrInputExists(interfaceRequiredName, CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceRequired: component " << this->GetName() << " already has an interface named \""
<< interfaceRequiredName << "\"" << std::endl;
return 0;
}
if (InterfacesRequired.AddItem(interfaceRequiredName, interfaceRequired)) {
return interfaceRequired;
}
return 0;
}
mtsInterfaceRequired * mtsComponent::AddInterfaceRequiredUsingMailbox(const std::string & interfaceRequiredName,
mtsMailBox * mailBox,
mtsRequiredType required)
{
if (this->InterfaceRequiredOrInputExists(interfaceRequiredName, CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceRequired: component " << this->GetName() << " already has an interface named \""
<< interfaceRequiredName << "\"" << std::endl;
return 0;
}
mtsInterfaceRequired * interfaceRequired = new mtsInterfaceRequired(interfaceRequiredName, this, mailBox, required);
if (interfaceRequired) {
if (InterfacesRequired.AddItem(interfaceRequiredName, interfaceRequired)) {
return interfaceRequired;
}
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceRequired: unable to add interface \""
<< interfaceRequiredName << "\"" << std::endl;
if (interfaceRequired) {
delete interfaceRequired;
}
} else {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceRequired: unable to create interface for \""
<< interfaceRequiredName << "\"" << std::endl;
}
return 0;
}
mtsInterfaceRequired * mtsComponent::AddInterfaceRequired(const std::string & interfaceRequiredName,
mtsRequiredType required)
{
if (this->InterfaceRequiredOrInputExists(interfaceRequiredName, CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceRequired: component " << this->GetName() << " already has an interface named \""
<< interfaceRequiredName << "\"" << std::endl;
return 0;
}
mtsInterfaceRequired * interfaceRequired =
this->AddInterfaceRequiredWithoutSystemEventHandlers(interfaceRequiredName,
required);
if (interfaceRequired) {
interfaceRequired->AddSystemEventHandlers();
}
return interfaceRequired;
}
mtsInterfaceRequired * mtsComponent::AddInterfaceRequiredWithoutSystemEventHandlers(const std::string & interfaceRequiredName,
mtsRequiredType required)
{
// by default, no mailbox for base component, events are not queued
return this->AddInterfaceRequiredUsingMailbox(interfaceRequiredName, 0, required);
}
mtsInterfaceInput * mtsComponent::AddInterfaceInput(const std::string & interfaceInputName)
{
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceInput: adding input interface \"" << interfaceInputName
<< "\" to component \"" << this->GetName()
<< "\" can't be performed from mtsComponent, this method must be derived." << std::endl;
return 0;
}
mtsInterfaceInput * mtsComponent::AddInterfaceInputExisting(const std::string & interfaceInputName,
mtsInterfaceInput * interfaceInput) {
if (this->InterfaceRequiredOrInputExists(interfaceInputName, CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceInput: component " << this->GetName() << " already has an interface named \""
<< interfaceInputName << "\"" << std::endl;
return 0;
}
if (InterfacesInput.AddItem(interfaceInputName, interfaceInput)) {
return interfaceInput;
}
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceInputExisting: component \"" << this->GetName()
<< "\" already has a required or input interface \"" << interfaceInputName
<< "\"" << std::endl;
return 0;
}
mtsInterfaceOutput * mtsComponent::AddInterfaceOutputExisting(const std::string & interfaceOutputName,
mtsInterfaceOutput * interfaceOutput) {
if (this->InterfaceProvidedOrOutputExists(interfaceOutputName, CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceOutput: component " << this->GetName() << " already has an interface named \""
<< interfaceOutputName << "\"" << std::endl;
return 0;
}
if (InterfacesOutput.AddItem(interfaceOutputName, interfaceOutput)) {
return interfaceOutput;
}
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceOutputExisting: component \"" << this->GetName()
<< "\" already has a provided or output interface \"" << interfaceOutputName
<< "\"" << std::endl;
return 0;
}
std::vector<std::string> mtsComponent::GetNamesOfInterfacesRequiredOrInput(void) const
{
std::vector<std::string> all = InterfacesRequired.GetNames();
std::vector<std::string> added = InterfacesInput.GetNames();
all.insert(all.end(), added.begin(), added.end());
return all;
}
std::vector<std::string> mtsComponent::GetNamesOfInterfacesRequired(void) const
{
return InterfacesRequired.GetNames();
}
std::vector<std::string> mtsComponent::GetNamesOfInterfacesInput(void) const
{
return InterfacesInput.GetNames();
}
const mtsInterfaceProvided * mtsComponent::GetInterfaceProvidedFor(const std::string & interfaceRequiredName) {
mtsInterfaceRequired * interfaceRequired =
InterfacesRequired.GetItem(interfaceRequiredName, CMN_LOG_LEVEL_RUN_VERBOSE);
return interfaceRequired ? interfaceRequired->GetConnectedInterface() : 0;
}
mtsStateTable * mtsComponent::GetStateTable(const std::string & stateTableName)
{
return this->StateTables.GetItem(stateTableName, CMN_LOG_LEVEL_INIT_ERROR);
}
bool mtsComponent::AddStateTable(mtsStateTable * existingStateTable, bool addInterfaceProvided)
{
const std::string tableName = existingStateTable->GetName();
const std::string interfaceName = "StateTable" + tableName;
if (!this->StateTables.AddItem(tableName,
existingStateTable,
CMN_LOG_LEVEL_INIT_ERROR)) {
CMN_LOG_CLASS_INIT_ERROR << "AddStateTable: can't add state table \"" << tableName
<< "\" to task \"" << this->GetName() << "\"" << std::endl;
return false;
}
if (addInterfaceProvided) {
mtsInterfaceProvided * providedInterface = this->AddInterfaceProvided(interfaceName);
if (!providedInterface) {
CMN_LOG_CLASS_INIT_ERROR << "AddStateTable: can't add provided interface \"" << interfaceName
<< "\" to task \"" << this->GetName() << "\"" << std::endl;
return false;
}
providedInterface->AddTag("State table");
providedInterface->AddCommandWrite(&mtsStateTable::DataCollectionStart,
existingStateTable,
"StartCollection");
providedInterface->AddCommandWrite(&mtsStateTable::DataCollectionStop,
existingStateTable,
"StopCollection");
providedInterface->AddCommandRead(&mtsStateTable::GetIndexReader,
existingStateTable,
"GetIndexReader");
providedInterface->AddEventWrite(existingStateTable->DataCollection.BatchReady,
"BatchReady", mtsStateTable::IndexRange());
providedInterface->AddEventVoid(existingStateTable->DataCollection.CollectionStarted,
"CollectionStarted");
providedInterface->AddEventWrite(existingStateTable->DataCollection.CollectionStopped,
"CollectionStopped", size_t(0));
providedInterface->AddEventWrite(existingStateTable->DataCollection.Progress,
"Progress", size_t(0));
}
CMN_LOG_CLASS_INIT_DEBUG << "AddStateTable: added state table \"" << tableName
<< "\" and corresponding interface \"" << interfaceName
<< "\" to task \"" << this->GetName() << "\"" << std::endl;
return true;
}
// Execute all commands in the mailbox. This is just a temporary implementation, where
// all commands in a mailbox are executed before moving on the next mailbox. The final
// implementation will probably look at timestamps. We may also want to pass in a
// parameter (enum) to set the mailbox processing policy.
size_t mtsComponent::ProcessMailBoxes(InterfacesProvidedMapType & interfaces)
{
size_t numberOfCommands = 0;
InterfacesProvidedMapType::iterator iterator = interfaces.begin();
const InterfacesProvidedMapType::iterator end = interfaces.end();
for (;
iterator != end;
++iterator) {
numberOfCommands += iterator->second->ProcessMailBoxes();
}
return numberOfCommands;
}
size_t mtsComponent::ProcessQueuedEvents(void) {
InterfacesRequiredMapType::iterator iterator = InterfacesRequired.begin();
const InterfacesRequiredMapType::iterator end = InterfacesRequired.end();
size_t numberOfEvents = 0;
for (;
iterator != end;
iterator++) {
numberOfEvents += iterator->second->ProcessMailBoxes();
}
return numberOfEvents;
}
void mtsComponent::ToStream(std::ostream & outputStream) const
{
outputStream << "Component name: " << Name << std::endl;
InterfacesProvided.ToStream(outputStream);
InterfacesRequired.ToStream(outputStream);
InterfacesOutput.ToStream(outputStream);
InterfacesInput.ToStream(outputStream);
}
void mtsComponent::UseSeparateLogFileDefault(bool forwardToLogger)
{
std::string filename = this->GetName() + "-log.txt";
this->UseSeparateLogFile(filename, forwardToLogger);
}
void mtsComponent::UseSeparateLogFileDefaultWithDate(bool forwardToLogger)
{
std::string currentDateTime;
osaGetDateTimeString(currentDateTime);
std::string filename = this->GetName() + "-" + currentDateTime + "-log.txt";
this->UseSeparateLogFile(filename, forwardToLogger);
}
void mtsComponent::UseSeparateLogFile(const std::string & filename, bool forwardToLogger)
{
if (this->UseSeparateLogFileFlag) {
CMN_LOG_CLASS_INIT_ERROR << "UseSeparateLogFile: flag already set for component \""
<< this->GetName() << "\", no-op" << std::endl;
return;
}
CMN_LOG_CLASS_INIT_DEBUG << "UseSeparateLogFile: called for component \""
<< this->GetName() << "\"" << std::endl;
// create the multiplexer and log file if needed
if (!this->LoDMultiplexerStreambuf) {
this->LoDMultiplexerStreambuf = new cmnLogger::StreamBufType();
}
// if there is already a log file, remove it from multiplexer, close it and delete it
if (this->LogFile) {
this->LoDMultiplexerStreambuf->RemoveChannel(*(this->LogFile));
this->LogFile->close();
delete this->LogFile;
CMN_LOG_CLASS_INIT_DEBUG << "UseSeparateLogFile: closed and removed previous log file for component \""
<< this->GetName() << "\"" << std::endl;
}
// create a new log file and add it to the multiplexer
this->LogFile = new std::ofstream();
this->LogFile->open(filename.c_str());
if (this->LogFile->is_open()) {
// set the multiplexer and change flag at the end!
CMN_LOG_CLASS_INIT_DEBUG << "UseSeparateLogFile: opened log file \"" << filename
<< "\" for component \"" << this->GetName() << "\"" << std::endl;
this->LoDMultiplexerStreambuf->AddChannel(*(this->LogFile), CMN_LOG_ALLOW_ALL);
if (forwardToLogger) {
// note that if the component multiplexer already existed
// and the cmnLogger multiplexer was already added, this
// line has no effect
this->LoDMultiplexerStreambuf->AddMultiplexer(cmnLogger::GetMultiplexer());
}
this->UseSeparateLogFileFlag = true;
} else {
// can't open file, do not switch flag
CMN_LOG_CLASS_INIT_ERROR << "UseSeparateLogFile: can't open log file \"" << filename
<< "\" for component \"" << this->GetName() << "\"" << std::endl;
}
}
cmnLogger::StreamBufType * mtsComponent::GetLogMultiplexer(void) const
{
if (this->UseSeparateLogFileFlag) {
ThisType * nonConstThis = const_cast<ThisType *>(this);
return nonConstThis->LoDMultiplexerStreambuf;
}
return cmnGenericObject::GetLogMultiplexer();
}
void mtsComponent::KillSeparateLogFile(void)
{
if (this->LoDMultiplexerStreambuf) {
this->LoDMultiplexerStreambuf->RemoveAllChannels();
delete this->LoDMultiplexerStreambuf;
this->LoDMultiplexerStreambuf = 0;
}
if (this->LogFile) {
this->LogFile->close();
delete this->LogFile;
this->LogFile = 0;
}
}
bool mtsComponent::AreAllInterfacesRequiredConnected(const bool log)
{
// loop through the required interfaces and make sure they are all
// connected. The method doesn't end if it finds an interface that
// is not connected so it can log all un-connected interfaces.
bool allConnected = true;
InterfacesRequiredMapType::const_iterator requiredIterator = InterfacesRequired.begin();
const mtsInterfaceProvided * connectedInterface;
for (;
requiredIterator != InterfacesRequired.end();
requiredIterator++) {
connectedInterface = requiredIterator->second->GetConnectedInterface();
if (!connectedInterface) {
if (requiredIterator->second->IsRequired() == MTS_REQUIRED) {
if (log) {
CMN_LOG_CLASS_INIT_ERROR << "AreAllInterfacesRequiredConnected: component \"" << this->GetName()
<< "\", void pointer to required/input interface \""
<< requiredIterator->first
<< "\" (required/input not connected to provided/output)" << std::endl;
allConnected = false;
} else {
// no log, we can just abort
return false;
}
}
else if (log) {
CMN_LOG_CLASS_INIT_VERBOSE << "AreAllInterfacesRequiredConnected: component \"" << this->GetName()
<< "\", void pointer to optional required/input interface \""
<< requiredIterator->first
<< "\" (required/input not connected to provided/output)" << std::endl;
}
}
}
return allConnected;
}
bool mtsComponent::IsRunning(void) const
{
return (this->State == mtsComponentState::ACTIVE);
}
bool mtsComponent::IsStarted(void) const
{
return (this->State >= mtsComponentState::READY);
}
bool mtsComponent::IsTerminated(void) const
{
return (this->State == mtsComponentState::FINISHED);
}
bool mtsComponent::IsEndTask(void) const
{
return (this->State >= mtsComponentState::FINISHING);
}
const mtsComponentState & mtsComponent::GetState(void) const
{
return this->State;
}
void mtsComponent::GetState(mtsComponentState &state) const
{
state = this->State;
}
bool mtsComponent::WaitForState(mtsComponentState desiredState, double timeout)
{
if (timeout != 0.0) {
CMN_LOG_CLASS_INIT_VERBOSE << "WaitForState: called for component \"" << this->GetName()
<< "\" for state \"" << desiredState << "\" has no effect for mtsComponent" << std::endl;
}
return true;
}
mtsInterfaceRequired * mtsComponent::EnableDynamicComponentManagement(void)
{
// Extend internal required interface (to Manager Component) to include event handlers
mtsInterfaceRequired * required = AddInterfaceRequired(mtsManagerComponentBase::GetNameOfInterfaceInternalRequired());
if (!required) {
CMN_LOG_CLASS_INIT_ERROR << "EnableDynamicComponentManagement: failed to add internal required interface to component "
<< "\"" << GetName() << "\"" << std::endl;
return 0;
} else {
CMN_LOG_CLASS_INIT_VERBOSE << "EnableDynamicComponentManagement: successfully added internal required interface" << std::endl;
}
// Check if manager component service object has already been created
if (ManagerComponentServices) {
CMN_LOG_CLASS_INIT_WARNING << "EnableDynamicComponentManagement: redefine ManagerComponentServices object" << std::endl;
delete ManagerComponentServices;
}
ManagerComponentServices = new mtsManagerComponentServices(required);
ManagerComponentServices->InitializeInterfaceInternalRequired();
CMN_LOG_CLASS_INIT_VERBOSE << "EnableDynamicComponentManagement: successfully initialized dynamic component management services" << std::endl;
return required;
}
bool mtsComponent::AddInterfaceInternal(const bool useManagerComponentServices)
{
// Add required interface
std::string interfaceName;
if (useManagerComponentServices) {
// If a user component needs to use the dynamic component management services,
// mtsComponent::EnableDynamicComponentManagement() should be called beforehand
// in the user component's constructor so that the internal required interface and DCC
// service object is properly initialized.
// Only validity of such internal structures is checked here.
mtsInterfaceRequired * required = GetInterfaceRequired(mtsManagerComponentBase::GetNameOfInterfaceInternalRequired());
if (!required) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceInternal: dynamic component management service (1) is not properly initialized" << std::endl;
return false;
}
if (!ManagerComponentServices) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceInternal: dynamic component management service (2) is not properly initialized" << std::endl;
return false;
}
}
if (InterfaceProvidedToManager) {
CMN_LOG_CLASS_INIT_WARNING << "InterfaceProvidedToManager already initialized!" << std::endl;
return true;
}
// Add provided interface (can't be done in constructor)
interfaceName = mtsManagerComponentBase::GetNameOfInterfaceInternalProvided();
mtsInterfaceProvided *provided = AddInterfaceProvided(interfaceName);
if (!provided) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceInternal: failed to add internal provided interface: " << interfaceName << std::endl;
return false;
}
provided->AddTag("System");
provided->AddCommandWriteReturn(&mtsComponent::InterfaceInternalCommands_GetEndUserInterface, this,
mtsManagerComponentBase::CommandNames::GetEndUserInterface);
provided->AddCommandWriteReturn(&mtsComponent::InterfaceInternalCommands_AddObserverList, this,
mtsManagerComponentBase::CommandNames::AddObserverList);
provided->AddCommandWriteReturn(&mtsComponent::InterfaceInternalCommands_RemoveEndUserInterface, this,
mtsManagerComponentBase::CommandNames::RemoveEndUserInterface);
provided->AddCommandWriteReturn(&mtsComponent::InterfaceInternalCommands_RemoveObserverList, this,
mtsManagerComponentBase::CommandNames::RemoveObserverList);
provided->AddEventWrite(EventGeneratorChangeState, mtsManagerComponentBase::EventNames::ChangeState,
mtsComponentStateChange());
// Save this interface so that we can call it later (to process the mailboxes)
InterfaceProvidedToManager = provided;
return true;
}
const mtsManagerComponentServices *mtsComponent::GetManagerComponentServices(void) const
{
if (ManagerComponentServices && ManagerComponentServices->IsConnected()) {
return ManagerComponentServices;
}
else {
CMN_LOG_CLASS_RUN_WARNING << GetName()
<< ": ManagerComponentServices not available, using mtsManagerLocal instead" << std::endl;
return mtsManagerLocal::GetInstance()->GetManagerComponentServices();
}
}
// Internal command
void mtsComponent::InterfaceInternalCommands_GetEndUserInterface(const mtsEndUserInterfaceArg & argin,
mtsEndUserInterfaceArg & argout)
{
CMN_ASSERT(argin.OriginalInterface);
argout = argin; // not really needed
argout.EndUserInterface = 0;
argout.EndUserInterface =
reinterpret_cast<size_t>(reinterpret_cast<mtsInterfaceProvided*>(argin.OriginalInterface)
->GetEndUserInterface(argin.UserName));
}
void mtsComponent::InterfaceInternalCommands_AddObserverList(const mtsEventHandlerList & argin,
mtsEventHandlerList & argout)
{
CMN_ASSERT(argin.Provided);
argin.Provided->AddObserverList(argin, argout);
}
void mtsComponent::InterfaceInternalCommands_RemoveEndUserInterface(const mtsEndUserInterfaceArg & argin,
mtsEndUserInterfaceArg & argout)
{
CMN_ASSERT(argin.OriginalInterface);
CMN_ASSERT(argin.EndUserInterface);
argout = argin; // not really needed
mtsInterfaceProvided *original = reinterpret_cast<mtsInterfaceProvided*>(argin.OriginalInterface);
mtsInterfaceProvided *endUser = reinterpret_cast<mtsInterfaceProvided*>(argin.EndUserInterface);
argout.EndUserInterface = reinterpret_cast<size_t>(original->RemoveEndUserInterface(endUser, argin.UserName));
}
void mtsComponent::InterfaceInternalCommands_RemoveObserverList(const mtsEventHandlerList & argin,
mtsEventHandlerList & argout)
{
CMN_ASSERT(argin.Provided);
argin.Provided->RemoveObserverList(argin, argout);
}
bool mtsComponent::SetReplayMode(void) {
if (!(this->State == mtsComponentState::CONSTRUCTED)) {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayMode: component \"" << GetName() << "\" failed to set replay mode." << std::endl;
return false;
}
this->ReplayMode = true;
return true;
}
bool mtsComponent::SetReplayData(const std::string & stateTableName, const std::string & fileName) {
if (!this->ReplayMode) {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayData: component \"" << GetName() << "\" not in replay mode." << std::endl;
return false;
}
// find state table and disable AutoAdvance
mtsStateTable * stateTable = this->StateTables.GetItem(stateTableName);
if (!stateTable) {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayData: component \"" << GetName() << "\", state table \"" << stateTableName << "\" was not found." << std::endl;
return false;
}
stateTable->SetAutomaticAdvance(false);
// get number of columns, i.e. all elements in state table
const size_t numberOfElements = stateTable->GetNumberOfElements();
mtsGenericObject * data = 0;
// make sure we don't overwrite timestamp when we will advance the state table
for (size_t index = 0; index < numberOfElements;index++) {
data = stateTable->GetStateVectorElement(index);
data->SetAutomaticTimestamp(false);
}
std::ifstream input(fileName.c_str());
if (!input.is_open()) {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayData: component \"" << GetName() << "\" unable to open file \"" << fileName << "\"." << std::endl;
return false;
}
// code below is adapted from http://stackoverflow.com/questions/3482064/counting-the-number-of-lines-in-a-text-file/3482093#3482093
// new lines will be skipped unless we stop it from happening:
input.unsetf(std::ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
size_t numberOfLines = std::count(std::istream_iterator<char>(input),
std::istream_iterator<char>(),
'\n');
// return to the beginning of the file and restore whitespace skipping
input.seekg(0);
input.setf(std::ios_base::skipws);
// resize state table and start loading data
stateTable->SetSize(numberOfLines);
for (size_t line = 0; line < numberOfLines; line++) {
stateTable->Start();
for (size_t index = 0; index < numberOfElements; index++) {
data = stateTable->GetStateVectorElement(index);
std::cerr << "data before de-serialization: " << *data << std::endl;
if (!data) {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayData: component \"" << GetName() << "\", state table \""
<< stateTableName << "\", failed to retrieve data element " << index << std::endl;
return false;
}
data->FromStreamRaw(input, ',');
std::cerr << "reader data " << index << " = " << *data << std::endl;
if (index != numberOfElements-1) {
char c;
input >> c;
if( c != ',') {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayData: component \"" << GetName() << "\", error parsing state table \""
<< stateTableName << "\" at line " << line << ", element " << index << std::endl;
}
}
}
stateTable->Advance();
stateTable->ReplayAdvance();
}
return true;
}
bool mtsComponent::SetReplayTime(const double CMN_UNUSED(time)) {
if (!this->ReplayMode) {
CMN_LOG_CLASS_RUN_ERROR << "SetReplayTime: component \"" << GetName() << "\" not in replay mode." << std::endl;
return false;
}
return true;
}
|