0

My question is: How to send a line of a file to another agent every 2 seconds using ticker behaviours?

More specifically, in the first iteration, the agent sends the first line. In the second, the agent sends the second line etc.

My code below:

package pack1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import jade.wrapper.ControllerException;

public class Agent2 extends Agent {

    private static final long serialVersionUID = 1L;
    int nombre_ligne = 0;
    BufferedReader lecteurAvecBuffer = null;

    @Override
    protected void setup() {
        FileInputStream fis;
        try {
            fis = new FileInputStream("/home/hduser/Bureau/word.txt");
            @SuppressWarnings("resource")
            LineNumberReader l = new LineNumberReader(new BufferedReader(
                    new InputStreamReader(fis)));
            while ((l.readLine()) != null) {
                nombre_ligne = l.getLineNumber();
            }
            lecteurAvecBuffer = new BufferedReader(new FileReader(
                    "/home/hduser/Bureau/esclave1/abc.txt"));
            int a = 1;
            while (a <= ((int) nombre_ligne) / 3) {
                a++;
                String word = lecteurAvecBuffer.readLine();
                addBehaviour(new TickerBehaviour(this, 2000) {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected void onTick() {
                        ACLMessage message = new ACLMessage(ACLMessage.INFORM);
                        message.addReceiver(new AID("agent1", AID.ISLOCALNAME));
                        message.setContent(word);
                        send(message);
                    }
                });
                a++;
            }
            lecteurAvecBuffer.close();
        } catch (FileNotFoundException exc) {
            System.out.println("Erreur d'ouverture");
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }

    protected void takeDown() {
        System.out.println("Destruction de l'agent");
    }

    @Override
    protected void afterMove() {
        try {
            System.out.println(" La Destination : "
                    + this.getContainerController().getContainerName());
        } catch (ControllerException e) {
            e.printStackTrace();
        }    
    }    
}
ArK
  • 20,698
  • 67
  • 109
  • 136

1 Answers1

0

You don't say nothing about what is the problem with your code. I guess you get at least a compiler message about:

 message.setContent(word);

As you access a local variable from an inner class, you must declare the variable as final in the context, like:

 final String word = lecteurAvecBuffer.readLine();
PeterMmm
  • 24,152
  • 13
  • 73
  • 111