All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mytechia.robobo.rob.comm.ConnectionRob Maven / Gradle / Ivy

There is a newer version: 1.1.8
Show newest version
/*******************************************************************************
 *
 *   Copyright 2016 Mytech Ingenieria Aplicada 
 *   Copyright 2016 Julio Gómez 
 *
 *   This file is part of Robobo ROB Interface Library.
 *
 *   Robobo ROB Interface Library is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robobo ROB Interface Library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robobo ROB Interface Library.  If not, see .
 *
 ******************************************************************************/

package com.mytechia.robobo.rob.comm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * This class checks if the send message were lost.
 *
 * @author Julio Alberto Gomez Fernandez
 */
public class ConnectionRob {

    private static final Logger LOGGER= LoggerFactory.getLogger(ConnectionRob.class);

    protected final ArrayList sentRoboCommands = new ArrayList();

    
    public ConnectionRob() {}

    
    public  void addSentRoboCommand(RoboCommand sentRoboCommand){
        
        if(sentRoboCommand==null){
            return;
        }
        
        synchronized(sentRoboCommands){
            sentRoboCommands.add(sentRoboCommand);
        }
    }


    public boolean receivedAck(AckMessage ackMessage){

        if(ackMessage==null){
            return false;
        }

        RoboCommand foundRoboCommand=null;

        synchronized(sentRoboCommands) {

            for (RoboCommand roboCommand : this.sentRoboCommands) {
                if (roboCommand.getSequenceNumber() == ackMessage.getSequenceNumber()) {
                    foundRoboCommand = roboCommand;
                    break;
                }
            }

            if (foundRoboCommand != null) {
                this.sentRoboCommands.remove(foundRoboCommand);
            }
        }

        return (foundRoboCommand!=null);

    }

    public  List checkLostRoboCommands(){

        List lostRoboCommands = new ArrayList();

        synchronized (sentRoboCommands) {

            for (RoboCommand roboCommand : sentRoboCommands) {

                if(roboCommand.reachedMaximunNumberTransmissions()) {
                    if(roboCommand.exceededWaitingTimeAck()) {
                        lostRoboCommands.add(roboCommand);
                    }
                }
            }

            sentRoboCommands.removeAll(lostRoboCommands);
        }

        return lostRoboCommands;
    }

    public List resendRoboCommands(){

        List resendRoboCommands = new ArrayList();

        synchronized (sentRoboCommands) {

            for(RoboCommand roboCommand: sentRoboCommands){

                if(roboCommand.exceededWaitingTimeAck()){
                    if(!roboCommand.reachedMaximunNumberTransmissions()) {
                        resendRoboCommands.add(roboCommand);
                    }
                }

            }

        }

        return resendRoboCommands;
    }


    boolean wasSentRoboCommand(RoboCommand roboCommand){
        synchronized (sentRoboCommands) {
            return sentRoboCommands.contains(roboCommand);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy