Subversion Repositories DevTools

Rev

Rev 2265 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2263 kivins 1
//## begin module%1.7%.codegen_version preserve=yes
2
//   Read the documentation to learn more about C++ code generator
3
//   versioning.
4
//## end module%1.7%.codegen_version
5
 
6
//## begin module%420720460243.cm preserve=no
7
//## end module%420720460243.cm
8
 
9
//## begin module%420720460243.cp preserve=no
10
//	C O P Y R I G H T   N O T I C E
11
//	This material is confidential to ERG and may not be disclosed in whole
12
//	or in part to any third party nor used in any manner whatsoever other
13
//	than for the purposes expressly consented to by ERG in writing.
14
//
15
//	This material is also copyright and may not be reproduced, stored in a
16
//	retrieval system or transmitted in any form or by any means in whole or
17
//	in part without the express written consent of ERG.
18
//## end module%420720460243.cp
19
 
20
//## Module: TransactionStream%420720460243; Pseudo Package body
21
//## Subsystem: MASS::Dev::Tools::TxnTestManager::src%41F5A79001E4
22
//## Source file: Z:\MASS_Dev\Tools\TxnTestManager\src\TransactionStream.cpp
23
 
24
//## begin module%420720460243.additionalIncludes preserve=no
25
//## end module%420720460243.additionalIncludes
26
 
27
//## begin module%420720460243.includes preserve=yes
28
#pragma warn -com
29
#include <LoggingMacros.h>
30
#pragma warn +com
31
 
32
#include <vcl.h>
33
#pragma hdrstop
34
//## end module%420720460243.includes
35
 
36
// UdFileManifest
37
#include "UdFileManifest.h"
38
// TransactionStream
39
#include "TransactionStream.h"
40
//## begin module%420720460243.additionalDeclarations preserve=yes
41
#if defined( WIN32 ) || defined( __BCPLUSPLUS__ ) || defined( __BORLANDC__ )
42
   const char g_separator = '\\';
43
#else
44
   const char g_separator = '/';
45
#endif
46
 
47
#include <iomanip>
48
#include <fstream>
49
#include <sstream>
50
#include <time.h>
51
#include <sys/stat.h>
52
//## end module%420720460243.additionalDeclarations
53
 
54
 
55
// Class TransactionStream 
56
 
57
//## Operation: TransactionStream%4207228700FB
58
TransactionStream::TransactionStream (const std::string &folder, const std::string &batchPrefix, const std::string &batchSuffix, const bool &buildManifest, const std::string &manifestPrefix, const std::string &manifestSuffix, const std::string &pathmapTarget)
59
  //## begin TransactionStream::TransactionStream%4207228700FB.hasinit preserve=no
60
      : m_batchNumber(1),
61
        m_batchCount(0),
62
        m_batchPrefix(batchPrefix),
63
        m_batchSize(0),
64
        m_batchSuffix(batchSuffix),
65
        m_folder(folder),
66
        m_stream(0),
2267 kivins 67
        m_manifest(0),
68
        m_filename("")
2263 kivins 69
  //## end TransactionStream::TransactionStream%4207228700FB.hasinit
70
  //## begin TransactionStream::TransactionStream%4207228700FB.initialization preserve=yes
71
  //## end TransactionStream::TransactionStream%4207228700FB.initialization
72
{
73
  //## begin TransactionStream::TransactionStream%4207228700FB.body preserve=yes
74
 
75
	if ( buildManifest )
76
	{
77
		m_manifest = new UdFileManifest(
78
			folder,
79
			manifestPrefix,
80
			manifestSuffix,
81
			pathmapTarget );
82
	}
83
 
84
  //## end TransactionStream::TransactionStream%4207228700FB.body
85
}
86
 
87
 
88
TransactionStream::~TransactionStream()
89
{
90
  //## begin TransactionStream::~TransactionStream%420720460243_dest.body preserve=yes
91
 
92
	close();
93
 
94
	if ( m_manifest )
95
	{
96
		delete m_manifest;
97
		m_manifest = 0;
98
	}
99
 
100
 
101
  //## end TransactionStream::~TransactionStream%420720460243_dest.body
102
}
103
 
104
 
105
 
106
//## Other Operations (implementation)
107
//## Operation: buildFileName%420821AD02EF
108
std::string & TransactionStream::buildFileName (std::string &filename, const std::string &folder, const std::string &prefix, const std::string &suffix, const std::string &batchName, const unsigned &batchNumber)
109
{
110
  //## begin TransactionStream::buildFileName%420821AD02EF.body preserve=yes
111
 
112
	static const int	base = 62;
113
	static const char	encoding[ base ] =
114
		"0123456789"
115
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
116
		"abcdefghijklmnopqrstuvwxyz";
117
 
118
	/**
119
	 *	TDateTime is in local time, and the DCE currently uses UTC.  Therefore,
120
	 *	we use time() to get seconds since midnight in UTC, and use TDateTime
121
	 *	only to get msec.
122
	 */
123
	unsigned short	hour;
124
	unsigned short	minute;
125
	unsigned short	second;
126
	unsigned short	millisecond;
127
	struct stat		statbuf;
128
	time_t			now;
129
	char			encodingTime[ 9 ];
130
 
131
	/**
132
	 *	Do this until we have a filename, but no more than 10,000 iterations,
133
	 *	which should be a little over 10 seconds.
134
	 */
135
	for ( int safetyLimit = 10000; safetyLimit; --safetyLimit )
136
	{
137
		TDateTime::CurrentDateTime().DecodeTime(
138
			&hour,
139
			&minute,
140
			&second,
141
			&millisecond );
142
 
143
		time( &now );
144
 
145
		encodingTime[ 6 ] = encoding[ millisecond % base ];
146
		millisecond /= base;
147
		encodingTime[ 5 ] = encoding[ millisecond % base ];
148
 
149
		encodingTime[ 4 ] = encoding[ now % base ];
150
		now /= base;
151
		encodingTime[ 3 ] = encoding[ now % base ];
152
		now /= base;
153
		encodingTime[ 2 ] = encoding[ now % base ];
154
		now /= base;
155
		encodingTime[ 1 ] = encoding[ now % base ];
156
		now /= base;
157
		encodingTime[ 0 ] = encoding[ now % base ];
158
 
159
		for ( int counter=0; counter < base; ++counter )
160
		{
161
			encodingTime[ 7 ] = encoding[ counter ];
162
			encodingTime[ 8 ] = 0;
163
 
164
			std::stringstream name;
165
			if ( folder.length() )
166
			{
167
				name << folder;
168
				if ( folder[ folder.length() - 1 ] != g_separator )
169
				{
170
					name << g_separator;
171
				}
172
			}
173
 
174
			if ( prefix.length() )
175
			{
176
				name << prefix;
177
			}
178
 
179
			name << encodingTime;
180
 
181
			if ( batchName.length() )
182
			{
183
				name << '_' << batchName << '_' << std::setw( 4 ) << std::setfill( '0' ) << std::right << batchNumber;
184
			}
185
 
186
			if ( suffix.length() )
187
			{
188
				name << suffix;
189
			}
190
 
191
			filename = name.str();
192
 
193
			if ( stat( filename.c_str(), &statbuf ) == -1 )
194
			{
195
				return ( filename );
196
			}
197
		}
198
 
199
		Sleep( 1 );
200
	}
201
 
202
	MTHROW( std::runtime_error, \
203
		"Cannot build a filename that is not already in use in \"" \
204
		<< folder \
205
		<< "\" with prefix \"" \
206
		<< prefix \
207
		<< "\" and suffix \"" \
208
		<< suffix \
209
		<< "\"." );
210
 
211
  //## end TransactionStream::buildFileName%420821AD02EF.body
212
}
213
 
214
//## Operation: close%420819C203E4
215
void TransactionStream::close ()
216
{
217
  //## begin TransactionStream::close%420819C203E4.body preserve=yes
218
 
219
	if ( m_stream )
220
	{
2267 kivins 221
      m_stream->close();
2263 kivins 222
		delete m_stream;
223
		m_stream = 0;
2267 kivins 224
 
225
		AnsiString tmpathname, targetpathname;
226
      tmpathname = "../data/tmp/" + AnsiString( m_filename.c_str());
227
      targetpathname = AnsiString( m_folder.c_str()) + "\\" + AnsiString( m_filename.c_str());
228
      if (FileExists(tmpathname))
229
      {
230
         if (!RenameFile(tmpathname, targetpathname))
231
         {
232
            MessageDlg(	"cannot rename temporary UD file " + tmpathname
233
                        + " to " + targetpathname,
234
								mtConfirmation,TMsgDlgButtons() << mbOK, 0 ) ;
235
	      }
236
      }
237
      else
238
      {
239
         MessageDlg(	"cannot rename " + tmpathname + " as it does not exist",
240
							mtConfirmation,TMsgDlgButtons() << mbOK, 0 ) ;
241
	   }    
242
 
2263 kivins 243
	}
244
 
245
  //## end TransactionStream::close%420819C203E4.body
246
}
247
 
248
//## Operation: incrementCount%420723C60224
249
void TransactionStream::incrementCount ()
250
{
251
  //## begin TransactionStream::incrementCount%420723C60224.body preserve=yes
252
 
253
	/**
254
	 *	When we have a batch size (and so we batch), then we may need to
255
	 *	start a new batch.
256
	 */
257
	if ( m_batchSize )
258
	{
2265 kivins 259
		if ( m_batchCount++ >= m_batchSize )
2263 kivins 260
		{
261
			close();
262
			m_batchCount = 0;
263
			++m_batchNumber;
264
			open();
265
		}
266
	}
267
 
268
  //## end TransactionStream::incrementCount%420723C60224.body
269
}
270
 
271
//## Operation: newBatch%420722B50215
272
const bool TransactionStream::newBatch (const unsigned &size, const std::string &name)
273
{
274
  //## begin TransactionStream::newBatch%420722B50215.body preserve=yes
2267 kivins 275
	/**
276
	 *	Do we have a batch in progress
277
	 */
278
	if ( m_batchCount++ > 0 )
279
		{
280
			close();
281
			++m_batchNumber;
282
		}
2263 kivins 283
 
284
	m_batchSize		= size;
285
	m_batchCount	= 0;
286
	m_batchName		= name;
287
 
288
	return ( open() );
289
 
290
  //## end TransactionStream::newBatch%420722B50215.body
291
}
292
 
293
//## Operation: open%420819AE03C4
294
const bool TransactionStream::open ()
295
{
296
  //## begin TransactionStream::open%420819AE03C4.body preserve=yes
297
 
298
	close();	// Close the stream should it be open.
299
 
300
	std::string filename;
2267 kivins 301
   AnsiString tmpathname;
302
	buildFileName( filename, "", m_batchPrefix, m_batchSuffix, m_batchName, m_batchNumber );
303
 
304
	m_filename = filename; //save for later
305
   tmpathname = "../data/tmp/" + AnsiString( filename.c_str());
2263 kivins 306
 
2267 kivins 307
 
2263 kivins 308
	m_stream = new std::ofstream(
2267 kivins 309
		tmpathname.c_str(),
2263 kivins 310
		std::ios_base::binary | std::ios_base::out );
311
	if ( m_stream->is_open() )
312
	{
313
		if ( m_manifest )
314
		{
315
			m_manifest->addUdFile( filename );
316
		}
317
		return ( true );
318
	}
319
 
320
	// When we can't open the file, then roll back.
321
	delete m_stream;
322
	m_stream = 0;
323
 
324
	MERROR( "Cannot open \"" << filename << "\" for writing." );
325
	return ( false );
326
 
327
  //## end TransactionStream::open%420819AE03C4.body
328
}
329
 
330
//## Operation: write%4207231D014A
331
const bool TransactionStream::write (const void *buffer, const unsigned &length)
332
{
333
  //## begin TransactionStream::write%4207231D014A.body preserve=yes
334
 
335
	if ( m_stream )
336
	{
337
		m_stream->write( reinterpret_cast< const char * >( buffer ), length );
338
 
339
		if ( m_stream->good() )
340
		{
341
			return ( true );
342
		}
343
		else
344
		{
345
			MERROR( "Cannot write " << length << " byte(s) to stream." );
346
		}
347
	}
348
	else
349
	{
350
		MERROR( "Cannot write to stream because it is not open." );
351
	}
352
 
353
	return ( false );
354
 
355
  //## end TransactionStream::write%4207231D014A.body
356
}
357
 
358
// Additional Declarations
359
  //## begin TransactionStream%420720460243.declarations preserve=yes
360
  //## end TransactionStream%420720460243.declarations
361
 
362
const void TransactionStream::logToManifest (const std::string & info, const bool &addTime )
363
{
364
     TDateTime t;
365
     String st = "";
366
     String details;
367
 
368
     if (addTime) 
369
     {
370
        t = Time();
371
        st = TimeToStr(t) + "   ";
372
     }
373
 
374
     details = "# " + st + AnsiString(info.c_str());
375
 
376
     //pass the data on without added value at this stage
377
     if ( m_manifest )
378
     {
379
         m_manifest->addTestDetails(details.c_str());
380
     }
381
}
382
 
383
 
384
//## begin module%420720460243.epilog preserve=yes
385
//## end module%420720460243.epilog