summaryrefslogtreecommitdiffstats
path: root/rnprotocol/rnpembedded.hh
blob: 2e7c454baf8ddb26bdc1eb922734a6e3a9be6c65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef RNPEMBEDDED_HH
#define RNPEMBEDDED_HH
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/****************************************************************************
 *
 *
 * COMMENTS:
 *
 * 
 ****************************************************************************/
#include "rnprotocol/rnprotocol.hh"

namespace rnp
  {
  
/** RNP messages may be embedded in messages of the carrier protocol
    We will use for now only HTTP carrier or none at all
    But we define a BadCarrier for testing purposes
*/


/** Class containing definitions and some helper functions
*/
class RnpTransport
  {
    public:
      enum CarrierProtocol
        {
	  crp_Unknown,
	  crp_Rnp,
	  crp_Http,
	  crp_BadCarrier, 
	  //....
	  crp_HowMany
	 };
	
      static const char* getCarrierName(CarrierProtocol) throw();
    private:
      static const char* carrierNames[]; 
   };

/** 
  RnpReceiver is a class that is able to receive an message in a CommBuffer and decide if it is a
  valid Rnp message, embedded or not. 
  
  It is designed to be used by a NbJob for receiving the message and cooperate with it.
  
  If it is an invalid message or the message buffer can't be allocated,
  the rest of the message is discarded and the NbJob has to close the connection and do appropriate cleaning

  The receiver has two buffers, a fixed length one, for header and a dynamic one for the RNP message  
*/

class RnpReceiver
  {
    public:
      /// Default constructor
      RnpReceiver() throw();
      
      /// Destructor
      ~RnpReceiver() throw();
      
      /// Resets the receiver, preparing for a new message
      void reset() throw();
      
      /// Returns a pointer to the current buffer (the header one or the message one)
      akg::CommBuffer* getCurrentBuffer() throw();
      
      /** Returns a pointer to the message buffer, which contains the RNP message, 
          whitout any carrier header */
      akg::CommBuffer* getMessageBuffer() throw();
      
      /// Returns 'true' if the whole message was received, 'false' if more data is expected
      bool        validateMessage() throw();
      
      /** Returns 'true' if an error occured and the message has to be discarded
          If validate()==false and isDiscarding()==true => NbJob has to reset receiver and close connection*/
      bool        isDiscarding() const throw();
      
      /// Returns the type of the carrier protocol
      RnpTransport::CarrierProtocol  getCarrierProtocol() const throw();
      
      /// Returns the size of the carrier header
      int         getCarrierHeaderSize() const throw();
      
      /// Returns a pointer to the carrier header
      const void* getCarrierHeader() throw();
      
    private:
      
      enum Status
        {
	  waitingHeader,
	  readingMessage,
	  discarding
	 };
      
      
      Status  status; 
      
      akg::CommBuffer headerBuffer;
      akg::CommBuffer rnpMessageBuffer;
      
      RnpHeader *rnpHeader;
      
      RnpTransport::CarrierProtocol carrier;
      int               carrierHeaderLength;

      static const int   headerBufferLength;
      
      bool isHttpCarrier() throw();
      bool isRnpCarrier() throw();
      bool prepareMessageBuffer() throw();

   };
   
class RnpCarrier;

/** Class for creating an embedded RNP message. Most methods are inherited
    from RnpProtocolEncoder, it offers just convenient methods for 
    dealing with carriers
*/
class RnpTransmitter : public RnpProtocolEncoder
  {
    public:
      /// Default constructor
      RnpTransmitter() throw();
      
      /// Destructor
      ~RnpTransmitter() throw();
      
      /// Starts a new message, as a request, embedded in a specified protocol
      bool startRequest(RnpQuark serverType, RnpTransport::CarrierProtocol) throw();
      
      /// Starts a new message, as an answer, embedded in a specified protocol
      bool startAnswer(RnpQuark serverType, RnpTransport::CarrierProtocol) throw();
      
      /// ends the message, puts the carrier headers and, if requested, changes endianness
      akg::CommBuffer* endMessage() throw();
      
      /// Returns the carrier protocol
      RnpTransport::CarrierProtocol getCarrierProtocol() throw();
      
      /// Returns the total size of the buffer
      int  getBufferSize() const throw();
      
      /// Return the space left in the buffer
      int  getNotFilledSize() const throw();
      
      /// Returns the data size in the buffer
      int  getDataSize() const throw();
    private:
          
      RnpTransport::CarrierProtocol carrierType;
      
      /** Creates and returns a RnpCarrier object, based on the type got as parameter
          It assignes the object to 'carrier' and it also destroys the previous
	  assigned object
      */
      RnpCarrier* getCarrierObject(RnpTransport::CarrierProtocol) throw();
      RnpCarrier* carrier;
   }; 

/** Base class for the various carriers, is itself the RNP carrier
*/
class RnpCarrier
  {
    public:
      /// Default constructor
      RnpCarrier() throw();
      
      /// Virtual destructor
      virtual ~RnpCarrier() throw();
      
      /// Returns the type of the object
      RnpTransport::CarrierProtocol getType() throw();
      
      /// Returns the length of the request header
      virtual int  getRequestHeaderLength() throw();
      
      /// Returns the length of the answer header
      virtual int  getAnswerHeaderLength() throw();
      
      /** Write the header directly into the reserved space of the buffer, 
          since the rest of the message is already there*/
      virtual void putHeader(akg::CommBuffer*) throw();
      
    protected:
      /// The type of the carrier
      RnpTransport::CarrierProtocol type;
      
      /// Flag for 'putHeader' to know which header to write
      bool requestHeader;
        
   }; 

/** The HTTP-carrier
*/
class HttpRnpCarrier : public RnpCarrier
  {
    public:
      /// Default constructor
      HttpRnpCarrier() throw();
      
      /// Returns the length of the request header
      int  getRequestHeaderLength() throw();
      
      /// Returns the length of the answer header
      int  getAnswerHeaderLength() throw();
      
      /// Writes the header into the buffer
      void putHeader(akg::CommBuffer*) throw();
      
   private:   
      static const char theRequestHeader[];
      static const char theAnswerHeader[];
   };

/** A 'bad carrier', just for testing purposes
*/
class BadRnpCarrier : public RnpCarrier
  {
    public:
      BadRnpCarrier() throw();
      
      int  getRequestHeaderLength() throw();
      int  getAnswerHeaderLength() throw();
      void putHeader(akg::CommBuffer*) throw();
      
    private:  
      static const char theHeader[];
   };
   
} //namespace   	      
#endif