File size: 48,824 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 | /* -*- 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, Anton Deguet
Created on: 2008-11-13
(C) Copyright 2008-2025 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/mtsInterfaceRequired.h>
#include <cisstMultiTask/mtsFunctionVoid.h>
#include <cisstMultiTask/mtsFunctionVoidReturn.h>
#include <cisstMultiTask/mtsFunctionWrite.h>
#include <cisstMultiTask/mtsFunctionWriteReturn.h>
#include <cisstMultiTask/mtsFunctionRead.h>
#include <cisstMultiTask/mtsFunctionQualifiedRead.h>
#include <cisstMultiTask/mtsEventReceiver.h>
#include <cisstCommon/cmnStrings.h>
#include <cisstCommon/cmnSerializer.h>
#include <cisstCommon/cmnDataFunctions.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsParameterTypes.h>
mtsInterfaceRequired::mtsInterfaceRequired(const std::string & interfaceName,
mtsComponent * component,
mtsMailBox * mailBox,
mtsRequiredType required):
mtsInterface(interfaceName, component),
Required(required),
MailBox(mailBox),
InterfaceProvided(0),
MailBoxSize(DEFAULT_MAIL_BOX_AND_ARGUMENT_QUEUES_SIZE),
ArgumentQueuesSize(DEFAULT_MAIL_BOX_AND_ARGUMENT_QUEUES_SIZE),
ConnectionID(InvalidConnectionID),
FunctionsVoid("FunctionsVoid"),
FunctionsVoidReturn("FunctionsVoidReturn"),
FunctionsWrite("FunctionsWrite"),
FunctionsWriteReturn("FunctionsWriteReturn"),
FunctionsRead("FunctionsRead"),
FunctionsQualifiedRead("FunctionsQualifiedRead"),
EventReceiversVoid("EventReceiversVoid"),
EventReceiversWrite("EventReceiversWrite"),
EventHandlersVoid("EventHandlersVoid"),
EventHandlersWrite("EventHandlersWrite")
{
FunctionsVoid.SetOwner(*this);
FunctionsVoidReturn.SetOwner(*this);
FunctionsWrite.SetOwner(*this);
FunctionsWriteReturn.SetOwner(*this);
FunctionsRead.SetOwner(*this);
FunctionsQualifiedRead.SetOwner(*this);
EventReceiversVoid.SetOwner(*this);
EventReceiversWrite.SetOwner(*this);
EventHandlersVoid.SetOwner(*this);
EventHandlersWrite.SetOwner(*this);
}
mtsInterfaceRequired::~mtsInterfaceRequired()
{
FunctionsVoid.DeleteAll();
FunctionsVoidReturn.DeleteAll();
FunctionsWrite.DeleteAll();
FunctionsWriteReturn.DeleteAll();
FunctionsRead.DeleteAll();
FunctionsQualifiedRead.DeleteAll();
}
osaThreadSignal * mtsInterfaceRequired::GetThreadSignal(void)
{
return &ThreadSignalForBlockingCommands;
}
const mtsInterfaceProvided * mtsInterfaceRequired::GetConnectedInterface(void) const
{
return InterfaceProvided;
}
bool mtsInterfaceRequired::SetMailBoxSize(size_t desiredSize)
{
if (!this->MailBox) {
CMN_LOG_CLASS_INIT_WARNING << "SetMailBoxSize: interface \"" << this->GetFullName()
<< "\" is not queuing commands, calling SetMailBoxSize has no effect"
<< std::endl;
return false;
}
if (this->GetConnectedInterface()) {
CMN_LOG_CLASS_INIT_ERROR << "SetMailBoxSize: interface \"" << this->GetFullName()
<< "\", can't modify mail box size while the interface is connected."
<< std::endl;
return false;
}
this->MailBoxSize = desiredSize;
this->MailBox->SetSize(desiredSize);
return true;
}
bool mtsInterfaceRequired::SetArgumentQueuesSize(size_t desiredSize)
{
if (!this->MailBox) {
CMN_LOG_CLASS_INIT_WARNING << "SetArgumentQueuesSize: interface \"" << this->GetFullName()
<< "\" is not queuing commands, calling SetArgumentQueuesSize has no effect"
<< std::endl;
return false;
}
if (this->GetConnectedInterface()) {
CMN_LOG_CLASS_INIT_ERROR << "SetArgumentQueuesSize: interface \"" << this->GetFullName()
<< "\", can't modify argument queues size while the interface is connected."
<< std::endl;
return false;
}
if (desiredSize > this->MailBoxSize) {
CMN_LOG_CLASS_INIT_WARNING << "SetArgumentQueuesSize: interface \"" << this->GetFullName()
<< "\" new size (" << desiredSize
<< ") is smaller than command mail box size (" << this->MailBoxSize
<< "), the extra space won't be used" << std::endl;
}
this->ArgumentQueuesSize = desiredSize;
return true;
}
bool mtsInterfaceRequired::SetMailBoxAndArgumentQueuesSize(size_t desiredSize)
{
return (this->SetMailBoxSize(desiredSize)
&& this->SetArgumentQueuesSize(desiredSize));
}
bool mtsInterfaceRequired::UseQueueBasedOnInterfacePolicy(mtsEventQueueingPolicy queueingPolicy,
const std::string & methodName,
const std::string & eventName)
{
bool interfaceHasMailbox = (this->MailBox != 0);
if (interfaceHasMailbox) {
if (queueingPolicy == MTS_EVENT_NOT_QUEUED) {
CMN_LOG_CLASS_INIT_DEBUG << methodName << ": event handler for \"" << eventName
<< "\" will not be queued while the corresponding required interface \""
<< this->GetFullName() << "\" uses queued event handlers by default." << std::endl;
return false;
} else {
return true;
}
} else {
if (queueingPolicy == MTS_EVENT_QUEUED) {
CMN_LOG_CLASS_INIT_ERROR << methodName << ": event handler for \"" << eventName
<< "\" has been added as queued while the corresponding required interface \""
<< this->GetFullName() << "\" has been created without a mailbox." << std::endl;
return true;
} else {
return false;
}
}
}
bool mtsInterfaceRequired::AddEventHandlerToReceiver(const std::string & eventName, mtsCommandVoid * handler) const
{
bool result = false;
ReceiverVoidInfo * receiverInfo = EventReceiversVoid.GetItem(eventName, CMN_LOG_LEVEL_RUN_VERBOSE);
if (receiverInfo) {
receiverInfo->Pointer->SetHandlerCommand(handler);
result = true;
}
return result;
}
bool mtsInterfaceRequired::AddEventHandlerToReceiver(const std::string & eventName, mtsCommandWriteBase * handler) const
{
bool result = false;
ReceiverWriteInfo *receiverInfo = EventReceiversWrite.GetItem(eventName, CMN_LOG_LEVEL_RUN_VERBOSE);
if (receiverInfo) {
receiverInfo->Pointer->SetHandlerCommand(handler);
result = true;
}
return result;
}
mtsInterfaceRequiredDescription mtsInterfaceRequired::GetDescription() const
{
mtsInterfaceRequiredDescription desc;
desc.InterfaceName = GetName();
GetDescription(desc);
return desc;
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctions(void) const
{
std::vector<std::string> commands = GetNamesOfFunctionsVoid();
std::vector<std::string> tmp = GetNamesOfFunctionsVoidReturn();
commands.insert(commands.begin(), tmp.begin(), tmp.end());
tmp.clear();
tmp = GetNamesOfFunctionsWrite();
commands.insert(commands.begin(), tmp.begin(), tmp.end());
tmp.clear();
tmp = GetNamesOfFunctionsWriteReturn();
commands.insert(commands.begin(), tmp.begin(), tmp.end());
tmp.clear();
tmp = GetNamesOfFunctionsRead();
commands.insert(commands.begin(), tmp.begin(), tmp.end());
tmp.clear();
tmp = GetNamesOfFunctionsQualifiedRead();
commands.insert(commands.begin(), tmp.begin(), tmp.end());
return commands;
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctionsVoid(void) const
{
return FunctionsVoid.GetNames();
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctionsVoidReturn(void) const
{
return FunctionsVoidReturn.GetNames();
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctionsWrite(void) const
{
return FunctionsWrite.GetNames();
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctionsWriteReturn(void) const
{
return FunctionsWriteReturn.GetNames();
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctionsRead(void) const
{
return FunctionsRead.GetNames();
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfFunctionsQualifiedRead(void) const
{
return FunctionsQualifiedRead.GetNames();
}
mtsFunctionVoid * mtsInterfaceRequired::GetFunctionVoid(const std::string & functionName) const
{
return dynamic_cast<mtsFunctionVoid *>(FunctionsVoid.GetItem(functionName, CMN_LOG_LOD_INIT_ERROR)->Pointer);
}
mtsFunctionVoidReturn * mtsInterfaceRequired::GetFunctionVoidReturn(const std::string & functionName) const
{
return dynamic_cast<mtsFunctionVoidReturn *>(FunctionsVoidReturn.GetItem(functionName, CMN_LOG_LOD_INIT_ERROR)->Pointer);
}
mtsFunctionWrite * mtsInterfaceRequired::GetFunctionWrite(const std::string & functionName) const
{
return dynamic_cast<mtsFunctionWrite *>(FunctionsWrite.GetItem(functionName, CMN_LOG_LOD_INIT_ERROR)->Pointer);
}
mtsFunctionWriteReturn * mtsInterfaceRequired::GetFunctionWriteReturn(const std::string & functionName) const
{
return dynamic_cast<mtsFunctionWriteReturn *>(FunctionsWriteReturn.GetItem(functionName, CMN_LOG_LOD_INIT_ERROR)->Pointer);
}
mtsFunctionRead * mtsInterfaceRequired::GetFunctionRead(const std::string & functionName) const
{
return dynamic_cast<mtsFunctionRead *>(FunctionsRead.GetItem(functionName, CMN_LOG_LOD_INIT_ERROR)->Pointer);
}
mtsFunctionQualifiedRead * mtsInterfaceRequired::GetFunctionQualifiedRead(const std::string & functionName) const
{
return dynamic_cast<mtsFunctionQualifiedRead *>(FunctionsQualifiedRead.GetItem(functionName, CMN_LOG_LOD_INIT_ERROR)->Pointer);
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfEventHandlersVoid(void) const
{
return EventHandlersVoid.GetNames();
}
std::vector<std::string> mtsInterfaceRequired::GetNamesOfEventHandlersWrite(void) const
{
return EventHandlersWrite.GetNames();
}
mtsCommandVoid * mtsInterfaceRequired::GetEventHandlerVoid(const std::string & eventName) const
{
return EventHandlersVoid.GetItem(eventName);
}
mtsCommandWriteBase * mtsInterfaceRequired::GetEventHandlerWrite(const std::string & eventName) const
{
return EventHandlersWrite.GetItem(eventName);
}
bool mtsInterfaceRequired::ConnectTo(mtsInterfaceProvided * interfaceProvided)
{
// get the end user interface
mtsInterfaceProvided * endUserInterface = interfaceProvided->GetEndUserInterface(this->GetName());
if (!endUserInterface) {
CMN_LOG_CLASS_INIT_ERROR << "ConnectTo: failed to get an end user provided interface for \""
<< interfaceProvided->GetFullName() << "\" while connecting the required interface \""
<< this->GetFullName() << "\"" << std::endl;
return false;
}
// provide a different log message based on result
if (endUserInterface == interfaceProvided) {
CMN_LOG_CLASS_INIT_VERBOSE << "ConnectTo: connecting required interface \""
<< this->GetFullName() << "\" to existing provided interface \""
<< endUserInterface->GetFullName() << "\"" << std::endl;
} else {
CMN_LOG_CLASS_INIT_VERBOSE << "ConnectTo: connecting required interface \""
<< this->GetFullName() << "\" to newly created provided interface \""
<< endUserInterface->GetFullName() << "\"" << std::endl;
}
bool success = BindCommands(endUserInterface);
mtsEventHandlerList eventList(endUserInterface);
GetEventList(eventList);
endUserInterface->AddObserverList(eventList, eventList);
if (!CheckEventList(eventList))
success = false;
return success;
}
bool mtsInterfaceRequired::DetachCommands(void)
{
// Set pointer to interface provided to 0.
// We do this first so that if the component is still running (e.g., this required interface
// is MTS_OPTIONAL), it will detect that the required interface is disconnected before trying
// to invoke any function objects that may be in the process of being detached.
InterfaceProvided = 0;
// In the future, it may be better to set the pointers to NOPVoid, NOPRead, etc., which can
// be static members of the corresponding command classes.
FunctionInfoMapType::iterator iter;
for (iter = FunctionsVoid.begin(); iter != FunctionsVoid.end(); iter++) {
iter->second->Detach();
}
for (iter = FunctionsVoidReturn.begin(); iter != FunctionsVoidReturn.end(); iter++) {
iter->second->Detach();
}
for (iter = FunctionsWrite.begin(); iter != FunctionsWrite.end(); iter++) {
iter->second->Detach();
}
for (iter = FunctionsWriteReturn.begin(); iter != FunctionsWriteReturn.end(); iter++) {
iter->second->Detach();
}
for (iter = FunctionsRead.begin(); iter != FunctionsRead.end(); iter++) {
iter->second->Detach();
}
for (iter = FunctionsQualifiedRead.begin(); iter != FunctionsQualifiedRead.end(); iter++) {
iter->second->Detach();
}
return true;
}
mtsRequiredType mtsInterfaceRequired::IsRequired(void) const
{
return this->Required;
}
bool mtsInterfaceRequired::AddSystemEventHandlers(void)
{
mtsCommandVoid * voidCommand =
this->AddEventHandlerVoid(&mtsInterfaceRequired::BlockingCommandExecutedHandler,
this, "BlockingCommandExecuted",
MTS_EVENT_NOT_QUEUED);
if (!(voidCommand)) {
CMN_LOG_CLASS_INIT_ERROR << "AddSystemEventHandlers: unable to add void event handler \"BlockingCommandExecuted\" to interface \""
<< this->GetFullName() << "\"" << std::endl;
return false;
}
voidCommand =
this->AddEventHandlerVoid(&mtsInterfaceRequired::BlockingCommandReturnExecutedHandler,
this, "BlockingCommandReturnExecuted",
MTS_EVENT_NOT_QUEUED);
if (!(voidCommand)) {
CMN_LOG_CLASS_INIT_ERROR << "AddSystemEventHandlers: unable to add void event handler \"BlockingCommandReturnExecuted\" to interface \""
<< this->GetFullName() << "\"" << std::endl;
return false;
}
return true;
}
void mtsInterfaceRequired::BlockingCommandExecutedHandler(void)
{
this->ThreadSignalForBlockingCommands.Raise();
}
void mtsInterfaceRequired::BlockingCommandReturnExecutedHandler(void)
{
this->ThreadSignalForBlockingCommands.Raise();
}
bool mtsInterfaceRequired::BindCommands(const mtsInterfaceProvided * interfaceProvided)
{
bool success = true;
bool result;
if (!interfaceProvided) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: required interface \""
<< this->GetFullName() << "\" is not connected to a valid provided interface" << std::endl;
return false;
}
// Bind the command pointers. This may not be thread-safe if the
// client component is active. We could execute this in the thread
// of the client component to achieve thread-safety (see, for
// example, the way that GetEndUserInterface and AddObserverList
// are executed in the thread of the server component). For now,
// we assume that the client component (with required interface)
// will not be active during the connection process, though the
// server component (with provided interface) might be
// active. Note also that we set the InterfaceProvided member data
// at the end of this function, so that the required interface is
// not considered connected until after the command binding is
// done (though event receivers/handlers have yet to be set up).
FunctionInfoMapType::iterator iter;
mtsFunctionVoid * functionVoid;
for (iter = FunctionsVoid.begin();
iter != FunctionsVoid.end();
iter++) {
if (!iter->second->Pointer) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: found null function pointer for void command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\"" << std::endl;
result = false;
} else {
functionVoid = dynamic_cast<mtsFunctionVoid *>(iter->second->Pointer);
if (!functionVoid) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: incorrect function pointer for void command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\" (got \""
<< typeid(iter->second->Pointer).name() << "\")" << std::endl;
result = false;
} else {
result = functionVoid->Bind(interfaceProvided->GetCommandVoid(iter->first, iter->second->Required));
if (!result) {
if (iter->second->Required == MTS_OPTIONAL) {
CMN_LOG_CLASS_INIT_VERBOSE << "BindCommands: couldn't find optional void command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsVoid())
<< std::endl;
} else {
CMN_LOG_CLASS_INIT_WARNING << "BindCommands: failed for void command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsVoid())
<< std::endl;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "BindCommands: succeeded for void command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
}
}
if (iter->second->Required == MTS_OPTIONAL) {
result = true;
}
success &= result;
}
mtsFunctionVoidReturn * functionVoidReturn;
for (iter = FunctionsVoidReturn.begin();
iter != FunctionsVoidReturn.end();
iter++) {
if (!iter->second->Pointer) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: found null function pointer for void with return command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\"" << std::endl;
result = false;
} else {
functionVoidReturn = dynamic_cast<mtsFunctionVoidReturn *>(iter->second->Pointer);
if (!functionVoidReturn) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: incorrect function pointer for void with return command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\" (got \""
<< typeid(iter->second->Pointer).name() << "\")" << std::endl;
result = false;
} else {
result = functionVoidReturn->Bind(interfaceProvided->GetCommandVoidReturn(iter->first, iter->second->Required));
if (!result) {
if (iter->second->Required == MTS_OPTIONAL) {
CMN_LOG_CLASS_INIT_VERBOSE << "BindCommands: couldn't find optional void with return command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsVoidReturn())
<< std::endl;
} else {
CMN_LOG_CLASS_INIT_WARNING << "BindCommands: failed for void with return command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsVoidReturn())
<< std::endl;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "BindCommands: succeeded for void with return command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
}
}
if (iter->second->Required == MTS_OPTIONAL) {
result = true;
}
success &= result;
}
mtsFunctionWrite * functionWrite;
for (iter = FunctionsWrite.begin();
iter != FunctionsWrite.end();
iter++) {
if (!iter->second->Pointer) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: found null function pointer for write command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\"" << std::endl;
result = false;
} else {
functionWrite = dynamic_cast<mtsFunctionWrite *>(iter->second->Pointer);
if (!functionWrite) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: incorrect function pointer for write command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\" (got \""
<< typeid(iter->second->Pointer).name() << "\")" << std::endl;
result = false;
} else {
result = functionWrite->Bind(interfaceProvided->GetCommandWrite(iter->first, iter->second->Required));
if (!result) {
if (iter->second->Required == MTS_OPTIONAL) {
CMN_LOG_CLASS_INIT_VERBOSE << "BindCommands: couldn't find optional write command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsWrite())
<< std::endl;
} else {
CMN_LOG_CLASS_INIT_WARNING << "BindCommands: failed for write command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsWrite())
<< std::endl;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "BindCommands: succeeded for write command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
}
}
if (iter->second->Required == MTS_OPTIONAL) {
result = true;
}
success &= result;
}
mtsFunctionWriteReturn * functionWriteReturn;
for (iter = FunctionsWriteReturn.begin();
iter != FunctionsWriteReturn.end();
iter++) {
if (!iter->second->Pointer) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: found null function pointer for write with return command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\"" << std::endl;
result = false;
} else {
functionWriteReturn = dynamic_cast<mtsFunctionWriteReturn *>(iter->second->Pointer);
if (!functionWriteReturn) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: incorrect function pointer for write with return command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\" (got \""
<< typeid(iter->second->Pointer).name() << "\")" << std::endl;
result = false;
} else {
result = functionWriteReturn->Bind(interfaceProvided->GetCommandWriteReturn(iter->first, iter->second->Required));
if (!result) {
if (iter->second->Required == MTS_OPTIONAL) {
CMN_LOG_CLASS_INIT_VERBOSE << "BindCommands: couldn't find optional write with return command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsWriteReturn())
<< std::endl;
} else {
CMN_LOG_CLASS_INIT_WARNING << "BindCommands: failed for write with return command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsWriteReturn())
<< std::endl;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "BindCommands: succeeded for write with return command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
}
}
if (iter->second->Required == MTS_OPTIONAL) {
result = true;
}
success &= result;
}
mtsFunctionRead * functionRead;
for (iter = FunctionsRead.begin();
iter != FunctionsRead.end();
iter++) {
if (!iter->second->Pointer) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: found null function pointer for read command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\"" << std::endl;
result = false;
} else {
functionRead = dynamic_cast<mtsFunctionRead *>(iter->second->Pointer);
if (!functionRead) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: incorrect function pointer for read command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\" (got \""
<< typeid(iter->second->Pointer).name() << "\")" << std::endl;
result = false;
} else {
result = functionRead->Bind(interfaceProvided->GetCommandRead(iter->first, iter->second->Required));
if (!result) {
if (iter->second->Required == MTS_OPTIONAL) {
CMN_LOG_CLASS_INIT_VERBOSE << "BindCommands: couldn't find optional read command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsRead())
<< std::endl;
} else {
CMN_LOG_CLASS_INIT_WARNING << "BindCommands: failed for read command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsRead())
<< std::endl;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "BindCommands: succeeded for read command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
}
}
if (iter->second->Required == MTS_OPTIONAL) {
result = true;
}
success &= result;
}
mtsFunctionQualifiedRead * functionQualifiedRead;
for (iter = FunctionsQualifiedRead.begin();
iter != FunctionsQualifiedRead.end();
iter++) {
if (!iter->second->Pointer) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: found null function pointer for qualified read command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\"" << std::endl;
result = false;
} else {
functionQualifiedRead = dynamic_cast<mtsFunctionQualifiedRead *>(iter->second->Pointer);
if (!functionQualifiedRead) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: incorrect function pointer for qualified read command \""
<< iter->first << "\" in interface \"" << this->GetFullName() << "\" (got \""
<< typeid(iter->second->Pointer).name() << "\")" << std::endl;
result = false;
} else {
result = functionQualifiedRead->Bind(interfaceProvided->GetCommandQualifiedRead(iter->first, iter->second->Required));
if (!result) {
if (iter->second->Required == MTS_OPTIONAL) {
CMN_LOG_CLASS_INIT_VERBOSE << "BindCommands: couldn't find optional qualified read command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsQualifiedRead())
<< std::endl;
} else {
CMN_LOG_CLASS_INIT_WARNING << "BindCommands: failed for qualified read command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName()
<< "\"), the following commands are available: "
<< cmnDataHumanReadable(interfaceProvided->GetNamesOfCommandsQualifiedRead())
<< std::endl;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "BindCommands: succeeded for qualified read command \""
<< iter->first << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
}
}
if (iter->second->Required == MTS_OPTIONAL) {
result = true;
}
success &= result;
}
if (!success) {
CMN_LOG_CLASS_INIT_ERROR << "BindCommands: required commands missing (connecting \""
<< this->GetFullName() << "\" to \""
<< interfaceProvided->GetFullName() << "\")"<< std::endl;
}
// Save pointer to provided interface in class
InterfaceProvided = interfaceProvided;
return success;
}
void mtsInterfaceRequired::GetEventList(mtsEventHandlerList & eventList)
{
// Make sure event list is empty
eventList.VoidEvents.clear();
eventList.WriteEvents.clear();
EventReceiverVoidMapType::iterator iterReceiverVoid;
for (iterReceiverVoid = EventReceiversVoid.begin();
iterReceiverVoid != EventReceiversVoid.end();
iterReceiverVoid++) {
eventList.VoidEvents.push_back(mtsEventHandlerList::InfoVoid(iterReceiverVoid->first,
iterReceiverVoid->second->Pointer->GetCommand(),
iterReceiverVoid->second->Required));
}
EventReceiverWriteMapType::iterator iterReceiverWrite;
for (iterReceiverWrite = EventReceiversWrite.begin();
iterReceiverWrite != EventReceiversWrite.end();
iterReceiverWrite++) {
eventList.WriteEvents.push_back(mtsEventHandlerList::InfoWrite(iterReceiverWrite->first,
iterReceiverWrite->second->Pointer->GetCommand(),
iterReceiverWrite->second->Required));
}
EventHandlerVoidMapType::iterator iterEventVoid;
for (iterEventVoid = EventHandlersVoid.begin();
iterEventVoid != EventHandlersVoid.end();
iterEventVoid++) {
// First, attempt to add it to the event receiver (if one is present).
// If there is no event receiver, add it directly to the provided interface.
// Note that event handlers are considered optional.
if (!AddEventHandlerToReceiver(iterEventVoid->first, iterEventVoid->second))
eventList.VoidEvents.push_back(mtsEventHandlerList::InfoVoid(iterEventVoid->first,
iterEventVoid->second,
MTS_OPTIONAL));
}
EventHandlerWriteMapType::iterator iterEventWrite;
for (iterEventWrite = EventHandlersWrite.begin();
iterEventWrite != EventHandlersWrite.end();
iterEventWrite++) {
// First, attempt to add it to the event receiver (if one is present)
// If there is no event receiver, add it directly to the provided interface
// Note that event handlers are considered optional.
if (!AddEventHandlerToReceiver(iterEventWrite->first, iterEventWrite->second))
eventList.WriteEvents.push_back(mtsEventHandlerList::InfoWrite(iterEventWrite->first,
iterEventWrite->second,
MTS_OPTIONAL));
}
eventList.SetValid(true);
}
// PK: This could be moved out of mtsInterfaceRequired (perhaps to mtspEventHandlerList?)
bool mtsInterfaceRequired::CheckEventList(mtsEventHandlerList & eventList) const
{
bool success = true;
// Now, check the results
size_t i;
for (i = 0; i < eventList.VoidEvents.size(); i++) {
if (!eventList.VoidEvents[i].Result) {
CMN_LOG_CLASS_INIT_DEBUG << "CheckEventList: failed to add observer for void event \""
<< eventList.VoidEvents[i].EventName << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< eventList.Provided->GetFullName() << "\")"<< std::endl;
if (eventList.VoidEvents[i].Required == MTS_REQUIRED) {
success = false;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "CheckEventList: succeeded to add observer for void event \""
<< eventList.VoidEvents[i].EventName << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< eventList.Provided->GetFullName() << "\")"<< std::endl;
}
}
for (i = 0; i < eventList.WriteEvents.size(); i++) {
if (!eventList.WriteEvents[i].Result) {
CMN_LOG_CLASS_INIT_DEBUG << "CheckEventList: failed to add observer for write event \""
<< eventList.WriteEvents[i].EventName << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< eventList.Provided->GetFullName() << "\")"<< std::endl;
if (eventList.WriteEvents[i].Required == MTS_REQUIRED) {
success = false;
}
} else {
CMN_LOG_CLASS_INIT_DEBUG << "CheckEventList: succeeded to add observer for write event \""
<< eventList.WriteEvents[i].EventName << "\" (connecting \""
<< this->GetFullName() << "\" to \""
<< eventList.Provided->GetFullName() << "\")"<< std::endl;
}
}
return success;
}
void mtsInterfaceRequired::DisableAllEvents(void)
{
EventHandlersVoid.ForEachVoid(&mtsCommandBase::Disable);
EventHandlersWrite.ForEachVoid(&mtsCommandBase::Disable);
}
void mtsInterfaceRequired::EnableAllEvents(void)
{
EventHandlersVoid.ForEachVoid(&mtsCommandBase::Enable);
EventHandlersWrite.ForEachVoid(&mtsCommandBase::Enable);
}
size_t mtsInterfaceRequired::ProcessMailBoxes(void)
{
// one of constructor of mtsInterfaceRequired allows a null
// pointer to be passed as the second argument. This is used
// for the ExecIn interface.
if (!MailBox) {
//CMN_LOG_CLASS_RUN_WARNING << "ProcessMailBoxes: called on interface \""
// << this->GetFullName() << "\" which doesn't have a mail box." << std::endl;
return 0;
}
size_t numberOfCommands = 0;
while (MailBox->ExecuteNext()) {
numberOfCommands++;
}
return numberOfCommands;
}
void mtsInterfaceRequired::ToStream(std::ostream & outputStream) const
{
outputStream << "Required Interface name: " << this->GetFullName() << std::endl;
FunctionsVoid.ToStream(outputStream);
FunctionsRead.ToStream(outputStream);
FunctionsWrite.ToStream(outputStream);
FunctionsQualifiedRead.ToStream(outputStream);
EventHandlersVoid.ToStream(outputStream);
EventHandlersWrite.ToStream(outputStream);
}
bool mtsInterfaceRequired::AddFunction(const std::string & functionName, mtsFunctionVoid & function,
mtsRequiredType required)
{
function.SetThreadSignal(&(this->ThreadSignalForBlockingCommands));
return FunctionsVoid.AddItem(functionName, new FunctionInfo(function, required));
}
bool mtsInterfaceRequired::AddFunction(const std::string & functionName, mtsFunctionVoidReturn & function,
mtsRequiredType required)
{
function.SetThreadSignal(&(this->ThreadSignalForBlockingCommands));
return FunctionsVoidReturn.AddItem(functionName, new FunctionInfo(function, required));
}
bool mtsInterfaceRequired::AddFunction(const std::string & functionName, mtsFunctionWrite & function,
mtsRequiredType required)
{
function.SetThreadSignal(&(this->ThreadSignalForBlockingCommands));
return FunctionsWrite.AddItem(functionName, new FunctionInfo(function, required));
}
bool mtsInterfaceRequired::AddFunction(const std::string & functionName, mtsFunctionWriteReturn & function,
mtsRequiredType required)
{
function.SetThreadSignal(&(this->ThreadSignalForBlockingCommands));
return FunctionsWriteReturn.AddItem(functionName, new FunctionInfo(function, required));
}
bool mtsInterfaceRequired::AddFunction(const std::string & functionName, mtsFunctionRead & function,
mtsRequiredType required)
{
function.SetThreadSignal(&(this->ThreadSignalForBlockingCommands));
return FunctionsRead.AddItem(functionName, new FunctionInfo(function, required));
}
bool mtsInterfaceRequired::AddFunction(const std::string & functionName, mtsFunctionQualifiedRead & function,
mtsRequiredType required)
{
function.SetThreadSignal(&(this->ThreadSignalForBlockingCommands));
return FunctionsQualifiedRead.AddItem(functionName, new FunctionInfo(function, required));
}
bool mtsInterfaceRequired::AddEventReceiver(const std::string & eventName, mtsEventReceiverVoid & receiver, mtsRequiredType required)
{
receiver.SetRequired(eventName, this);
return EventReceiversVoid.AddItem(eventName, new ReceiverVoidInfo(receiver, required));
}
bool mtsInterfaceRequired::AddEventReceiver(const std::string & eventName, mtsEventReceiverWrite & receiver, mtsRequiredType required)
{
receiver.SetRequired(eventName, this);
return EventReceiversWrite.AddItem(eventName, new ReceiverWriteInfo(receiver, required));
}
mtsCommandVoid * mtsInterfaceRequired::AddEventHandlerVoid(mtsCallableVoidBase * callable,
const std::string & eventName,
mtsEventQueueingPolicy queueingPolicy)
{
CMN_ASSERT(callable);
bool queued = this->UseQueueBasedOnInterfacePolicy(queueingPolicy, "AddEventHandlerVoid", eventName);
if (queued) {
if (MailBox) {
EventHandlersVoid.AddItem(eventName, new mtsCommandQueuedVoid(callable, eventName, MailBox, this->ArgumentQueuesSize));
} else {
CMN_LOG_CLASS_INIT_ERROR << "No mailbox for queued event handler void \"" << eventName << "\"" << std::endl;
}
} else {
EventHandlersVoid.AddItem(eventName, new mtsCommandVoid(callable, eventName));
}
mtsCommandVoid * handler = EventHandlersVoid.GetItem(eventName);
AddEventHandlerToReceiver(eventName, handler); // does nothing if event receiver does not exist
return handler;
}
bool mtsInterfaceRequired::RemoveEventHandlerVoid(const std::string & eventName)
{
mtsCommandVoid * handler = 0;
AddEventHandlerToReceiver(eventName, handler);
return EventHandlersVoid.RemoveItem(eventName, CMN_LOG_LEVEL_INIT_WARNING);
}
bool mtsInterfaceRequired::RemoveEventHandlerWrite(const std::string & eventName)
{
mtsCommandWriteBase * handler = 0;
AddEventHandlerToReceiver(eventName, handler);
return EventHandlersWrite.RemoveItem(eventName, CMN_LOG_LEVEL_INIT_WARNING);
}
void mtsInterfaceRequired::GetDescription(mtsInterfaceRequiredDescription & requiredInterfaceDescription) const
{
// Serializer initialization
std::stringstream streamBuffer;
cmnSerializer serializer(streamBuffer);
// Extract "IsRequired" attribute
requiredInterfaceDescription.IsRequired = (IsRequired() == MTS_REQUIRED);
// Extract void functions
requiredInterfaceDescription.FunctionVoidNames = GetNamesOfFunctionsVoid();
// Extract write functions
requiredInterfaceDescription.FunctionWriteNames = GetNamesOfFunctionsWrite();
// Extract read functions
requiredInterfaceDescription.FunctionReadNames = GetNamesOfFunctionsRead();
// Extract qualified read functions
requiredInterfaceDescription.FunctionQualifiedReadNames = GetNamesOfFunctionsQualifiedRead();
// Extract void return functions
requiredInterfaceDescription.FunctionVoidReturnNames = GetNamesOfFunctionsVoidReturn();
// Extract write return functions
requiredInterfaceDescription.FunctionWriteReturnNames = GetNamesOfFunctionsWriteReturn();
// Extract void event handlers
mtsCommandVoidDescription elementEventVoidHandler;
mtsInterfaceRequired::EventHandlerVoidMapType::MapType::const_iterator itVoid = EventHandlersVoid.begin();
const mtsInterfaceRequired::EventHandlerVoidMapType::MapType::const_iterator itVoidEnd = EventHandlersVoid.end();
for (; itVoid != itVoidEnd; ++itVoid) {
elementEventVoidHandler.Name = itVoid->second->GetName();
requiredInterfaceDescription.EventHandlersVoid.push_back(elementEventVoidHandler);
}
// Extract write event handlers
mtsCommandWriteDescription elementEventWriteHandler;
mtsInterfaceRequired::EventHandlerWriteMapType::MapType::const_iterator itWrite = EventHandlersWrite.begin();
const mtsInterfaceRequired::EventHandlerWriteMapType::MapType::const_iterator itWriteEnd = EventHandlersWrite.end();
for (; itWrite != itWriteEnd; ++itWrite) {
elementEventWriteHandler.Name = itWrite->second->GetName();
// argument serialization
streamBuffer.str("");
serializer.Serialize(*(itWrite->second->GetArgumentPrototype()));
elementEventWriteHandler.ArgumentPrototypeSerialized = streamBuffer.str();
requiredInterfaceDescription.EventHandlersWrite.push_back(elementEventWriteHandler);
}
}
|