00001 #include "otsdaq-core/NetworkUtilities/ReceiverSocket.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutMacros.h"
00004 #include "otsdaq-core/NetworkUtilities/NetworkConverters.h"
00005
00006 #include <iostream>
00007 #include <sstream>
00008 #include <iomanip>
00009
00010 #include <arpa/inet.h>
00011 #include <sys/time.h>
00012
00013 using namespace ots;
00014
00015
00016
00017 ReceiverSocket::ReceiverSocket(std::string IPAddress, unsigned int port)
00018 : Socket(IPAddress, port)
00019 , addressLength_(sizeof(fromAddress_))
00020 , numberOfBytes_(0)
00021 , readCounter_ (0)
00022 {
00023 __COUT__ << "ReceiverSocket constructor " << IPAddress << ":" << port << __E__;
00024 }
00025
00026
00027
00028
00029 ReceiverSocket::ReceiverSocket(void)
00030 : addressLength_(sizeof(fromAddress_))
00031 , numberOfBytes_(0)
00032 , readCounter_ (0)
00033 {
00034 __COUT__ << "ReceiverSocket constructor" << __E__;
00035 }
00036
00037
00038 ReceiverSocket::~ReceiverSocket(void)
00039 {
00040 }
00041
00042
00043 int ReceiverSocket::receive(std::string& buffer, unsigned int timeoutSeconds,
00044 unsigned int timeoutUSeconds, bool verbose)
00045 {
00046 return receive(buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
00047 }
00048
00049
00050
00051
00052
00053 int ReceiverSocket::receive(std::string& buffer, unsigned long& fromIPAddress,
00054 unsigned short& fromPort, unsigned int timeoutSeconds, unsigned int timeoutUSeconds,
00055 bool verbose)
00056 {
00057
00058 std::lock_guard<std::mutex> lock(receiveMutex_);
00059
00060
00061 timeout_.tv_sec = timeoutSeconds;
00062 timeout_.tv_usec = timeoutUSeconds;
00063
00064 FD_ZERO(&fileDescriptor_);
00065 FD_SET(socketNumber_ , &fileDescriptor_);
00066 select(socketNumber_+1, &fileDescriptor_, 0, 0, &timeout_);
00067
00068 if(FD_ISSET(socketNumber_, &fileDescriptor_))
00069 {
00070 buffer.resize(maxSocketSize_);
00071 if ((numberOfBytes_ = recvfrom(socketNumber_, &buffer[0], maxSocketSize_, 0,
00072 (struct sockaddr *)&fromAddress_, &addressLength_)) == -1)
00073 {
00074 __COUT__ << "At socket with IPAddress: " << getIPAddress() << " port: " << getPort() << std::endl;
00075 __SS__ << "Error reading buffer from\tIP:\t";
00076 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
00077 fromIPAddress = fromAddress_.sin_addr.s_addr;
00078 fromPort = fromAddress_.sin_port;
00079 for(int i = 0; i < 4; i++)
00080 {
00081 ss << ((fromIPAddress << (i * 8)) & 0xff);
00082 if (i < 3)
00083 ss << ".";
00084 }
00085 ss << "\tPort\t" << ntohs(fromPort) << " IP " << fromIP << std::endl;
00086 __COUT__ << "\n" << ss.str();
00087 return -1;
00088 }
00089
00090
00091 fromIPAddress = fromAddress_.sin_addr.s_addr;
00092 fromPort = fromAddress_.sin_port;
00093
00094
00095
00096
00097
00098
00099
00100
00101 buffer.resize(numberOfBytes_);
00102 readCounter_ = 0;
00103
00104 if(verbose)
00105 {
00106 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
00107
00108 __COUT__ << "Receiving " <<
00109 " at: " << getIPAddress() <<
00110 ":" << getPort() <<
00111 " from: " << fromIP;
00112 std::cout << ":" << ntohs(fromPort) <<
00113 " size: " << buffer.size() << std::endl;
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 }
00127 }
00128 else
00129 {
00130 ++readCounter_;
00131
00132 if(verbose)
00133 __COUT__
00134 << "No new messages for " << timeoutSeconds+timeoutUSeconds/1000.
00135 << "s (Total " << readCounter_*(timeoutSeconds+timeoutUSeconds/1000.)
00136 << "s). Read request timed out receiving on " <<
00137 " " << getIPAddress() <<
00138 ":" << getPort()
00139 << std::endl;
00140 return -1;
00141 }
00142
00143 return 0;
00144 }
00145
00146
00147 int ReceiverSocket::receive(std::vector<uint32_t>& buffer, unsigned int timeoutSeconds, unsigned int timeoutUSeconds, bool verbose)
00148 {
00149 return receive(buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
00150 }
00151
00152
00153
00154
00155
00156 int ReceiverSocket::receive(std::vector<uint32_t>& buffer, unsigned long& fromIPAddress, unsigned short& fromPort, unsigned int timeoutSeconds, unsigned int timeoutUSeconds, bool verbose)
00157 {
00158
00159 std::lock_guard<std::mutex> lock(receiveMutex_);
00160
00161
00162 timeout_.tv_sec = timeoutSeconds;
00163 timeout_.tv_usec = timeoutUSeconds;
00164
00165 FD_ZERO(&fileDescriptor_);
00166 FD_SET(socketNumber_ , &fileDescriptor_);
00167 select(socketNumber_+1, &fileDescriptor_, 0, 0, &timeout_);
00168 __COUT__ << "Is this a successful reeeaaad???" << std::endl;
00169
00170 if(FD_ISSET(socketNumber_, &fileDescriptor_))
00171 {
00172 buffer.resize(maxSocketSize_/sizeof(uint32_t));
00173 if ((numberOfBytes_ = recvfrom(socketNumber_, &buffer[0], maxSocketSize_, 0, (struct sockaddr *)&fromAddress_, &addressLength_)) == -1)
00174 {
00175 __COUT__ << "At socket with IPAddress: " << getIPAddress() << " port: " << getPort() << std::endl;
00176 __SS__ << "Error reading buffer from\tIP:\t";
00177 for(int i = 0; i < 4; i++)
00178 {
00179 ss << ((fromIPAddress << (i * 8)) & 0xff);
00180 if (i < 3)
00181 ss << ".";
00182 }
00183 ss << "\tPort\t" << fromPort << std::endl;
00184 __COUT__ << "\n" << ss.str();
00185 return -1;
00186 }
00187
00188
00189 fromIPAddress = fromAddress_.sin_addr.s_addr;
00190 fromPort = fromAddress_.sin_port;
00191
00192
00193
00194
00195
00196
00197
00198
00199 buffer.resize(numberOfBytes_/sizeof(uint32_t));
00200 readCounter_ = 0;
00201 }
00202 else
00203 {
00204 ++readCounter_;
00205 struct sockaddr_in sin;
00206 socklen_t len = sizeof(sin);
00207 getsockname(socketNumber_, (struct sockaddr *)&sin, &len);
00208
00209 if(verbose)
00210 __COUT__ << __COUT_HDR_FL__
00211 << "No new messages for " << timeoutSeconds+timeoutUSeconds/1000.
00212 << "s (Total " << readCounter_*(timeoutSeconds+timeoutUSeconds/1000.)
00213 << "s). Read request timed out for port: " << ntohs(sin.sin_port)
00214 << std::endl;
00215 return -1;
00216 }
00217 __COUT__ << "This a successful reeeaaad" << std::endl;
00218 return 0;
00219 }