24 #include "BuiltInObjectTypeIDs.h"
28 #include <netinet/in.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <sys/types.h>
42 #include <netinet/in.h>
66 struct sockaddr_in cli_addr;
67 socklen_t clilen =
sizeof(cli_addr);
68 bzero((
char*)&cli_addr,
sizeof(cli_addr));
69 logToMe->info(
"PDBCommunicator: about to wait for request from Internet");
70 socketFD = accept(socketFDIn, (
struct sockaddr*)&cli_addr, &clilen);
72 logToMe->error(
"PDBCommunicator: could not get FD to internet socket");
73 logToMe->error(strerror(errno));
74 errMsg =
"Could not get socket ";
75 errMsg += strerror(errno);
81 logToMe->info(
"PDBCommunicator: got request from Internet");
87 std::string serverAddress,
88 std::string& errMsg) {
126 logToMe->trace(
"PDBCommunicator: About to connect to the remote host");
145 struct addrinfo hints;
146 struct addrinfo *result, *rp;
148 sprintf(port,
"%d", portNumber);
150 memset(&hints, 0,
sizeof(
struct addrinfo));
151 hints.ai_family = AF_INET;
152 hints.ai_socktype = SOCK_STREAM;
154 hints.ai_protocol = 0;
156 int s = getaddrinfo(serverAddress.c_str(), port, &hints, &result);
158 logToMe->error(
"PDBCommunicator: could not get addr info");
159 logToMe->error(strerror(errno));
160 errMsg =
"Could not get addr info ";
161 errMsg += strerror(errno);
162 std::cout << errMsg << std::endl;
167 bool connected =
false;
168 for (rp = result; rp != NULL; rp = rp->ai_next) {
171 logToMe->trace(
"PDBCommunicator: creating socket....");
172 socketFD = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
176 if (::connect(
socketFD, rp->ai_addr, rp->ai_addrlen) != -1) {
181 std::cout <<
"Connection error, to retry..." << std::endl;
186 if (connected ==
true) {
192 logToMe->error(
"PDBCommunicator: could not connect to server: address info is null");
193 logToMe->error(strerror(errno));
194 errMsg =
"Could not connect to server: address info is null with ip=" + serverAddress +
195 ", and port=" + port;
196 errMsg += strerror(errno);
197 std::cout << errMsg << std::endl;
202 freeaddrinfo(result);
210 logToMe->trace(
"PDBCommunicator: Successfully connected to the remote host");
211 logToMe->trace(
"PDBCommunicator: Socket FD is " + std::to_string(
socketFD));
226 std::string& errMsg) {
229 struct sockaddr_un server;
232 socketFD = socket(AF_UNIX, SOCK_STREAM, 0);
234 logToMe->error(
"PDBCommunicator: could not get FD to local server socket");
235 logToMe->error(strerror(errno));
236 errMsg =
"Could not get FD to local server socket ";
237 errMsg += strerror(errno);
246 server.sun_family = AF_UNIX;
247 strcpy(server.sun_path, fName.c_str());
248 if (::connect(socketFD, (
struct sockaddr*)&server,
sizeof(
struct sockaddr_un)) < 0) {
249 logToMe->error(
"PDBCommunicator: could not connect to local server socket");
250 logToMe->error(strerror(errno));
251 errMsg =
"Could not connect to local server socket ";
252 errMsg += strerror(errno);
274 logToMe->trace(
"PDBCommunicator: about to wait for request from same machine");
275 socketFD = accept(socketFDIn, 0, 0);
277 logToMe->error(
"PDBCommunicator: could not get FD to local socket");
278 logToMe->error(strerror(errno));
279 errMsg =
"Could not get socket ";
280 errMsg += strerror(errno);
287 logToMe->trace(
"PDBCommunicator: got request from same machine");
301 logToMe->trace(
"PDBCommunicator: closing connection to the server");
304 logToMe->trace(
"PDBCommunicator: could not send close connection message");
354 logToMe->debug(
"getSizeOfNextObject: we've done this before");
360 int receivedBytes = 0;
361 int receivedTotal = 0;
362 int bytesToReceive = (int)(
sizeof(int16_t));
364 while (receivedTotal < (
int)(
sizeof(int16_t))) {
366 (
char*)((
char*)(&
nextTypeID) + receivedTotal *
sizeof(char)),
367 bytesToReceive)) < 0) {
369 std::string(
"PDBCommunicator: could not read next message type") + strerror(errno);
388 }
else if (receivedBytes == 0) {
390 "PDBCommunicator: the other side closed the socket when we try to read the type");
393 <<
"PDBCommunicator: the other side closed the socket when we try to get next type"
399 logToMe->info(
"PDBCommunicator: Retry to see whether network can recover");
400 PDB_COUT <<
"PDBCommunicator: Retry to see whether network can recover"
418 logToMe->info(std::string(
"PDBCommunicator: receivedBytes for reading type is ") +
419 std::to_string(receivedBytes));
420 receivedTotal = receivedTotal + receivedBytes;
421 bytesToReceive =
sizeof(int16_t) - receivedTotal;
425 logToMe->trace(
"PDBCommunicator: typeID of next object is " + std::to_string(
nextTypeID));
426 logToMe->trace(
"PDBCommunicator: getting the size of the next object:");
431 bytesToReceive = (int)(
sizeof(
size_t));
433 while (receivedTotal < (
int)(
sizeof(size_t))) {
435 (
char*)((
char*)(&
msgSize) + receivedTotal *
sizeof(char)),
436 bytesToReceive)) < 0) {
437 std::string errMsg =
"PDBCommunicator: could not read next message size:" +
438 std::to_string(receivedTotal) + strerror(errno);
456 }
else if (receivedBytes == 0) {
458 "PDBCommunicator: the other side closed the socket when we try to get next size");
461 <<
"PDBCommunicator: the other side closed the socket when we try to get next size"
466 PDB_COUT <<
"PDBCommunicator: Retry to see whether network can recover"
468 logToMe->info(
"PDBCommunicator: Retry to see whether network can recover");
485 logToMe->info(std::string(
"PDBCommunicator: receivedBytes for reading size is ") +
486 std::to_string(receivedBytes));
487 receivedTotal = receivedTotal + receivedBytes;
488 bytesToReceive =
sizeof(size_t) - receivedTotal;
492 logToMe->trace(
"PDBCommunicator: size of next object is " + std::to_string(
msgSize));
501 while (end != start) {
504 ssize_t numBytes = write(
socketFD, start, end - start);
507 logToMe->error(
"PDBCommunicator: error in socket write");
508 logToMe->trace(
"PDBCommunicator: tried to write " + std::to_string(end - start) +
510 logToMe->trace(
"PDBCommunicator: Socket FD is " + std::to_string(
socketFD));
511 logToMe->error(strerror(errno));
515 PDB_COUT <<
"PDBCommunicator: Retry to see whether network can recover"
517 logToMe->info(
"PDBCommunicator: Retry to see whether network can recover");
531 logToMe->trace(
"PDBCommunicator: wrote " + std::to_string(numBytes) +
" and are " +
532 std::to_string(end - start - numBytes) +
" to go!");
547 char* start = dataIn;
551 while (cur - start < (
long)
msgSize) {
553 ssize_t numBytes = read(
socketFD, cur, msgSize - (cur - start));
554 this->
logToMe->trace(
"PDBCommunicator: received bytes: " + std::to_string(numBytes));
558 "PDBCommunicator: error reading socket when trying to accept text message");
559 logToMe->error(strerror(errno));
574 }
else if (numBytes == 0) {
575 logToMe->info(
"PDBCommunicator: the other side closed the socket when we do the read");
576 PDB_COUT <<
"PDBCommunicator: the other side closed the socket when we doTheRead"
581 logToMe->info(
"PDBCommunicator: Retry to see whether network can recover");
582 PDB_COUT <<
"PDBCommunicator: Retry to see whether network can recover"
600 this->
logToMe->trace(
"PDBCommunicator: " + std::to_string(msgSize - (cur - start)) +
624 PDB_COUT <<
"To reconnect..." << std::endl;
642 errMsg =
"Can't reconnect because I'm a server";
std::string serverAddress
bool doTheWrite(char *start, char *end)
size_t getSizeOfNextObject()
bool reconnect(std::string &errMsg)
bool needToSendDisconnectMsg
bool connectToLocalServer(PDBLoggerPtr logToMeIn, std::string fName, std::string &errMsg)
bool doTheRead(char *dataIn)
void setNeedsToDisconnect(bool option)
bool sendObject(Handle< ObjType > &sendMe, std::string &errMsg)
bool connectToInternetServer(PDBLoggerPtr logToMeIn, int portNumber, std::string serverAddress, std::string &errMsg)
int16_t getObjectTypeID()
bool pointToFile(PDBLoggerPtr logToMeIn, int socketFDIn, std::string &errMsg)
bool pointToInternet(PDBLoggerPtr logToMeIn, int socketFDIn, std::string &errMs)
virtual ~PDBCommunicator()
void setLongConnection(bool longConnection)
std::shared_ptr< PDBLogger > PDBLoggerPtr