A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBCommunicator.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 /*
20  * File: PDBCommunicator.h
21  * Author: Chris
22  *
23  * Created on September 26, 2015, 9:01 AM
24  */
25 
26 #ifndef PDBCOMMUNICATOR_H
27 #define PDBCOMMUNICATOR_H
28 
29 #include <memory>
30 
31 #include "Handle.h"
32 #include "PDBLogger.h"
33 #include <stdlib.h>
34 #include <cstring>
35 
36 // This class the encoding/decoding of IPC sockets messages in PDB
37 namespace pdb {
38 
39 // create a smart pointer for PDBCommunicator objects
41 typedef std::shared_ptr<PDBCommunicator> PDBCommunicatorPtr;
42 
44 
45 public:
46  // here are the ways that you can set up a PDBCommunicator...
47  // the first two assume that the PDBCommunicator is on the server side:
48 
49  // #1, you can connect to a Unix domain socket that is pointed at a file (this returns a true
50  // if there is an error, and in this case it sets errMsg to describe the error)
51  bool pointToFile(PDBLoggerPtr logToMeIn, int socketFDIn, std::string& errMsg);
52 
53  // #2, you can connect to an internet socket...
54  bool pointToInternet(PDBLoggerPtr logToMeIn, int socketFDIn, std::string& errMs);
55 
56  // the next two assume we are on the client side:
57 
58  // #3 connect to a server on the internet...
60  int portNumber,
61  std::string serverAddress,
62  std::string& errMsg);
63 
64  // #4, connect to a local server via a UNIX domain socket
65  bool connectToLocalServer(PDBLoggerPtr logToMeIn, std::string fName, std::string& errMsg);
66 
67  // see the size of an object that someone is sending us; blocks until the next object shows up
68  size_t getSizeOfNextObject();
69 
70  // gets the next object over the communicator... reads the object to the specified location
71  template <class ObjType>
72  Handle<ObjType> getNextObject(void* readToHere, bool& success, std::string& errMsg);
73 
74  // gets the next object over the communicator... reads the object to a temporary allocator
75  // with just enough RAM to store the object
76  template <class ObjType>
77  Handle<ObjType> getNextObject(bool& success, std::string& errMsg);
78 
79  // sends an object over the communication channel... return false on error
80  template <class ObjType>
81  bool sendObject(Handle<ObjType>& sendMe, std::string& errMsg);
82 
83  // sends a bunch of binary data over a channel
84  bool sendBytes(void* data, size_t size, std::string& errMsg);
85 
86  // receives a bunch of binary data over a channel
87  bool receiveBytes(void* data, std::string& errMsg);
88 
89  // note that the file descriptor corresponding to the socket is always closed by the destructor!
90  virtual ~PDBCommunicator();
91 
92  // default constructor
94 
95  // gets the type of the next object (calls getTypeID () on it)
96  int16_t getObjectTypeID();
97 
98  void setNeedsToDisconnect(bool option);
99 
100  int getSocketFD();
101 
102  bool isSocketClosed();
103 
104  bool isLongConnection();
105 
107 
108  bool reconnect(std::string& errMsg);
109 
110 private:
111  // write from start to end to the output socket
112  bool doTheWrite(char* start, char* end);
113 
114  // read the message data from socket
115  bool doTheRead(char* dataIn);
116 
117  // the size of the next message; keep these two declarations together so they can be read into
118  // at once!
119  size_t msgSize;
120 
121  // this points to the location that we read/write from
122  int socketFD;
123 
124  // for logging
126 
127  // remember whether we have read the size of the next message or not
129 
130  // record the type of the next object
131  int16_t nextTypeID;
132 
133  // This is for automatic connection tear-down.
134  // Moved this logic from Chris' message-based communication framework to here.
136 
137  // JiaNote: to add logic to support long connection
139 
141 
142 
143  // JiaNote: to add logic to support reconnection
145 
146  std::string serverAddress;
147 
148  std::string fileName;
149 
151 };
152 }
153 
154 #include "CommunicatorTemplates.cc"
155 
156 #endif /* PDBCOMMUNICATOR_H */
bool doTheWrite(char *start, char *end)
bool reconnect(std::string &errMsg)
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)
bool sendBytes(void *data, size_t size, std::string &errMsg)
Handle< ObjType > getNextObject(void *readToHere, bool &success, std::string &errMsg)
bool receiveBytes(void *data, std::string &errMsg)
bool pointToFile(PDBLoggerPtr logToMeIn, int socketFDIn, std::string &errMsg)
bool pointToInternet(PDBLoggerPtr logToMeIn, int socketFDIn, std::string &errMs)
std::shared_ptr< PDBCommunicator > PDBCommunicatorPtr
void setLongConnection(bool longConnection)
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40