Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Sliding window
- */
- public class ReliableSender extends SenderProtocol {
- private Channel channel;
- private Timer timer;
- private Packet[] window;
- private int windowSize;
- private int base;
- private double timeoutValue;
- private int nextSeqNum;
- ReliableSender(Channel aChannel, int aWindowSize, double aTimeout) {
- channel = aChannel;
- timer = new Timer(this);
- windowSize = aWindowSize;
- timeoutValue = aTimeout;
- nextSeqNum = 1;
- window = new Packet[windowSize];
- base = nextSeqNum;
- }
- void send(Data aDataChunk) {
- Packet sentPacket = new Packet(nextSeqNum, aDataChunk);
- channel.send(sentPacket);
- window[nextSeqNum % windowSize] = sentPacket;
- boolean windowEmpty = nextSeqNum == base;
- if(windowEmpty)
- timer.start(timeoutValue);
- nextSeqNum++;
- boolean windowFull = nextSeqNum % windowSize == base % windowSize;
- if(windowFull)
- blockData();
- Simulator.getInstance().log("reliable sender sent " + sentPacket);
- }
- public void receive(Packet aPacket) {
- Simulator.getInstance().log("reliable sender receives " + aPacket);
- base = aPacket.getSeqNum() + 1;
- acceptData();
- boolean windowEmpty = base == nextSeqNum;
- if(windowEmpty)
- timer.stop();
- else
- timer.start(timeoutValue);
- }
- public void timeout(Timer aTimer) {
- Packet sentPacket;
- Simulator.getInstance().log("*** reliable sender timeouts ***");
- timer.start(timeoutValue);
- for(int i = base; i < nextSeqNum; i++) {
- sentPacket = window[i % windowSize];
- Simulator.getInstance().log("reliable sender resends " + sentPacket);
- channel.send(sentPacket);
- }
- }
- }
Add Comment
Please, Sign In to add comment