// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

// PURPOSE:

#ifndef BD_REMOTE_TASK_MANAGER_H
#define BD_REMOTE_TASK_MANAGER_H

#include <bdCore/bdCore.h>

#include <bdLobby/bdLobbyErrorCode.h>
#include <bdLobby/bdRemoteTaskManager/bdRemoteTask.h>

BD_REFERENCE(bdLobbyConnection);


// default timeout in seconds for remote tasks (<= 0 is infinite)
#define BD_DEFAULT_REMOTE_TASK_TIMEOUT (0.0f)

/// Internal helper class used by the various services to perform remote tasks.
class bdRemoteTaskManager 
{
	public:

		BD_DECLARE_NEW_AND_DELETE_OPERATORS

		bdRemoteTaskManager(bdLobbyConnectionRef connection,
							const bdBool useEncryption = true);

		virtual ~bdRemoteTaskManager();

		/// Creates a remote task
		/// \param task[in|out] pointer to a remote task object to which the 
		///			new task has been assigned
		/// \param serviceID[in] The service ID of this task
		/// \param taskID[in] A unique task ID
		/// \param queryParams[in] A bitbuffer that is appended to the payload of the query
		/// \return The error code of the operation. This function may fail
		///		if too many tasks are in use or too many tasks have been created
		///		within a certain time period
		bdLobbyErrorCode startTask(bdRemoteTask **task,
								   const bdUByte8 serviceID,
								   const bdUByte8 taskID,
								   bdBitBuffer &queryParams,
								   const bdFloat32 timeout = BD_DEFAULT_REMOTE_TASK_TIMEOUT);
	

		/// \return the connection ID. This will be 0 if the LSG has yet to respond with a connection ID.
		bdUInt64 getConnectionID() const;

	protected:		

		void handleTaskReply(bdBitBufferRef buffer);
	
	protected:		

		friend class bdLobbyServiceImpl;

		bdLinkedList<bdRemoteTask*> m_tasks;

		bdLobbyConnectionRef m_lobbyConnection;

		bdBool m_encryptedConnection;

		bdUInt64 m_connectionID;
};

#endif // BD_REMOTE_TASK_MANAGER_H
