/*
*	Structures and definitions used by SUT and the driver.
*
*	Written by Sergey Vasilevskiy, 2002-2005.
*/

// Note: this file is shared between Benchcraft and front-end server (SUTServer).

#pragma once

#include "..\inc\EGenUtilities_stdafx.h"
#include "..\inc\TxnHarnessStructs.h"
#include "PortDefinitions.h"	// TPC/IP ports to listen on

#define ALL_TXN			= 0xFFFFFFFF


// max number of records in the 'MEE index'->'machine' table. Cannot start more than this number of MEEs.
const int iMaxStartableMEEs = 256;

// max length of MEE driver name (must be synchronized with Benchcraft profile)
const int iMaxMEENameLen = 64;

// Constants used to indicate the transaction type in TDriverMessage::iTxnType
enum TXN_TYPES
{
	BROKER_VOLUME	= 0,	
	CUSTOMER_POSITION,	//1
    MARKET_FEED,        //2
	MARKET_WATCH,		//3	
	SECURITY_DETAIL,	//4
	TRADE_LOOKUP,		//5
	TRADE_ORDER,		//6
    TRADE_RESULT,       //7
	TRADE_STATUS,		//8
	TRADE_UPDATE,		//9
	DATA_MAINTENANCE,	//10
	CHECKPOINT,			//11
	MEE_TABLE,			//12	// command to receive MEE number->machine translation table for SendToMarket
	NUM_OF_TXN_TYPES,	//13	
};

// Constants used to indicate the type of transaction request from MEE to SUT.
/*enum MEE_TXN_TYPES
{
    MARKET_FEED     = 0,
    TRADE_RESULT
};*/

// MEE number to machine table entry for one MEE
typedef struct TMEEIndexToMachineEntry
{	
	char			szMEEName[iMaxMEENameLen];	// MEE name as entered in Benchcraft profile
	char			szMachineName[32];	// machine name for this MEE index
	unsigned short	shPort;	// port for this MEE index
} *PMEEIndexToMachineEntry;

// MEE number to machine translation table.
// Sent from a CE to the SUT before the first transaction request.
typedef struct TMEEIndexTable
{
    TXN_TYPES			    eTxnType;   // should always be MEE_TABLE
	int						iMEECount;	// number of the following records
	// table entries - number depends on how many MEEs are started 
	TMEEIndexToMachineEntry	MEE[iMaxStartableMEEs];
	
    //  Simple constructor.
    //
    TMEEIndexTable() { eTxnType = MEE_TABLE; }
} *PMEEIndexTable;

// Parameter structure for Checkpoint. Defined here because it is not a TPC-E transaction
// (although behaves like one).
//
typedef struct TCheckpointInput
{
	long			lDuration;	// Checkpoint duration in seconds
} *PCheckpointInput;

//Message sent from User Emulator Driver to SUT
typedef struct TDriverMessage
{
	TXN_TYPES			eTxnType;
	union 
	{
		TPCE::TBrokerVolumeTxnInput			BVInput;
		TPCE::TCustomerPositionTxnInput		CPInput;
        TPCE::TMarketFeedTxnInput           MFInput;
		TPCE::TMarketWatchTxnInput			MWInput;
		TPCE::TSecurityDetailTxnInput		SDInput;
        TPCE::TTradeLookupTxnInput			TLInput;
		TPCE::TTradeOrderTxnInput			TOInput;
        TPCE::TTradeResultTxnInput          TRInput;
		TPCE::TTradeStatusTxnInput			TSInput;
		TPCE::TTradeUpdateTxnInput			TUInput;
		TPCE::TDataMaintenanceTxnInput		DMInput;

		//TMEEIndexTable		MEEIndexTable;
		TCheckpointInput	CheckpointInput;
	} input;
} *PDriverMessage;

// Message sent from SUT to User Emulator and Market Exchange Emulator drivers
// after a transaction has been executed.
//
typedef struct TSUTReplyMessage
{
	int		iStatus;		// status or error type (0=no error)
	int		iErrorCode;		// multiples of ERR_DEADLOCK_VICTIM mean there were deadlock retries
	int		iEGenStatus;	// status code from EGen transaction harness

    TXN_TYPES			                    eTxnType;
    //  Transaction output data. It is required to be received by the driver.
    //
    union 
	{
		TPCE::TBrokerVolumeTxnOutput		BVOutput;
		TPCE::TCustomerPositionTxnOutput    CPOutput;
        TPCE::TDataMaintenanceTxnOutput		DMOutput;
        TPCE::TMarketFeedTxnOutput			MFOutput;
		TPCE::TMarketWatchTxnOutput			MWOutput;
		TPCE::TSecurityDetailTxnOutput		SDOutput;
        TPCE::TTradeLookupTxnOutput			TLOutput;
		TPCE::TTradeOrderTxnOutput			TOOutput;
        TPCE::TTradeResultTxnOutput			TROutput;
		TPCE::TTradeStatusTxnOutput			TSOutput;
		TPCE::TTradeUpdateTxnOutput			TUOutput;
	} output;

    // Minimum number of bytes to receive to determine the overall size.
    //
    DWORD MinLength() { return offsetof(TSUTReplyMessage, output); }
} *PSUTReplyMessage;

// Transaction request sizes.
//
const DWORD	TxnReqSize[NUM_OF_TXN_TYPES] =
{   // Note: the order here must match the order in TXN_TYPES.
    //
    offsetof(TDriverMessage, input) + sizeof(TPCE::TBrokerVolumeTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TCustomerPositionTxnInput),
    offsetof(TDriverMessage, input) + sizeof(TPCE::TMarketFeedTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TMarketWatchTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TSecurityDetailTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TTradeLookupTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TTradeOrderTxnInput),
    offsetof(TDriverMessage, input) + sizeof(TPCE::TTradeResultTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TTradeStatusTxnInput),
	offsetof(TDriverMessage, input) + sizeof(TPCE::TTradeUpdateTxnInput),
    offsetof(TDriverMessage, input) + sizeof(TPCE::TDataMaintenanceTxnInput),
    offsetof(TDriverMessage, input) + sizeof(TCheckpointInput),
    offsetof(TDriverMessage, input) + sizeof(TMEEIndexTable)
};

// Transaction response sizes (for output data).
//
const DWORD TxnRespSize[NUM_OF_TXN_TYPES] =
{   // Note: the order here must match the order in TXN_TYPES.
    //
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TBrokerVolumeTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TCustomerPositionTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TMarketFeedTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TMarketWatchTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TSecurityDetailTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TTradeLookupTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TTradeOrderTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TTradeResultTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TTradeStatusTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TTradeUpdateTxnOutput),
    offsetof(TSUTReplyMessage, output) + sizeof(TPCE::TDataMaintenanceTxnOutput),
    offsetof(TSUTReplyMessage, output), // CHECKPOINT
    offsetof(TSUTReplyMessage, output)  // MEE_TABLE
};