📄 commctl.cpp
字号:
}
Part* Signal::getRightPart()
{
return m_pMyRightPart;
}
void Signal::update()
{
m_pMyLeftPart->update();
m_pMyRightPart->update();
}
BOOL Signal::isDone()
{
return m_pMyLeftPart->getDoneFlag() && m_pMyRightPart->getDoneFlag();
}
BOOL Signal::isCollision()
{
return m_pMyLeftPart->getCollisionFlag() || m_pMyRightPart->getCollisionFlag();
}
BOOL Signal::isSent()
{
return m_pMyLeftPart->isSent() && m_pMyRightPart->isSent();
}
//////////////////////////////////////////////////////////////////////
// Station class Construction/Destruction
//////////////////////////////////////////////////////////////////////
Station::Station(int id, int position, Network* network, Medium* medium)
{
myPosition = position;
m_pMyMedium = medium;
m_pMyNetwork = network;
myId = id;
myState = IDLE;
mySendRequest = FALSE;
m_bIsSenting = FALSE;
myBackoffCount = 0;
}
Station::~Station(void)
{
}
void Station::setSendRequest( BOOL flag )
{
mySendRequest = flag;
}
int Station::getState()
{
return myState;
}
void Station::setState(int x)
{
myState=x;
}
int Station::getBackoffCount()
{
return myBackoffCount;
}
int Station::getBackoffRemain()
{
return myCounter.getCount();
}
int Station::getPosition()
{
return myPosition;
}
void Station::sleep(int time)
{
::Sleep(time);
}
void Station::backoff(int count)
{
int slotTime = 200;
int srandt = (unsigned int)pow(2, count);
clock_t tm = ::clock() + (clock_t)this + ::GetCurrentThreadId();
int index = tm % srandt;
myCounter.setCount(index * slotTime);
TRACE("count = %d ms time = %ld, dbtm = %d\n", count, tm, index);
while(myCounter.getCount()>0)
{
sleep(50);
}
}
void Station::update()
{
if (myState==BACKOFF)
{
myCounter.decrement();
}
}
BOOL Station::send()
{
myState = CARRIER_SENSE;
while(!m_pMyMedium->isFree(myPosition))
{
sleep(100);
}
myState = TRANSMIT;
if (FALSE == m_pMyMedium->send(myId, myPosition, m_pMyNetwork->getFrameSize()))
{
myState = BACKOFF;
return FALSE;
}
else
{
myState = IDLE;
myBackoffCount = 0;
return TRUE;
}
}
void Station::run()
{
while (TRUE)
{
if (mySendRequest==TRUE)
{
mySendRequest=FALSE;
m_bIsSenting = TRUE;
myBackoffCount = 0;//0
while (send()==FALSE)
{
myBackoffCount++;
backoff(myBackoffCount);
}
m_bIsSenting = FALSE;
sleep(100);
return;
}
// sleep(100);
}
}
//////////////////////////////////////////////////////////////////////
// Network class Construction/Destruction
//////////////////////////////////////////////////////////////////////
Network::Network()
{
// initializes the medium and also indicates the number of stations
myFrameSize = 150;
m_nCount = 0;
m_pMyMedium = NULL;
m_pMyMedium = new Medium();
for (int j = 0; j < NUM_STATIONS; j++)
{
myStations[j] = NULL;
handleArray[j] = NULL;
}
}
Network::~Network()
{
for (int index = 0; index<m_nCount; index++) //NUM_STATIONS
{
if (handleArray[index] != NULL && myStations[index]->GetIsSenting())
{
if(0 == TerminateThread(handleArray[index], 0))
{
TRACE("Kill thread fail!\n");
}
handleArray[index] = NULL;
}
}
if (NULL != m_pMyMedium)
{
delete m_pMyMedium;
m_pMyMedium = NULL;
}
for (int j = 0; j < NUM_STATIONS; j++)
{
if (NULL != myStations[j])
{
delete myStations[j];
myStations[j] = NULL;
}
}
}
void Network::stop()
{
for(int j=0; j<m_nCount; j++)
myStations[j]->setState(0); //setting the state of stations to idle
update();
}
void RunStationThread(void *lpParam)
{
((Station *)lpParam)->run();
_endthread();
}
void Network::addStation(int x)
{
myStations[m_nCount] = new Station(m_nCount,x, this, m_pMyMedium);
// _beginthread(RunStationThread,0,(void *)myStations[i]);
m_nCount++;
}
int Network::convertPositionToPixel(int position)
{
return (int)((double)(position*400)/(double)m_pMyMedium->getBusLength());
}
int Network::getFrameSize()
{
return myFrameSize;
}
void Network::setFrameSize(int size)
{
myFrameSize = size;
}
void Network::update()
{
int index;
if(m_nCount > 1)
{
m_pMyMedium->update();
}
for (index = 0; index<m_nCount; index++) //NUM_STATIONS
{
myStations[index]->update();
}
}
void Network::paint(CDC* pDC)
{
int index;
int pixel;
char s[128];
CRect rect;
ASSERT(NULL != pDC);
CBrush brushWhite(RGB(255,255,255)); //White brush
CBrush brushGreen(RGB(0,255,0)); //Green brush
CBrush brushBlue(RGB(0,0,255)); //Blue brush
CBrush brushRed(RGB(255,0,0)); //Red brush
CBrush brushBlack(RGB(0,0,0)); //Black brush
CBrush brushBk(::GetSysColor(COLOR_BTNFACE));
int busLength = m_pMyMedium->getBusLength();
int stationNumber = 0;
int backoffCount = 0;
int backoffRemain = 0;
pDC->SetBkMode(TRANSPARENT);
//draw backgroud
rect.SetRect(0, 0, 1024, 768);
pDC->FillRect(&rect, &brushBk);
//draw states
/*
rect.SetRect(120,350,140,370);
pDC->FillRect(&rect, &brushWhite);
pDC->TextOut(150, 355, "IDLE", strlen("IDLE"));
rect.SetRect(240,350,260,370);
pDC->FillRect(&rect, &brushGreen);
pDC->TextOut(270, 355, "TRANSMIT", strlen("TRANSMIT"));
rect.SetRect(360,350,380,370);
pDC->FillRect(&rect, &brushBlue);
pDC->TextOut(380, 355, "CARRIER-SENSE", strlen("CARRIER-SENSE"));
rect.SetRect(480,350,500,370);
pDC->FillRect(&rect, &brushRed);
pDC->TextOut(510, 355, "BACK-OFF", strlen("BACK-OFF"));
*/
rect.SetRect(100,350,120,370);
pDC->FillRect(&rect, &brushWhite);
pDC->TextOut(121, 355, "IDLE", strlen("IDLE"));
rect.SetRect(230,350,250,370);
pDC->FillRect(&rect, &brushGreen);
pDC->TextOut(251, 355, "TRANSMIT", strlen("TRANSMIT"));
rect.SetRect(355,350,375,370);
pDC->FillRect(&rect, &brushBlue);
pDC->TextOut(376, 355, "CARRIER-SENSE", strlen("CARRIER-SENSE"));
rect.SetRect(490,350,510,370);
pDC->FillRect(&rect, &brushRed);
pDC->TextOut(511, 355, "BACK-OFF", strlen("BACK-OFF"));
// draw bus
pDC->TextOut(90, 225, "0", strlen("0"));
rect.SetRect(100,200,500,210);
pDC->FillRect(&rect, &brushBlack);
memset(s, 0, sizeof(s));
sprintf(s, "%d", busLength);
pDC->TextOut(500, 225, s, strlen(s));
pDC->MoveTo(100,210);
pDC->LineTo(100,220);
pDC->MoveTo(499,210);
pDC->LineTo(499,220);
// draw stations and indicate their states
for (index = 0; index<m_nCount; index++)//NUM_STATIONS
{
pixel = convertPositionToPixel(myStations[index]->getPosition());
stationNumber = index + 1;
rect.SetRect(88+pixel,230,88+pixel+25,255);
switch (myStations[index]->getState())
{
case IDLE:
pDC->FillRect(&rect, &brushWhite);
break;
case CARRIER_SENSE:
pDC->FillRect(&rect, &brushBlue);
break;
case TRANSMIT:
pDC->FillRect(&rect, &brushGreen);
break;
case BACKOFF:
pDC->FillRect(&rect, &brushRed);
break;
default:
pDC->FillRect(&rect, &brushBk);
break;
}
memset(s, 0, sizeof(s));
sprintf(s, "Station %d", stationNumber);
pDC->TextOut(85+pixel, 270, s, strlen(s));
pDC->MoveTo(100+pixel,200);
pDC->LineTo(100+pixel,230);
memset(s, 0, sizeof(s));
sprintf(s, "Station %d", stationNumber);
// pDC->TextOut(50, 30+index*40+15, s, strlen(s));
pDC->TextOut(40, 10+index*32+15, s, strlen(s));
if (myStations[index]->getState() == BACKOFF)
{
backoffCount = myStations[index]->getBackoffCount();
backoffRemain = myStations[index]->getBackoffRemain();
memset(s, 0, sizeof(s));
sprintf(s, "k = %d", backoffCount);
pDC->TextOut(90+pixel-3, 300, s, strlen(s));
memset(s, 0, sizeof(s));
sprintf(s, "t = %d", backoffRemain);
pDC->TextOut(90+pixel-3, 320, s, strlen(s));
}
}
// draw frames
int start, end, id;
Signal** signals = m_pMyMedium->getSignals();
for ( index=0; index<MAX_SIGNALS; index++ )
{
BOOL bCollisioned = FALSE;
if ( signals[index] != NULL )
{
id = signals[index]->getStationId();
start = convertPositionToPixel(signals[index]->getLeftPart()->getStart());
end = convertPositionToPixel(signals[index]->getLeftPart()->getEnd());
// rect.SetRect(100+start,(id+1)*40,100+start+abs(end-start),(id+1)*40+8);
rect.SetRect(100+start,(id+1)*32,100+start+abs(end-start),(id+1)*32+8);
bCollisioned = signals[index]->getLeftPart()->getCollisionFlag() \
|| signals[index]->getRightPart()->getCollisionFlag();
// if (signals[index]->getLeftPart()->getCollisionFlag() )
if ( bCollisioned )
{
pDC->FillRect(&rect, &brushRed);
}
else
{
pDC->FillRect(&rect, &brushBlack);
}
start = convertPositionToPixel(signals[index]->getRightPart()->getStart());
end = convertPositionToPixel(signals[index]->getRightPart()->getEnd());
// rect.SetRect(100+end,(id+1)*40,100+end+abs(start-end),(id+1)*40+8);
rect.SetRect(100+end,(id+1)*32,100+end+abs(start-end),(id+1)*32+8);
// if (signals[index]->getRightPart()->getCollisionFlag() )
if ( bCollisioned )
{
pDC->FillRect(&rect, &brushRed);
}
else
{
pDC->FillRect(&rect, &brushBlack);
}
}
}
}
void Network::OnLButtonDown(UINT nFlags, CPoint point)
{
if (nFlags == 1)//LButtonDown
{
if(m_nCount==1)
{
//
}
else if (m_nCount > 1)
{
int index;
int pixel;
for (index = 0; index<NUM_STATIONS; index++)
{
if (NULL == myStations[index])
break;
pixel = convertPositionToPixel(myStations[index]->getPosition());
if ((point.x>=88+pixel)&&(point.x<=113+pixel)&&(point.y>=230)&&(point.y<=255))
{
if(!(myStations[index]->GetIsSenting()))
{
myStations[index]->setSendRequest(TRUE);
handleArray[index] = (HANDLE) _beginthread(RunStationThread,0,(void *)myStations[index]);
}
return;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -