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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSocketCommunicator.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSocketCommunicator.h"
#include "vtkClientSocket.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkServerSocket.h"
#include "vtkSocketController.h"
#include "vtkStdString.h"
#include "vtkTypeTraits.h"
#include <cassert>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
// Uncomment the following line to help with debugging. When
// ENABLE_SYNCHRONIZED_COMMUNICATION is defined, every Send() blocks until the
// receive is successful.
//#define ENABLE_SYNCHRONIZED_COMMUNICATION
class vtkSocketCommunicator::vtkMessageBuffer
{
public:
typedef std::vector<char> MessageType;
typedef std::list<MessageType> QueueType;
typedef std::map<int, QueueType> BufferType;
BufferType Buffer; // key --> tag, value-->queue of messages.
bool HasBufferredMessages()
{
return this->Buffer.size() != 0;
}
bool HasMessage(int tag)
{
BufferType::iterator iter = this->Buffer.find(tag);
if (iter == this->Buffer.end())
{
return false;
}
return (iter->second.size() != 0);
}
void Push(int tag, int numchars, char* data)
{
this->Buffer[tag].push_back(MessageType());
MessageType& msg = this->Buffer[tag].back();
msg.insert(msg.end(), data, (data+numchars));
}
void Pop(int tag)
{
this->Buffer[tag].pop_front();
if (this->Buffer[tag].size() == 0)
{
this->Buffer.erase(tag);
}
}
MessageType& Head(int tag)
{
return this->Buffer[tag].front();
}
};
#define vtkSocketCommunicatorErrorMacro(msg)\
if (this->ReportErrors)\
{\
vtkErrorMacro(msg)\
}
// The handshake checks that the client and server are using the same
// version of this source file. It first compares a fixed integer
// hash identifier to make sure the hash algorithms match. Then it
// compares hash strings. Note that the integer id exchange used to
// represent the CVS revision number of this file, so the value must
// be larger than the last revision which used that strategy.
#define vtkSocketCommunicatorHashId 100 /* MD5 */
#include "vtkSocketCommunicatorHash.h"
vtkStandardNewMacro(vtkSocketCommunicator);
vtkCxxSetObjectMacro(vtkSocketCommunicator, Socket, vtkClientSocket);
//----------------------------------------------------------------------------
vtkSocketCommunicator::vtkSocketCommunicator()
{
this->Socket = NULL;
this->NumberOfProcesses = 2;
this->SwapBytesInReceivedData = vtkSocketCommunicator::SwapNotSet;
this->RemoteHas64BitIds = -1; // Invalid until handshake.
this->PerformHandshake = 1;
this->IsServer = 0;
this->LogStream = 0;
this->LogFile = 0;
this->TagMessageLength = 0;
this->BufferMessage = false;
this->ReportErrors = 1;
this->ReceivedMessageBuffer = new vtkSocketCommunicator::vtkMessageBuffer();
}
//----------------------------------------------------------------------------
vtkSocketCommunicator::~vtkSocketCommunicator()
{
this->SetSocket(0);
this->SetLogStream(0);
delete this->ReceivedMessageBuffer;
this->ReceivedMessageBuffer = 0;
}
//----------------------------------------------------------------------------
void vtkSocketCommunicator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "SwapBytesInReceivedData: ";
if (this->SwapBytesInReceivedData == SwapOff)
{
os << "Off\n";
}
if (this->SwapBytesInReceivedData == SwapOn)
{
os << "On\n";
}
if (this->SwapBytesInReceivedData == SwapNotSet)
{
os << "NotSet\n";
}
os << indent << "IsServer: "
<< (this->IsServer ? "yes" : "no") << endl;
os << indent << "RemoteHas64BitIds: "
<< (this->RemoteHas64BitIds ? "yes" : "no") << endl;
os << indent << "Socket: ";
if (this->Socket)
{
os << endl;
this->Socket->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
os << indent << "Perform a handshake: "
<< ( this->PerformHandshake ? "Yes" : "No" ) << endl;
os << indent << "ReportErrors: " << this->ReportErrors << endl;
}
//----------------------------------------------------------------------------
void vtkSocketCommunicator::SetLogStream(ostream* stream)
{
if(this->LogStream != stream)
{
// If the log stream is our own log file, close the file.
if(this->LogFile && this->LogFile == this->LogStream)
{
delete this->LogFile;
this->LogFile = 0;
}
// Use the given log stream.
this->LogStream = stream;
}
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::GetIsConnected()
{
if (this->Socket)
{
return this->Socket->GetConnected();
}
return 0;
}
//----------------------------------------------------------------------------
void vtkSocketCommunicator::SetNumberOfProcesses(int vtkNotUsed(num))
{
vtkErrorMacro("Can not change the number of processes.");
return;
}
//----------------------------------------------------------------------------
ostream* vtkSocketCommunicator::GetLogStream()
{
return this->LogStream;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::LogToFile(const char* name)
{
return this->LogToFile(name, 0);
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::LogToFile(const char* name, int append)
{
// Close old logging file.
delete this->LogFile;
this->LogFile = 0;
this->LogStream = 0;
// Log to given file, if any.
if(name && name[0])
{
this->LogFile = new ofstream(name, (ios::out |
(append? ios::ate : ios::trunc)));
if(!this->LogFile)
{
return 0;
}
if(!*this->LogFile)
{
delete this->LogFile;
this->LogFile = 0;
return 0;
}
this->LogStream = this->LogFile;
}
return 1;
}
//-----------------------------------------------------------------------------
int vtkSocketCommunicator::SendVoidArray(const void *data, vtkIdType length,
int type, int remoteProcessId, int tag)
{
if(this->CheckForErrorInternal(remoteProcessId)) { return 0; }
#ifdef VTK_USE_64BIT_IDS
// Special case for type ids. If the remote does not have 64 bit ids, we
// need to convert them before sending them.
if ((type == VTK_ID_TYPE) && !this->RemoteHas64BitIds)
{
std::vector<int> newData;
newData.resize(length);
std::copy(reinterpret_cast<const vtkIdType *>(data),
reinterpret_cast<const vtkIdType *>(data) + length,
newData.begin());
return this->SendVoidArray(&newData[0], length, VTK_INT,
remoteProcessId, tag);
}
#endif
int typeSize;
vtkStdString typeName;
switch (type)
{
vtkTemplateMacro(typeSize = sizeof(VTK_TT);
typeName = vtkTypeTraits<VTK_TT>().SizedName());
default:
vtkWarningMacro(<< "Invalid data type " << type);
typeSize = 1;
typeName = "???";
break;
}
// Special case for logging.
if (type == VTK_CHAR)
{
typeName = "char";
}
const char *byteData = reinterpret_cast<const char *>(data);
int maxSend = VTK_INT_MAX/typeSize;
// If sending an array longer than the maximum number that can be held
// in an integer, break up the array into pieces.
while (length >= maxSend)
{
if (!this->SendTagged(byteData, typeSize, maxSend, tag, typeName))
{
return 0;
}
byteData += maxSend*typeSize;
length -= maxSend;
}
if (!this->SendTagged(byteData, typeSize, length, tag, typeName))
{
return 0;
}
#ifdef ENABLE_SYNCHRONIZED_COMMUNICATION
int status[3] = {0, 0, 0};
this->ReceiveTagged(status, sizeof(int), 3, 9876543,
"ENABLE_SYNCHRONIZED_COMMUNICATION#1");
assert(status[0] == 9876543 && status[2] == 9876544 &&
(status[1] == 1 || status[1] == 2));
this->SendTagged(status, sizeof(int), 3, 9876544,
"ENABLE_SYNCHRONIZED_COMMUNICATION#2");
#endif
return 1;
}
//-----------------------------------------------------------------------------
inline vtkIdType vtkSocketCommunicatorMin(vtkIdType a, vtkIdType b)
{
return (a < b)? a : b;
}
//-----------------------------------------------------------------------------
int vtkSocketCommunicator::ReceiveVoidArray(void *data, vtkIdType length,
int type, int remoteProcessId,
int tag)
{
this->Count = 0;
if (this->CheckForErrorInternal(remoteProcessId))
{
return 0;
}
#ifdef VTK_USE_64BIT_IDS
// Special case for type ids. If the remote does not have 64 bit ids, we
// need to convert them before sending them.
if ((type == VTK_ID_TYPE) && !this->RemoteHas64BitIds)
{
std::vector<int> newData;
newData.resize(length);
int retval = this->ReceiveVoidArray(&newData[0], length, VTK_INT,
remoteProcessId, tag);
std::copy(newData.begin(), newData.end(),
reinterpret_cast<vtkIdType *>(data));
return retval;
}
#endif
int typeSize;
vtkStdString typeName;
switch (type)
{
vtkTemplateMacro(typeSize = sizeof(VTK_TT);
typeName = vtkTypeTraits<VTK_TT>().SizedName());
default:
vtkWarningMacro(<< "Invalid data type " << type);
typeSize = 1;
typeName = "???";
break;
}
// Special case for logging.
if (type == VTK_CHAR)
{
typeName = "char";
}
char *byteData = reinterpret_cast<char *>(data);
int maxReceive = VTK_INT_MAX/typeSize;
// If receiving an array longer than the maximum number that can be held
// in an integer, break up the array into pieces.
int ret = 0;
while (this->ReceiveTagged(byteData, typeSize,
vtkSocketCommunicatorMin(maxReceive, length), tag, typeName))
{
this->Count += this->TagMessageLength;
byteData += this->TagMessageLength * typeSize;
length -= this->TagMessageLength;
if (this->TagMessageLength < maxReceive)
{
// words_received in this packet is exactly equal to maxReceive, then it
// means that the sender is sending atleast one more packet for this
// message. Otherwise, we have received all the packets for this message
// and we no longer need to iterate.
ret = 1;
break;
}
}
// Some crazy special crud for RMIs that may one day screw someone up in
// a weird way. No, I did not write this, but I'm sure there is code that
// relies on it.
// (This is setting the process id for the sender in the message).
if (ret && (tag == vtkMultiProcessController::RMI_TAG))
{
int *idata = reinterpret_cast<int *>(data);
idata[2] = 1;
vtkByteSwap::SwapLE(&idata[2]);
}
#ifdef ENABLE_SYNCHRONIZED_COMMUNICATION
int status[3] = {9876543, 1, 9876544};
int other_status[3] = {-1, -1, -1};
assert(this->SendTagged(status, sizeof(int), 3, 9876543,
"ENABLE_SYNCHRONIZED_COMMUNICATION#1"));
assert(this->ReceiveTagged(other_status, sizeof(int), 3, 9876544,
"ENABLE_SYNCHRONIZED_COMMUNICATION#2"));
assert(other_status[0] == status[0] && other_status[1] == status[1] &&
other_status[2] == status[2]);
#endif
return ret;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::Handshake()
{
if (!this->Socket)
{
vtkErrorMacro("No socket set. Cannot perform handshake.");
return 0;
}
if (this->Socket->GetConnectingSide())
{
return this->ClientSideHandshake();
}
else
{
return this->ServerSideHandshake();
}
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::ServerSideHandshake()
{
this->IsServer = 1;
if ( this->PerformHandshake )
{
// Handshake to determine if the client machine has the same endianness
char clientIsBE;
if(!this->ReceiveTagged(&clientIsBE, static_cast<int>(sizeof(char)),
1, vtkSocketController::ENDIAN_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Endian handshake failed.");
return 0;
}
vtkDebugMacro(<< "Client is " << ( clientIsBE ? "big" : "little" )
<< "-endian");
#ifdef VTK_WORDS_BIGENDIAN
char IAmBE = 1;
#else
char IAmBE = 0;
#endif
vtkDebugMacro(<< "I am " << ( IAmBE ? "big" : "little" ) << "-endian");
if(!this->SendTagged(&IAmBE, static_cast<int>(sizeof(char)),
1, vtkSocketController::ENDIAN_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Endian handshake failed.");
return 0;
}
if ( clientIsBE != IAmBE )
{
this->SwapBytesInReceivedData = vtkSocketCommunicator::SwapOn;
}
else
{
this->SwapBytesInReceivedData = vtkSocketCommunicator::SwapOff;
}
// Check to make sure the client and server have the same version of the
// socket communicator.
int myVersion = vtkSocketCommunicator::GetVersion();
int clientVersion;
if (!this->ReceiveTagged(&clientVersion, static_cast<int>(sizeof(int)),
1, vtkSocketController::VERSION_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Version handshake failed. "
"Perhaps there is a client/server version mismatch.");
return 0;
}
if (!this->SendTagged(&myVersion, static_cast<int>(sizeof(int)),
1, vtkSocketController::VERSION_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Version handshake failed. "
"Perhaps there is a client/server version mismatch.");
return 0;
}
if (myVersion != clientVersion)
{
vtkSocketCommunicatorErrorMacro("Client/server version mismatch.");
return 0;
}
// Compare hashes of this source file from each side.
const char myHash[] = vtkSocketCommunicatorHash;
char clientHash[sizeof(myHash)];
if (!this->ReceiveTagged(&clientHash,
1, static_cast<int>(sizeof(clientHash)),
vtkSocketController::HASH_TAG, 0) ||
!this->SendTagged(&myHash,
1, static_cast<int>(sizeof(myHash)),
vtkSocketController::HASH_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Version hash handshake failed. "
"Perhaps there is a client/server version mismatch.");
return 0;
}
if (strncmp(myHash, clientHash, sizeof(myHash)-1) != 0)
{
vtkSocketCommunicatorErrorMacro("Client/server version hash mismatch.");
return 0;
}
// Handshake to determine if remote has 64 bit ids.
#ifdef VTK_USE_64BIT_IDS
int IHave64BitIds = 1;
#else
int IHave64BitIds = 0;
#endif
if (!this->ReceiveTagged(&(this->RemoteHas64BitIds),
static_cast<int>(sizeof(int)), 1,
vtkSocketController::IDTYPESIZE_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Id Type Size handshake failed.");
return 0;
}
vtkDebugMacro(<< "Remote has 64 bit ids: " << this->RemoteHas64BitIds);
if (!this->SendTagged(&IHave64BitIds, static_cast<int>(sizeof(int)), 1,
vtkSocketController::IDTYPESIZE_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Id Type Size handshake failed.");
return 0;
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::ClientSideHandshake()
{
this->IsServer = 0;
if (!this->PerformHandshake)
{
return 1;
}
// Handshake to determine if the server machine has the same endianness
#ifdef VTK_WORDS_BIGENDIAN
char IAmBE = 1;
#else
char IAmBE = 0;
#endif
vtkDebugMacro(<< "I am " << ( IAmBE ? "big" : "little" ) << "-endian");
if(!this->SendTagged(&IAmBE, static_cast<int>(sizeof(char)),
1, vtkSocketController::ENDIAN_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Endian handshake failed.");
return 0;
}
char serverIsBE;
if (!this->ReceiveTagged(&serverIsBE, static_cast<int>(sizeof(char)), 1,
vtkSocketController::ENDIAN_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Endian handshake failed.");
return 0;
}
vtkDebugMacro(<< "Server is " << ( serverIsBE ? "big" : "little" ) << "-endian");
if ( serverIsBE != IAmBE )
{
this->SwapBytesInReceivedData = vtkSocketCommunicator::SwapOn;
}
else
{
this->SwapBytesInReceivedData = vtkSocketCommunicator::SwapOff;
}
// Check to make sure the client and server have the same version of the
// socket communicator.
int myVersion = vtkSocketCommunicator::GetVersion();
int serverVersion;
if (!this->SendTagged(&myVersion, static_cast<int>(sizeof(int)),
1, vtkSocketController::VERSION_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Version handshake failed. "
"Perhaps there is a client/server version mismatch.");
return 0;
}
if (!this->ReceiveTagged(&serverVersion, static_cast<int>(sizeof(int)),
1, vtkSocketController::VERSION_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Version handshake failed. "
"Perhaps there is a client/server version mismatch.");
return 0;
}
if (myVersion != serverVersion)
{
vtkSocketCommunicatorErrorMacro("Client/server version mismatch.");
return 0;
}
// Compare hashes of this source file from each side.
const char myHash[] = vtkSocketCommunicatorHash;
char serverHash[sizeof(myHash)];
if (!this->SendTagged(&myHash,
1, static_cast<int>(sizeof(myHash)),
vtkSocketController::HASH_TAG, 0) ||
!this->ReceiveTagged(&serverHash,
1, static_cast<int>(sizeof(serverHash)),
vtkSocketController::HASH_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Version hash handshake failed. "
"Perhaps there is a client/server version mismatch.");
return 0;
}
if (strncmp(myHash, serverHash, sizeof(myHash)-1) != 0)
{
vtkSocketCommunicatorErrorMacro("Client/server version hash mismatch.");
return 0;
}
// Handshake to determine if remote has 64 bit ids.
#ifdef VTK_USE_64BIT_IDS
int IHave64BitIds = 1;
#else
int IHave64BitIds = 0;
#endif
if (!this->SendTagged(&IHave64BitIds, static_cast<int>(sizeof(int)), 1,
vtkSocketController::IDTYPESIZE_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Id Type Size handshake failed.");
return 0;
}
if (!this->ReceiveTagged(&(this->RemoteHas64BitIds),
static_cast<int>(sizeof(int)), 1,
vtkSocketController::IDTYPESIZE_TAG, 0))
{
vtkSocketCommunicatorErrorMacro("Id Type Size handshake failed.");
return 0;
}
vtkDebugMacro(<< "Remote has 64 bit ids: " << this->RemoteHas64BitIds);
return 1;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::WaitForConnection(int port)
{
if ( this->GetIsConnected() )
{
vtkSocketCommunicatorErrorMacro("Communicator port " << 1 << " is occupied.");
return 0;
}
vtkServerSocket * soc = vtkServerSocket::New();
if (soc->CreateServer(port) != 0)
{
soc->Delete();
return 0;
}
int ret = this->WaitForConnection(soc);
soc->Delete();
return ret;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::WaitForConnection(vtkServerSocket* socket,
unsigned long msec/*=0*/)
{
if ( this->GetIsConnected() )
{
vtkSocketCommunicatorErrorMacro("Communicator port " << 1 << " is occupied.");
return 0;
}
if (!socket)
{
return 0;
}
vtkClientSocket *cs= socket->WaitForConnection(msec);
if (cs)
{
this->SetSocket(cs);
cs->Delete();
}
if (!this->Socket)
{
return 0;
}
return this->ServerSideHandshake();
}
//----------------------------------------------------------------------------
void vtkSocketCommunicator::CloseConnection()
{
if (this->Socket)
{
this->Socket->CloseSocket();
this->Socket->Delete();
this->Socket = 0;
}
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::ConnectTo (const char* hostName, int port )
{
if ( this->GetIsConnected() )
{
vtkSocketCommunicatorErrorMacro("Communicator port " << 1 << " is occupied.");
return 0;
}
vtkClientSocket* tmp = vtkClientSocket::New();
if(tmp->ConnectToServer(hostName, port))
{
vtkSocketCommunicatorErrorMacro("Can not connect to " << hostName << " on port " << port);
tmp->Delete();
return 0;
}
this->SetSocket(tmp);
tmp->Delete();
vtkDebugMacro("Connected to " << hostName << " on port " << port);
return this->ClientSideHandshake();
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::SendTagged(const void* data, int wordSize,
int numWords, int tag,
const char* logName)
{
if(!this->Socket->Send(&tag, static_cast<int>(sizeof(int))))
{
vtkSocketCommunicatorErrorMacro("Could not send tag.");
return 0;
}
int length = wordSize * numWords;
if(!this->Socket->Send(&length,
static_cast<int>(sizeof(int))))
{
vtkSocketCommunicatorErrorMacro("Could not send length.");
return 0;
}
// Only do the actual send if there is some data in the message.
if (length > 0)
{
if(!this->Socket->Send(data, length))
{
vtkSocketCommunicatorErrorMacro("Could not send message.");
return 0;
}
}
// Log this event.
this->LogTagged("Sent", data, wordSize, numWords, tag, logName);
return 1;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::ReceivedTaggedFromBuffer(
void* data, int wordSize, int numWords, int tag, const char* logName)
{
this->TagMessageLength = 0;
vtkMessageBuffer::MessageType &message =
this->ReceivedMessageBuffer->Head(tag);
if (static_cast<unsigned int>(numWords * wordSize) < message.size())
{
vtkSocketCommunicatorErrorMacro("Message truncated."
"Receive buffer size (" << (wordSize * numWords) << ") is less than "
"message length (" << message.size() << ")");
return 0;
}
// The static_cast is OK since we split messages > VTK_INT_MAX.
this->TagMessageLength = static_cast<int>(message.size())/wordSize;
memcpy(data, &message[0], message.size());
this->ReceivedMessageBuffer->Pop(tag);
this->FixByteOrder(data, wordSize, numWords);
// Log this event.
this->LogTagged("Receive(from Buffer)", data, wordSize, numWords, tag, logName);
return 1;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::ReceiveTagged(void* data, int wordSize,
int numWords, int tag,
const char* logName)
{
if (this->ReceivedMessageBuffer->HasMessage(tag))
{
// If a message for the given tag was already received, it will be in the
// queue, so simply return that.
return this->ReceivedTaggedFromBuffer(data, wordSize, numWords, tag, logName);
}
// Since the message queue for the \c tag is empty, try to receive the message
// over the socket.
this->TagMessageLength = 0;
int success = 0;
int length = -1;
while ( !success )
{
int recvTag = -1;
length = -1;
if (!this->Socket->Receive(&recvTag, static_cast<int>(sizeof(int))))
{
vtkSocketCommunicatorErrorMacro("Could not receive tag. " << tag);
return 0;
}
if (this->SwapBytesInReceivedData == vtkSocketCommunicator::SwapOn)
{
vtkSwap4(reinterpret_cast<char*>(&recvTag));
}
if (!this->Socket->Receive(&length, static_cast<int>(sizeof(int))))
{
vtkSocketCommunicatorErrorMacro("Could not receive length.");
return 0;
}
if (this->SwapBytesInReceivedData == vtkSocketCommunicator::SwapOn)
{
vtkSwap4(reinterpret_cast<char*>(&length));
}
else if (this->SwapBytesInReceivedData == vtkSocketCommunicator::SwapNotSet)
{
// Clearly, we still haven;t determined our endianness. In that case, the
// only legal communication should be vtkSocketController::ENDIAN_TAG.
// However, I am not flagging an error since applications may used socket
// communicator without the handshake part (where it's assumed that the
// application takes over the handshaking). So if the message is for
// endianness check, then we simply adjust the length.
if (tag == vtkSocketController::ENDIAN_TAG)
{
// ignore the length the we received, just set it to what we want.
length = numWords* wordSize;
}
}
if(recvTag != tag)
{
// There's a tag mismatch, call the error handler. If the error handler
// tells us that the mismatch is non-fatal, we keep on receiving,
// otherwise we quit with an error.
char* idata = new char[length + sizeof(recvTag) + sizeof(length)];
char* ptr = idata;
memcpy(ptr, (void*)&recvTag, sizeof(recvTag));
ptr += sizeof(recvTag);
memcpy(ptr, (void*)&length, sizeof(length));
ptr += sizeof(length);
this->BufferMessage = false;
this->ReceivePartialTagged(ptr, 1, length, tag, "Wrong tag");
int res = this->InvokeEvent(vtkCommand::WrongTagEvent, idata);
// if res == 1, then it implies that the observer has processed the
// message.
// if res == 0 and this->BufferMessage is true, it implies that the
// observer wants us to buffer this message for later use.
if (this->BufferMessage)
{
// TODO: we may want to optimize this to avoid multiple copying of the
// data.
if (this->LogStream)
{
*this->LogStream << "Bufferring last message (" << recvTag <<")" << endl;
}
this->ReceivedMessageBuffer->Push(recvTag, length, ptr);
}
delete [] idata;
if (res || this->BufferMessage)
{
continue;
}
vtkSocketCommunicatorErrorMacro(
"Tag mismatch: got " << recvTag << ", expecting " << tag << ".");
return 0;
}
else
{
success = 1;
}
}
if ((numWords * wordSize) < length)
{
vtkSocketCommunicatorErrorMacro("Message truncated."
"Receive buffer size (" << (wordSize * numWords) << ") is less than "
"message length (" << length << ")");
return 0;
}
this->TagMessageLength = length/wordSize;
return this->ReceivePartialTagged(data, wordSize, length/wordSize, tag, logName);
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::ReceivePartialTagged(void* data, int wordSize,
int numWords, int tag,
const char* logName)
{
// Only do the actual receive if there is some data to receive
if (wordSize*numWords > 0)
{
if(!this->Socket->Receive(data, wordSize*numWords))
{
vtkSocketCommunicatorErrorMacro("Could not receive message.");
return 0;
}
}
this->FixByteOrder(data, wordSize, numWords);
// Log this event.
this->LogTagged("Received", data, wordSize, numWords, tag, logName);
return 1;
}
//----------------------------------------------------------------------------
void vtkSocketCommunicator::FixByteOrder(void* data, int wordSize, int numWords)
{
// Unless we're dealing with chars, then check byte ordering.
// This is really bad and should probably use some enum for types
if (this->SwapBytesInReceivedData == vtkSocketCommunicator::SwapOn)
{
if (wordSize == 4)
{
vtkDebugMacro(<< " swapping 4 range, size = " << wordSize
<< " length = " << numWords);
vtkSwap4Range(reinterpret_cast<char*>(data), numWords);
}
else if (wordSize == 8)
{
vtkDebugMacro(<< " swapping 8 range, size = " << wordSize
<< " length = " << numWords );
vtkSwap8Range(reinterpret_cast<char*>(data), numWords);
}
}
}
//----------------------------------------------------------------------------
bool vtkSocketCommunicator::HasBufferredMessages()
{
return this->ReceivedMessageBuffer->HasBufferredMessages();
}
//----------------------------------------------------------------------------
template <class T, class OutType>
void vtkSocketCommunicatorLogArray(ostream& os, T* array, int length, int max,
OutType*)
{
if(length > 0)
{
int num = (length <= max)? length:max;
os << " data={" << static_cast<OutType>(array[0]);
for(int i=1; i < num; ++i)
{
os << " " << static_cast<OutType>(array[i]);
}
if(length > max)
{
os << " ...";
}
os << "}";
}
}
//----------------------------------------------------------------------------
void vtkSocketCommunicator::LogTagged(const char* name, const void* data,
int wordSize, int numWords,
int tag, const char* logName)
{
if(this->LogStream)
{
// Log the general event information.
*this->LogStream << name;
if(logName)
{
*this->LogStream << " " << logName;
}
*this->LogStream << " data: tag=" << tag
<< " wordSize=" << wordSize
<< " numWords=" << numWords;
// If this is a string, log the first 70 characters. If this is
// an array of data values, log the first few.
if(wordSize == static_cast<int>(sizeof(char)) && logName &&
(strcmp(logName, "char") == 0))
{
const char* chars = reinterpret_cast<const char*>(data);
if((chars[numWords-1]) == 0 &&
(static_cast<int>(strlen(chars)) == numWords-1))
{
// String data. Display the first 70 characters.
*this->LogStream << " data={";
if(numWords <= 71)
{
*this->LogStream << chars;
}
else
{
this->LogStream->write(reinterpret_cast<const char*>(data), 70);
*this->LogStream << " ...";
}
*this->LogStream << "}";
}
else
{
// Not string data. Display the characters as integer values.
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const char*>(data),
numWords, 6, static_cast<int*>(0));
}
}
else if ((wordSize == 1) && logName && (strcmp(logName, "Int8") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeInt8*>(data),
numWords, 6, static_cast<vtkTypeInt16*>(0));
}
else if ((wordSize == 1) && logName && (strcmp(logName, "UInt8") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeUInt8*>(data),
numWords, 6, static_cast<vtkTypeUInt16*>(0));
}
else if ((wordSize == 2) && logName && (strcmp(logName, "Int16") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeInt16*>(data),
numWords, 6, static_cast<vtkTypeInt16*>(0));
}
else if ((wordSize == 2) && logName && (strcmp(logName, "UInt16") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeUInt16*>(data),
numWords, 6, static_cast<vtkTypeUInt16*>(0));
}
else if ((wordSize == 4) && logName && (strcmp(logName, "Int32") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeInt32*>(data),
numWords, 6, static_cast<vtkTypeInt32*>(0));
}
else if ((wordSize == 4) && logName && (strcmp(logName, "UInt32") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeUInt32*>(data),
numWords, 6, static_cast<vtkTypeUInt32*>(0));
}
else if ((wordSize == 8) && logName && (strcmp(logName, "Int64") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeInt64*>(data),
numWords, 6, static_cast<vtkTypeInt64*>(0));
}
else if ((wordSize == 8) && logName && (strcmp(logName, "UInt64") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeUInt64*>(data),
numWords, 6, static_cast<vtkTypeUInt64*>(0));
}
else if ((wordSize == 4) && logName && (strcmp(logName, "Float32") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeFloat32*>(data),
numWords, 6, static_cast<vtkTypeFloat32*>(0));
}
else if ((wordSize == 8) && logName && (strcmp(logName, "Float64") == 0))
{
vtkSocketCommunicatorLogArray(*this->LogStream,
reinterpret_cast<const vtkTypeFloat64*>(data),
numWords, 6, static_cast<vtkTypeFloat64*>(0));
}
*this->LogStream << endl;
}
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::CheckForErrorInternal(int id)
{
if(id == 0)
{
vtkSocketCommunicatorErrorMacro("Can not connect to myself!");
return 1;
}
else if(id >= this->NumberOfProcesses)
{
vtkSocketCommunicatorErrorMacro("No port for process " << id << " exists.");
return 1;
}
return 0;
}
//-----------------------------------------------------------------------------
void vtkSocketCommunicator::Barrier()
{
int junk = 0;
if (this->IsServer)
{
this->Send(&junk, 1, 1, BARRIER_TAG);
this->Receive(&junk, 1, 1, BARRIER_TAG);
}
else
{
this->Receive(&junk, 1, 1, BARRIER_TAG);
this->Send(&junk, 1, 1, BARRIER_TAG);
}
}
//-----------------------------------------------------------------------------
int vtkSocketCommunicator::BroadcastVoidArray(
void *data, vtkIdType length, int type, int root)
{
return this->Superclass::BroadcastVoidArray(data, length, type, root);
}
//-----------------------------------------------------------------------------
int vtkSocketCommunicator::GatherVoidArray(const void *, void *,
vtkIdType, int, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::GatherVVoidArray(const void *, void *,
vtkIdType, vtkIdType *,
vtkIdType *, int, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::ScatterVoidArray(const void *, void *,
vtkIdType, int, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::ScatterVVoidArray(const void *, void *,
vtkIdType *, vtkIdType *,
vtkIdType, int, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::AllGatherVoidArray(const void *, void *,
vtkIdType, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::AllGatherVVoidArray(const void *, void *,
vtkIdType, vtkIdType *,
vtkIdType *, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::ReduceVoidArray(const void *, void *,
vtkIdType, int, int, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::ReduceVoidArray(const void *, void *,
vtkIdType, int, Operation *, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::AllReduceVoidArray(const void *, void *,
vtkIdType, int, int)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
int vtkSocketCommunicator::AllReduceVoidArray(const void *, void *,
vtkIdType, int, Operation *)
{
vtkErrorMacro("Collective operations not supported on sockets.");
return 0;
}
//-----------------------------------------------------------------------------
int vtkSocketCommunicator::GetVersion()
{
return vtkSocketCommunicatorHashId;
}
|