⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.cpp

📁 模拟P2P各种网络环境的,适合新手们的学习,不错的源码.
💻 CPP
字号:
#include "Node.h"
#include "Agent.h"
#include "Link.h"

ostream &operator<<(ostream &os, const Node &node)
{
	return os << node.mAID << '\t' << node.mNodeID << '\t' << node.getType() << '\t' << node.mLinkSet.size() << '\t' << node.mAgentList.size() << '\t' << node.mNodeProperty.getLable();
}

Node::Node(): mAID(-1), mNodeID(-1), mActive(true)
{ setType(INV_NODE); }

void Node::setType(int type)
{ mNodeProperty.setType(type); }

int Node::getType() const
{ return mNodeProperty.getType(); }

void Node::reset() /* reset mActive and mAgentList; */
{ mActive = true; mAgentList.clear(); }

void Node::setAID(int id)
{ mAID = id; }

int Node::getAID() const
{ return mAID; }

void Node::setID(int id)
{ mNodeID = id; }

int Node::getID() const
{ return mNodeID; }

void Node::setLinkTo(int id)
{ mLinkSet.insert(id); }

bool Node::haveLinkTo(int id) const
{ return mLinkSet.find(id) != mLinkSet.end(); }

Link *Node::getVLink(Agent *agent)
{
	int id = agent->getID();
	return mVLinkMap.find(id) == mVLinkMap.end() ? NULL : mVLinkMap[id];
}

set<int> Node::getLinkSet() const
{ return mLinkSet; }

void Node::setProperty(const NodeProperty &property)
{ mNodeProperty = property; }

NodeProperty Node::getProperty() const
{ return mNodeProperty; }

list<Agent *> Node::getAgentList() const
{ return mAgentList; }

bool Node::isActive() const
{ return mActive; }

bool Node::handle(SimEvent *event) /* for further development */
{ return true; }

void Node::attach(Agent *agent)
{
	int id1, id2;
	unsigned long band1, band2;

	Link *pl = new Link();
	LinkProperty property;
	BandwidthManager manager;

	property.setType(VIR_LIN);
	id1 = agent->getID();
	id2 = agent->getNode()->getID();
	band1 = (unsigned long)agent->getInBandwidth();
	band2 = (unsigned long)agent->getOutBandwidth();
	
	if(id1 < id2) {
		pl->setFrom(id1);
		pl->setTo(id2);
		property.setBandwidth(band1 + band2);
		manager.setBandwidth(true, band2);
		manager.setBandwidth(false, band1);
		pl->setProperty(property);
		pl->setBandwidthManager(manager);
	}
	else {
		pl->setFrom(id2);
		pl->setTo(id1);
		property.setBandwidth(band1 + band2);
		manager.setBandwidth(true, band1);
		manager.setBandwidth(false, band2);
		pl->setProperty(property);
		pl->setBandwidthManager(manager);
	}

	mVLinkMap[id1] = pl;
	mAgentList.push_back(agent);
}

void Node::detach(Agent *agent)
{
	int id;

	id = agent->getID();
	Link *pl = mVLinkMap[id];
	if(!pl)
		delete pl;
	mVLinkMap.erase(id);
	mAgentList.remove(agent);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -