rasdaman complete source
akgnet_nbcomm.hh
Go to the documentation of this file.
1 /*
2 * This file is part of rasdaman community.
3 *
4 * Rasdaman community is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Rasdaman community is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
18 rasdaman GmbH.
19 *
20 * For more information please see <http://www.rasdaman.org>
21 * or contact Peter Baumann via <baumann@rasdaman.com>.
22 /
34 #ifndef AKGNET_NBCOMM_HH
35 #define AKGNET_NBCOMM_HH
36 
37 #include "akgnet_server.hh"
38 
39 namespace akg
40 {
41 
49 class NbJob
50 {
51 public:
55  static void setCurrentTime() throw();
56 
61  static void setTimeoutInterval(time_t x) throw();
62 
64  static time_t getTimeoutInterval() throw();
65 
66 public:
68  enum acceptStatus
69  {
70  acs_nopending = 0,
71  acs_Iambusy = 1,
72  acs_accepted = 2
73  };
75  enum workingStatus
76  {
77  wks_notdefined = 0,
78  wks_accepting = 1, // job is ready to accept a connection
79  wks_reading = 2, // job is reading data
80  wks_writing = 3, // job is writing data
81  wks_processing = 4 // job is processing the request
82  };
83 
84  virtual ~NbJob() throw();
86  workingStatus getStatus() throw();
87 
91  bool isOperationPending() throw();
92 
94  bool isAccepting() throw();
95 
97  bool isReading() throw();
98 
100  bool isWriting() throw();
101 
103  bool isProcessing() throw();
104 
108  virtual void initOnAttach(Selector *pselector) throw() =0;
109 
119  virtual acceptStatus acceptConnection(ListenSocket& listenSocket) throw() =0;
120 
128  bool readPartialMessage() throw();
129 
137  bool writePartialMessage() throw();
138 
140  void clearConnection() throw();
141 
143  int getSocket() throw();
144 
146  int getErrno() throw();
147  //######################################
154  virtual bool cleanUpIfTimeout() throw();
155 
161  virtual void processRequest() throw() =0;
162  //######################################
163 protected:
165  virtual bool validateMessage() throw() =0;
166 
168  virtual void executeOnAccept() throw();
169 
171  virtual void executeOnWriteReady() throw() =0;
172 
174  virtual void specificCleanUpOnTimeout() throw() =0;
175 
177  virtual void executeOnReadError() throw() =0;
178 
180  virtual void executeOnWriteError() throw() =0;
181  //######################################
182 protected:
184  NbJob(FileDescriptor&) throw() ;
185 
187  bool setReading() throw();
188 
190  bool setWriting() throw();
191  workingStatus status;
192 
196  FileDescriptor &fdRef;
197 
201  Selector *selectorPtr;
202 
205  CommBuffer *currentBufferPtr;
206 
207  // for timeout
208  time_t lastActionTime;
209 
211  void action() throw();
212 
213  static time_t timeOutInterv;
214  static time_t currentTime;
215 };
216 
217 /* Base class for generic non-blocking server jobs
218  */
219 
224 class NbServerJob : public NbJob
225 {
226 public:
228  NbServerJob() throw();
229 
233  void initOnAttach(Selector *pselector) throw();
234 
238  acceptStatus acceptConnection(ListenSocket& listenSocket) throw();
239 
241  SocketAddress getClientSocketAddress() throw();
242 
244  HostAddress getClientHostAddress() throw();
245 protected:
246 
248  void readyToWriteAnswer() throw();
249 
250  ServerSocket serverSocket;
251 };
252 
253 /* Base class for generic non-blocking client jobs
254  */
255 
260 class NbClientJob : public NbJob
261 {
262 public:
264  NbClientJob() throw();
265 
267  bool connectToServer(const char* serverHost, int serverPort) throw();
268 
269 
275  void initOnAttach(Selector *pselector) throw();
276 
281  acceptStatus acceptConnection(ListenSocket& listenSocket) throw();
282 protected:
283 
285  void readyToReadAnswer() throw();
286 
287  ClientSocket clientSocket;
288 };
289 
303 class NbCommunicator : public GenericServer
304 {
305 public:
307  NbCommunicator() throw();
308 
310  NbCommunicator(int newMaxJobs);
311 
313  ~NbCommunicator() throw();
314 
316  bool initJobs(int newMaxJobs);
317 
319  int getMaxJobs() throw();
320 
325  bool attachJob(NbJob&) throw();
326 
330  bool deattachJob(NbJob&) throw();
331 
333  bool runServer() throw();
334 
336  bool runClient() throw();
337 protected:
341  virtual bool executeBeforeSelect() throw();
342 
346  virtual bool executeAfterSelect() throw();
350  virtual bool executeOnTimeout() throw();
351 private:
352  typedef NbJob *JobPtr;
353 
354  JobPtr *jobPtr;
355  int maxJobs;
356 
363  bool mainLoop() throw();
364 
366  void dispatchReadRequest() throw();
367 
369  void dispatchWriteRequest() throw();
370 
372  void connectNewClients() throw();
373 
375  void lookForTimeout() throw();
376 
378  void processJobs() throw();
379 
384  bool mayExit() throw();
385 
386 };
387 
388 } //namespace
389 #endif
390