📄 naivenetworkstub.java
字号:
/* * @(#)$Id: NaiveNetworkStub.java,v 1.5 2004/07/02 23:59:22 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package simulator.schedulers.network.node.naive;import java.net.InetAddress;import services.network.Payload;import simulator.schedulers.network.IPMessage;import simulator.schedulers.network.NetworkClient;import simulator.schedulers.network.NetworkScheduler;import simulator.schedulers.network.NetworkStub;/** * Class NaiveNetworkStub * */public class NaiveNetworkStub implements NetworkStub { public static final int OUTGOING = 0; public static final int INCOMING = 1; protected NetworkScheduler scheduler; protected double[] bandwidth; protected boolean shareBandwidth; protected InetAddress myIP; protected boolean online; private NetworkClient UDPHandler; private NetworkClient TCPHandler; /** * Constructor NaiveNetworkStub * * @param scheduler * @param inBandwidth * @param outBandwidth * @param shareBandwidth * @param myIP */ public NaiveNetworkStub(NetworkScheduler scheduler, double inBandwidth, double outBandwidth, boolean shareBandwidth, InetAddress myIP) { this.scheduler = scheduler; this.bandwidth = new double[2]; this.bandwidth[INCOMING] = inBandwidth; this.bandwidth[OUTGOING] = outBandwidth; this.shareBandwidth = shareBandwidth; this.myIP = myIP; this.online = true; } /** * Method getBandwidth * * @param direction * @return */ public double getBandwidth(int direction) { return (((direction == OUTGOING) || shareBandwidth) ? bandwidth[OUTGOING] : bandwidth[INCOMING]); } /** * Method getNewFlowBandwidth * * @param direction * @return */ protected double getNewFlowBandwidth(int direction) { return (((direction == OUTGOING) || shareBandwidth) ? bandwidth[OUTGOING] : bandwidth[INCOMING]); } /** * Method setProtocolHandler * * @param protocol * @param handler */ public void setProtocolHandler(int protocol, NetworkClient handler) { if (protocol == IPMessage.PROTOCOL_TCP) { this.TCPHandler = handler; } if (protocol == IPMessage.PROTOCOL_UDP) { this.UDPHandler = handler; } } /** * Method send * * @param destination * @param protocol * @param data */ public void send(InetAddress destination, int protocol, Payload data) { if (myIP.equals(destination)) { handleMessage(myIP, protocol, data); } else { IPMessage theMessage = IPMessage.allocate(myIP, destination, protocol, data); scheduler.scheduleMessage(theMessage); } } /** * Method handleMessage * * @param theMessage */ public void handleMessage(IPMessage theMessage) { handleMessage(theMessage.getSource(), theMessage.getProtocol(), theMessage.getData()); IPMessage.free(theMessage); } /** * Method setStatus * * @param status */ public void setStatus(boolean status) { online = status; } /** * Method getStatus * @return */ public boolean getStatus() { return online; } /** * Method handleMessage * * @param source * @param protocol * @param data */ protected void handleMessage(InetAddress source, int protocol, Payload data) { if ( !online) { return; } switch (protocol) { case IPMessage.PROTOCOL_TCP: TCPHandler.handleNetwork(source, data); break; case IPMessage.PROTOCOL_UDP: UDPHandler.handleNetwork(source, data); break; default: throw new RuntimeException("Unknown IP Protocol " + protocol); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -