/*
*	Class representing a server listening for MEE requests on the SUT.
*
*	©Copyright 2002-2006, Microsoft Corporation
*	Written by Sergey Vasilevskiy.
*/

#include "stdafx.h"

class CMEEServer : public CBaseServer<TMEEServerLocals>
{
	char*				m_szDBServer;
	char*				m_szDB;
	bool				m_bVerbose;
	eClientDriverType	m_eClientType;
    char				m_szMEEToUse[iMaxMEENameLen];

	virtual TMEEServerLocals* InitializeLocal(void)	//initializes thread local variables
	{
		return new TMEEServerLocals(m_szDBServer, m_szDB, m_eClientType, &m_szMEEToUse[0]);
	};

    void PrintMEETable(PMEEIndexTable input);
	
	int ReceiveMEETable(char* mes, TMEEServerLocals* pMFLocal, int iThreadNum);

	//actually processes received message
	virtual int ProcessMessage(char* mes, DWORD iTotalBytesRecvd, TMEEServerLocals* pLocal, int iThreadNum, PSUTReplyMessage pSUTReply);
	
    int DoMarketFeed(TPCE::PMarketFeedTxnInput input, TPCE::TMarketFeedTxnOutput &Output, TPCE::CMarketFeed &Txn, int iThreadNum);
    int DoTradeResult(TPCE::PTradeResultTxnInput input, TPCE::TTradeResultTxnOutput &Output, TPCE::CTradeResult &Txn, int iThreadNum);

    void PrintError(TPCE::CODBCERR* pODBCErr, TPCE::CDateTime& TimeNow);   // helper routine to display an error message

public:

	CMEEServer(	char*               szDBServer, 
                char*               szDB, 
                unsigned short      shPort, 
                bool                bVerbose, 
				HANDLE              hListenStopEvent, 
                eClientDriverType   eClientType,
                char*				szMEEToUse)
		: CBaseServer<TMEEServerLocals>("localhost", shPort, max(sizeof(TMEEIndexTable), sizeof(TDriverMessage)), 
                    hListenStopEvent, bVerbose)
		, m_szDBServer(NULL)
		, m_szDB(NULL)
		, m_bVerbose(bVerbose)
		, m_eClientType(eClientType)
	{
		m_szDBServer = new char[strlen(szDBServer)+1];
		strcpy(m_szDBServer, szDBServer);

		m_szDB = new char[strlen(szDB)+1];
		strcpy(m_szDB, szDB);

        m_szMEEToUse[0] = '\0';	// for when no -m option is specified
        if (szMEEToUse != NULL)
        {
            strcpy(m_szMEEToUse, szMEEToUse);
        }
		cout<<"Exiting CMEEServer c'tor."<<endl;
	}

	~CMEEServer()
	{
		if (m_iDeadlockCount > 0)
		{
			OutputStdin("Total deadlocks: %d\n", m_iDeadlockCount);
		}

		if (m_szDBServer)
			delete m_szDBServer;
		if (m_szDB)
			delete m_szDB;
	};
};
