00001 #include "otsdaq-core/NetworkUtilities/ReceiverSocket.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.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
00024 }
00025
00026
00027
00028
00029 ReceiverSocket::ReceiverSocket(void)
00030 : addressLength_(sizeof(fromAddress_))
00031 , numberOfBytes_(0)
00032 , readCounter_ (0)
00033 {
00034
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, (struct sockaddr *)&fromAddress_, &addressLength_)) == -1)
00072 {
00073 __COUT__ << "At socket with IPAddress: " << getIPAddress() << " port: " << getPort() << std::endl;
00074 __SS__ << "Error reading buffer from\tIP:\t";
00075 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
00076 fromIPAddress = fromAddress_.sin_addr.s_addr;
00077 fromPort = fromAddress_.sin_port;
00078 for(int i = 0; i < 4; i++)
00079 {
00080 ss << ((fromIPAddress << (i * 8)) & 0xff);
00081 if (i < 3)
00082 ss << ".";
00083 }
00084 ss << "\tPort\t" << ntohs(fromPort) << " IP " << fromIP << std::endl;
00085 __COUT__ << "\n" << ss.str();
00086 return -1;
00087 }
00088
00089
00090 fromIPAddress = fromAddress_.sin_addr.s_addr;
00091 fromPort = fromAddress_.sin_port;
00092
00093
00094
00095
00096
00097
00098
00099
00100 buffer.resize(numberOfBytes_);
00101 readCounter_ = 0;
00102
00103 if(verbose)
00104 {
00105 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
00106
00107 __COUT__ << "Receiving " <<
00108 " at: " << getIPAddress() <<
00109 ":" << getPort() <<
00110 " from: " << fromIP;
00111 std::cout << ":" << ntohs(fromPort) <<
00112 " size: " << buffer.size() << std::endl;
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 }
00126 }
00127 else
00128 {
00129 ++readCounter_;
00130
00131 if(verbose)
00132 __COUT__
00133 << "No new messages for " << timeoutSeconds+timeoutUSeconds/1000.
00134 << "s (Total " << readCounter_*(timeoutSeconds+timeoutUSeconds/1000.)
00135 << "s). Read request timed out receiving on " <<
00136 " " << getIPAddress() <<
00137 ":" << getPort()
00138 << std::endl;
00139 return -1;
00140 }
00141
00142 return 0;
00143 }
00144
00145
00146 int ReceiverSocket::receive(std::vector<uint32_t>& buffer, unsigned int timeoutSeconds, unsigned int timeoutUSeconds, bool verbose)
00147 {
00148 return receive(buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
00149 }
00150
00151
00152
00153
00154
00155 int ReceiverSocket::receive(std::vector<uint32_t>& buffer, unsigned long& fromIPAddress, unsigned short& fromPort, unsigned int timeoutSeconds, unsigned int timeoutUSeconds, bool verbose)
00156 {
00157
00158 std::lock_guard<std::mutex> lock(receiveMutex_);
00159
00160
00161 timeout_.tv_sec = timeoutSeconds;
00162 timeout_.tv_usec = timeoutUSeconds;
00163
00164 FD_ZERO(&fileDescriptor_);
00165 FD_SET(socketNumber_ , &fileDescriptor_);
00166 select(socketNumber_+1, &fileDescriptor_, 0, 0, &timeout_);
00167 __COUT__ << "Is this a successful reeeaaad???" << std::endl;
00168
00169 if(FD_ISSET(socketNumber_, &fileDescriptor_))
00170 {
00171 buffer.resize(maxSocketSize_/sizeof(uint32_t));
00172 if ((numberOfBytes_ = recvfrom(socketNumber_, &buffer[0], maxSocketSize_, 0, (struct sockaddr *)&fromAddress_, &addressLength_)) == -1)
00173 {
00174 __COUT__ << "At socket with IPAddress: " << getIPAddress() << " port: " << getPort() << std::endl;
00175 __SS__ << "Error reading buffer from\tIP:\t";
00176 for(int i = 0; i < 4; i++)
00177 {
00178 ss << ((fromIPAddress << (i * 8)) & 0xff);
00179 if (i < 3)
00180 ss << ".";
00181 }
00182 ss << "\tPort\t" << fromPort << std::endl;
00183 __COUT__ << "\n" << ss.str();
00184 return -1;
00185 }
00186
00187
00188 fromIPAddress = fromAddress_.sin_addr.s_addr;
00189 fromPort = fromAddress_.sin_port;
00190
00191
00192
00193
00194
00195
00196
00197
00198 buffer.resize(numberOfBytes_/sizeof(uint32_t));
00199 readCounter_ = 0;
00200 }
00201 else
00202 {
00203 ++readCounter_;
00204 struct sockaddr_in sin;
00205 socklen_t len = sizeof(sin);
00206 getsockname(socketNumber_, (struct sockaddr *)&sin, &len);
00207
00208 if(verbose)
00209 __COUT__ << __COUT_HDR_FL__
00210 << "No new messages for " << timeoutSeconds+timeoutUSeconds/1000.
00211 << "s (Total " << readCounter_*(timeoutSeconds+timeoutUSeconds/1000.)
00212 << "s). Read request timed out for port: " << ntohs(sin.sin_port)
00213 << std::endl;
00214 return -1;
00215 }
00216 __COUT__ << "This a successful reeeaaad" << std::endl;
00217 return 0;
00218 }