Vithushan
Vithushan

Reputation: 513

Get JADE agent address while receiving message

I'm developing and application using jade where you can communicate between computers. What i want to know is how to get address of an agent who sends a message while receiving a message and want to display in the sysout statement in "else" with the message.

protected void setup(){ 
        super.setup();

        //receiving message
        addBehaviour(new CyclicBehaviour(this){
            public void action() {
                ACLMessage msg= receive();

                if (msg!=null){
                    String s =  msg.getContent();
                    String os, browser, tool, agentName, finalVal;

                    String[] elements = s.split(":");

                    if(elements.length > 0){
                        if(elements[0].equals("property_file")){
                            String[] property = new String[elements.length-1];

                            for(int i = 1; i < elements.length; i++){
                                property[i-1] = elements[i];
                                System.out.println(property[i-1]);
                            }
                            os = property[0];
                            browser = property[1];
                            tool = property[2];
                            agentName = msg.getSender().getName();
                            finalVal = agentName + ":" + os + ":" + browser + ":" + tool;

                            CreateText cret = new CreateText();
                            cret.writeText(finalVal);
                        }else{
                            System.out.println( "<=" + myAgent.getLocalName() + " gets reply \"" + msg.getContent() + "\" sent by " + msg.getSender().getLocalName() + '\n');
                            sendRep(msg);
                        }
                    }
                    block();
                }
            }
        });
    }

The format if address will be something like "http://[email protected]:7778/acc" and have to use is if i want to send a message later. The function will be like this. Here i have hardcoded the value of address.

public void sendMessage(String message){        
        ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
        AID r = new AID("someName@superContainer ", AID.ISGUID);
        //r.addAddresses("http://[email protected]:7778/acc");

        msg.setContent(message);
        msg.addReceiver(r);
        send(msg);              
    }

Upvotes: 0

Views: 1303

Answers (1)

Vithushan
Vithushan

Reputation: 513

Finally found the answer for this.

ACLMessage msg= receive();
String[] addressArr = msg.getSender().getAddressesArray();
System.out.println(addressArr[0]);

1st element of the array will have the value of the address of the agent we receiving a message from

Upvotes: 1

Related Questions