State模式(二)

2014-11-24 11:53:44 · 作者: · 浏览: 3


void TCPState::Close(TCPConnection*)
{
cout << "TCPState::Close()" << endl;
}

void TCPState::Synchronize(TCPConnection*)
{
cout << "TCPState::Synchronize()" << endl;
}

void TCPState::Acknowledge(TCPConnection*)
{
cout << "TCPState::Acknowledge()" << endl;
}

void TCPState::Send(TCPConnection*)
{
cout << "TCPState::Send()" << endl;
}

void TCPState::ChangeState(TCPConnection* conn, TCPState* state)
{
cout << "TCPState::ChangeState()" << endl;
conn->ChangeState(state);
}

//监听状态类
TCPListen::~TCPListen(void)
{
cout << "call ~ TCPListen()" << endl;
}

TCPState* TCPListen::Instance(void)
{
return new TCPListen();
}

void TCPListen::Send(TCPConnection* conn)
{
//send SYN, recv SYN ACK, etc;
this->ChangeState(conn, TCPEstablished::Instance());
}

TCPListen::TCPListen(void)
{
cout << "call TCPListen()" << endl;
}

TCPClosed::~TCPClosed(void)
{
cout << "call ~ TCPClosed()" << endl;
}

TCPState* TCPClosed::Instance(void)
{
return new TCPClosed();
}

TCPClosed::TCPClosed(void)
{
cout << "call TCPClosed()" << endl;
}

void TCPClosed::ActiveOpen(TCPConnection* conn)
{
//send SYN, receive SYN,ACK; etc.
this->ChangeState(conn, TCPEstablished::Instance());
}

void TCPClosed::PassiveOpen(TCPConnection* conn)
{
// send FIN, receive ACK of FIN, etc;
this->ChangeState(conn, TCPListen::Instance());
}

TCPEstablished::~TCPEstablished(void)
{
cout << "call ~ TCPEstablished()" << endl;
}

TCPState* TCPEstablished::Instance(void)
{
return new TCPEstablished();
}

void TCPEstablished::Transmit(TCPConnection* conn, TCPStream* st)
{
conn->ProcessStream(st);
}

void TCPEstablished::Close(TCPConnection* conn)
{
this->ChangeState(conn, TCPListen::Instance());
}

TCPEstablished::TCPEstablished(void)
{
cout << "call TCPEstablished()" << endl;
}

class StateApplication
{
public:
StateApplication(void);
~StateApplication(void);

int doRun(void);
};

StateApplication::StateApplication(void){}

StateApplication::~StateApplication(void){}

int StateApplication::doRun(void)
{
TCPConnection tcpConn;
tcpConn.ActiveOpen();
tcpConn.Acknowledge();
tcpConn.Send();
tcpConn.Synchronize();
tcpConn.Close();
tcpConn.PassiveOpen();
tcpConn.Close();
return 0;
}
#endif
#include "State.h"

int main(int argc, char** argv)
{
StateApplication oApp;

oApp.doRun();
return 0;
}

作者:icechenbing