rasdaman complete source
rnprotocol.hh
Go to the documentation of this file.
1 #ifndef RNPROTOCOL_HH
2 #define RNPROTOCOL_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"
34 #ifdef AFTERV52
35 #include "akglogging.hh"
36 #include "rnpexception.hh"
37 #else
38 #define AKGLOGLN(a,b,c)
39 #endif
40 
41 namespace rnp
42 {
43 
44 //using namespace akg;
45 
48 #define RNP_DEFAULTBUFFERSIZE 1024
49 
51 typedef int RnpQuark;
52 
59 class Rnp
60 {
61 public:
65  static const RnpQuark rnpProtocolId;//always little endian!!!
66 
68  {
69  bigEndian = 0,
71  };
72 
74  {
75  fgt_None = 0,
80  //...
81  // to know how many where defined
83  };
84  enum DataType
85  {
86  dtt_None = 0,
88  dtt_Int32 = 2,
92  dtt_NullPtr = 6, // NULL pointer
93  //...
94  // to know how many where defined
96  };
97 
98  // the type of the error, so the receiver can rebuild it
99  enum ErrorType
100  {
101  ert_Unknown = 0, // unknown error type, no exception, something else
102  ert_StlException, // ... has a "what()" - member
103  ert_AkgSerializable, // akg serializable exception, we don't carry usual exceptions!
104  ert_Other, // other exceptions
105 
107  };
108 
110  {
111  erp_None = 0,
112  erp_whatValue = 1, // used by "exception"
113  erp_Key = 2, // key of "akgexception"
114  erp_Value = 3, // value of "akgexception"
115 
117  };
118 
120  static const char* getFragmentTypeName(RnpQuark) throw();
121  static const char* getDataTypeName(RnpQuark) throw();
122  static const char* getEndiannessName(Endianness) throw();
123  static const char* getErrorTypeName(RnpQuark) throw();
124  static const char* getErrorParamName(RnpQuark) throw();
125 
128  virtual const char* getParameterTypeName(RnpQuark) const throw() =0;
129  virtual const char* getCommandName(RnpQuark) const throw() =0;
130 
132  static RnpQuark swapBytes(RnpQuark) throw();
133  static Endianness detectHostEndianness() throw();
134 
135 #ifdef AFTERV52
136  static AkgLogConnection logConn;
138 #endif
139 protected:
141  static const char* undefValue;
142  static const char* endiannessNames[2];
143  static const char* fragmentTypeNames[fgt_HowMany];
144  static const char* dataTypeNames[dtt_HowMany];
145  static const char* errorTypeNames[ert_HowMany];
146  static const char* errorParamNames[erp_HowMany];
147 };
148 
149 struct RnpFragmentHeader;
150 
153 struct RnpHeader
154 {
155  RnpQuark protocolId;
161  RnpQuark nrFragments;
162  RnpQuark serverType;
163  RnpQuark authInfoStart;
164  RnpQuark authInfoLength;
165  RnpQuark comprInfoStart;
166  RnpQuark comprInfoLength;
167  RnpQuark dataStart;
168  RnpQuark dataLength;
169  RnpQuark _unused[5];
170  // sizeof = 64
171 
173  bool isRnpMessage() const throw();
174 
176  Rnp::Endianness getEndianness() const throw();
177 
179  RnpQuark getTotalLength() const throw();
180 
183  bool changeEndianness(Rnp::Endianness) throw();
184 
186  RnpFragmentHeader* getFirstFragment() const throw();
187 };
188 
193 {
195  RnpQuark paramType;
196 
198  RnpQuark dataType;
199 
201  RnpQuark dataLength;
202 
204  // (Length is always 4bytes aligned!, at least Sun requires it)
205  RnpQuark totalLength;
206 
208  RnpParameter* getNextParameter() const throw();
209 
211  void* getData() const throw();
212 
214  RnpQuark getDataLength() const throw();
215 
220  void changeToHostEndianness() throw();
221  void changeToPartnerEndianness() throw();
222 
223  RnpQuark computeTotalAlignedLength() throw();
224  RnpQuark getPaddLength() const throw();
225 };
226 
231 {
233  RnpQuark fragmType;
234 
236  RnpQuark command;
237 
239  RnpQuark nrParams;
240 
242  RnpQuark totalLength;
243 
245  RnpFragmentHeader* getNextFragment() const throw();
246 
248  RnpParameter* getFirstParameter() const throw();
249 
252  void changeEndianness() throw();
253 };
254 
265 {
266 public:
268  RnpProtocolEncoder() throw();
270  ~RnpProtocolEncoder() throw();
271 
273  void setBuffer(akg::CommBuffer*) throw();
274 
276  bool allocateBuffer(int maxMessageLength) throw();
277 
281  bool adjustBufferSize(int differenceSize) throw();
282 
283  int getBufferSize() throw();
284 
294  void startMessage(RnpQuark serverType, int carrierHeaderSize = 0) throw();
295 
300  void setDesiredEndianness(Rnp::Endianness) throw();
301 
305  void setFinalEndianness(Rnp::Endianness) throw();
306 
308  void startFragment(Rnp::FragmentType, RnpQuark command) throw();
309 
311  void addStringParameter(RnpQuark parameterType, const char*) throw();
312 
314  void addInt32Parameter(RnpQuark parameterType, int) throw();
315 
317  void addFloat32Parameter(RnpQuark parameterType, float) throw();
318 
320  void addDouble64Parameter(RnpQuark parameterType, double) throw();
321 
323  void addOpaqueParameter(RnpQuark parameterType, const void*, int size) throw();
324 
326  void endFragment() throw();
327 
329  akg::CommBuffer* endMessage() throw();
330 
332  int getCarrierHeaderSize() throw();
333 
334 protected:
335 
337 
338 private:
339 
341  void addParameter(RnpQuark parameterType, Rnp::DataType, const void *data, int length) throw();
342 
344  bool changeToPartnerEndianness(Rnp::Endianness) throw();
345 
346  bool allocated;
347  int carrierHeaderSize;
348  Rnp::Endianness finalEndianness;
349 
350  RnpHeader *rnpHeader;
351  RnpFragmentHeader *currFragment;
352  RnpParameter *currParameter;
353 };
354 
363 {
364 public:
366  RnpProtocolDecoder() throw();
367 
374  bool decode(akg::CommBuffer*) throw();
375 
377  RnpQuark getDestinationServerType() const throw();
378 
380  Rnp::Endianness getDesiredEndianness() const throw();
381 
383  Rnp::Endianness getOriginalEndianness() const throw();
384 
386  int getMessageLength() const throw();
387 
389  int getMessageVersion() const throw();
390 
392  RnpQuark countFragments() const throw();
393 
395  const RnpFragmentHeader* getFirstFragment() const throw();
396 
398  const RnpFragmentHeader* getNextFragment() const throw();
399 
401  RnpQuark getFragmentType() const throw();
402 
404  const char* getFragmentTypeName() const throw();
405 
407  RnpQuark getCommand() const throw();
408 
410  int countParameters() const throw();
411 
413  RnpQuark getFragmentLength() const throw();
414 
416  const RnpParameter* getFirstParameter() const throw();
417 
419  const RnpParameter* getNextParameter() const throw();
420 
422  RnpQuark getParameterType() const throw();
423 
425  RnpQuark getDataType() const throw();
426 
428  const void* getData() const throw();
429 
431  const char* getDataAsString() const throw();
432 
434  int getDataAsInteger() const throw();
435 
437  float getDataAsFloat() const throw();
438 
440  double getDataAsDouble() const throw();
441 
443  const void* getDataAsOpaque() const throw();
444 
446  int getDataLength() const throw();
447 
448 private:
449  akg::CommBuffer *commBuffer;
450  Rnp::Endianness originalEndianness;
451  mutable RnpHeader *rnpHeader;
452  mutable RnpFragmentHeader *currFragment;
453  mutable int currFragmentIdx;
454 
455  mutable RnpParameter *currParameter;
456  mutable int currParameterIdx;
457 
459  void printRnpHeader(RnpHeader*) const throw();
460 
462  bool testIntegrity() const throw();
463 
464 
466  bool isRnpMessage() const throw();
467 
469  bool changeToHostEndianness() throw();
470 };
471 
472 
473 } //namespace
474 #endif
Definition: rnprotocol.hh:78
static const char * getDataTypeName(RnpQuark)
Definition: rnprotocol.hh:103
RnpQuark authInfoStart
Definition: rnprotocol.hh:163
Definition: rnprotocol.hh:114
Definition: rnprotocol.hh:59
Definition: rnprotocol.hh:230
Definition: rnprotocol.hh:90
int RnpQuark
The basic type used in RNP. It is always 32-bit long.
Definition: rnprotocol.hh:51
static RnpQuark swapBytes(RnpQuark)
Helper functions for endianness.
RnpQuark nrParams
Number of parameters.
Definition: rnprotocol.hh:239
Definition: rnprotocol.hh:264
RnpQuark serverType
Definition: rnprotocol.hh:162
Definition: rnprotocol.hh:70
bool isRnpMessage() const
Returns 'true' if this is a valid RNP header.
Definition: rnprotocol.hh:362
ErrorType
Definition: rnprotocol.hh:99
char majorVersion
Definition: rnprotocol.hh:158
Definition: rnprotocol.hh:104
Definition: rnprotocol.hh:91
FragmentType
Definition: rnprotocol.hh:73
Definition: rnprotocol.hh:192
Definition: rnprotocol.hh:82
RnpQuark paramType
The logical type of the parameter. Server dependent.
Definition: rnprotocol.hh:195
RnpFragmentHeader * getFirstFragment() const
Returns a pointer to the first fragment. Header has to be in host endianness.
bool changeEndianness(Rnp::Endianness)
static const char * getErrorTypeName(RnpQuark)
DataType
Definition: rnprotocol.hh:84
static Endianness detectHostEndianness()
char messageEndianness
Definition: rnprotocol.hh:156
Definition: rnpcommunication.hh:44
static const char * getEndiannessName(Endianness)
RnpQuark comprInfoLength
Definition: rnprotocol.hh:166
RnpQuark authInfoLength
Definition: rnprotocol.hh:164
static const char * undefValue
Arrays containing the names of the various elements.
Definition: rnprotocol.hh:141
Definition: rnprotocol.hh:112
Definition: rnprotocol.hh:101
RnpQuark totalMessageLength
Definition: rnprotocol.hh:160
Definition: rnprotocol.hh:77
Definition: rnprotocol.hh:153
Definition: rnprotocol.hh:86
RnpQuark dataStart
Definition: rnprotocol.hh:167
ErrorParam
Definition: rnprotocol.hh:109
static const RnpQuark rnpProtocolId
Definition: rnprotocol.hh:65
static const char * errorTypeNames[ert_HowMany]
Definition: rnprotocol.hh:145
akg::CommBuffer * commBuffer
Definition: rnprotocol.hh:336
Definition: rnprotocol.hh:89
Definition: rnprotocol.hh:106
Definition: rnprotocol.hh:69
RnpQuark command
The command. Server dependent.
Definition: rnprotocol.hh:236
RnpQuark _unused[5]
Definition: rnprotocol.hh:169
static const char * dataTypeNames[dtt_HowMany]
Definition: rnprotocol.hh:144
RnpQuark comprInfoStart
Definition: rnprotocol.hh:165
char minorVersion
Definition: rnprotocol.hh:159
RnpQuark nrFragments
Definition: rnprotocol.hh:161
int getCommand()
Definition: rnprotocol.hh:76
RnpQuark dataType
The data type of the parameter. One of Rnp::DataType.
Definition: rnprotocol.hh:198
static const char * getErrorParamName(RnpQuark)
Definition: rnprotocol.hh:102
RnpQuark getTotalLength() const
Returns the total length of the message, regardless of endianness.
RnpQuark totalLength
Total length of teh parameter, header + data + alignment bytes.
Definition: rnprotocol.hh:205
RnpQuark dataLength
The length of the data.
Definition: rnprotocol.hh:201
Definition: rnprotocol.hh:88
Definition: rnprotocol.hh:111
static const char * errorParamNames[erp_HowMany]
Definition: rnprotocol.hh:146
static const char * getFragmentTypeName(RnpQuark)
Functions to get the names of the various elements.
Definition: rnprotocol.hh:79
Definition: rnprotocol.hh:95
Definition: rnprotocol.hh:92
Endianness
Definition: rnprotocol.hh:67
Definition: rnprotocol.hh:116
Rnp::Endianness getEndianness() const
Returns the message endianness.
static const char * fragmentTypeNames[fgt_HowMany]
Definition: rnprotocol.hh:143
char desiredEndianness
Definition: rnprotocol.hh:157
RnpQuark dataLength
Definition: rnprotocol.hh:168
RnpQuark fragmType
The type of the fragment. One of Rnp::FragmentType.
Definition: rnprotocol.hh:233
Definition: rnprotocol.hh:113
virtual const char * getCommandName(RnpQuark) const =0
Definition: akgnet_commbuffer.hh:57
Definition: rnprotocol.hh:75
RnpQuark totalLength
Total length of the fragment, this header + all parameters.
Definition: rnprotocol.hh:242
virtual const char * getParameterTypeName(RnpQuark) const =0
Definition: rnprotocol.hh:87
static const char * endiannessNames[2]
Definition: rnprotocol.hh:142
RnpQuark protocolId
Definition: rnprotocol.hh:155