rasdaman complete source
rnpcommunication.hh
Go to the documentation of this file.
1 #ifndef RNPCOMMUNICATION_HH
2 #define RNPCOMMUNICATION_HH
3 /*
4 * This file is part of rasdaman community.
5 *
6 * Rasdaman community is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Rasdaman community is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
20 rasdaman GmbH.
21 *
22 * For more information please see <http://www.rasdaman.org>
23 * or contact Peter Baumann via <baumann@rasdaman.com>.
24 */
25 /****************************************************************************
26  *
27  *
28  * COMMENTS:
29  *
30  *
31  ****************************************************************************/
32 
33 #include "network/akgnetwork.hh"
35 #include <vector>
36 
37 #ifdef AFTERV52
38 #include <akglogging.hh>
39 #include <akg_exception.hh>
40 #else
41 #include <exception>
42 #endif
43 
44 namespace rnp
45 {
46 using namespace akg;
47 using namespace std;
48 
49 class RnpBaseClientComm;
50 
59 class RnpClientJob : public NbClientJob
60 {
61 public:
63  RnpClientJob() throw();
64 
68  void init(CommBuffer *transmitterBuffer, RnpBaseClientComm *newClientComm) throw();
69 
71  void processRequest() throw();
72 
75  CommBuffer* getAnswerBuffer() throw();
76 
78  bool isAnswerOk() throw();
79 
81  bool isInvalidFormat() throw();
82 
85  void clearAnswerBuffer() throw();
86 protected:
88  bool validateMessage() throw();
89  void executeOnWriteReady() throw();
90  void specificCleanUpOnTimeout() throw();
91  void executeOnReadError() throw();
92  void executeOnWriteError() throw();
93 
95  void resetState() throw();
96 private:
97  RnpBaseClientComm *clientCommPtr;
98 
99  RnpReceiver rnpReceiver;
100  bool answerOk;
101  bool invalidFormat;
102 };
103 
118 {
119 public:
122 
125  RnpBaseClientComm(const char* serverHost, int serverPort, RnpQuark serverType, RnpTransport::CarrierProtocol = RnpTransport::crp_Rnp) throw();
126 
128  virtual ~RnpBaseClientComm() throw();
129 
132  void setConnectionParameters(const char* serverHost, int serverPort) throw();
133 
135  void setCarrierProtocol(RnpTransport::CarrierProtocol) throw();
137  RnpTransport::CarrierProtocol getCarrierProtocol() throw();
138 
139  // callback from RnpClientJob
140  void jobIsReady() throw();
141 
142  // Set the maximal retry count (retries to connect to the server)
143  void setMaxRetry(unsigned int newMaxRetry);
144 
146  unsigned int getMaxRetry();
147 
148 protected:
149  // stuff for helping creating the function calls
152 
154  void startRequest(RnpQuark command, int transmitterBufferSize = RNP_DEFAULTBUFFERSIZE);
155 
160  bool sendRequestGetAnswer();
161 
166  virtual bool checkForExceptions();
167 
168 #ifdef AFTERV52
169  // reassembles and throws an AkgSerializableException. Returns if it isn't an Akg...
170  void reassembleAkgSerializable() throw(AkgSerializableException);
171 
172  // reassembles and throws a STL-exception. Returns only if it isn't a stl-exception
173  void reassembleStlException() throw(RnpStlException);
174 #endif
175  void clearAnswer() throw();
177 
182  void initDefaultCommunication() throw();
183 
184  // encoding and decoding
186  RnpTransmitter encoder;
187  CommBuffer transmitterBuffer; // to go, use internal of encoder
188 
189  // stuff for non blocking communication
190  RnpClientJob clientJob; // the client job
191  NbCommunicator *communicatorPtr; // the communicator to be used
192  NbCommunicator internalCommunicator; // an internal communicator, if you dont like that you put another one
193 
194  // connection parameters
195  const char* serverHost;
196  unsigned int serverPort;
197  unsigned int maxRetry;
198 
200  void printCurrentParameter() throw();
201 };
202 
203 
204 //############ Server side ###################################
205 
206 class RnpBaseServerComm;
207 
215 class RnpServerJob : public NbServerJob
216 {
217 public:
219  RnpServerJob() throw();
220 
223  void init(RnpBaseServerComm*) throw();
224 
226  void processRequest() throw();
227 
228 protected:
230  bool validateMessage() throw();
231  void executeOnAccept() throw();
232  void executeOnWriteReady() throw();
233  void specificCleanUpOnTimeout() throw();
234  void executeOnReadError() throw();
235  void executeOnWriteError() throw();
236 
237  void resetJob() throw();
238 
239  RnpBaseServerComm *serverCommPtr;
240 
242 
244 };
245 
261 {
262 public:
264  RnpBaseServerComm() throw();
265 
267  virtual ~RnpBaseServerComm() throw();
268 
271  bool setServerJobs(int nrOfServerJobs) throw();
272 
274  int countServerJobs() throw();
275 
277  void connectToCommunicator(NbCommunicator&);
278 
281  bool disconnectFromCommunicator() throw();
282 
284  void setTransmitterBufferSize(int) throw();
285 
287  int getTransmitterBufferSize() throw();
288 
295  virtual void processRequest(CommBuffer *receiverBuffer, CommBuffer *transmiterBuffer, RnpTransport::CarrierProtocol, RnpServerJob *callingJob) throw();
296 
299  void communicatorShouldExit() throw();
300 
301 protected:
304  virtual void decodeFragment() = 0;
305 
307  const char* getNextAsString(RnpQuark parameterType) const;
308 
310  int getNextAsInteger(RnpQuark parameterType) const;
311 
313  float getNextAsFloat(RnpQuark parameterType) const;
314 
316  double getNextAsDouble(RnpQuark parameterType) const;
317 
319  const void* getNextAsOpaque(RnpQuark parameterType) const;
320 
322  int getCurrentParameterLength() const throw();
323 #ifdef AFTERV52
324  void answerAkgSerializable(AkgSerializableException&) throw();
326 #endif
327  void answerSTLException(exception&) throw();
329 
331  void answerUnknownError() throw();
332 
334  void discardFragment() throw();
335 
337  void startOkAnswer() throw();
338 
340  void endOkAnswer() throw();
341 
344 
345 private:
348  virtual RnpServerJob* createJob();
349 
350  vector<RnpServerJob*> serverJob;
351 
352  int nrServerJobs;
353 
354  NbCommunicator *communicator;
355 
356  int transmitterBufferSize;
357 };
358 
359 } // namespace
360 #endif
Definition: rnpembedded.hh:155
RnpTransport::CarrierProtocol carrierProtocol
Definition: rnpcommunication.hh:151
int RnpQuark
The basic type used in RNP. It is always 32-bit long.
Definition: rnprotocol.hh:51
RnpTransmitter encoder
Definition: rnpcommunication.hh:343
Definition: akgnet_commbuffer.hh:39
Definition: rnpcommunication.hh:215
Definition: rnprotocol.hh:362
Definition: rnpembedded.hh:55
Definition: rnpembedded.hh:82
Definition: rnpcommunication.hh:44
Definition: rnpcommunication.hh:260
RnpProtocolDecoder decoder
Definition: rnpcommunication.hh:342
RnpBaseServerComm * serverCommPtr
Definition: rnpcommunication.hh:239
RnpReceiver rnpReceiver
Definition: rnpcommunication.hh:241
Definition: akgnet_nbcomm.hh:303
Definition: akgnet_nbcomm.hh:260
Definition: akgnet_nbcomm.hh:224
CarrierProtocol
Definition: rnpembedded.hh:52
Definition: rnpcommunication.hh:59
CommBuffer transmiterBuffer
Definition: rnpcommunication.hh:243
#define RNP_DEFAULTBUFFERSIZE
Definition: rnprotocol.hh:48
RnpQuark serverType
Definition: rnpcommunication.hh:150
Definition: rnpcommunication.hh:117
Definition: akgnet_commbuffer.hh:57