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
PDBServer.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  * File: PDBServer.h
20  * Author: Chris
21  *
22  * Created on September 25, 2015, 5:04 PM
23  */
24 
25 #ifndef PDBSERVER_H
26 #define PDBSERVER_H
27 
28 #include <memory>
29 namespace pdb {
30 
31 // create a smart pointer for PDBServer objects
32 class PDBServer;
33 typedef std::shared_ptr<PDBServer> PDBServerPtr;
34 }
35 
36 #include "PDBAlarm.h"
37 #include "PDBCommWork.h"
38 #include "PDBLogger.h"
39 #include "PDBWork.h"
40 #include "PDBCommunicator.h"
41 #include <string>
42 #include <map>
43 
44 // This class encapsulates a multi-threaded sever in PDB. The way it works is that one simply
45 // registers
46 // an event handler (encapsulated inside of a PDBWorkPtr); whenever there is a new connection that
47 // comes
48 // over the given port (or file in the case of a local socket) a PWBWorker is asked to handle the
49 // connection using the appropriate port using a cloned version of the specified PDBWork object.
50 //
51 
52 namespace pdb {
53 
54 class ServerFunctionality;
55 
56 class PDBServer {
57 public:
58  // server set up for comminication over the internet, with the specified number of threads
59  // (PDBWorker objects) to handle connections to the server.
60  PDBServer(int portNumberIn, int numConnections, PDBLoggerPtr myLogger);
61 
62  // server set up for communication using the local file system, with the specified number of
63  // threads
64  // to handle connections to the server. Log to the specified logger.
65  PDBServer(string fileName, int numConnections, PDBLoggerPtr myLogger);
66 
67  // a server has many possible functionalities... storage, catalog client, query planning, etc.
68  // to create and add a functionality, call this. The Functionality class must derive from the
69  // ServerFunctionality class, which means that it must implement the pure virtual function
70  // RegisterHandlers (PDBServer &) that registers any special handlers that the class needs in
71  // order to
72  // perform its required tasks
73  template <class Functionality, class... Args>
74  void addFunctionality(Args&&... args);
75 
76  // gets access to a particular functionality... this might be called (for example)
77  template <class Functionality>
78  Functionality& getFunctionality();
79 
80  // asks the server to handle a particular request coming over the wire with the particular work
81  // type
82  void registerHandler(int16_t typeID, PDBCommWorkPtr handledBy);
83 
84  // like registerHandler but repeat the work in a time interval
85  // TODO: to be implemented later.
86  // void registerTimedHandler (uint32_t intervalInMilliseconds, PDBWorkPtr handledBy);
87 
88  // starts the server---this creates all of the threads and lets the server start taking
89  // requests; this
90  // call will never return. Note that if runMeAtStart is not null, then runMeAtStart is executed
91  // before the server starts handling requests
92  void startServer(PDBWorkPtr runMeAtStart);
93 
94  // asks the server to signal all of the threads activily handling connections that a certain
95  // event
96  // has occured; this effectively just has us call PDBWorker.signal (signalWithMe) for all of the
97  // workers that are currently handling requests. Any that indicate that they have died as a
98  // result
99  // of the signal are forgotten (allowed to go out of scope) and then replaced with a new
100  // PDBWorker
101  // object
102  void signal(PDBAlarm signalWithMe);
103 
104  // tell the server to start listening for people who want to connect
105  void listen();
106 
107  // asks us to handle one request that is coming over the given PDBCommunicator; return true if
108  // this
109  // is not the last request over this PDBCommunicator object; buzzMeWhenDone is sent to the
110  // worker that
111  // is spawned to handle the request
112  bool handleOneRequest(PDBBuzzerPtr buzzMeWhenDone, PDBCommunicatorPtr myCommunicator);
113 
114  void stop(); // added by Jia
115 
116  // Someone added this, but it is BAD!! This should not be exposed
117  // Jia: I understand it is bad, however we need to create threads in a handler, and I feel you
118  // do
119  // not want multiple worker queue in one process. So I temprarily enabled this...
121 
122  // gets access to logger
124 
125 private:
126  // used to ask the most recently-added functionality to register its handlers
128 
129  // when we get a message over the input socket, we'll handle it using the registered handler
130  map<int16_t, PDBCommWorkPtr> handlers;
131 
132  // this is where all of our workers to handle the server requests live
134 
135  // handles a request using the given PDBCommunicator to obtain the data
136  void handleRequest(PDBCommunicatorPtr myCommunicator);
137 
138  // true when the server is done
139  bool allDone;
140 
141  // the port number for an internet server
143 
144  // the number of connections to allow simultanously (a new thread is created for each of these)
146 
147  // the file to use for a UNIX file-based connection
148  string unixFile;
149 
150  // where to log to
152 
153  // true if this is an internet server
155 
156  // used to run the server
157  pthread_t listenerThread;
158 
159  // this is the socket we are listening to
160  int sockFD;
161 
162  // this maps the name of a functionality class to a position
163  std::map<std::string, int> allFunctionalityNames;
164 
165  // this gives us each of the functionalities that the server can perform
166  std::vector<shared_ptr<ServerFunctionality>> allFunctionalities;
167 };
168 }
169 
170 #include "ServerTemplates.cc"
171 
172 #endif /* PDBSERVER_H */
map< int16_t, PDBCommWorkPtr > handlers
Definition: PDBServer.h:130
void startServer(PDBWorkPtr runMeAtStart)
Definition: PDBServer.cc:348
shared_ptr< PDBWork > PDBWorkPtr
Definition: PDBWork.h:47
pthread_t listenerThread
Definition: PDBServer.h:157
PDBLoggerPtr getLogger()
Definition: PDBServer.cc:233
PDBWorkerQueuePtr getWorkerQueue()
Definition: PDBServer.cc:228
void signal(PDBAlarm signalWithMe)
Definition: PDBServer.cc:344
PDBLoggerPtr myLogger
Definition: PDBServer.h:151
std::shared_ptr< PDBCommunicator > PDBCommunicatorPtr
bool handleOneRequest(PDBBuzzerPtr buzzMeWhenDone, PDBCommunicatorPtr myCommunicator)
Definition: PDBServer.cc:246
void listen()
Definition: PDBServer.cc:93
int numConnections
Definition: PDBServer.h:145
PDBServer(int portNumberIn, int numConnections, PDBLoggerPtr myLogger)
Definition: PDBServer.cc:49
shared_ptr< PDBBuzzer > PDBBuzzerPtr
Definition: PDBBuzzer.h:32
shared_ptr< PDBWorkerQueue > PDBWorkerQueuePtr
std::shared_ptr< PDBServer > PDBServerPtr
Definition: PDBServer.h:32
void registerHandler(int16_t typeID, PDBCommWorkPtr handledBy)
Definition: PDBServer.cc:81
void handleRequest(PDBCommunicatorPtr myCommunicator)
Definition: PDBServer.cc:237
std::map< std::string, int > allFunctionalityNames
Definition: PDBServer.h:163
std::vector< shared_ptr< ServerFunctionality > > allFunctionalities
Definition: PDBServer.h:166
PDBAlarm
Definition: PDBAlarm.h:28
std::shared_ptr< PDBCommWork > PDBCommWorkPtr
Definition: PDBCommWork.h:37
string unixFile
Definition: PDBServer.h:148
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40
PDBWorkerQueuePtr myWorkers
Definition: PDBServer.h:133
Functionality & getFunctionality()
void addFunctionality(Args &&...args)
void registerHandlersFromLastFunctionality()
Definition: PDBServer.cc:374