-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.cpp
More file actions
48 lines (41 loc) · 1.35 KB
/
host.cpp
File metadata and controls
48 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "host.h"
#include <cstdlib>
#include <iostream>
Host::~Host()
{
for (int i = 0; i < (int)services_.size(); ++i)
{
delete services_[i];
}
}
// 호스트와 설치된 서비스를 전부 초기화한다.
void Host::initialize()
{
}
// 링크를 랜덤으로 하나 선택하여 패킷을 전송한다.
void Host::send(Packet *packet)
{
int num = rand() % links_.size();
std::cout << "Host #" << id() << ": sending packet (from: " << packet->srcAddress().toString() << ", to: " << packet->destAddress().toString() << ", " << packet->dataString().length() << " bytes)" << std::endl;
links_.at(num)->whatLink(this, packet);
}
void Host::receiving()
{
bool isTrue = false;
std::cout << services_.size();
std::cout << "Host #" << id() << ": received packet, destination port: " << packets->destPort() << std::endl;
for(int i = 0; i < (int)services_.size(); ++i)
{
if(services_[i]->getPort() == packets->destPort())
{
services_[i]->result();
isTrue = true;
break;
}
}
if (!isTrue)
{
std::cout << "Host #" << id() << ": no service for packet (from: " << packets->srcAddress().toString() << ", to: " << packets->destAddress().toString() << ", " << packets->dataString().length() << " bytes" << std::endl;
delete packets;
}
}