Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <sys/types.h>
00015 #include <sys/ipc.h>
00016 #include <sys/msg.h>
00017 #include <sys/socket.h>
00018 #include <errno.h>
00019 #include <string.h>
00020
00021
00022 #include "FWCore/Framework/interface/MessageReceiverForSource.h"
00023 #include "FWCore/Utilities/interface/Exception.h"
00024 #include "MessageForSource.h"
00025 #include "MessageForParent.h"
00026
00027 using namespace edm::multicore;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 MessageReceiverForSource::MessageReceiverForSource(int parentSocket, int parentPipe) :
00040 m_parentSocket(parentSocket),
00041 m_parentPipe(parentPipe),
00042 m_maxFd(parentPipe),
00043 m_startIndex(0),
00044 m_numberOfConsecutiveIndices(0),
00045 m_numberToSkip(0)
00046 {
00047 if (parentSocket > parentPipe) {
00048 m_maxFd = parentSocket;
00049 }
00050 m_maxFd++;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 void
00093 MessageReceiverForSource::receive()
00094 {
00095 unsigned long previousStartIndex = m_startIndex;
00096 unsigned long previousConsecutiveIndices = m_numberOfConsecutiveIndices;
00097
00098
00099 ssize_t rc, rc2;
00100
00101 {
00102 MessageForParent parentMessage;
00103 errno = 0;
00104
00105
00106 if ((rc = send(m_parentSocket, reinterpret_cast<char *>(&parentMessage), parentMessage.sizeForBuffer(), 0)) != static_cast<int>(parentMessage.sizeForBuffer())) {
00107 m_numberOfConsecutiveIndices=0;
00108 m_startIndex=0;
00109 if (rc == -1) {
00110 throw cms::Exception("MulticoreCommunicationFailure") << "failed to send data to controller: errno=" << errno << " : " << strerror(errno);
00111 }
00112 throw cms::Exception("MulticoreCommunicationFailure") << "Unable to write full message to controller (" << rc << " of " << parentMessage.sizeForBuffer() << " byte written)";
00113 }
00114 }
00115
00116 {
00117 MessageForSource message;
00118 fd_set readSockets, errorSockets;
00119 errno = 0;
00120
00121 do {
00122
00123 FD_ZERO(&errorSockets); FD_SET(m_parentPipe, &errorSockets); FD_SET(m_parentSocket, &errorSockets);
00124 FD_ZERO(&readSockets); FD_SET(m_parentSocket, &readSockets);
00125 struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0;
00126 while (((rc = select(m_maxFd, &readSockets, NULL, &errorSockets, &tv)) < 0) && (errno == EINTR)) {}
00127
00128 if (rc == 0) {
00129
00130 while (((rc2 = write(m_parentPipe, "\0", 1)) < 0) && (errno == EINTR)) {}
00131 if (rc2 < 0) {
00132 if (errno == EPIPE) {
00133 throw cms::Exception("MulticoreCommunicationFailure") << "Parent process appears to be dead.";
00134 }
00135 throw cms::Exception("MulticoreCommunicationFailure") << "Cannot write to parent: errno=" << errno << " : " << strerror(errno);
00136 }
00137 }
00138 } while (rc == 0);
00139
00140
00141 if (FD_ISSET(m_parentSocket, &errorSockets) || FD_ISSET(m_parentPipe, &errorSockets)) {
00142 throw cms::Exception("MulticoreCommunicationFailure") << "Cannot communicate with parent (fd=" << m_parentSocket << "): errno=" << errno << " : " << strerror(errno);
00143 }
00144
00145 if (!FD_ISSET(m_parentSocket, &readSockets)) {
00146 throw cms::Exception("MulticoreCommunicationFailure") << "Unable to read from parent socket";
00147 }
00148
00149
00150
00151
00152
00153 rc = recv(m_parentSocket, &message, MessageForSource::sizeForBuffer(), 0);
00154 if (rc < 0) {
00155 m_numberOfConsecutiveIndices=0;
00156 m_startIndex=0;
00157 throw cms::Exception("MulticoreCommunicationFailure")<<"failed to receive data from controller: errno="<<errno<<" : "<<strerror(errno);
00158 }
00159
00160 if (rc != (int)MessageForSource::sizeForBuffer()) {
00161 m_numberOfConsecutiveIndices=0;
00162 m_startIndex=0;
00163 throw cms::Exception("MulticoreCommunicationFailure")<<"Incorrect number of bytes received from controller (got " << rc << ", expected " << MessageForSource::sizeForBuffer() << ")";
00164 }
00165
00166 m_startIndex = message.startIndex;
00167 m_numberOfConsecutiveIndices = message.nIndices;
00168 m_numberToSkip = m_startIndex-previousStartIndex-previousConsecutiveIndices;
00169
00170
00171 }
00172
00173 return;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182