/*
*	©Copyright 2002-2003, Microsoft Corporation
*	Microsoft Confidential
*	Written by Sergey Vasilevskiy.
*/

#include "TxnHarness_stdafx.h"

using namespace TPCE;

void CSendToMarket::FindMEEToUse()
{
    int i;

	if (m_pMEETable == NULL)
	{
		printf("SendToMarket: no MEE table received yet!\n");

		throw new CSendToMarketErr(CSendToMarketErr::eMEETableNotReceivedYet, "CSendToMarket::FindMEE");
	}

	// Now find that MEEToUse in the table and store its index.
	//
	for (i = 0; i < m_pMEETable->iMEECount; ++i)
	{
		if (strcmp(m_pMEETable->MEE[i].szMEEName, m_szMEEToUse) == 0)
		{	// Found the match.
			//
			m_iMEEToUse = i;
			break;
		}
	}

	CDateTime TimeNow;

	if (i == m_pMEETable->iMEECount)
	{
		printf("%s SendToMarket: %s cannot be found!\n", 
				TimeNow.ToStr(iDateTimeOutputStyle),
				m_szMEEToUse);

		throw new CSendToMarketErr(CSendToMarketErr::eMEENameDoesNotExist, "CSendToMarket::FindMEE");
	}
	else
	{
		printf("%s SendToMarket: using %s on %s:%hu\n", 
				TimeNow.ToStr(iDateTimeOutputStyle),
				m_pMEETable->MEE[m_iMEEToUse].szMEEName,
				m_pMEETable->MEE[m_iMEEToUse].szMachineName, 
				m_pMEETable->MEE[m_iMEEToUse].shPort);
	}
}

void CSendToMarket::CreateSocketAndConnect(char *szServer, unsigned short port, SOCKET &ret_sock)
{
	SOCKADDR_IN	addr;
	int			ret;
	PHOSTENT	phe;
	//char		szMsg[128];
	int			iSendBufSize = 4096;	// socket send buffer size
	int			iRecvBufSize = 1024;	// socket receive buffer size - don't receive anything so it should be small

	ret_sock = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, NULL, 0);
	if (ret_sock == INVALID_SOCKET)
		ThrowSystemError("WSASocket()");

	ret = setsockopt(ret_sock, SOL_SOCKET, SO_SNDBUF, (char*)&iSendBufSize, sizeof(iSendBufSize));
	if (ret == SOCKET_ERROR)
		ThrowSystemError("setsockopt(SO_SNDBUF)");

	ret = setsockopt(ret_sock, SOL_SOCKET, SO_RCVBUF, (char*)&iRecvBufSize, sizeof(iRecvBufSize));
	if (ret == SOCKET_ERROR)
		ThrowSystemError("setsockopt(SO_RCVBUF)");

	//zero the struct
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);	//Convert to network ordering

	if ( (phe = gethostbyname(szServer)) == NULL )
		ThrowSystemError("gethostbyname()");

	memcpy((char  *)&(addr.sin_addr), phe->h_addr, phe->h_length);	//copy the obtained IP address

	//_snprintf(szMsg, sizeof(szMsg)-1, "TradeOrder: connecting to %i.%i.%i.%i:%d\n",
	//	(int)phe->h_addr[0], (int)phe->h_addr[1], (int)phe->h_addr[2], (int)phe->h_addr[3], port);
	//cout<<szMsg;

	ret = connect(ret_sock, (const sockaddr*)&addr, sizeof(addr));
	if (ret == SOCKET_ERROR)
		ThrowSystemError("connect()");

}

bool CSendToMarket::SendToMarket(TTradeRequest &trade_mes)
{
	int ret;	

	// open the connection to this MEE if it doesn't exist yet
	if (m_sockfd == INVALID_SOCKET)
	{
		FindMEEToUse();	// set m_iMEEToUse

		CreateSocketAndConnect(m_pMEETable->MEE[m_iMEEToUse].szMachineName, 
								m_pMEETable->MEE[m_iMEEToUse].shPort, m_sockfd);
	}

	ret = send(m_sockfd, (char *)&trade_mes, sizeof(TTradeRequest), 0);

	if (ret == SOCKET_ERROR)
		ThrowSystemError("send");

	return true;
}