📄 packetrenderapplet.java
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.List;
import javax.swing.Timer;
class PacketWindow {
protected PacketRenderApplet applet;
protected int start_sqn;
protected int window_size;
protected int window[];
public int finished = 0;
public PacketWindow(PacketRenderApplet applet, int start_sqn, int window_size) {
this.applet = applet;
this.start_sqn = start_sqn;
this.window_size = window_size;
this.window = new int[applet.numPackets];
for (int i=0; i<applet.numPackets; ++i)
this.window[i] = 0;
}
public void receivePacket (Packet packet) {}
}
class PacketTimer extends Timer {
public int packet_no;
public PacketTimer (int delay, ActionListener actionListener, int packet_no) {
super (delay, actionListener);
this.packet_no = packet_no;
}
}
class TimeoutHandler implements ActionListener {
private SenderWindow swin;
public PacketTimer timer;
public void actionPerformed (ActionEvent e) {
synchronized (swin.applet.paintLock) {
Thread thread = new SimplePacketThread(swin.applet, timer.packet_no);
swin.applet.addThread(thread);
thread.start();
swin.applet.fillPacketNumbersList(swin.applet.packetType.getSelectedItem());
}
}
public void setTimer (PacketTimer timer) {
this.timer = timer;
}
public TimeoutHandler (SenderWindow swin) {
this.swin = swin;
}
}
class SenderWindow extends PacketWindow {
Vector timers = new Vector();
private int timeout=25000;
public SenderWindow (PacketRenderApplet applet, int start_sqn, int window_size) {
super(applet, start_sqn, window_size);
}
public void drawWindow (Color color) {
synchronized (applet.paintLock) {
applet.gBuffer.setColor(color);
applet.gBuffer.draw3DRect(applet.left + start_sqn * applet.gap - 5, applet.top - 5, applet.gap * window_size, applet.packetSize + 10, true);
applet.repaint();
}
}
synchronized public void addTimer (int packet_no) {
TimeoutHandler timeoutHandler = new TimeoutHandler (this);
PacketTimer timer = new PacketTimer (timeout, timeoutHandler, packet_no);
timeoutHandler.setTimer(timer);
timers.addElement(timer);
timer.start();
}
synchronized public void resumeTimer (int packet_no) {
Enumeration e = timers.elements();
while (e.hasMoreElements()) {
PacketTimer timer = (PacketTimer) e.nextElement();
if (timer.packet_no == packet_no) {
timer.start();
}
}
}
synchronized public void stopTimer (int packet_no) {
Enumeration e = timers.elements();
while (e.hasMoreElements()) {
PacketTimer timer = (PacketTimer) e.nextElement();
if (timer.packet_no == packet_no) {
timer.stop();
}
}
}
synchronized public void removeTimer (int packet_no) {
Enumeration e = timers.elements();
stopTimer(packet_no);
while (e.hasMoreElements()) {
PacketTimer timer = (PacketTimer) e.nextElement();
if (timer.packet_no == packet_no) {
timers.removeElement(timer);
}
}
}
synchronized public void startAllTimers () {
Enumeration e = timers.elements();
while (e.hasMoreElements()) {
PacketTimer timer = (PacketTimer) e.nextElement();
timer.start();
}
}
synchronized public void stopAllTimers () {
Enumeration e = timers.elements();
while (e.hasMoreElements()) {
PacketTimer timer = (PacketTimer) e.nextElement();
timer.stop();
}
}
synchronized public void removeAllTimers () {
stopAllTimers();
timers.removeAllElements();
}
public void receivePacket (Packet packet) {
synchronized (applet.paintLock) {
applet.drawStatus("Ack " + packet.packet_no + " received", Color.green);
applet.repaint();
}
if ((packet.packet_no >= start_sqn) && (packet.packet_no < start_sqn + window_size)) {
removeTimer(packet.packet_no);
window[packet.packet_no] = 1;
int i = start_sqn;
int index;
int j=0;
while ((i < applet.numPackets)&&(window[i] == 1)) {
index = i + window_size;
if (index < applet.numPackets) {
synchronized (applet.paintLock) {
Thread thread = new SimplePacketThread(applet, index, 400*j);
applet.addThread(thread);
thread.start();
addTimer(index);
}
j++;
}
i++;
}
drawWindow (Color.black);
start_sqn = i;
drawWindow (Color.gray);
}
if (start_sqn == applet.numPackets) {
drawWindow(Color.black);
finished = 1;
synchronized (applet.paintLock) {
applet.endButtonState();
}
}
}
}
class ReceiverWindow extends PacketWindow {
public ReceiverWindow (PacketRenderApplet applet, int start_sqn, int window_size) {
super(applet, start_sqn, window_size);
}
public void drawWindow (Color color) {
synchronized (applet.paintLock) {
applet.gBuffer.setColor(color);
applet.gBuffer.draw3DRect(applet.left + start_sqn * applet.gap - 5, applet.bottom - 5, applet.gap * window_size, applet.packetSize + 10, true);
applet.repaint();
}
}
public void receivePacket (Packet packet) {
synchronized (applet.paintLock) {
applet.drawStatus("Packet " + packet.packet_no + " received", Color.green);
applet.repaint();
}
if (packet.packet_no >= start_sqn + window_size) return;
if ((packet.packet_no >= start_sqn)) {
window[packet.packet_no] = 1;
int i = start_sqn;
int index;
while ((i < applet.numPackets)&&(window[i] == 1)) {
i++;
}
drawWindow (Color.black);
start_sqn = i;
drawWindow (Color.gray);
}
synchronized (applet.paintLock) {
Thread thread = new SimpleAckThread(applet, packet.packet_no);
applet.addThread(thread);
thread.start();
}
if (start_sqn == applet.numPackets) {
drawWindow(Color.black);
finished = 1;
}
}
}
class Packet {
public int packet_no;
public String type;
public Color color;
public int packetSize;
public Packet(int packet_no, String type) {
this.packet_no = packet_no;
this.type = type;
if (type.equals("PACKET")) {
this.color = Color.red;
}
else if (type.equals("ACK")) {
this.color = Color.yellow;
}
packetSize = 15;
}
}
// This Thread draws a packet starting from location x0, y0 to location x1, y1
// You can also specify the speed with which packet moves and the color of the packet
class PacketDrawingThread extends Thread {
protected PacketRenderApplet applet;
private int x;
private int y0;
private int y1;
private int oldy0;
private Object lock = new Object();
private int suspend = 0;
private int disappear = 0;
public Packet packet;
public PacketDrawingThread(PacketRenderApplet applet, Packet packet, int x, int y0, int y1) {
this.applet = applet;
this.x = x;
this.y0 = y0;
this.y1 = y1;
this.oldy0 = y0;
this.packet = packet;
}
public void suspendThread() {
suspend = 1;
}
public void resumeThread() {
suspend = 0;
resume();
}
public void run()
{
int yinterval;
int distanceRemaining;
int temp;
synchronized (applet.paintLock) {
if (packet.type == "PACKET") {
applet.drawStatus("Packet " + packet.packet_no + " sent", Color.green);
}
else if (packet.type == "ACK") {
applet.drawStatus("Ack " + packet.packet_no + " sent", Color.green);
}
applet.gBuffer.setColor(packet.color);
applet.gBuffer.fillRect(x, y0, packet.packetSize, packet.packetSize);
applet.gBuffer.setFont(new Font(null, Font.BOLD, 14));
applet.gBuffer.setColor(Color.black);
applet.gBuffer.drawString(String.valueOf(packet.packet_no), x+packet.packetSize/4, y0+packet.packetSize - 3);
applet.repaint();
}
while (y1 != y0) {
if (suspend == 1) suspend();
try {this.sleep(30);}
catch(Exception e) {}
yinterval = applet.speed;
if (y1 >= y0) {
distanceRemaining = y1 - y0;
if (distanceRemaining > yinterval)
y0 += yinterval;
else
y0 = y1;
}
else {
distanceRemaining = y0 - y1;
if (distanceRemaining > yinterval)
y0 -= yinterval;
else
y0 = y1;
}
synchronized (applet.paintLock) {
applet.gBuffer.setColor(Color.black);
applet.gBuffer.fillRect(x, oldy0, packet.packetSize, packet.packetSize);
applet.gBuffer.setColor(packet.color);
applet.gBuffer.fillRect(x, y0, packet.packetSize, packet.packetSize);
applet.gBuffer.setFont(new Font(null, Font.BOLD, 14));
applet.gBuffer.setColor(Color.black);
applet.gBuffer.drawString(String.valueOf(packet.packet_no), x+packet.packetSize/4, y0+packet.packetSize - 3);
oldy0 = y0;
applet.repaint();
}
}
synchronized (applet.paintLock) {
applet.callback_function (this);
}
}
public void disappear() {
this.stop();
synchronized (applet.paintLock) {
applet.gBuffer.setColor(Color.black);
applet.gBuffer.fillRect(x, oldy0, packet.packetSize, packet.packetSize);
applet.repaint();
}
}
}
class SimplePacketThread extends PacketDrawingThread {
private int sleepDuration = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -