00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "request_template.h"
00024
00025
00026
00027
00028 #include <string.h>
00029
00030
00031
00032
00033
00034
00035
00036
00037 RequestTemplate::RequestTemplate()
00038 {
00039
00040
00041 MyInt = 0;
00042 MyCharPointer = NULL;
00043 MyStringLength = -1;
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 RequestTemplate::~RequestTemplate()
00053 {
00054 if ( MyCharPointer != NULL )
00055 {
00056 delete [] MyCharPointer;
00057 }
00058
00059
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069 void RequestTemplate::SetMyInteger( int p_int ) { MyInt = p_int; }
00070
00071 bool RequestTemplate::SetMyString( const char * p_str )
00072 {
00073 bool return_status = true;
00074
00075 if ( p_str == NULL
00076 || MyStringLength < strlen(p_str)
00077 )
00078 {
00079
00080
00081
00082
00083
00084 if ( MyCharPointer != NULL )
00085 {
00086 delete [] MyCharPointer;
00087
00088
00089
00090 MyCharPointer = NULL;
00091 }
00092
00093 MyStringLength = -1;
00094 }
00095
00096
00097 if ( p_str != NULL )
00098 {
00099
00100
00101
00102
00103
00104 if ( MyCharPointer == NULL )
00105 {
00106 MyStringLength = strlen(p_str);
00107
00108 if ( (MyCharPointer = new char[ MyStringLength + 1 ]) == NULL )
00109 {
00110
00111 return_status = false;
00112 }
00113 }
00114
00115 if ( MyCharPointer != NULL )
00116 {
00117
00118
00119
00120 strcpy( MyCharPointer, p_str );
00121 }
00122 }
00123
00124 return return_status;
00125 }
00126
00127 int RequestTemplate::GetMyInteger() const { return MyInt; }
00128
00129 char * RequestTemplate::GetMyString() const { return MyCharPointer; }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 long RequestTemplate::BufferInitAlloc()
00141 {
00142 long ExpectedBufferLength = 0;
00143
00144
00145
00146
00147
00148
00149
00150 ExpectedBufferLength = MutableServerRequest::BufferInitAlloc();
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 ExpectedBufferLength += 15;
00166
00167
00168
00169
00170 ExpectedBufferLength += 1;
00171
00172
00173
00174 if ( MyCharPointer != NULL )
00175 {
00176 ExpectedBufferLength += strlen( MyCharPointer );
00177 }
00178
00179
00180
00181 ExpectedBufferLength += 1;
00182
00183
00184 return ExpectedBufferLength;
00185 }
00186
00187 void RequestTemplate::FormatDerivativeData()
00188 {
00189
00190
00191
00192
00193
00194
00195 MutableServerRequest::FormatDerivativeData();
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 char _myint[18];
00206
00207
00208
00209 sprintf( _myint, "%d\n MyInt );
00210
00211 MessageBuffer += _myint;
00212
00213 // Add the string, if it is not null
00214
00215 if ( MyCharPointer != NULL )
00216 {
00217 MessageBuffer += MyCharPointer;
00218 }
00219
00220 // Terminate the string line
00221
00222 MessageBuffer += "\n";
00223
00224 // THE BASE CLASS PROCESSING WILL TERMINATE THE ENTIRE MESSAGE
00225 // WITH AN ADDITIONAL '\N'
00226
00227 }
00228 //------------------------------------------------------------------------------
00229 void RequestTemplate::ParseDerivativeData()
00230 {
00231 // First, parse the leading part of the buffer....that is,
00232 // the buffer part used by the base class(es):
00233 //
00234 // THIS CALL MUST BE PERFORMED AT THE START OF THIS METHOD,
00235 // AND IT MUST BE CALLED FOR THE CLASS FROM WHICH THIS ONE IS DERIVED.
00236 //
00237 MutableServerRequest::ParseDerivativeData();
00238
00239 long _index; // end of line index
00240 std::string _readline; // work buffer
00241
00242 // NOW PARSE THE PORTION OF THE BUFFER RELEVANT TO THIS MESSAGE CLASS
00243
00244 // NOTE THAT ALL ERRORS ARE REPORTED BY THROWING A worm_exception OBJECT.
00245 // THERE ARE TWO WAYS TO FORMAT THE ERROR MESSAGE CONTAINED THEREIN, BOTH
00246 // ARE DEMONSTRATE HEREIN.
00247 //
00248 // FYI, THE worm_exception IS CAUGHT AND REPORTED BY THE SERVER
00249
00250
00251 // - - - - - - - - - - - - - - - - - - - - - - - - - -
00252 // Handle the example's first data line
00253
00254
00255 // Find the end of the next line in the buffer
00256 //
00257 if ( (_index = MessageBuffer.find("\n")) == MessageBuffer.npos )
00258 {
00259 // First way to throw a worm_exception object
00260 throw worm_exception("Unterminated message while parsing RequestTemplate MyInt line");
00261 }
00262
00263 // extract the line from the buffer
00264 //
00265 _readline = MessageBuffer.substr( 0 , _index );
00266
00267
00268 // Remove the line from the buffer
00269 //
00270 MessageBuffer.erase( 0 , _index + 1 );
00271
00272
00273 // Parse the line's value (MyInt)
00274 //
00275 if ( sscanf( _readline.c_str(), "%d", &MyInt ) != 1 )
00276 {
00277 // Second way to throw a worm_exception object -- append string (int, float) value
00278 worm_exception my_exception("RequestTemplate::ParseDerivativeData() Error: ");
00279 my_exception += "Failed to parse MyInt value";
00280 throw my_exception;
00281 }
00282
00283
00284 // - - - - - - - - - - - - - - - - - - - - - - - - - -
00285 // Handle the example's second data line
00286 // (the string value [which may be empty])
00287
00288 // Find then end of the next line in the buffer
00289 //
00290 if ( (_index = MessageBuffer.find("\n")) == MessageBuffer.npos )
00291 {
00292 throw worm_exception("Unterminated message while parsing RequestTemplate MyString line");
00293 }
00294
00295 // extract the line from the buffer
00296 //
00297 _readline = MessageBuffer.substr( 0 , _index );
00298
00299 // Remove the line from the buffer
00300 //
00301 MessageBuffer.erase( 0 , _index + 1 );
00302
00303 // Set the string value
00304 //
00305 if ( _readline.size() == 0 )
00306 {
00307 // empty line
00308 SetMyString( NULL );
00309 }
00310 else
00311 {
00312 // something in the line
00313 SetMyString( _readline.c_str() );
00314 }
00315
00316 // Any remaining buffer (resulting from further class derivation or
00317 // just the end-of-message '\n') parse elsewhere
00318 }
00319 //------------------------------------------------------------------------------