File size: 69,893 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 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 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 | /* -*- 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): Peter Kazanzides, Min Yang Jung, Anton Deguet
Created on: 2010-08-29
(C) Copyright 2010-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/mtsConfig.h>
#include <cisstMultiTask/mtsManagerComponent.h>
#include <cisstMultiTask/mtsManagerLocal.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstMultiTask/mtsInterfaceOutput.h>
#include <cisstMultiTask/mtsInterfaceInput.h>
#include <cisstMultiTask/mtsManagerComponentServices.h>
#include <cisstOSAbstraction/osaGetTime.h>
#include <cisstOSAbstraction/osaSleep.h>
#include <cisstOSAbstraction/osaDynamicLoader.h>
#include <cisstCommon/cmnUnits.h>
CMN_IMPLEMENT_SERVICES_DERIVED(mtsManagerComponent, mtsManagerComponentBase);
mtsManagerComponent::mtsManagerComponent()
: mtsManagerComponentBase(mtsManagerComponentBase::ComponentNames::ManagerComponent),
ComponentMap("ComponentMap"),
ConnectionID(0),
InterfaceComponentFunctionMap("InterfaceComponentFunctionMap", true)
{
this->mTags.clear();
this->AddTag("System");
ComponentMap.SetOwner(*this);
InterfaceComponentFunctionMap.SetOwner(*this);
// Add provided interface for dynamic component management
AddInterfaceComponent();
// Add provided interface for system-wide thread-safe logging
mtsInterfaceProvided * provided = AddInterfaceProvided(
mtsManagerComponentBase::InterfaceNames::InterfaceSystemLoggerProvided);
if (provided) {
provided->AddCommandWrite(&mtsManagerComponent::LogInterface::PrintLog,
&(this->Logger), mtsManagerComponentBase::CommandNames::PrintLog);
provided->AddEventWrite(Logger.EventPrintLog,
mtsManagerComponentBase::EventNames::PrintLog,
mtsLogMessage());
}
// Add this component to the map
ComponentMap.AddItem(mtsManagerComponentBase::ComponentNames::ManagerComponent, this);
}
mtsManagerComponent::~mtsManagerComponent()
{
}
void mtsManagerComponent::Startup(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Manager Component starts" << std::endl;
}
void mtsManagerComponent::Run(void)
{
mtsManagerComponentBase::Run();
}
void mtsManagerComponent::Cleanup(void)
{
}
std::string mtsManagerComponent::GetInterfaceUID(
const std::string & processName, const std::string & componentName, const std::string & interfaceName)
{
return (processName.empty()) ? (componentName + ":" + interfaceName)
: processName + ":" + componentName + ":" + interfaceName;
}
//***************************** Dynamic Component Management (provided interface) ***********************************
bool mtsManagerComponent::AddInterfaceComponent(void)
{
// InterfaceComponent's required interface is not created here but is created
// when a user component with internal interfaces connects to the manager
// component.
// See mtsManagerComponent::AddNewClientComponent() for the dynamic
// creation of required interfaces.
// Add provided interface to which InterfaceInternal's required interface connects.
std::string interfaceName = mtsManagerComponentBase::GetNameOfInterfaceComponentProvided();
// Return if provided interface already exists
if (GetInterfaceProvided(interfaceName)) {
return true;
}
mtsInterfaceProvided * provided = AddInterfaceProvided(interfaceName);
if (!provided) {
CMN_LOG_CLASS_INIT_ERROR << "AddInterfaceComponent: failed to add \"Component\" provided interface: "
<< interfaceName << std::endl;
return false;
}
//***************************** Component creation *********************************************
provided->AddCommandWriteReturn(&mtsManagerComponent::ComponentCreate,
this, mtsManagerComponentBase::CommandNames::ComponentCreate);
provided->AddCommandWriteReturn(&mtsManagerComponent::ComponentAdd,
this, mtsManagerComponentBase::CommandNames::ComponentAdd);
provided->AddCommandWriteReturn(&mtsManagerComponent::ComponentRemove,
this, mtsManagerComponentBase::CommandNames::ComponentRemove);
provided->AddCommandQualifiedRead(&mtsManagerComponent::ComponentGet,
this, mtsManagerComponentBase::CommandNames::ComponentGet);
provided->AddCommandWrite(&mtsManagerComponent::ComponentConfigure,
this, mtsManagerComponentBase::CommandNames::ComponentConfigure);
//***************************** Component connection management *********************************
provided->AddCommandWriteReturn(&mtsManagerComponent::ComponentConnect,
this, mtsManagerComponentBase::CommandNames::ComponentConnect);
provided->AddCommandWriteReturn(&mtsManagerComponent::ComponentDisconnect,
this, mtsManagerComponentBase::CommandNames::ComponentDisconnect);
//***************************** Component state management **************************************
provided->AddCommandWrite(&mtsManagerComponent::ComponentStart,
this, mtsManagerComponentBase::CommandNames::ComponentStart);
provided->AddCommandWrite(&mtsManagerComponent::ComponentStop,
this, mtsManagerComponentBase::CommandNames::ComponentStop);
provided->AddCommandWrite(&mtsManagerComponent::ComponentResume,
this, mtsManagerComponentBase::CommandNames::ComponentResume);
provided->AddCommandQualifiedRead(&mtsManagerComponent::ComponentGetState,
this, mtsManagerComponentBase::CommandNames::ComponentGetState);
//************************************* Dynamic loading *****************************************
provided->AddCommandQualifiedRead(&mtsManagerComponent::LoadLibrary,
this, mtsManagerComponentBase::CommandNames::LoadLibrary);
//****************************** Getters ********************************************************
provided->AddCommandRead(&mtsManagerComponent::GetNamesOfProcesses,
this, mtsManagerComponentBase::CommandNames::GetNamesOfProcesses);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetNamesOfComponents,
this, mtsManagerComponentBase::CommandNames::GetNamesOfComponents);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetDescriptionsOfComponents,
this, mtsManagerComponentBase::CommandNames::GetDescriptionsOfComponents);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetNamesOfInterfaces,
this, mtsManagerComponentBase::CommandNames::GetNamesOfInterfaces);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetDescriptionsOfInterfaces,
this, mtsManagerComponentBase::CommandNames::GetDescriptionsOfInterfaces);
provided->AddCommandRead(&mtsManagerComponent::GetListOfConnections,
this, mtsManagerComponentBase::CommandNames::GetListOfConnections);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetListOfComponentClasses,
this, mtsManagerComponentBase::CommandNames::GetListOfComponentClasses);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetInterfaceProvidedDescription,
this, mtsManagerComponentBase::CommandNames::GetInterfaceProvidedDescription);
provided->AddCommandQualifiedRead(&mtsManagerComponent::GetInterfaceRequiredDescription,
this, mtsManagerComponentBase::CommandNames::GetInterfaceRequiredDescription);
provided->AddEventWrite(this->InterfaceComponentEvents_AddComponent,
mtsManagerComponentBase::EventNames::AddComponent, mtsDescriptionComponent());
provided->AddEventWrite(this->InterfaceComponentEvents_ChangeState,
mtsManagerComponentBase::EventNames::ChangeState, mtsComponentStateChange());
provided->AddEventWrite(this->InterfaceComponentEvents_AddConnection,
mtsManagerComponentBase::EventNames::AddConnection, mtsDescriptionConnection());
provided->AddEventWrite(this->InterfaceComponentEvents_RemoveConnection,
mtsManagerComponentBase::EventNames::RemoveConnection, mtsDescriptionConnection());
CMN_LOG_CLASS_INIT_VERBOSE << "AddInterfaceComponent: successfully added \"Component\" interfaces" << std::endl;
return true;
}
// Note that this dynamically creates a new component. Do not confuse with mtsComponent::Create().
void mtsManagerComponent::ComponentCreate(const mtsDescriptionComponent & componentDescription, bool & result)
{
result = false;
// CreateComponentDynamically is in LCM so that it can be run in the caller's thread when needed.
mtsManagerLocal * LCM = mtsManagerLocal::GetInstance();
mtsComponent * newComponent = LCM->CreateComponentDynamically(componentDescription.ClassName,
componentDescription.ComponentName,
componentDescription.ConstructorArgSerialized);
if (newComponent) {
ComponentAdd(newComponent, result);
if (result) {
CMN_LOG_CLASS_RUN_VERBOSE << GetName() << ": successfully created and added component: "
<< "\"" << componentDescription.ComponentName << "\" of type \""
<< componentDescription.ClassName << "\"" << std::endl;
result = true;
}
else {
CMN_LOG_CLASS_RUN_ERROR << GetName() << ": failed to add component: "
<< "\"" << componentDescription.ComponentName << "\" of type \""
<< componentDescription.ClassName << "\"" << std::endl;
}
}
else {
CMN_LOG_CLASS_RUN_ERROR << GetName() << ": failed to create component: "
<< "\"" << componentDescription.ComponentName << "\" of type \""
<< componentDescription.ClassName << "\"" << std::endl;
}
}
void mtsManagerComponent::ComponentAdd(const mtsComponentPointer & componentPtr, bool & result)
{
result = false;
mtsComponent * component = componentPtr.GetPointer();
std::string componentName = component->GetName();
// If component does not yet have a valid name, assign one now, based on the class
// name and the pointer value (to ensure that name is unique).
if (componentName.empty()) {
componentName.assign(component->Services()->GetName());
char buf[20];
sprintf(buf, "_%p", component);
componentName.append(buf);
CMN_LOG_CLASS_INIT_DEBUG << "AddComponent: assigning name \"" << componentName << "\"" << std::endl;
component->SetName(componentName);
}
if (ComponentMap.FindItem(componentName)) {
CMN_LOG_CLASS_INIT_ERROR << "AddComponent: component with name \"" << componentName
<< "\" already exists" << std::endl;
return;
}
if (componentName == mtsManagerComponentBase::ComponentNames::ManagerLocal) {
// Special case handling: when the LCM is added, we set the Connection ID for the internal
// required interface, because the connection was done in mtsManagerLocal rather than by
// calling Connect.
std::string requiredName = mtsManagerComponentBase::GetNameOfInterfaceInternalRequired();
mtsInterfaceRequired *required = component->GetInterfaceRequired(requiredName);
if (required && (required->ConnectionID == InvalidConnectionID)) {
required->ConnectionID = ConnectionID++;
}
}
// If dynamic component management is enabled
if (component->GetInterfaceRequired(mtsManagerComponentBase::GetNameOfInterfaceInternalRequired())) {
if (!mtsManagerComponentBase::IsManagerComponent(componentName)) {
// Add internal provided and required interface for dynamic component management service
// (except for LCM, which was already done, and MCS, for which it is not applicable)
if (!component->AddInterfaceInternal(true)) {
CMN_LOG_CLASS_INIT_ERROR << "AddComponent: failed to add \"Internal\" provided and required interfaces: "
<< componentName << std::endl;
return;
}
}
}
// If dynamic component management is not enabled
else {
// Add an internal provided interface. This interface is connected to the
// manager component and is used to inform it of the change of
// the running state of this component (more features can be added later).
if (!component->AddInterfaceInternal()) {
CMN_LOG_CLASS_INIT_ERROR << "AddComponent: failed to add \"Internal\" provided interfaces: "
<< componentName << std::endl;
return;
}
}
if (!ComponentMap.AddItem(componentName, component)) {
CMN_LOG_CLASS_INIT_ERROR << "AddComponent: failed to add component to local component manager: "
<< componentName << std::endl;
return;
}
// Connect user component's internal interface to the manager component.
// That is, connect InterfaceInternal.Required to InterfaceComponent.Provided.
// This enables user components to use dynamic component composition services
// through cisstMultiTask's thread-safe command pattern.
// Create InterfaceComponent's required interface which will be connected
// to connect user component's InterfaceInternal's provided interface.
if (!AddNewClientComponent(componentName)) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentAdd: "
<< "failed to add InterfaceComponent's required interface to MCS: "
<< "\"" << componentName << "\"" << std::endl;
return;
}
// Connect user component to the manager component. If a component
// has InterfaceInternal's required interface which provides dynamic
// component control services, the required interface gets connected to
// InterfaceComponent's provided interface.
if (!ConnectToManagerComponent(componentName)) {
CMN_LOG_CLASS_INIT_ERROR << "AddComponent: failed to connect component \"" << componentName << "\" "
<< "to MCS" << std::endl;
return;
}
CMN_LOG_CLASS_INIT_VERBOSE << "ComponentAdd: connected user component "
<< "\"" << componentName << "\" to manager component "
<< "\"" << GetName() << "\""
<< std::endl;
CMN_LOG_CLASS_INIT_VERBOSE << "ComponentAdd: successfully added component: " << componentName << std::endl;
mtsDescriptionComponent componentDesc("", componentName, component->GetTags());
InterfaceComponentEvents_AddComponent(componentDesc);
result = true;
}
void mtsManagerComponent::ComponentRemove(const std::string & componentName, bool & result)
{
result = false;
if (mtsManagerComponentBase::IsManagerComponent(componentName)) {
CMN_LOG_CLASS_RUN_VERBOSE << "ComponentRemove: ignoring manager component " << componentName << std::endl;
}
else {
if (DisconnectFromManagerComponent(componentName))
result = ComponentMap.RemoveItem(componentName);
}
}
void mtsManagerComponent::ComponentGet(const std::string & componentName, mtsComponentPointer & componentPtr) const
{
componentPtr.SetPointer(ComponentMap.GetItem(componentName));
}
void mtsManagerComponent::ComponentConfigure(const mtsDescriptionComponent & arg)
{
mtsComponent * component = GetComponent(arg.ComponentName);
if (!component) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentConfigure - no component found: "
<< arg.ComponentName << std::endl;
return;
}
// For now, using ConstructorArgSerialized field.
component->Configure(arg.ConstructorArgSerialized);
}
void mtsManagerComponent::ComponentConnect(const mtsDescriptionConnection & arg, bool & result)
{
result = ConnectInternal(arg.Client.ComponentName, arg.Client.InterfaceName,
arg.Server.ComponentName, arg.Server.InterfaceName,
arg.Requestor);
}
void mtsManagerComponent::ComponentDisconnect(const mtsDescriptionConnection & arg, bool & result)
{
if ((arg.Client.ComponentName.empty() || arg.Server.ComponentName.empty())
&& (arg.ConnectionID != InvalidConnectionID)) {
// Disconnect based on ConnectionID (also check that either Client or Server
// component name is empty to avoid infinite loop)
mtsDescriptionConnection argCopy(arg);
if (FindByConnectionID(argCopy)) {
ComponentDisconnect(argCopy, result);
}
else {
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: could not find connection with ID "
<< arg.ConnectionID << std::endl;
result = false;
}
return;
}
result = true;
if (!GetComponent(arg.Client.ComponentName)) {
result = false;
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: did not find client component: "
<< arg.Client.ComponentName << std::endl;
}
if (!GetComponent(arg.Server.ComponentName)) {
result = false;
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: did not find server component: "
<< arg.Server.ComponentName << std::endl;
}
if (result) {
if (DisconnectInternal(arg.Client.ComponentName, arg.Client.InterfaceName,
arg.Server.ComponentName, arg.Server.InterfaceName, arg.Requestor))
{
CMN_LOG_CLASS_RUN_VERBOSE << "ComponentDisconnect: successfully disconnected: " << arg << std::endl;
}
else {
result = false;
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: failed to disconnect: " << arg << std::endl;
}
}
}
void mtsManagerComponent::ComponentStart(const mtsComponentStatusControl & arg)
{
// Check if command is for this component
if (arg.ComponentName == this->GetName()) {
CMN_LOG_CLASS_RUN_WARNING << "ComponentStart for " << arg.ComponentName << " ignored." << std::endl;
return;
}
CMN_LOG_CLASS_RUN_VERBOSE << "ComponentStart: starting component " << arg.ComponentName << " from MCS" << std::endl;
mtsComponent * component = GetComponent(arg.ComponentName);
if (component) {
if (component->GetState() == mtsComponentState::CONSTRUCTED) {
mtsTaskContinuous *task = dynamic_cast<mtsTaskContinuous *>(component);
if (task->CreatesThread()) {
// Start an internal thread (if needed)
component->Create();
// Wait for internal thread to be created
osaSleep(arg.DelayInSecond);
}
else {
// If the component does not create its own thread, then we do not create (or start) it
// because otherwise it would capture the current (MCS) thread.
CMN_LOG_CLASS_RUN_WARNING << "ComponentStart: not starting component (task) " << arg.ComponentName
<< " because it would capture MCS thread" << std::endl;
return;
}
}
component->Start();
}
else {
CMN_LOG_CLASS_RUN_ERROR << "ComponentStart: could find component \"" << arg.ComponentName << "\"" << std::endl;
}
}
void mtsManagerComponent::ComponentStop(const mtsComponentStatusControl & arg)
{
// Check if command is for this component (MCS)
if (arg.ComponentName == this->GetName()) {
CMN_LOG_CLASS_RUN_WARNING << "ComponentStop for " << arg.ComponentName << " ignored." << std::endl;
return;
}
mtsComponent * component = GetComponent(arg.ComponentName);
if (component) {
component->Suspend();
}
else {
CMN_LOG_CLASS_RUN_ERROR << "ComponentStop: could not find component \"" << arg.ComponentName << "\"" << std::endl;
}
}
void mtsManagerComponent::ComponentResume(const mtsComponentStatusControl & arg)
{
mtsComponent * component = GetComponent(arg.ComponentName);
if (component) {
// Wait if desired
osaSleep(arg.DelayInSecond);
// Resume (Start) the component
component->Start();
}
else {
CMN_LOG_CLASS_RUN_ERROR << "ComponentResume: did not find component \""
<< arg.ComponentName << "\"" << std::endl;
}
}
void mtsManagerComponent::ComponentGetState(const mtsDescriptionComponent &arg,
mtsComponentState &state) const
{
mtsComponent * component = GetComponent(arg.ComponentName);
if (component) {
component->GetState(state);
}
else {
CMN_LOG_CLASS_RUN_ERROR << "ComponentGetState: did not find component \""
<< arg.ComponentName << "\"" << std::endl;
}
}
void mtsManagerComponent::LoadLibrary(const mtsDescriptionLoadLibrary &lib, bool &result) const
{
osaDynamicLoader dl;
result = dl.Load(lib.LibraryName.c_str());
}
void mtsManagerComponent::GetNamesOfProcesses(std::vector<std::string> & names) const
{
names.clear();
names.push_back("");
}
void mtsManagerComponent::GetNamesOfComponents(const std::string & processName,
std::vector<std::string> & names) const
{
// PK TODO: could check processName
ComponentMap.GetNames(names);
}
void mtsManagerComponent::GetDescriptionsOfComponents(const std::string & processName,
std::vector<mtsDescriptionComponent> & descriptions) const
{
descriptions.clear();
ComponentMapType::MapType::const_iterator it = ComponentMap.begin();
const ComponentMapType::MapType::const_iterator itEnd = ComponentMap.end();
for (; it != itEnd; ++it) {
mtsDescriptionComponent desc;
desc.ProcessName = "";
desc.ComponentName = it->first;
mtsComponent * component = it->second;
if (component) {
desc.ClassName = component->Services()->GetName();
desc.Tags = component->mTags;
}
descriptions.push_back(desc);
}
}
void mtsManagerComponent::GetNamesOfInterfaces(
const mtsDescriptionComponent & arg, mtsDescriptionInterface & interfaces) const
{
mtsComponent * component = GetComponent(arg.ComponentName);
if (component) {
interfaces.ProcessName = arg.ProcessName;
interfaces.ComponentName = arg.ComponentName;
// Get a list of required interfaces
interfaces.InterfaceRequiredNames = component->GetNamesOfInterfacesRequiredOrInput();
// Get a list of provided interfaces
interfaces.InterfaceProvidedNames = component->GetNamesOfInterfacesProvidedOrOutput();
}
else {
CMN_LOG_CLASS_RUN_ERROR << "GetNamesOfInterfaces: did not find component \""
<< arg.ComponentName << "\"" << std::endl;
}
}
void mtsManagerComponent::GetDescriptionsOfInterfaces(
const mtsDescriptionComponent & arg, mtsDescriptionInterfaceFull & interfaces) const
{
mtsComponent * component = GetComponent(arg.ComponentName);
if (component) {
std::vector<std::string>::const_iterator it;
// Get a list of required interfaces
std::vector<std::string> interfaceRequiredNames = component->GetNamesOfInterfacesRequiredOrInput();
for (it = interfaceRequiredNames.begin(); it != interfaceRequiredNames.end(); ++it) {
mtsInterfaceRequired * required = component->GetInterfaceRequired(*it);
if (required) {
mtsDescriptionInterfaceFullName desc;
desc.ProcessName = arg.ProcessName;
desc.ComponentName = arg.ComponentName;
desc.InterfaceName = *it;
desc.Tags = required->GetTags();
interfaces.Required.push_back(desc);
}
}
// Get a list of provided interfaces
std::vector<std::string> interfaceProvidedNames = component->GetNamesOfInterfacesProvidedOrOutput();
for (it = interfaceProvidedNames.begin(); it != interfaceProvidedNames.end(); ++it) {
mtsInterfaceProvided * provided = component->GetInterfaceProvided(*it);
if (provided) {
mtsDescriptionInterfaceFullName desc;
desc.ProcessName = arg.ProcessName;
desc.ComponentName = arg.ComponentName;
desc.InterfaceName = *it;
desc.Tags = provided->GetTags();
interfaces.Provided.push_back(desc);
}
}
}
else {
CMN_LOG_CLASS_RUN_ERROR << "GetDescriptionsOfInterfaces: did not find component \""
<< arg.ComponentName << "\"" << std::endl;
}
}
void mtsManagerComponent::GetListOfConnections(std::vector<mtsDescriptionConnection> & listOfConnections) const
{
// Loop through components, looking at required and input interfaces, which can have at most
// one connection (as opposed to provided or output interfaces, which can have multiple).
typename ComponentMapType::const_iterator itComp;
for (itComp = ComponentMap.begin(); itComp != ComponentMap.end(); itComp++) {
mtsComponent *component = itComp->second;
// Get a list of required interfaces
std::vector<std::string> interfaceRequiredNames = component->GetNamesOfInterfacesRequired();
typename std::vector<std::string>::const_iterator itReq;
for (itReq = interfaceRequiredNames.begin(); itReq != interfaceRequiredNames.end(); ++itReq) {
const mtsInterfaceRequired * required = component->GetInterfaceRequired(*itReq);
CMN_ASSERT(required);
const mtsInterfaceProvided * provided = required->GetConnectedInterface();
if (provided) {
provided = provided->GetOriginalInterface();
mtsDescriptionConnection conn("", itComp->first, *itReq,
"", provided->GetComponent()->GetName(), provided->GetName(),
required->GetConnectionID());
listOfConnections.push_back(conn);
}
}
// Get a list of input interfaces
std::vector<std::string> interfaceInputNames = component->GetNamesOfInterfacesInput();
typename std::vector<std::string>::const_iterator itIn;
for (itIn = interfaceInputNames.begin(); itIn != interfaceInputNames.end(); ++itIn) {
const mtsInterfaceInput * input = component->GetInterfaceInput(*itIn);
CMN_ASSERT(input);
const mtsInterfaceOutput * output = input->GetConnectedInterface();
if (output) {
mtsDescriptionConnection conn("", itComp->first, *itIn,
"", output->GetComponent()->GetName(), output->GetName(),
input->GetConnectionID());
listOfConnections.push_back(conn);
}
}
}
// Sort the list based on ConnectionID (lowest to highest)
std::sort(listOfConnections.begin(), listOfConnections.end());
}
void mtsManagerComponent::GetListOfComponentClasses(const std::string &processName,
std::vector <mtsDescriptionComponentClass> & listOfComponentClasses) const
{
// PK TODO: check processName
// Loop through the class register, looking for components that can be created with one argument
// or derived from mtsComponent (and have dynamic creation enabled).
cmnClassRegister::const_iterator it = cmnClassRegister::begin();
while (it != cmnClassRegister::end()) {
if (it->second->OneArgConstructorAvailable()) {
// CMN_DYNAMIC_CREATION_ONEARG or CMN_DYNAMIC_CREATION_SETNAME
mtsDescriptionComponentClass classInfo;
classInfo.ClassName = it->first;
const cmnClassServicesBase *argServices = it->second->GetConstructorArgServices();
if (argServices) {
classInfo.ArgType = argServices->GetName();
classInfo.ArgTypeId = argServices->TypeInfoPointer()->name();
}
listOfComponentClasses.push_back(classInfo);
}
else if (it->second->HasDynamicCreation() && it->second->IsDerivedFrom<mtsComponent>()) {
// Backward compatibility (CMN_DYNAMIC_CREATION)
mtsDescriptionComponentClass classInfo;
classInfo.ClassName = it->first;
classInfo.ArgType = "std::string";
classInfo.ArgTypeId = typeid(std::string).name();
listOfComponentClasses.push_back(classInfo);
}
it++;
}
}
void mtsManagerComponent::GetInterfaceProvidedDescription(const mtsDescriptionInterface & intfc,
mtsInterfaceProvidedDescription & description) const
{
if (intfc.InterfaceProvidedNames.size() < 1) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceProvidedDescription: provided interface name not specified for component \""
<< intfc.ComponentName << "\"" << std::endl;
return;
}
// Get component specified
mtsComponent * component = GetComponent(intfc.ComponentName);
if (!component) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceProvidedDescription: did not find component \""
<< intfc.ComponentName << "\"" << std::endl;
return;
}
// Get provided interface specified
std::string interfaceName(intfc.InterfaceProvidedNames[0]);
mtsInterfaceProvided * interfaceProvided = component->GetInterfaceProvided(interfaceName);
if (!interfaceProvided) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceProvidedDescription: no provided interface \""
<< interfaceName << "\" found in component \"" << intfc.ComponentName << "\"" << std::endl;
return;
}
// Extract complete information about all commands and event generators in
// the provided interface specified. Argument prototypes are serialized.
description.InterfaceName = interfaceName;
if (!interfaceProvided->GetDescription(description)) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceProvidedDescription: failed to get complete information of \""
<< interfaceName << "\" found in component \"" << intfc.ComponentName << "\"" << std::endl;
description.InterfaceName.clear();
}
}
void mtsManagerComponent::GetInterfaceRequiredDescription(const mtsDescriptionInterface & intfc,
mtsInterfaceRequiredDescription & description) const
{
if (intfc.InterfaceRequiredNames.size() < 1) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceRequiredDescription: required interface name not specified for component \""
<< intfc.ComponentName << "\"" << std::endl;
return;
}
// Get the component instance specified
mtsComponent * component = GetComponent(intfc.ComponentName);
if (!component) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceRequiredDescription: did not find component \""
<< intfc.ComponentName << "\"" << std::endl;
return;
}
// Get the required interface specified
std::string interfaceName(intfc.InterfaceRequiredNames[0]);
mtsInterfaceRequired * requiredInterface = component->GetInterfaceRequired(interfaceName);
if (!requiredInterface) {
CMN_LOG_CLASS_RUN_ERROR << "GetInterfaceRequiredDescription: no required interface \""
<< interfaceName << "\" found in component \"" << intfc.ComponentName << "\"" << std::endl;
return;
}
// Extract complete information about all functions and event handlers in
// a required interface. Argument prototypes are fetched with serialization.
description.InterfaceName = interfaceName;
requiredInterface->GetDescription(description);
}
mtsComponent * mtsManagerComponent::GetComponent(const std::string &componentName) const
{
return ComponentMap.GetItem(componentName);
}
//***************************************** System-wide Logging ********************************************
void mtsManagerComponent::LogInterface::PrintLog(const mtsLogMessage & log)
{
std::string now;
osaGetDateTimeString(now, ':');
std::string msg(log.Message, log.Length);
std::stringstream ss;
ss << "|" << now << " " << log.ProcessName << "| " << msg;
mtsLogMessage _log(ss.str().c_str(), ss.str().size());
_log.ProcessName = log.ProcessName;
// Generate system-wide thread-safe logging event
EventPrintLog(_log);
}
//********************************** Connect/Disconnect Internal *************************************************
bool mtsManagerComponent::ConnectInternal(const std::string & clientComponentName, const std::string & clientInterfaceName,
const std::string & serverComponentName, const std::string & serverInterfaceName,
const std::string & requestComponentName)
{
std::vector<std::string> options;
std::stringstream allOptions;
std::ostream_iterator< std::string > output(allOptions, ", ");
mtsComponent * clientComponent = GetComponent(clientComponentName);
if (!clientComponent) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: failed to get client component: \""
<< clientComponentName << "\"" << std::endl;
return false;
}
mtsComponent * serverComponent = GetComponent(serverComponentName);
if (!serverComponent) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: failed to get server component: \""
<< serverComponentName << "\"" << std::endl;
return false;
}
// First, try to figure out if the interface is either provided or output
mtsInterfaceProvided * serverInterfaceProvided = serverComponent->GetInterfaceProvided(serverInterfaceName);
mtsInterfaceOutput * serverInterfaceOutput = serverComponent->GetInterfaceOutput(serverInterfaceName);
if (!serverInterfaceProvided && !serverInterfaceOutput) {
// test for swapped interfaces
CMN_LOG_CLASS_INIT_DEBUG << "ComponentConnect: looking for provided/output interface in first component as well" << std::endl;
serverInterfaceProvided = clientComponent->GetInterfaceProvided(clientInterfaceName);
serverInterfaceOutput = clientComponent->GetInterfaceOutput(clientInterfaceName);
if (!serverInterfaceProvided && !serverInterfaceOutput) {
options = serverComponent->GetNamesOfInterfacesProvidedOrOutput();
if (options.size() == 0) {
allOptions << "no provided/output interface available for this component";
} else {
allOptions << "available provided/output interface(s): ";
std::copy(options.begin(), options.end(), output);
}
return false;
} else {
CMN_LOG_CLASS_INIT_DEBUG << "ComponentConnect: Swapping client/server" << std::endl;
return ConnectInternal(serverComponentName, serverInterfaceName,
clientComponentName, clientInterfaceName, requestComponentName);
}
}
// Now, handle the connections. First, we look for connection between required/provided interfaces. Then, we look
// for connection between input/output interfaces.
if (serverInterfaceProvided) {
mtsInterfaceRequired * clientInterfaceRequired = clientComponent->GetInterfaceRequired(clientInterfaceName);
if (!clientInterfaceRequired) {
options = clientComponent->GetNamesOfInterfacesRequiredOrInput();
if (options.size() == 0) {
allOptions << "no required/input interface available for this component";
} else {
allOptions << "available required/input interface(s): ";
std::copy(options.begin(), options.end(), output);
}
return false;
}
// Check if the required interface is already connected
const mtsInterfaceProvided * interfaceConnected = clientInterfaceRequired->GetConnectedInterface();
if (interfaceConnected) {
interfaceConnected = interfaceConnected->GetOriginalInterface();
if (interfaceConnected == serverInterfaceProvided) {
CMN_LOG_CLASS_INIT_ERROR << "Connect: failed to connect - already connected interfaces: \""
<< GetInterfaceUID("", clientComponentName, clientInterfaceName)
<< "\" - \""
<< GetInterfaceUID("", serverComponentName, serverInterfaceName)
<< "\"" << std::endl;
}
else {
CMN_LOG_CLASS_INIT_ERROR << "Connect: failed to connect: \""
<< GetInterfaceUID("", clientComponentName, clientInterfaceName)
<< "\" - \""
<< GetInterfaceUID("", serverComponentName, serverInterfaceName)
<< " -- required interface already connected to: \""
<< GetInterfaceUID("", interfaceConnected->GetComponentName(), interfaceConnected->GetName())
<< "\"" << std::endl;
}
return false;
}
bool success = false;
// If the server is this component (ManagerComponent), or if the server component is not active, or
// if the server component is the same as the requestor (and therefore waiting for the connection),
// we can use the previous implementation (mtsInterfaceRequired::ConnectTo), which directly calls the methods.
if ((serverComponent == this) ||
!serverComponent->IsRunning() ||
(serverComponentName == requestComponentName)) {
success = clientInterfaceRequired->ConnectTo(serverInterfaceProvided);
}
else {
InterfaceComponentFunctionType * serverFunctionSet = InterfaceComponentFunctionMap.GetItem(serverComponentName);
if (!serverFunctionSet) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: failed to connect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", failed to get function set for " << serverComponentName << std::endl;
return false;
}
if (!serverFunctionSet->GetEndUserInterface.IsValid()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentConnect: GetEndUserInterface invalid for " << serverComponentName << std::endl;
return false;
}
if (!serverFunctionSet->AddObserverList.IsValid()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentConnect: AddObserverList invalid for " << serverComponentName << std::endl;
return false;
}
mtsEndUserInterfaceArg endUserInterfaceArg(reinterpret_cast<size_t>(serverInterfaceProvided), clientInterfaceName, 0);
#if (CISST_OS == CISST_LINUX_XENOMAI && CISST_MTS_64BIT)
{
// See void mtsComponent::InterfaceInternalCommands_GetEndUserInterface()
// 11/20/13: Patch for cisstDataGenerator
//endUserInterfaceArg.EndUserInterface = endUserInterfaceArg.OriginalInterface->GetEndUserInterface(clientInterfaceName);
mtsInterfaceProvided * originalInterface = reinterpret_cast<mtsInterfaceProvided*>(endUserInterfaceArg.OriginalInterface);
CMN_ASSERT(originalInterface);
endUserInterfaceArg.EndUserInterface =
reinterpret_cast<size_t>(originalInterface->GetEndUserInterface(clientInterfaceName));
}
#else
serverFunctionSet->GetEndUserInterface(endUserInterfaceArg, endUserInterfaceArg);
#endif
mtsInterfaceProvided * endUserInterface = reinterpret_cast<mtsInterfaceProvided*>(endUserInterfaceArg.EndUserInterface);
if (!endUserInterface) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentConnect: failed to get end-user interface for " << serverComponentName << std::endl;
return false;
}
success = clientInterfaceRequired->BindCommands(endUserInterface);
mtsEventHandlerList eventList(endUserInterface);
clientInterfaceRequired->GetEventList(eventList);
#if (CISST_OS == CISST_LINUX_XENOMAI && CISST_MTS_64BIT)
{
// From void mtsInterfaceProvided::AddObserverList(const mtsEventHandlerList & argin, mtsEventHandlerList & argout)
size_t i;
for (i = 0; i < eventList.VoidEvents.size(); i++) {
eventList.VoidEvents[i].Result = eventList.Provided->AddObserver(eventList.VoidEvents[i].EventName, eventList.VoidEvents[i].HandlerPointer);
}
for (i = 0; i < eventList.WriteEvents.size(); i++) {
eventList.WriteEvents[i].Result = eventList.Provided->AddObserver(eventList.WriteEvents[i].EventName, eventList.WriteEvents[i].HandlerPointer);
}
}
#else
serverFunctionSet->AddObserverList(eventList, eventList);
#endif
if (!clientInterfaceRequired->CheckEventList(eventList))
success = false;
}
if (success) {
// Assign ConnectionID
clientInterfaceRequired->ConnectionID = ConnectionID;
CMN_LOG_CLASS_INIT_VERBOSE << "ComponentConnect: successfully connected required/provided: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName << std::endl;
} else {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: failed to connect required/provided: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName << std::endl;
}
}
else { // Input/Output connection
if (!serverInterfaceOutput) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: failed to connect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", server does not have provided or output interface." << std::endl;
return false;
}
mtsInterfaceInput * clientInterfaceInput = clientComponent->GetInterfaceInput(clientInterfaceName);
if (!clientInterfaceInput) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: failed to connect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", client does not have input interface." << std::endl;
return false;
}
if (clientInterfaceInput->ConnectTo(serverInterfaceOutput)) {
// Assign ConnectionID
clientInterfaceInput->ConnectionID = ConnectionID;
CMN_LOG_CLASS_INIT_VERBOSE << "ComponentConnect: component \""
<< this->GetName()
<< "\" input interface \"" << clientInterfaceName
<< "\" successfully connected to output interface \""
<< serverInterfaceName << "\"" << std::endl;
} else {
CMN_LOG_CLASS_INIT_ERROR << "ComponentConnect: component \""
<< this->GetName()
<< "\" input interface \"" << clientInterfaceName
<< "\" failed to connect to output interface \""
<< serverInterfaceName << "\"" << std::endl;
return false;
}
}
// Successful connection, now update data structures
mtsDescriptionConnection description(
"", clientComponentName, clientInterfaceName,
"", serverComponentName, serverInterfaceName,
ConnectionID);
// Increase connection id
if (ConnectionID + 1 == InvalidConnectionID) {
CMN_LOG_CLASS_INIT_ERROR << "Connect: connection id approached its upper limit: " << InvalidConnectionID << std::endl;
} else {
++ConnectionID;
}
CMN_LOG_CLASS_RUN_VERBOSE << "ComponentConnect: successfully connected: " << description << std::endl;
// Generate the event
InterfaceComponentEvents_AddConnection(description);
return true;
}
bool mtsManagerComponent::DisconnectInternal(const std::string & clientComponentName, const std::string & clientInterfaceName,
const std::string & serverComponentName, const std::string & serverInterfaceName,
const std::string & requestComponentName)
{
mtsComponent * clientComponent = GetComponent(clientComponentName);
if (!clientComponent) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: failed to get client component: \"" << clientComponentName << "\"" << std::endl;
return false;
}
mtsComponent * serverComponent = GetComponent(serverComponentName);
if (!serverComponent) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: failed to get server component: \"" << serverComponentName << "\"" << std::endl;
return false;
}
mtsInterfaceProvided * serverInterfaceProvided = serverComponent->GetInterfaceProvided(serverInterfaceName);
mtsInterfaceOutput * serverInterfaceOutput = serverComponent->GetInterfaceOutput(serverInterfaceName);
if (!serverInterfaceProvided && !serverInterfaceOutput) {
// test for swapped interfaces
CMN_LOG_CLASS_INIT_DEBUG << "ComponentDisconnect: looking for provided/output interface in first component as well" << std::endl;
serverInterfaceProvided = clientComponent->GetInterfaceProvided(clientInterfaceName);
serverInterfaceOutput = clientComponent->GetInterfaceOutput(clientInterfaceName);
if (!serverInterfaceProvided && !serverInterfaceOutput) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: failed to get provided/output interface \"" << serverInterfaceName << "\""
<< " in component \"" << serverComponentName << "\"" << std::endl;
return false;
} else {
CMN_LOG_CLASS_INIT_DEBUG << "ComponentDisconnect: Swapping client/server" << std::endl;
return DisconnectInternal(serverComponentName, serverInterfaceName, clientComponentName, clientInterfaceName,
requestComponentName);
}
}
// Now, handle the disconnection. First, we look for connection between required/provided interfaces. Then, we look
// for connection between input/output interfaces.
ConnectionIDType thisConnectionID = InvalidConnectionID;
if (serverInterfaceProvided) {
mtsInterfaceRequired *clientInterfaceRequired = clientComponent->GetInterfaceRequired(clientInterfaceName);
if (!clientInterfaceRequired) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: failed to disconnect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", client does not have required interface." << std::endl;
return false;
}
// Following check should not be necessary -- serverInterfaceProvided should always be the original interface
// because we obtained it via a string query.
if (serverInterfaceProvided->GetOriginalInterface()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: failed to disconnect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", did not get original interface." << std::endl;
return false;
}
// Now, get the end-user interface for this client
mtsInterfaceProvided *endUserInterface = serverInterfaceProvided->FindEndUserInterfaceByName(clientInterfaceName);
if (!endUserInterface) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: failed to disconnect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", could not find end-user interface." << std::endl;
return false;
}
bool success = false;
// If the server is this component (ManagerComponent), or if it is not active, or if it is the same as the requestor,
// we can directly call the methods. Note that we could use the StateChange mutex to make sure that the state
// does not change during execution of this method, but that is unlikely.
if ((serverComponent == this) || !serverComponent->IsRunning() || (serverComponentName == requestComponentName)) {
mtsEventHandlerList eventList(endUserInterface);
clientInterfaceRequired->GetEventList(eventList);
// Following could use serverInterfaceProvided or endUserInterface
endUserInterface->RemoveObserverList(eventList, eventList);
success = clientInterfaceRequired->CheckEventList(eventList);
// Now, pause/stop the client component (if not this component). In the future, the component could be left
// running if the required interface is MTS_OPTIONAL.
if (clientComponent != this)
clientComponent->Suspend();
clientInterfaceRequired->DetachCommands();
if (serverInterfaceProvided->RemoveEndUserInterface(endUserInterface, clientInterfaceName) != 0) {
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: failed to remove end user interface for client "
<< clientComponentName << ":" << clientInterfaceName << std::endl;
success = false;
}
}
else {
mtsExecutionResult executionResult;
InterfaceComponentFunctionType * serverFunctionSet = InterfaceComponentFunctionMap.GetItem(serverComponentName);
if (!serverFunctionSet) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: failed to get function set for " << serverComponentName << std::endl;
return false;
}
if (!serverFunctionSet->RemoveEndUserInterface.IsValid()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: RemoveEndUserInterface invalid for " << serverComponentName << std::endl;
return false;
}
if (!serverFunctionSet->RemoveObserverList.IsValid()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: RemoveObserverList invalid for " << serverComponentName << std::endl;
return false;
}
mtsEventHandlerList eventList(endUserInterface);
clientInterfaceRequired->GetEventList(eventList);
executionResult = serverFunctionSet->RemoveObserverList(eventList, eventList);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: failed to execute command \"RemoveObserverList\" (error "
<< executionResult << ")" << std::endl;
}
success = clientInterfaceRequired->CheckEventList(eventList);
mtsEndUserInterfaceArg endUserInterfaceArg(reinterpret_cast<size_t>(serverInterfaceProvided),
clientInterfaceName,
reinterpret_cast<size_t>(endUserInterface));
if (clientComponent == this) {
// If client component in the Manager Component, server provided interface must be the internal
// interface. Since we are disconnecting it, we cannot use it, so we instead directly call the
// provided interface method. To be safe, we could suspend the server component.
if (serverInterfaceProvided->RemoveEndUserInterface(endUserInterface, clientInterfaceName) != 0) {
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: could not remove end user interface from "
<< serverComponentName << std::endl;
success = false;
}
}
else {
// Pause/stop the client component. In the future, the component could be left
// running if the required interface is MTS_OPTIONAL.
clientComponent->Suspend();
executionResult = serverFunctionSet->RemoveEndUserInterface(endUserInterfaceArg, endUserInterfaceArg);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "ComponentDisconnect: failed to execute command \"RemoveEndUserInterface\" (error "
<< executionResult << ")" << std::endl;
success = false;
}
if (endUserInterfaceArg.EndUserInterface != 0) {
CMN_LOG_CLASS_RUN_WARNING << "ComponentDisconnect: failed to remove end-user interface for " << serverComponentName << std::endl;
success = false;
}
}
}
clientInterfaceRequired->DetachCommands();
if (success) {
thisConnectionID = clientInterfaceRequired->ConnectionID;
clientInterfaceRequired->ConnectionID = InvalidConnectionID;
CMN_LOG_CLASS_INIT_VERBOSE << "ComponentDisconnect: successfully disconnected required/provided: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName << std::endl;
} else {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: failed to disconnect required/provided: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName << std::endl;
return false;
}
}
else { // Input/Output connection
if (!serverInterfaceOutput) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: failed to disconnect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", server does not have provided or output interface." << std::endl;
return false;
}
mtsInterfaceInput *clientInterfaceInput = clientComponent->GetInterfaceInput(clientInterfaceName);
if (!clientInterfaceInput) {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: failed to disconnect interfaces: "
<< clientComponentName << ":" << clientInterfaceName << " - "
<< serverComponentName << ":" << serverInterfaceName
<< ", client does not have input interface." << std::endl;
return false;
}
if (clientInterfaceInput->Disconnect()) {
thisConnectionID = clientInterfaceInput->ConnectionID;
clientInterfaceInput->ConnectionID = InvalidConnectionID;
CMN_LOG_CLASS_INIT_VERBOSE << "ComponentDisconnect: component \""
<< this->GetName()
<< "\" input interface \"" << clientInterfaceName
<< "\" successfully disconnected from output interface \""
<< serverInterfaceName << "\"" << std::endl;
} else {
CMN_LOG_CLASS_INIT_ERROR << "ComponentDisconnect: component \""
<< this->GetName()
<< "\" input interface \"" << clientInterfaceName
<< "\" failed to disconnect from output interface \""
<< serverInterfaceName << "\"" << std::endl;
return false;
}
}
mtsDescriptionConnection description(
"", clientComponentName, clientInterfaceName,
"", serverComponentName, serverInterfaceName,
thisConnectionID);
// Generate the event
InterfaceComponentEvents_RemoveConnection(description);
return true;
}
bool mtsManagerComponent::FindByConnectionID(mtsDescriptionConnection & arg) const
{
typename ComponentMapType::const_iterator itComp;
for (itComp = ComponentMap.begin(); itComp != ComponentMap.end(); itComp++) {
mtsComponent *component = itComp->second;
// Get a list of required interfaces
std::vector<std::string> interfaceRequiredNames = component->GetNamesOfInterfacesRequired();
typename std::vector<std::string>::const_iterator itReq;
for (itReq = interfaceRequiredNames.begin(); itReq != interfaceRequiredNames.end(); ++itReq) {
const mtsInterfaceRequired * required = component->GetInterfaceRequired(*itReq);
if (required && (required->ConnectionID == arg.ConnectionID)) {
const mtsInterfaceProvided * provided = required->GetConnectedInterface();
if (provided) {
provided = provided->GetOriginalInterface();
arg.Client.ProcessName = "";
arg.Client.ComponentName = itComp->first;
arg.Client.InterfaceName = *itReq;
arg.Server.ProcessName = "";
arg.Server.ComponentName = provided->GetComponent()->GetName();
arg.Server.InterfaceName = provided->GetName();
return true;
}
}
}
// Get a list of input interfaces
std::vector<std::string> interfaceInputNames = component->GetNamesOfInterfacesInput();
typename std::vector<std::string>::const_iterator itIn;
for (itIn = interfaceInputNames.begin(); itIn != interfaceInputNames.end(); ++itIn) {
const mtsInterfaceInput * input = component->GetInterfaceInput(*itIn);
if (input && (input->ConnectionID == arg.ConnectionID)) {
const mtsInterfaceOutput * output = input->GetConnectedInterface();
if (output) {
arg.Client.ProcessName = "";
arg.Client.ComponentName = itComp->first;
arg.Client.InterfaceName = *itIn;
arg.Server.ProcessName = "";
arg.Server.ComponentName = output->GetComponent()->GetName();
arg.Server.InterfaceName = output->GetName();
return true;
}
}
}
}
return false;
}
//**************************************************************************************************************************
bool mtsManagerComponent::AddNewClientComponent(const std::string & clientComponentName)
{
// Create a new set of function objects
InterfaceComponentFunctionType * newFunctionSet = new InterfaceComponentFunctionType;
const std::string interfaceName = mtsManagerComponentBase::GetNameOfInterfaceComponentRequiredFor(clientComponentName);
mtsInterfaceRequired * required = AddInterfaceRequired(interfaceName);
if (!required) {
CMN_LOG_CLASS_INIT_ERROR << "AddNewClientComponent: failed to create \"Component\" required interface: " << interfaceName << std::endl;
return false;
}
required->AddFunction(mtsManagerComponentBase::CommandNames::GetEndUserInterface,
newFunctionSet->GetEndUserInterface);
required->AddFunction(mtsManagerComponentBase::CommandNames::AddObserverList,
newFunctionSet->AddObserverList);
required->AddFunction(mtsManagerComponentBase::CommandNames::RemoveEndUserInterface,
newFunctionSet->RemoveEndUserInterface);
required->AddFunction(mtsManagerComponentBase::CommandNames::RemoveObserverList,
newFunctionSet->RemoveObserverList);
required->AddEventHandlerWrite(&mtsManagerComponent::HandleChangeStateFromComponent, this,
mtsManagerComponentBase::EventNames::ChangeState);
// Remember a required interface (InterfaceComponent's required interface)
// to connect it to the provided interface (InterfaceInternals's provided
// interface).
if (!InterfaceComponentFunctionMap.AddItem(clientComponentName, newFunctionSet)) {
CMN_LOG_CLASS_INIT_ERROR << "AddNewClientComponent: failed to add \"Component\" required interface: "
<< "\"" << clientComponentName << "\", " << interfaceName << std::endl;
return false;
}
CMN_LOG_CLASS_INIT_VERBOSE << "AddNewClientComponent: successfully added new client component: "
<< clientComponentName << std::endl;
return true;
}
void mtsManagerComponent::HandleChangeStateFromComponent(const mtsComponentStateChange &stateChange)
{
CMN_LOG_CLASS_RUN_VERBOSE << "MCS got ChangeState event for " << stateChange.ComponentName << std::endl;
InterfaceComponentEvents_ChangeState(stateChange);
}
bool mtsManagerComponent::ConnectToManagerComponent(const std::string & componentName)
{
mtsComponent * component = GetComponent(componentName);
if (!component) {
CMN_LOG_CLASS_INIT_ERROR << "ConnectToManagerComponent: no component found with name of "
<< "\"" << componentName << "\"" << std::endl;
return false;
}
// Connect InterfaceComponent's required interface to InterfaceInternal's
// provided interface of the connecting component.
const std::string nameOfInterfaceComponentRequired
= mtsManagerComponentBase::GetNameOfInterfaceComponentRequiredFor(componentName);
if (!ConnectInternal(GetName(), nameOfInterfaceComponentRequired,
componentName, mtsManagerComponentBase::GetNameOfInterfaceInternalProvided(),
GetName())) {
CMN_LOG_CLASS_INIT_ERROR << "ConnectToManagerComponent: failed to connect: "
<< GetName() << ":" << nameOfInterfaceComponentRequired
<< " - "
<< componentName << ":" << mtsManagerComponentBase::GetNameOfInterfaceInternalProvided()
<< std::endl;
return false;
}
// If a component has support for the dynamic component control services,
// connect InterfaceInternal's required interface to InterfaceComponent's
// provided interface (except for LCM, which has already been done).
if (!mtsManagerComponentBase::IsManagerComponent(componentName) &&
component->GetInterfaceRequired(mtsManagerComponentBase::GetNameOfInterfaceInternalRequired())) {
if (!ConnectInternal(component->GetName(), mtsManagerComponentBase::GetNameOfInterfaceInternalRequired(),
GetName(), mtsManagerComponentBase::GetNameOfInterfaceComponentProvided(),
GetName())) {
CMN_LOG_CLASS_INIT_ERROR << "ConnectToManagerComponent: failed to connect: "
<< component->GetName() << ":" << mtsManagerComponentBase::GetNameOfInterfaceInternalRequired()
<< " - "
<< GetName() << ":" << mtsManagerComponentBase::GetNameOfInterfaceComponentProvided()
<< std::endl;
return false;
}
}
return true;
}
bool mtsManagerComponent::DisconnectFromManagerComponent(const std::string & componentName)
{
mtsComponent * component = ComponentMap.GetItem(componentName);
if (!component) {
CMN_LOG_CLASS_RUN_WARNING << "DisconnectFromManagerComponent: no component found with name of "
<< "\"" << componentName << "\"" << std::endl;
return false;
}
// If the component has support for dynamic component control services,
// disconnect InterfaceInternal's required interface from InterfaceComponent's
// provided interface.
if (component->GetInterfaceRequired(mtsManagerComponentBase::GetNameOfInterfaceInternalRequired())) {
if (!DisconnectInternal(componentName, mtsManagerComponentBase::GetNameOfInterfaceInternalRequired(),
GetName(), mtsManagerComponentBase::GetNameOfInterfaceComponentProvided(),
GetName())) {
CMN_LOG_CLASS_RUN_WARNING << "DisconnectFromManagerComponent: failed to disconnect: "
<< componentName << ":" << mtsManagerComponentBase::GetNameOfInterfaceInternalRequired()
<< " - "
<< GetName() << ":" << mtsManagerComponentBase::GetNameOfInterfaceComponentProvided()
<< std::endl;
}
}
// Remove internal connection between MCS and component
const std::string nameOfInterfaceComponentRequired
= mtsManagerComponentBase::GetNameOfInterfaceComponentRequiredFor(componentName);
if (!DisconnectInternal(GetName(), nameOfInterfaceComponentRequired,
componentName, mtsManagerComponentBase::GetNameOfInterfaceInternalProvided(),
GetName())) {
CMN_LOG_CLASS_RUN_WARNING << "DisconnectFromManagerComponent: failed to disconnect: "
<< GetName() << ":" << nameOfInterfaceComponentRequired
<< " - "
<< componentName << ":" << mtsManagerComponentBase::GetNameOfInterfaceInternalProvided()
<< std::endl;
}
// Next, remove the internal required interface
if (!RemoveInterfaceRequired(nameOfInterfaceComponentRequired)) {
CMN_LOG_CLASS_RUN_WARNING << "DisconnectFromManagerComponent: failed to remove required interface "
<< nameOfInterfaceComponentRequired << std::endl;
}
// Next, remove the function set
InterfaceComponentFunctionType * functionSet = InterfaceComponentFunctionMap.GetItem(componentName);
if (functionSet) {
InterfaceComponentFunctionMap.RemoveItem(componentName);
}
else {
CMN_LOG_CLASS_RUN_WARNING << "DisconnectFromManagerComponent: failed to get function set for component \""
<< componentName << "\"" << std::endl;
}
// For now, return true because failure to disconnect an interface is not critical
return true;
}
|