模拟行情服务器

//message.txt
18-7-30 4000 4001 4002 4003 4004 4000 4001 4002 4003 4004 4000 4001 4002 4003 4004 4000 4001 4002 4003 4004
18-7-35 4005 4006 4007
18-7-40 4008
18-7-45 4009 4010#
//SimulateMessage.h
#ifndef SIMULATE_MESSAGE_H
#define SIMULATE_MESSAGE_H
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <string>
#include <time.h>
#include <vector>
struct my_message
{
	time_t m_time;
	//size_t m_count;
	std::vector<std::string> m_prices;
};

class SimulateMessage
{
public:
	SimulateMessage();
	~SimulateMessage();
public:
	void prepare_message();
	time_t SimulateMessage::time_convert(const std::string& rawTime);
protected:
	void push_data_queue();
private:
	std::string m_strFileStream;
	std::string m_strs[1024];
};
#endif // SIMULATE_MESSAGE_H
//SimulateMessage.cpp
#include "stdafx.h"
#include "SimulateMessage.h"
std::vector<my_message> g_simuMsgQueue;
SimulateMessage::SimulateMessage()
{
	std::ifstream is ("message.txt", std::ifstream::binary);
	if (is) 
	{
		// get length of file:
		is.seekg (0, is.end);
		int length = is.tellg();
		is.seekg (0, is.beg);

		char * buffer = new char [length];
		is.read (buffer,length);
		m_strFileStream = buffer;
		
		is.close();
		delete[] buffer;
	}
}

SimulateMessage::~SimulateMessage()
{

}

void SimulateMessage::prepare_message()
{
	// 统计模拟行情测试次数
	std::cout << "\n2====================================" << std::endl;
	size_t _pos = m_strFileStream.find('#');
	m_strFileStream.erase(_pos);
	std::cout << "Before processing:\n" << m_strFileStream.c_str() << std::endl;
	int _count = 0;
	//std::string strs[1024];
	int _index = 0;
	while(true)
	{
		size_t _pos = m_strFileStream.find("\r\n");
		if(_pos != std::string::npos)
		{	
			std::string subString = m_strFileStream.substr(0, _pos);
			m_strs[_index++] = subString;

			m_strFileStream.erase(0, _pos + 1);
		}
		else
		{
			std::string subString = m_strFileStream;
			m_strs[_index] = subString;
			break;
		}
	}

	std::cout << "\n3====================================" << std::endl;
	for(int i = 0; i < 1024; i++)
	{
		int _size = m_strs[i].size();
		if(_size != 1 && _size != 0)
		{
			std::cout << m_strs[i].c_str()/* << std::endl*/;
		}
		else
		{
			_count = i;
			break;
		}
	}

	std::cout << "\n\n4====================================" << std::endl;
	std::cout << "count = " << _count << std::endl;
	for(int i = 0; i < _count; i++)
	{
		std::cout << m_strs[i].c_str();
	}

	// 统计模拟行情的发送时间和每次测试发送的行情个数
	std::cout << "\n\n5====================================" << std::endl;
	std::string sendMessages[1024];
	//std::vector<std::string> sendMessages;
	_index = 0;
	for(int i = 0; i < _count; i++)
	{
		my_message simuMsg;

		std::string sendMessage = m_strs[i];
		bool _isHead = true;
		std::string headString;
		while(true)
		{
			if(_isHead)
			{
				size_t _pos = sendMessage.find(' ');
				headString = sendMessage.substr(0, _pos);

				sendMessage.erase(0, _pos + 1);
				_pos = headString.find('\n');
				if(_pos != std::string::npos)
				{
					headString.erase(headString.begin());
				}
				_isHead = false;

				simuMsg.m_time =  time_convert(headString);
			}
			else
			{
				size_t _pos = sendMessage.find(' ');
				if(_pos != std::string::npos)
				{
					std::string subString = sendMessage.substr(0, _pos);
					sendMessages[_index++] = headString + " " + subString;
					simuMsg.m_prices.push_back(subString);

					sendMessage.erase(0, _pos + 1);
				}
				else
				{
					std::string subString = sendMessage;
					sendMessages[_index++] = headString + " " + subString;
					simuMsg.m_prices.push_back(subString);
					break;
				}
			}
		}
		g_simuMsgQueue.push_back(simuMsg);
	}
	for(int i = 0; i < 1024; ++i)
	{
		int _size = sendMessages[i].size();
		if(_size != 1 && _size != 0)
		{
			std::cout << sendMessages[i].c_str() << std::endl;
		}
		else
		{
			_count = i;
			break;
		}
	}

	std::cout << "\n6====================================" << std::endl;
	std::cout << "_count = " << _count << std::endl;
	my_message simuMsg;
	for(int i = 0; i < _count; i++)
	{
		std::string subMessage = sendMessages[i];
		_pos = subMessage.find(' ');
		std::string strTime = subMessage.substr(0, _pos);
		simuMsg.m_time =  time_convert(strTime);

		subMessage.erase(0, _pos + 1);
		std::string strPrice = subMessage.substr();

	}
}

void SimulateMessage::push_data_queue()
{
	my_message _message;
	time_t now;
	time(&now);
	_message.m_time = now + 30;
	std::string _price("3987.65");
	_message.m_prices.push_back(_price);
	_message.m_prices.push_back(_price);
	_message.m_prices.push_back(_price);
	g_simuMsgQueue.push_back(_message);
}

time_t SimulateMessage::time_convert(const std::string& rawTime)
{
	std::string strTime = rawTime;
	//std::string strTime = "2013-12-16-13-21-56";
	std::string strTimes[3];
	unsigned int _index = 0;
	while(true)
	{
		size_t _pos = strTime.find('-');
		std::string strCurrent = strTime.substr(0, _pos);
		if(_pos != std::string::npos)
		{
			std::string subString = strTime.substr(0, _pos);
			strTimes[_index++] = subString;

			strTime.erase(0, _pos + 1);
		}
		else
		{
			std::string subString = strTime;
			strTimes[_index] = subString;
			break;
		}
	}

	time_t now;
	time(&now);
	struct tm _tmTime = *localtime(&now);
	//_tmTime.tm_year		= atoi(strTimes[0].c_str()) - 1900;
	//_tmTime.tm_mon		= atoi(strTimes[1].c_str())  - 1;
	//_tmTime.tm_mday	= atoi(strTimes[2].c_str()) ;
	_tmTime.tm_hour		= atoi(strTimes[0].c_str()) ;
	_tmTime.tm_min		= atoi(strTimes[1].c_str()) ;
	_tmTime.tm_sec		= atoi(strTimes[2].c_str()) ;

	printf("%s", asctime(&_tmTime));

	return mktime(&_tmTime);
}
//ServerDlg.cpp
unsigned int __stdcall CServerDlg::ThreadSimulateProc(PVOID lpParam)
{
	::AllocConsole();    // 打开控件台资源
	freopen("CONOUT$", "w+t", stdout);    // 申请写
	freopen( "CONIN$", "r+t", stdin ); // 申请读
	printf("call console successfully !\n");

	CServerDlg *pServerDlg = (CServerDlg *)lpParam;

	SimulateMessage  *pSimuMsg = new SimulateMessage;
	pSimuMsg->prepare_message();
	int _count = 0;
	int _size = g_simuMsgQueue.size();
	std::cout << "\nsize of g_simuMsgQueue is " << _size << std::endl;

	int __count = 0;
	for(std::vector<my_message>::iterator it = g_simuMsgQueue.begin(); it != g_simuMsgQueue.end(); ++it)
	{
		my_message _message = *it;
		for(std::vector<std::string>::iterator itPrice = _message.m_prices.begin(); itPrice != _message.m_prices.end(); ++itPrice)
		{
			std::cout << _message.m_time << ": " << (*itPrice) << std::endl;
		}
		std::cout << std::endl;
	}

	while(g_simuMsgQueue.size() >= _count +1)
	{
		my_message _message = g_simuMsgQueue[_count++];
		while(true)
		{
			time_t _now;
			time(&_now);
			if(_now == _message.m_time)
			{
				int countSend = _message.m_prices.size();
				cout << "sum of messages is: " << _message.m_prices.size() << std::endl;
				int interval = 1000/countSend;
				int _count = 0;
				while(countSend >= _count + 1)
				{
					std::string strPrice = _message.m_prices[_count];
					char chSrz[256];
					strcpy(chSrz, strPrice.c_str());
					
				WaitForSingleObject( g_hMutex , INFINITE );
				for(std::deque<CONNID>::iterator it = connQueue.begin(); it != connQueue.end(); it++)
				{
					CServerDlg* pServerDlg = (CServerDlg*)lpParam;
					pServerDlg->m_Server.Send(*it, (const BYTE*)chSrz, strlen(chSrz) + 1);
				}
				ReleaseMutex( g_hMutex );
					Sleep(interval);
					++_count;
				}
				
				std::cout << "send simulated messages successfully!" << std::endl;
				break;
			}
			else
				continue;
		}
	}
	delete pSimuMsg;
	//::FreeConsole();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值