// remoce a packet reception FSM from queue public EZStationPacketFSM removeIncompletePacket() { EZStationPacketFSM temp = null; if (IncompletePacketsNum > 0) { temp = IncompletePackets[IncompletePacketsNum - 1]; IncompletePacketsNum--; } return(temp); }
// **************** handling the Incomplete Packets Queue ********************** // insert new packet reception FSM public void InsertNewIncompletePacket(EZFrame frame) { int i; // shifting the queue to the right for (i = IncompletePacketsNum; i > 0; i--) { IncompletePackets[i] = IncompletePackets[i - 1]; } // increasing number of Packets IncompletePacketsNum++; // adding new packet reception FSM IncompletePackets[0] = new EZStationPacketFSM(this, frame); }
// find a Packet Reception FSM using Sender node ID public EZStationPacketFSM findIncompletePacket(byte nodeID) { EZStationPacketFSM thepack = null; Boolean found = false; int i = 0; while ((!found) && (i < IncompletePacketsNum)) { if (IncompletePackets[i].PacketSenderID == nodeID) { found = true; thepack = IncompletePackets[i]; } i++; } return(thepack); }
// handling internal state according to input and triggering appropriate actions public void transitionAction(EZFrame frame) { switch (state) { case stIdle: // not in process process of receiving or sending if (frame.Ftype == t_Ping) { // received a ping frame. will acknowledge immediately... sendAcknowledge(frame.SenderID); state = stIdle; // state remains idle } else if (frame.Ftype == t_Acknowledge) // ignoring a random acknowledge { state = stIdle; } else { // any other type of frame // now looking up the incomplete Packets Queue EZStationPacketFSM incompletePack = findIncompletePacket(frame.SenderID); if (incompletePack != null) { incompletePack.transitionAction(frame); } else { // adding a new incomplete pack InsertNewIncompletePacket(frame); } state = stIdle; // still idle } break; case stSending: // in the process of sending a packet. still, handling the incoming if (frame.Ftype == t_Ping) { // received a ping frame. will acknowledge immediately... sendAcknowledge(frame.SenderID); state = stSending; // state remains stSending } else if (frame.Ftype == t_Acknowledge) // ignoring a random acknowledge { state = stSending; // keep on sending } else { // any other type of frame // now looking up the incomplete Packets Queue EZStationPacketFSM incompletePack = findIncompletePacket(frame.SenderID); if (incompletePack != null) { incompletePack.transitionAction(frame); } else { // adding a new incomplete pack InsertNewIncompletePacket(frame); } state = stSending; // still Sending } break; case stWaitingAcknowledge: // expecting a frame reception acknowledgement if (frame.Ftype == t_Ping) // casual ping. immediate reply { sendAcknowledge(frame.SenderID); // state remains state = stWaitingAcknowledge; } else if (frame.Ftype == t_Acknowledge) { // received and acknowledge frame if (frame.SenderID == PacketReceiverID) { // increasing number of sent frames FramesSent++; if (FramesSent == TotalFrames) { // all sent PacketReceiverID = 0; state = stIdle; } else { // need to send more frames state = stSending; sendNextFrame(); } } else { state = stWaitingAcknowledge; } } else { // assigning frame to proper Incomplete Packet recipient // any other type of frame // now looking up the incomplete Packets Queue EZStationPacketFSM incompletePack = findIncompletePacket(frame.SenderID); if (incompletePack != null) { incompletePack.transitionAction(frame); } else { // adding a new incomplete pack InsertNewIncompletePacket(frame); } state = stWaitingAcknowledge; // still Waiting for acknowledge } break; } // end big state switch }