
// Auto-generated file -- do not edit

#include "Box.h"

#include <sstream>

#include "autogen_TestProtocolServer.h"
#include "IOStream.h"

#include "TestContext.h"

#include "MemLeakFindOn.h"
TestProtocolObject::TestProtocolObject()
{
}
TestProtocolObject::~TestProtocolObject()
{
}
TestProtocolObject::TestProtocolObject(const TestProtocolObject &rToCopy)
{
}
std::auto_ptr<ProtocolObject> TestProtocolObject::DoCommand(TestProtocolServer &rProtocol, TestContext &rContext)
{
	THROW_EXCEPTION(ConnectionException, Conn_Protocol_TriedToExecuteReplyCommand)
}

TestProtocolServerError::TestProtocolServerError()
	: mType(0),
	  mSubType(0)
{
}
TestProtocolServerError::TestProtocolServerError(const TestProtocolServerError &rToCopy)
	: mType(rToCopy.mType),
	  mSubType(rToCopy.mSubType)
{
}
TestProtocolServerError::~TestProtocolServerError()
{
}
int TestProtocolServerError::GetType() const
{
	return 0;
}
TestProtocolServerError::TestProtocolServerError(int32_t Type, int32_t SubType)
	: mType(Type),
	  mSubType(SubType)
{
}
void TestProtocolServerError::WritePropertiesToStreamData(Protocol &rProtocol) const
{
	rProtocol.Write(mType);
	rProtocol.Write(mSubType);
}
bool TestProtocolServerError::IsError(int &rTypeOut, int &rSubTypeOut) const
{
	rTypeOut = mType;
	rSubTypeOut = mSubType;
	return true;
}
std::string TestProtocolServerError::GetMessage() const
{
	switch(mSubType)
	{
		default:
			std::ostringstream out;
			out << "Unknown subtype " << mSubType;
			return out.str();
	}
}
TestProtocolServerHello::TestProtocolServerHello()
	: mNumber32(0),
	  mNumber16(0),
	  mNumber8(0)
{
}
TestProtocolServerHello::TestProtocolServerHello(const TestProtocolServerHello &rToCopy)
	: mNumber32(rToCopy.mNumber32),
	  mNumber16(rToCopy.mNumber16),
	  mNumber8(rToCopy.mNumber8),
	  mText(rToCopy.mText)
{
}
TestProtocolServerHello::~TestProtocolServerHello()
{
}
int TestProtocolServerHello::GetType() const
{
	return 1;
}
void TestProtocolServerHello::SetPropertiesFromStreamData(Protocol &rProtocol)
{
	rProtocol.Read(mNumber32);
	rProtocol.Read(mNumber16);
	rProtocol.Read(mNumber8);
	rProtocol.Read(mText);
}
TestProtocolServerHello::TestProtocolServerHello(int32_t Number32, int16_t Number16, int8_t Number8, const std::string& Text)
	: mNumber32(Number32),
	  mNumber16(Number16),
	  mNumber8(Number8),
	  mText(Text)
{
}
void TestProtocolServerHello::WritePropertiesToStreamData(Protocol &rProtocol) const
{
	rProtocol.Write(mNumber32);
	rProtocol.Write(mNumber16);
	rProtocol.Write(mNumber8);
	rProtocol.Write(mText);
}
TestProtocolServerLists::TestProtocolServerLists()
{
}
TestProtocolServerLists::TestProtocolServerLists(const TestProtocolServerLists &rToCopy)
	: mLotsOfText(rToCopy.mLotsOfText)
{
}
TestProtocolServerLists::~TestProtocolServerLists()
{
}
int TestProtocolServerLists::GetType() const
{
	return 2;
}
void TestProtocolServerLists::SetPropertiesFromStreamData(Protocol &rProtocol)
{
	rProtocol.ReadVector(mLotsOfText);
}
TestProtocolServerListsReply::TestProtocolServerListsReply()
	: mNumberOfStrings(0)
{
}
TestProtocolServerListsReply::TestProtocolServerListsReply(const TestProtocolServerListsReply &rToCopy)
	: mNumberOfStrings(rToCopy.mNumberOfStrings)
{
}
TestProtocolServerListsReply::~TestProtocolServerListsReply()
{
}
int TestProtocolServerListsReply::GetType() const
{
	return 3;
}
TestProtocolServerListsReply::TestProtocolServerListsReply(int32_t NumberOfStrings)
	: mNumberOfStrings(NumberOfStrings)
{
}
void TestProtocolServerListsReply::WritePropertiesToStreamData(Protocol &rProtocol) const
{
	rProtocol.Write(mNumberOfStrings);
}
TestProtocolServerQuit::TestProtocolServerQuit()
{
}
TestProtocolServerQuit::TestProtocolServerQuit(const TestProtocolServerQuit &rToCopy)
{
}
TestProtocolServerQuit::~TestProtocolServerQuit()
{
}
int TestProtocolServerQuit::GetType() const
{
	return 4;
}
void TestProtocolServerQuit::SetPropertiesFromStreamData(Protocol &rProtocol)
{
}
void TestProtocolServerQuit::WritePropertiesToStreamData(Protocol &rProtocol) const
{
}
bool TestProtocolServerQuit::IsConversationEnd() const
{
	return true;
}
TestProtocolServerSimple::TestProtocolServerSimple()
	: mValue(0)
{
}
TestProtocolServerSimple::TestProtocolServerSimple(const TestProtocolServerSimple &rToCopy)
	: mValue(rToCopy.mValue)
{
}
TestProtocolServerSimple::~TestProtocolServerSimple()
{
}
int TestProtocolServerSimple::GetType() const
{
	return 5;
}
void TestProtocolServerSimple::SetPropertiesFromStreamData(Protocol &rProtocol)
{
	rProtocol.Read(mValue);
}
TestProtocolServerSimpleReply::TestProtocolServerSimpleReply()
	: mValuePlusOne(0)
{
}
TestProtocolServerSimpleReply::TestProtocolServerSimpleReply(const TestProtocolServerSimpleReply &rToCopy)
	: mValuePlusOne(rToCopy.mValuePlusOne)
{
}
TestProtocolServerSimpleReply::~TestProtocolServerSimpleReply()
{
}
int TestProtocolServerSimpleReply::GetType() const
{
	return 6;
}
TestProtocolServerSimpleReply::TestProtocolServerSimpleReply(int32_t ValuePlusOne)
	: mValuePlusOne(ValuePlusOne)
{
}
void TestProtocolServerSimpleReply::WritePropertiesToStreamData(Protocol &rProtocol) const
{
	rProtocol.Write(mValuePlusOne);
}
TestProtocolServerGetStream::TestProtocolServerGetStream()
	: mStartingValue(0),
	  mUncertainSize(0)
{
}
TestProtocolServerGetStream::TestProtocolServerGetStream(const TestProtocolServerGetStream &rToCopy)
	: mStartingValue(rToCopy.mStartingValue),
	  mUncertainSize(rToCopy.mUncertainSize)
{
}
TestProtocolServerGetStream::~TestProtocolServerGetStream()
{
}
int TestProtocolServerGetStream::GetType() const
{
	return 7;
}
void TestProtocolServerGetStream::SetPropertiesFromStreamData(Protocol &rProtocol)
{
	rProtocol.Read(mStartingValue);
	rProtocol.Read(mUncertainSize);
}
TestProtocolServerGetStream::TestProtocolServerGetStream(int32_t StartingValue, bool UncertainSize)
	: mStartingValue(StartingValue),
	  mUncertainSize(UncertainSize)
{
}
void TestProtocolServerGetStream::WritePropertiesToStreamData(Protocol &rProtocol) const
{
	rProtocol.Write(mStartingValue);
	rProtocol.Write(mUncertainSize);
}
TestProtocolServerSendStream::TestProtocolServerSendStream()
	: mValue(0)
{
}
TestProtocolServerSendStream::TestProtocolServerSendStream(const TestProtocolServerSendStream &rToCopy)
	: mValue(rToCopy.mValue)
{
}
TestProtocolServerSendStream::~TestProtocolServerSendStream()
{
}
int TestProtocolServerSendStream::GetType() const
{
	return 8;
}
void TestProtocolServerSendStream::SetPropertiesFromStreamData(Protocol &rProtocol)
{
	rProtocol.Read(mValue);
}
TestProtocolServerString::TestProtocolServerString()
{
}
TestProtocolServerString::TestProtocolServerString(const TestProtocolServerString &rToCopy)
	: mTest(rToCopy.mTest)
{
}
TestProtocolServerString::~TestProtocolServerString()
{
}
int TestProtocolServerString::GetType() const
{
	return 9;
}
void TestProtocolServerString::SetPropertiesFromStreamData(Protocol &rProtocol)
{
	rProtocol.Read(mTest);
}
TestProtocolServerString::TestProtocolServerString(const std::string& Test)
	: mTest(Test)
{
}
void TestProtocolServerString::WritePropertiesToStreamData(Protocol &rProtocol) const
{
	rProtocol.Write(mTest);
}
TestProtocolServer::TestProtocolServer(IOStream &rStream)
	: Protocol(rStream)
{
}
TestProtocolServer::~TestProtocolServer()
{
	DeleteStreamsToSend();
}
const char *TestProtocolServer::GetIdentString()
{
	return "Test-0.00";
}
std::auto_ptr<ProtocolObject> TestProtocolServer::MakeProtocolObject(int ObjType)
{
	switch(ObjType)
	{
	case 0:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerError);
		break;
	case 1:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerHello);
		break;
	case 2:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerLists);
		break;
	case 3:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerListsReply);
		break;
	case 4:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerQuit);
		break;
	case 5:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerSimple);
		break;
	case 6:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerSimpleReply);
		break;
	case 7:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerGetStream);
		break;
	case 8:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerSendStream);
		break;
	case 9:
		return std::auto_ptr<ProtocolObject>(new TestProtocolServerString);
		break;
	default:
		THROW_EXCEPTION(ConnectionException, Conn_Protocol_UnknownCommandRecieved)
	}
}
std::auto_ptr<TestProtocolObject> TestProtocolServer::Receive()
{
	std::auto_ptr<TestProtocolObject> preply((TestProtocolObject*)(Protocol::Receive().release()));


	return preply;
}

void TestProtocolServer::Send(const TestProtocolObject &rObject)
{
	Protocol::Send(rObject);
}

void TestProtocolServer::DoServer(TestContext &rContext)
{
	// Handshake with client
	Handshake();

	// Command processing loop
	bool inProgress = true;
	while(inProgress)
	{
		// Get an object from the conversation
		std::auto_ptr<TestProtocolObject> pobj(Receive());

		// Run the command
		std::auto_ptr<TestProtocolObject> preply((TestProtocolObject*)(pobj->DoCommand(*this, rContext).release()));
		
		// Send the reply
		Send(*(preply.get()));

		// Send any streams
		for(unsigned int s = 0; s < mStreamsToSend.size(); s++)
		{
			// Send the streams
			SendStream(*mStreamsToSend[s]);
		}
		// Delete these streams
		DeleteStreamsToSend();

		// Does this end the conversation?
		if(pobj->IsConversationEnd())
		{
			inProgress = false;
		}
	}	
}

void TestProtocolServer::SendStreamAfterCommand(IOStream *pStream)
{
	ASSERT(pStream != NULL);
	mStreamsToSend.push_back(pStream);
}

void TestProtocolServer::DeleteStreamsToSend()
{
	for(std::vector<IOStream*>::iterator i(mStreamsToSend.begin()); i != mStreamsToSend.end(); ++i)
	{
		delete (*i);
	}
	mStreamsToSend.clear();
}

