如何使ContractNetInitiator循环发送CFP?

时间:2019-07-01 18:03:08

标签: protocols agents-jade

我需要使用FIPA-ContractNet-Protocol将CFP(每秒)发送到代理列表(每五秒钟刷新一次,要求发送)。

我正在创建一个TickerBehaviour,并将对addBehaviour(new ContractNetInitiator(myAgent,msg))的调用放入onTick()方法中。

public class MainAgent extends Agent {

    private Vector<AID> agents;
    ACLMessage msg;

    private int nResponders;

    protected void setup() {
        agents = new Vector<AID>();
        elementosTotales = new ArrayList<Elemento>();
        ambienteGlobal = new Ambiente();
        msg = new ACLMessage(ACLMessage.CFP);

        addBehaviour(new SearchAgents(this, 5000));
        addBehaviour(new SendMessage(this, 1000));

    }
}


private class SearchAgents extends TickerBehaviour {
        public SearchAgents(Agent a, long period) {
            super(a, period);
            agents = new Vector<AID>();
        }

        public void onTick() {
            DFAgentDescription plantilla = new DFAgentDescription();
            ServiceDescription sd = new ServiceDescription();
            sd.setType("Enviar-Ambiente");// Busca los servicios que envian el ambiente
            plantilla.addServices(sd);

            try {
                DFAgentDescription[] resultado = DFService.search(myAgent, plantilla);
                agents.clear();
                for (int i = 0; i < resultado.length; ++i) {
                    agents.addElement(resultado[i].getName());
                }

            }

            catch (FIPAException fe) {
                fe.printStackTrace();
            }
        }

    }

private class SendMessage extends TickerBehaviour {

        public SendMessage(Agent a, long period) {
            super(a, period);
        }

        @Override
        protected void onTick() {
            msg.clearAllReceiver();
            if (agents.size() > 0) {
                nResponders = agents.size();//
                System.out.println(
                        getLocalName() + ": sending message to " + nResponders + " agents.");

                for (int i = 0; i < agents.size(); ++i) {
                    msg.addReceiver(agents.get(i));
                }
                msg.setProtocol(FIPANames.InteractionProtocol.FIPA_CONTRACT_NET);
                msg.setContent("mandar-ambiente");
                addBehaviour(new Comunicator(myAgent, msg));
            } else {
                System.out.println(getLocalName() + ": No available agents .");
            }

        }


    }


class Comunicator extends ContractNetInitiator {
        public int onEnd() {
            return super.onEnd();
        }

        public Comunicator(Agent a, ACLMessage msg) {
            super(a, msg);

        }

        //METHODS WITH CODE IN THEY
        protected void handleRefuse(ACLMessage refuse) {

        }

        protected void handleFailure(ACLMessage failure) {

        }

        protected void handleAllResponses(Vector responses, Vector acceptances) {

        }


}

行为通信器已多次创建,但消息已中断。

0 个答案:

没有答案