📄 stubio.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/IO/StubIO.cpp,v $
* $Date: 2003/05/13 18:42:04 $
*
Description:
Stubs out an IO layer, by 'faking' real new connections on
a configurable interval. New connections are then fed canned
data from a file. Output data is discarded.
\*____________________________________________________________________________*/
/* $SourceTop:$ */
#include <assert.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Pi2API.h"
#include "DblList.h" /* from Base library */
#include "PIStrStr.h" /* from Base library */
/* --- Values from configuration file --- */
#define KEY_CONF_SENDINTERVAL "SendInterval"
#define KEY_CONF_ACCEPTINTERVAL "AcceptInterval"
#define KEY_CONF_DATAFILE "DataFile"
#define KEY_CONF_HTTPEQUIV "HTTPEquiv"
#define KEY_CONF_ERRORAFTER "ErrorAfter"
/* --- other macros --- */
#define CONFIG_ERR(this, msg) \
{ PILog_addMessage( PIObject_getDB(this), \
PIObject_getConfigurationDB(this), PILOG_ERROR, (msg) ); }
/*
#define D { cerr << __FILE__ << ": " << __LINE__ << endl; };
*/
#define D
#define DEFAULT_INTERVAL (0)
/*____________________________________________________________________________*\
Description:
Documentation for this IO object.
\*____________________________________________________________________________*/
#if 0
/*___+++HTMLDOC_BEGIN+++___*/
Name:
StubIO
Description:
This object 'fakes' an IO connection by feeding canned data to the
reader. The canned data is read from files.
Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)
<TR>
<TD>AcceptInterval
<TD>0
<TD>0, -1, n
<TD>IO accept interval
<TD>AcceptInterval 0
<TR>
<TD>SendInterval
<TD>0
<TD>0, -1, n
<TD>IO output interval
<TD>SendInterval 0
<TR>
<TD>Data
<TD>+
<TD><filename>
<TD>File data to read
<TD>DataFile ./../tests/t2.tst
#define KEY_CONF_ERRORAFTER "ErrorAfter"
<TR>
<TD>HTTPEquiv
<TD>+
<TD><variable>: <value>
<TD>Variable value pair for DB
<TD>HTTPEquiv="RemoteAddr: 127.0.0.1"
<TR>
<TD>ErrorAfter
<TD>+
<TD><number>
<TD>Number of child IO objects to error after
<TD>ErrorAfter="10000"
</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>
<H4>Description of Options</H4>
<H5>AcceptInterval</H5>
Specify an interval measured in seconds to wait everytime a new connection
is generated by copying the prototype IO object.
<H5>SendInterval</H5>
Specify an interval measured in seconds to wait everytime data is sent.
<H5>DataFile</H5>
Specifies a file with data to return to the reader. The file path
should be absolute or relative to the current directory. This parameter
can be specified multiple times to specify different files that will
be assigned to different readers in cycles.
<H5>HTTPEquiv</H5>
Specifies an RFC822 variable value pair to put in this objects DB.
<H5>ErrorAfter</H5>
Specify the number of IO error objects to to error after. This facilites
can be used to test the server for a discrete number of IO requests before
exiting.
Returns:
PIAPI_COMPLETED on success.
PIAPI_ERROR and PIAPI_ABORT respectively for generic and severe
error conditions.
Example:
<PRE>
<Object>
Name StubIO
Class StubIOClass
AcceptInterval 0
SendInterval 0
DataFile ./../tests/t2.tst
</Object>
</PRE>
/*___+++HTMLDOC_END+++___*/
#endif
/*____________________________________________________________________________*\
*
Class:
Description:
Parse, store and add RFC822 headers to response DB's.
\*____________________________________________________________________________*/
class RFC822Header
{
private:
const char *pFKVariable;
PIString sValue;
int iOK;
public:
RFC822Header( const char *pLine, int iType )
: pFKVariable( 0 ),
iOK( 0 )
{
if ( !pLine )
{ return; };
int iLen = strlen( pLine );
int i;
for( i=0; i<iLen && (isalnum(pLine[i])); i++);
if ( !i || pLine[i]!=':' )
{ return; };
PIString sVariable( pLine, i );
for( i++; i<iLen && (isspace(pLine[i])); i++); /* scan past ws */
pFKVariable = PIDB_getFastKey( sVariable, iType );
sValue = &( pLine[i] );
iOK = 1;
};
/* --- Replace this header in DB --- */
void ReplaceHeader( PIDB *pDB, int iType )
{
assert( pDB );
assert( pFKVariable );
assert( iOK );
PIDB_replace( pDB, iType, pFKVariable,
(void *)(const char *)sValue, PIDBFLAG_FASTKEY );
};
inline int IsOK()
{ return iOK; };
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class IOResponseRecord
{
public:
char *pInData; /* this is the input data to the IO stream */
int iDataSize; /* size of pInData */
IOResponseRecord( char *pTheInData, int iTheDataSize )
: pInData( pTheInData ), iDataSize( iTheDataSize )
{};
~IOResponseRecord()
{
PI_DELETE( [] pInData );
};
static IOResponseRecord *MakeResponseRecord( const PIObject *pObject,
const char *pDataFile )
{
assert( pObject && pDataFile );
PIFInfo *pInfo = PIFInfo_new( pDataFile );
ifstream ifs;
if ( pInfo )
{ ifs.open( pDataFile ); };
if ( !pInfo || !ifs.good() )
{
PILog_addMessage( PIObject_getDB(pObject),
PIObject_getConfigurationDB(pObject), PILOG_ERROR,
"StubIO: Datafile, '%s' could not be opened.", pDataFile );
return 0;
};
int iDataSize = PIFInfo_getSize( pInfo );
PIFInfo_delete( pInfo );
char *pInData = PI_NEW( char[iDataSize] );
if ( pInData )
{
ifs.read( pInData, iDataSize );
};
if ( !pInData || !*pInData )
{
CONFIG_ERR( pObject, "StubIO: could not allocate output \
buffer or datafile is empty" );
return 0;
};
return PI_NEW( IOResponseRecord( pInData, iDataSize ) );
};
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class StubIO
{
private:
PIObject *pObject;
StubIO *pIOObject; /* prototype IO object from which this was created */
int iAcceptInterval;/* from config. 0 = no wait, -1 = infinite wait */
int iSendInterval; /* from config. 0 = no wait, -1 = infinite wait */
DblList lRecords; /* response records */
DblList lHTTPEquivalents;
/* rfc822 headers for DB */
int iErrorAfter; /* error after */
const IOResponseRecord *pIORecord;
/* current record of child IO */
int iNextRecord; /* prototype objects next record index for child IO */
int iPos; /* next position on pInData */
int bOK;
PIDB *pSaveDB;
/* --- parameters --- */
int Parameter( const char *pVar, const char *pVal, const char *pWhere )
{
assert( pVar );
assert( pVal );
PIOStrStream os;
os << pWhere << "StubIO: ";
if ( !PIUtil_stricmp( pVar, KEY_CONF_DATAFILE ) )
{
IOResponseRecord *pRec = IOResponseRecord::MakeResponseRecord(
pObject, pVal );
if ( !pRec )
{ return 0; };
lRecords.Append( (DblList::type)pRec );
}
else if ( !PIUtil_stricmp( pVar, KEY_CONF_ERRORAFTER ) )
{
iErrorAfter = atoi( pVal );
}
else if ( !PIUtil_stricmp( pVar, KEY_CONF_HTTPEQUIV ) )
{
RFC822Header *pHeader = PI_NEW( RFC822Header( pVal,
PIDBTYPE_STRING ) );
if ( !pHeader || !pHeader->IsOK() )
{
os << "Bad HTTPEquiv expression, '" << pVal << "'" << ends;
CONFIG_ERR( pObject, os.str() );
PI_DELETE( pHeader );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -