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

📄 netmodel.cc

📁 ns2.1b5版本中cbrp碼
💻 CC
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1991,1993 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *      This product includes software developed by the Computer Systems *      Engineering Group at Lawrence Berkeley Laboratory. * 4. Neither the name of the University nor of the Laboratory may be used *    to endorse or promote products derived from this software without *    specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char rcsid[] ="@(#) $Header: /usr/cvs/ns/ns-src/nam/netmodel.cc,v 1.1.1.1 1998/01/13 15:06:11 root Exp $ (LBL)";#endif#include <stdlib.h>#include <ctype.h>#include "math.h"#include "netview.h"#include "animation.h"#include "nam-queue.h"#include "nam-drop.h"#include "nam-packet.h"#include "nam-edge.h"#include "nam-node.h"#include "nam-trace.h"#include "netmodel.h"#include "paint.h"#include "state.h"#include <float.h>extern int lineno;static int next_pat;/*XXX */double defsize = 0.;class NetworkModelClass : public TclClass { public:	NetworkModelClass() : TclClass("NetworkModel") {}	TclObject* create(int argc, const char*const* argv) {		return (new NetModel);	}} networkmodel_class;NetModel::NetModel()	: drawables_(0), animations_(0), queues_(0),	  views_(0), nodes_(0){	/*XXX*/	int cacheSize = 100;	State::init(cacheSize);	int i;	for (i = 0; i < EDGE_HASH_SIZE; ++i)		hashtab_[i] = 0;	/*XXX*/	nymin_ = 1e6;	nymax_ = -1e6;	Paint *paint = Paint::instance();	int p = paint->thin();	nclass_ = 64;	paint_ = new int[nclass_];	for (i = 0; i < nclass_; ++i)		paint_[i] = p;}NetModel::~NetModel(){}void NetModel::update(double now){	Animation *a, *n;	for (a = animations_; a != 0; a = n) {		n = a->next();		a->update(now);	}	/*	 * Draw all animations and drawables on display to reflect	 * current time.	 */	now_ = now;	for (NetView* p = views_; p != 0; p = p->next_)		p->draw();}void NetModel::update(double now, Animation* a){	for (NetView* p = views_; p != 0; p = p->next_)		a->draw(p, now);}/* Reset all animations and queues to time 'now'. */void NetModel::reset(double now){	Animation* a;	for (a = animations_; a != 0; a = a->next())		a->reset(now);	for (a = drawables_; a != 0; a = a->next())		a->reset(now);	for (NamQueue* q = queues_; q != 0; q = q->next_)		q->reset(now);}/* * Draw this NetModel's animations and drawables (nodes, edges, * packets, queues). */void NetModel::render(NetView* view){	Animation *a;	for (a = animations_; a != 0; a = a->next())		a->draw(view, now_);	for (a = drawables_; a != 0; a = a->next())		a->draw(view, now_);}/* Return a pointer to the edge between 'src' and 'dst'. */NetModel::EdgeHashNode* NetModel::lookupEdge(int src, int dst) const{	EdgeHashNode* h;	for (h = hashtab_[ehash(src, dst)]; h != 0; h = h->next)		if (h->src == src && h->dst == dst)			break;	return (h);}void NetModel::enterEdge(NamEdge* e){	int src = e->src();	int dst = e->dst();	EdgeHashNode *h = lookupEdge(src, dst);	if (h != 0) {		/* XXX */		fprintf(stderr, "nam: duplicate edge (%d,%d)\n");		exit(1);	}	h = new EdgeHashNode;	h->src = src;	h->dst = dst;	h->queue = 0;	h->edge = e;	int k = ehash(src, dst);	h->next = hashtab_[k];	hashtab_[k] = h;}/* XXX Make this cheaper (i.e. cache it) */void NetModel::BoundingBox(BBox& bb){	/* ANSI C limits, from float.h */	bb.xmin = bb.ymin = FLT_MAX;	bb.xmax = bb.ymax = -FLT_MAX;		for (Animation* a = drawables_; a != 0; a = a->next())		a->merge(bb);}Animation* NetModel::inside(double now, float px, float py) const{	for (Animation* a = animations_; a != 0; a = a->next())		if (a->inside(now, px, py))			return (a);	for (Animation* d = drawables_; d != 0; d = d->next())		if (d->inside(now, px, py))			return (d);	return (0);}void NetModel::saveState(double tim){	State* state = State::instance();	StateInfo min;	min.time = 10e6;	min.offset = 0;	Animation *a, *n;	StateInfo si;	/*	 * Update the animation list first to remove any unnecessary	 * objects in the list.	 */	for (a = animations_; a != 0; a = n) {		n = a->next();		a->update(tim);	}	for (a = animations_; a != 0; a = n) {		n = a->next();		si = a->stateInfo();		if (si.time < min.time)			min = si;	}	if (min.offset)		state->set(tim, min);}/* Trace event handler. */void NetModel::handle(const TraceEvent& e, double now){	QueueItem *q;	EdgeHashNode *ehn;	NamEdge* ep;	double txtime;	if (e.time > State::instance()->next())		saveState(e.time);		switch (e.tt) {	case 'v':	/* 'variable' -- just execute it as a tcl command */		Tcl::instance().eval((char *)(e.image + e.ve.str));		break;			case 'h':		ehn = lookupEdge(e.pe.src, e.pe.dst);		if (ehn == 0 || (ep = ehn->edge) == 0)			return;		/*		 * If the current packet will arrive at its destination		 * at a later time, insert the arrival into the queue		 * of animations and set its paint id (id for X graphics		 * context.		 */		txtime = ep->txtime(e.pe.pkt.size);		if (e.time + txtime + ep->GetDelay() >= now) {			/* XXX */			NamPacket *p = new NamPacket(ep, e.pe.pkt, e.time,						     txtime, e.offset);			p->insert(&animations_);			p->paint(paint_[e.pe.pkt.attr & 0xff]);		}		break;			case '+':		ehn = lookupEdge(e.pe.src, e.pe.dst);		if (ehn != 0 && ehn->queue != 0) {			QueueItem *qi = new QueueItem(e.pe.pkt, e.time,						      e.offset);			qi->paint(paint_[e.pe.pkt.attr & 0xff]);			ehn->queue->enque(qi);			qi->insert(&animations_);		}		break;			case '-':		ehn = lookupEdge(e.pe.src, e.pe.dst);		if (ehn != 0 && ehn->queue != 0) {			q = ehn->queue->remove(e.pe.pkt);			delete q;		}		break;			case 'd':		/* Get packet (queue item) to drop. */		ehn = lookupEdge(e.pe.src, e.pe.dst);		if (ehn == 0 || ehn->queue == 0)			return;		q = ehn->queue->remove(e.pe.pkt);		if (q == 0) {			fprintf(stderr,				"trace file drops packet %d which is not in queue\n",				e.pe.pkt.id);			return;		}		float x, y;		q->position(x, y);		int pno = q->paint();		delete q;		/*		 * Compute the point at which the dropped packet disappears.		 * Let's just make this sufficiently far below the lowest		 * thing on the screen.		 *		 * Watch out for topologies that have all their nodes lined		 * up horizontally. In this case, nymin_ == nymax_ == 0.		 * Set the bottom to -0.028. This was chosen empirically.		 * The nam display was set to the maximum size and the lowest		 * position on the display was around -0.028.		 */		float bot;		if (nymin_ == nymax_ && nymin_ == 0.)			bot = -0.03;

⌨️ 快捷键说明

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