📄 csma.java
字号:
;
}
}
public void backoff(int count) {
int slotTime = 200;
myCounter = new Counter(
(int)((myRandom.nextDouble()*(double)Math.pow(2,count))*(double)slotTime)
);
while(myCounter.getCount()>0) {
sleep(50);
}
}
public void update() {
if (myState==BACKOFF) {
myCounter.decrement();
}
return;
}
public boolean send() {
myState = CARRIER_SENSE;
while(!myMedium.isFree(myPosition)) {
sleep(100);
}
myState = TRANSMIT;
if (myMedium.send(myId, myPosition, myNetwork.getFrameSize())==false) {
myState = BACKOFF;
return false;
}
else {
myState = IDLE;
return true;
}
}
public void run() {
while (true) {
if (mySendRequest==true) {
mySendRequest=false;
myBackoffCount = 0;
while (send()==false) {
backoff(myBackoffCount);
myBackoffCount++;
}
}
sleep(100);
}
}
}
/* the network class which simulates the network */
class Network {
public static final int NUM_STATIONS = 4;
Medium myMedium;
Station[] myStations;
int myFrameSize = 150;
int i=0;
public Network() {
/* initializes the medium and also indicates the number of stations */
myMedium = new Medium();
myStations = new Station[NUM_STATIONS];
}
public void stop()
{
for(int j=0;j<i;j++)
myStations[j].setState(0); //setting the state of stations to idle
update();
}
public void addStation(int x)
{
myStations[i] = new Station(i++,x, this, myMedium);
}
public void init() {
i=0;
/* the stations are initialized */
// myStations[0] = new Station(0, 10, this, myMedium);
// myStations[1] = new Station(1, 50, this, myMedium);
// myStations[2] = new Station(2, 90, this, myMedium);
}
public int convertPositionToPixel(int position) {
return (int)Math.round((double)(position*400)/(double)myMedium.getBusLength());
}
public int getFrameSize() {
return myFrameSize;
}
public void setFrameSize(int size) {
myFrameSize = size;
}
public void update() {
int index;
if(i>1)
myMedium.update();
for (index = 0; index</*NUM_STATIONS*/i; index++) {
myStations[index].update();
}
}
public void paint(Graphics g) {
int index;
int pixel;
Integer busLength = new Integer(myMedium.getBusLength());
Integer stationNumber;
Integer backoffCount;
Integer backoffRemain;
//draw states
g.setColor(Color.white);
g.fillRect(120,350,20,20);
g.setColor(Color.black);
g.drawString("IDLE",150,360);
g.setColor(Color.green);
g.fillRect(240,350,20,20);
g.setColor(Color.black);
g.drawString("TRANSMIT",270,360);
g.setColor(Color.blue);
g.fillRect(360,350,20,20);
g.setColor(Color.black);
g.drawString("CARRIER-SENSE",380,360);
g.setColor(Color.red);
g.fillRect(480,350,20,20);
g.setColor(Color.black);
g.drawString("BACK-OFF",510,360);
// draw bus
g.setColor(Color.black);
g.fillRect(100,200,400,10);
g.drawString("0",90,225);
g.drawString(busLength.toString(),500,225);
g.drawLine(100,210,100,220);
g.drawLine(498,210,498,220);
// draw stations and indicate their states
for (index = 0; index</*NUM_STATIONS*/i; index++) {
pixel = convertPositionToPixel(myStations[index].getPosition());
stationNumber = new Integer(index+1);
switch (myStations[index].getState()) {
case Station.IDLE:
g.setColor(Color.white);
break;
case Station.CARRIER_SENSE:
g.setColor(Color.blue);
break;
case Station.TRANSMIT:
g.setColor(Color.green);
break;
case Station.BACKOFF:
g.setColor(Color.red);
break;
default:
g.setColor(Color.pink);
}
g.fillRect(88+pixel,230,25,25);
g.setColor(Color.black);
g.drawRect(88+pixel,230,25,25);
g.drawString("Station "+stationNumber.toString(),85+pixel,270);
g.drawLine(100+pixel,200, 100+pixel,230);
g.drawString("Station "+stationNumber.toString(),50,30+index*40+15);
if (myStations[index].getState() == Station.BACKOFF) {
backoffCount = new Integer(myStations[index].getBackoffCount());
backoffRemain = new Integer(myStations[index].getBackoffRemain());
g.drawString("k = " + backoffCount.toString(),90+pixel-3,300);
g.drawString("t = " + backoffRemain.toString(),90+pixel-3,320);
}
}
// draw frames
int start, end, id;
Signal[] signals = myMedium.getSignals();
for ( index=0; index<myMedium.MAX_SIGNALS; index++ ) {
if ( signals[index] != null ) {
id = signals[index].getStationId();
start = convertPositionToPixel(signals[index].getLeftPart().getStart());
end = convertPositionToPixel(signals[index].getLeftPart().getEnd());
if (signals[index].getLeftPart().getCollisionFlag() )
{
g.setColor(Color.red);
}
else
g.setColor(Color.black);
g.fillRect(100+start,(id+1)*40,Math.abs(end-start),8);
start = convertPositionToPixel(signals[index].getRightPart().getStart());
end = convertPositionToPixel(signals[index].getRightPart().getEnd());
if (signals[index].getRightPart().getCollisionFlag() )
{
g.setColor(Color.red);
}
else
g.setColor(Color.black);
g.fillRect(100+end,(id+1)*40,Math.abs(start-end),8);
}
}
}
public boolean handleEvent(Event e) {
if (e.id == Event.MOUSE_DOWN) {
if(i==1)
{
Frame alarm = new alarmFrame("Please add another station..min 2 stations",400,410);
alarm.show();
System.out.println("Please add another station..min 2 stations");
}
else
{
int index;
int pixel;
for (index = 0; index<NUM_STATIONS; index++) {
pixel = convertPositionToPixel(myStations[index].getPosition());
if ((e.x>=88+pixel)&&(e.x<=113+pixel)&&(e.y>=230)&&(e.y<=255)) {
myStations[index].setSendRequest(true);
return true;
}
}
}}
return false;
}
}
class alarmFrame extends Frame
{
public alarmFrame(String msg,int x1,int y1)
{
setSize(170,140);
setLocation(x1-200,y1-350);
setLayout(new GridLayout(2,1));
Label m = new Label(msg);
Button ok = new Button("OK");
add(m);
add(ok);
}
public boolean action (Event ev1, Object arg)
{
if (ev1.target instanceof Button)
{
String label = (String)arg;
if (label.equals("OK"))
{
setVisible(false);
}
}
return true;
}
}
/* the csma class */
public class csma extends Applet {
private Image myOffScreenImage = null;
private Graphics myOffScreenGraphics = null;
private boolean myResized = true;
Network myNetwork;
public static Timer myTimer;
int clicked=0;
Button mySetSizeButton;
Button stopButton;
Button addButton;
Button clearButton;
TextField mySizeTextField;
TextField posTextField;
int start;
int posArray[] = new int[10];
int count=0;
public void init()
{
myNetwork = new Network();
// the network is initialized
myNetwork.init();
// the clock is started
myTimer = new Timer(myNetwork,this,50);
myTimer.start();
setBackground(Color.yellow);
mySetSizeButton = new Button("FrameSize:");
stopButton = new Button("Stop");
addButton = new Button("AddStation");
clearButton = new Button("Clear");
mySizeTextField = new TextField("150");
posTextField = new TextField(3);
setLayout (new BorderLayout());
Panel p = new Panel();
p.setLayout( new GridLayout(2,1));
Panel p1 = new Panel();
p1.setLayout(new FlowLayout());
p1.add(addButton);
p1.add(posTextField);
p.add(p1);
Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(stopButton);
panel.add(clearButton);
panel.add(mySetSizeButton);
panel.add(mySizeTextField);
p.add(panel);
add ("South", p );
setSize(600,470);
}
public void destroy() {
}
public void update( Graphics g ) {
if ( null == myOffScreenImage || myResized ) {
myOffScreenImage = createImage( size().width,size().height );
myOffScreenGraphics = myOffScreenImage.getGraphics();
myResized = false;
}
myOffScreenGraphics.setColor( getBackground() );
myOffScreenGraphics.fillRect( 0, 0, size().width,size().height );
myOffScreenGraphics.setColor( g.getColor() );
paint( myOffScreenGraphics );
g.drawImage( myOffScreenImage, 0, 0, this );
}
public void paint( Graphics g ) {
Integer time = new Integer(myTimer.myTime);
// draw time
g.setColor(Color.black);
// g.drawString("Time: "+time.toString(),80,10);
// draw network
myNetwork.paint(g);
}
public boolean handleEvent(Event e) {
if ( myNetwork.handleEvent(e) ) {
repaint();
return true;
}
return super.handleEvent(e);
}
public boolean action (Event ev1, Object arg) {
if (ev1.target instanceof Button)
{
String label = (String)arg;
if (label.equals("FrameSize:"))
{
myNetwork.setFrameSize(Integer.parseInt(mySizeTextField.getText()));
return true;
}
if(label.equals("Stop"))
{
System.out.println("pressed INIT!!");
//init();
myNetwork = new Network();
// the network is initialized
myNetwork.init();
// the clock is started
myTimer = new Timer(myNetwork,this,50);
myTimer.start();
setBackground(Color.yellow);
for(int k=0;k<count;k++)
myNetwork.addStation(posArray[k]);
//Network.stop();
System.out.println("returned from Init!!");
posTextField.setText("0");
}
if(label.equals("Clear"))
{
count=0;
myNetwork = new Network();
// the network is initialized
myNetwork.init();
// the clock is started
myTimer = new Timer(myNetwork,this,50);
myTimer.start();
setBackground(Color.yellow);
for(int k=0;k<count;k++)
myNetwork.addStation(posArray[k]);
//Network.stop();
System.out.println("returned from Init!!");
posTextField.setText("0");
}
if(label.equals("AddStation"))
{
int position = Integer.parseInt(posTextField.getText());
if((posTextField.getText()==null) || (posTextField.getText().equals("")))
{
alarmFrame a = new alarmFrame("Enter some value!",400,410);
a.show();
}
else
{
if(!(position>0 && position < 100))
{ alarmFrame a = new alarmFrame("Give Value between 0 and 100",400,410);
a.show();
}
else
{
int n=0;
for(int k=0;k<count;k++)
if(Math.abs(position-posArray[k])<15)
{
alarmFrame b = new alarmFrame("min. 15 distance",400,410);
b.show();
n=1;
}
if(n!=1)
{
posArray[count++]=position;
System.out.println("pressed the button!!");
System.out.println(position);
if(position!=0)
myNetwork.addStation(Integer.parseInt(posTextField.getText()));
}
}
}
}
else
return false;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -