Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C++

Push Framework - A C++ toolkit for high performance server development

Rate me:
Please Sign up or sign in to vote.
4.96/5 (86 votes)
23 May 2012Apache15 min read 260K   26.9K   316  
Write asynchronous, multithreaded servers in a few lines of code. Monitor realtime activity with a deploy-only dashboard.
#include "StdAfx.h"
#include "Bot.h"



Bot::Bot(Options_t& _options)
:options(_options)
{
	isLogged = false;
	//
}

Bot::~Bot(void)
{
	//
}

unsigned int Bot::getLoggedTime()
{
	if(isLogged)
	{
		COleDateTimeSpan dtSpan = COleDateTime::GetCurrentTime() - dtLoggedTime;
		return dtSpan.GetTotalSeconds();
	}
	return 0;
}

void Bot::setLogged()
{
	dtLoggedTime = COleDateTime::GetCurrentTime();
	isLogged = true;
}

void Bot::onPerformAutomatedJob()
{
	if (!isLogged)
		return;

	static int nCentiSec = 0;
	static int nSecs = 0;
	
	
	if(nCentiSec == 10)
	{
		nSecs++;
		nCentiSec = 0;
	}
	else
		nCentiSec ++;


	if (options.sendRoomChatDuration > 0 && (nSecs % options.sendRoomChatDuration ==  0) )
	{
		SendChatToJoinedRoom();
	}


	for (int i=0;i<options.directChatBulks;i++)
		SendChatToRandomClient();

	/*
	if (options.directChatDuration > 0 && (nSecs % options.directChatDuration == 0) )
		{
			for (int i=0;i<options.directChatBulks;i++)
				SendChatToRandomClient();
		}*/
	

	if (options.joinRoomDuration > 0 && (nSecs % options.joinRoomDuration == 0) )
	{
		JoinRandomRoom();
	}

	if (getLoggedTime() > options.stayDuration)
	{
		Logout();
	}
	
}

void Bot::onConnected( bool bResult )
{
	if(bResult)
	{
		cout << ChatAPI::getPseudo() << " has succesfully connected" << std::endl;
	}
	else
	{
		cout << ChatAPI::getPseudo() << " has failed to connect" << std::endl;
	}
}

void Bot::OnLoginResponse( bool bAccepted, std::string msg )
{
	if(bAccepted)
	{
		setLogged();
		cout << ChatAPI::getPseudo() << " has logged in" << std::endl;
	}
	else
	{
		cout << ChatAPI::getPseudo() << " was unable to login" << std::endl;
	}
}

void Bot::OnRooms( std::vector<std::string>& roomsList )
{
	this->roomsList = roomsList;
}

void Bot::onConnectionClosed( bool bPeerClose )
{
	isLogged = false;
	cout << ChatAPI::getPseudo() << " 'connection was closed" << std::endl;
}

void Bot::OnDirectChat( std::string sender, std::string msg )
{

	if (options.replyDirectProb > 0)
	{
		if (rand() % 100  > directChatSinking)
		{
			std::string str = "hi " + sender + " i received ur msg";
			SendDirectChat(sender, str);
		}
	}
	
}

void Bot::OnJoinRoom( std::string roomName )
{
	joinedRoom = roomName;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead
Tunisia Tunisia
Services:
http://www.pushframework.com/?page_id=890

Comments and Discussions