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
CompositeRequest.cc
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 #ifndef COMPOSITE_REQUEST_CC
20 #define COMPOSITE_REQUEST_CC
21 
22 #include "InterfaceFunctions.h"
24 
25 namespace pdb {
26 
27 template <class RequestType, class ResponseType, class ReturnType, class... RequestTypeParams>
28 ReturnType compositeRequest(
29  PDBLoggerPtr myLogger,
30  int port,
31  std::string address,
32  ReturnType onErr,
33  size_t bytesForRequest,
34  function<ReturnType(Handle<ResponseType>, PDBCommunicator)> processResponse,
35  RequestTypeParams&&... args) {
36 
37  PDBCommunicator temp;
38  string errMsg;
39  bool success;
40 
41  if (temp.connectToInternetServer(myLogger, port, address, errMsg)) {
42  myLogger->error(errMsg);
43  myLogger->error("compositeRequest: not able to connect to server.\n");
44  return onErr;
45  }
46 
47  // build the request
48  const UseTemporaryAllocationBlock tempBlock{bytesForRequest};
49  Handle<RequestType> request = makeObject<RequestType>(args...);
50  ;
51  if (!temp.sendObject(request, errMsg)) {
52  myLogger->error(errMsg);
53  myLogger->error("compositeRequest: not able to send request to server.\n");
54  return onErr;
55  }
56 
57  // get the response and process it
58  ReturnType finalResult;
59  void* memory = malloc(temp.getSizeOfNextObject());
60  {
61  Handle<ResponseType> result = temp.getNextObject<ResponseType>(memory, success, errMsg);
62  if (!success) {
63  myLogger->error(errMsg);
64  myLogger->error("compositeRequest: not able to get next object over the wire.\n");
65  return onErr;
66  }
67 
68  finalResult = processResponse(result, temp);
69  }
70  free(memory);
71  return finalResult;
72 }
73 }
74 #endif
ReturnType compositeRequest(PDBLoggerPtr myLogger, int port, std::string address, ReturnType onErr, size_t bytesForRequest, function< ReturnType(Handle< ResponseType >, PDBCommunicator)> processResponse, RequestTypeParams &&...args)
bool sendObject(Handle< ObjType > &sendMe, std::string &errMsg)
bool connectToInternetServer(PDBLoggerPtr logToMeIn, int portNumber, std::string serverAddress, std::string &errMsg)
Handle< ObjType > getNextObject(void *readToHere, bool &success, std::string &errMsg)
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40