FSM 到实际的 java 代码。不确定代码是否正确

发布于 2024-11-26 05:13:20 字数 2383 浏览 2 评论 0原文

这是为了学校,而不是家庭作业。我试图了解 RDT 是如何实现的,并且我能够找到另一所已经创建了模拟器的学校,但只需填写发送者和接收者。无论如何,我无法让它正常工作。发送方和接收方都有一些伪代码和 FSM 图。我相信发件人是正确的,但我不太确定收件人的情况。我以前从未处理过有限状态机图,所以我仍在尝试弄清楚这一点。

我将列出接收者以确保其正确,并在需要时发布更多代码。

FMS接收器

全局变量:

  astate =0 // can be 0-3, corresponds to state diagram for a/sender (3.15)
  astored_pkt // saved in case we need to retransmit - for a/sender
  bstate = 0 // can be 0-1,state diagram for b (3.14)
  bOnceThru = 0 // A flag to track if this is the first time through the receiver
  bstored_pkt // saved in case we need to retransmit - for b/sender

伪代码:

bInput
    if pkt is corrupt
        if bOnceThru==1
            send bstored_pkt
        else
            do nothing
    else
        if (seqno==0 && bstate==0) || (seqno==1 && bstate==1)
            deliver data to layer 5
            Print "B: got packet #"
            create bstored_pkt
            sendpkt(bstored_pkt)
            Print "B: sending ACK #"
            incr bstate mod 2
            bOnceThru=1
        else
            if bstate==1 || bOnceThru==1
                send bstored_pkt
                Print "B: sending ACK #"

Java代码:

protected void bInput(Packet packet){
    if(!isPktCorrupt(packet)){
        if(bOnceThru == 1){
            toLayer3(1,bstored_pkt);
        }else{
            return;
        }
    }else{
        if((packet.getSeqnum() == 0 && bstate == 0) ||
            (packet.getSeqnum() == 1 && bstate == 1)){
                toLayer5(packet.getPayload());
                System.out.println("B: got packet #");
                bstored_pkt = new Packet(packet);
                toLayer3(1,bstored_pkt);
                System.out.println("B: send ACK " + packet.getAcknum());
                bstate = (bstate + 1) % 2;
                if(packet.getSeqnum() == 0) bOnceThru = 1;
            }else{
                if(bstate == 1 || bOnceThru == 1){
                    toLayer3(1,bstored_pkt);
                    System.out.println("B: sending ACK " + packet.getAcknum());
                }
            }
    }

}

toLayer3和toLayer5是已经实现的方法,我不需要担心。此外,isPktCorrupt 是一种验证当前数据包与其存储内容的校验和的方法。

这看起来正确吗?伪代码是否正确?第一个数据包运行正常,然后第二个数据包继续尝试重新发送。但我不明白为什么。如果需要,我也会发布发件人。谢谢!

This is for school, but not homework. I'm trying to understand how RDT is implemented and i was able to find another school that had a simulator already created, but just have to fill in the sender and receiver. Anyway, I can't get it work properly. There is some Pseudo code and an FSM diagram for both the sender and receiver. I believe the sender is correct, but I'm not too sure about the receiver. I've never dealt with finite state machine diagrams before, so I'm still trying to figure that out too.

I'll list the receiver to make sure that's right and post more code if need be.

FMS receiver

Global Variables:

  astate =0 // can be 0-3, corresponds to state diagram for a/sender (3.15)
  astored_pkt // saved in case we need to retransmit - for a/sender
  bstate = 0 // can be 0-1,state diagram for b (3.14)
  bOnceThru = 0 // A flag to track if this is the first time through the receiver
  bstored_pkt // saved in case we need to retransmit - for b/sender

Pseudo Code:

bInput
    if pkt is corrupt
        if bOnceThru==1
            send bstored_pkt
        else
            do nothing
    else
        if (seqno==0 && bstate==0) || (seqno==1 && bstate==1)
            deliver data to layer 5
            Print "B: got packet #"
            create bstored_pkt
            sendpkt(bstored_pkt)
            Print "B: sending ACK #"
            incr bstate mod 2
            bOnceThru=1
        else
            if bstate==1 || bOnceThru==1
                send bstored_pkt
                Print "B: sending ACK #"

Java Code:

protected void bInput(Packet packet){
    if(!isPktCorrupt(packet)){
        if(bOnceThru == 1){
            toLayer3(1,bstored_pkt);
        }else{
            return;
        }
    }else{
        if((packet.getSeqnum() == 0 && bstate == 0) ||
            (packet.getSeqnum() == 1 && bstate == 1)){
                toLayer5(packet.getPayload());
                System.out.println("B: got packet #");
                bstored_pkt = new Packet(packet);
                toLayer3(1,bstored_pkt);
                System.out.println("B: send ACK " + packet.getAcknum());
                bstate = (bstate + 1) % 2;
                if(packet.getSeqnum() == 0) bOnceThru = 1;
            }else{
                if(bstate == 1 || bOnceThru == 1){
                    toLayer3(1,bstored_pkt);
                    System.out.println("B: sending ACK " + packet.getAcknum());
                }
            }
    }

}

toLayer3 and toLayer5 are methods implemented already, nothing i need to worry about. Also, isPktCorrupt is a method to verify the check sum of the current packet with what it has stored.

Does this seem correct? Is the pseudo code even correct? It runs ok for the first packet and then the second packet just keeps trying to resend. I can't see why though. If i need to, i'll post the Sender as well. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2024-12-03 05:13:20

这段代码是正确的。我确实在我的发件人中发现了一些错误(轻微),但它完全失败了。不过,我必须更改接收器代码中的一件事,那就是 bstate 的增量。因此,如果有人想知道或有类似的问题,这段代码确实与 FSM 相关并且它确实有效。

This code is correct. I did find some errors in my sender (minor), but it threw it completely off. I had to change one thing in the receiver code though, and that was the increment of bstate. So, in case anyone is wondering or has a similar question, this code does correlate to the FSM and it does work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文