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
SimpleRequestHandler.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 #ifndef SIMPLE_REQUEST_HANDLER_H
20 #define SIMPLE_REQUEST_HANDLER_H
21 
22 #include "PDBCommunicator.h"
23 #include "PDBCommWork.h"
25 #include "PDBBuzzer.h"
26 #include <memory>
27 
28 // This template is used to make a simple piece of work that accepts an object of type RequestType
29 // from the client,
30 // processes the request, then sends the response back via a communicator. The constructor for the
31 // class
32 // takes as an argument the lambda that is to be used to process the RequestType object
33 namespace pdb {
34 
35 template <class RequestType>
37 
38 public:
39  // this accepts the lambda that is used to process the RequestType object
40  SimpleRequestHandler(std::function<std::pair<bool, std::string>(Handle<RequestType>,
41  PDBCommunicatorPtr)> useMe) {
42  processRequest = useMe;
43  }
44 
46  return std::make_shared<SimpleRequestHandler<RequestType>>(processRequest);
47  }
48 
49  void execute(PDBBuzzerPtr callerBuzzer) {
50 
51  // first, get the request
53  PDBLoggerPtr myLogger = getLogger();
54  bool success;
55  std::string errMsg;
56  size_t objectSize = myCommunicator->getSizeOfNextObject();
57  myLogger->debug(std::string("SimpleRequestHandle: to receive object with size=") +
58  std::to_string(objectSize));
59  if (objectSize == 0) {
60  std::cout << "SimpleRequestHandler: object size=0" << std::endl;
61  myLogger->error("SimpleRequestHandler: object size=0");
62  callerBuzzer->buzz(PDBAlarm::GenericError);
63  return;
64  }
65  void* memory = malloc(myCommunicator->getSizeOfNextObject());
66  {
67  Handle<RequestType> request =
68  myCommunicator->getNextObject<RequestType>(memory, success, errMsg);
69 
70  if (!success) {
71  myLogger->error("SimpleRequestHandler: tried to get the next object and failed; " +
72  errMsg);
74  free(memory);
75  callerBuzzer->buzz(PDBAlarm::GenericError);
76  return;
77  }
78 
79  std::pair<bool, std::string> res = processRequest(request, myCommunicator);
80  if (!res.first) {
81  myLogger->error("SimpleRequestHandler: tried to process the request and failed; " +
82  errMsg);
84  free(memory);
85  callerBuzzer->buzz(PDBAlarm::GenericError);
86  return;
87  }
88 
89  myLogger->info("SimpleRequestHandler: finished processing requet.");
90  free(memory);
91  callerBuzzer->buzz(PDBAlarm::WorkAllDone);
92  }
93  return;
94  }
95 
96 private:
97  function<pair<bool, std::string>(Handle<RequestType>, PDBCommunicatorPtr)> processRequest;
98 };
99 }
100 
101 #endif
SimpleRequestHandler(std::function< std::pair< bool, std::string >(Handle< RequestType >, PDBCommunicatorPtr)> useMe)
void execute(PDBBuzzerPtr callerBuzzer)
function< pair< bool, std::string >Handle< RequestType >, PDBCommunicatorPtr)> processRequest
PDBCommunicatorPtr getCommunicator()
Definition: PDBCommWork.cc:28
std::shared_ptr< PDBCommunicator > PDBCommunicatorPtr
PDBLoggerPtr getLogger()
Definition: PDBWork.cc:36
shared_ptr< PDBBuzzer > PDBBuzzerPtr
Definition: PDBBuzzer.h:32
std::shared_ptr< PDBCommWork > PDBCommWorkPtr
Definition: PDBCommWork.h:37
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40
PDBCommunicatorPtr myCommunicator
Definition: PDBCommWork.h:68