JADE Agent Communication

Introduction

JADE is also known as Java agent development framework. It can be used to pass messages using agents which supports distributed communication. To read about jade and work with tutorial visit http://jade.tilab.com/. In this tutorial, I will try to show code for agent communication. For any type of communication to happen, you need a listener (receiver) and sender of the message. In jade we spawn the listener agent first and then multiple sender’s (with their own identity) can send message to the listener agent. In a real world application the listener could be used to trigger different events in your application.

In one of my group projects (Nyx, intrusion detection system), we used it as sensor agent (sense intrusion in network captured packets and alert), monitor agent (which listens for any message from sensor) and response agent (that does the action like alert, send email and so on).

Setup

Download jade from jade.tilab.com (all or bin). You can download the examples to practice. After download and extraction copy jade.jar to your working directory. By the way, I am using Linux as the execution platform (adjust path separators for your desired OS).

Code

In your working directory (where you have jade.jar), create two files named Receiver.java and Sender.java with following code.

// Sender.java
import jade.core.Agent;
import jade.core.AID;
import jade.core.behaviours.*;
import jade.lang.acl.*;
public class Sender extends Agent {
 protected void setup() {
// Setup answering behavior
 addBehaviour(new CyclicBehaviour(this) {
 public void action() {
 ACLMessage msg = receive();
 if (msg != null)
 System.out.println("Message: " + msg.getContent()
 + " ( " + msg.getSender().getName()+ " )");
 block();
 }
 });
// Send message to agent "a1"
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
 msg.setContent(" Hi from Sender ");
msg.addReceiver(new AID("a1", AID.ISLOCALNAME));
 send(msg);
}
}

// Receiver.java

import jade.core.Agent;
import jade.core.behaviours.*;
import jade.lang.acl.*;

public class Receiver extends Agent {
 protected void setup() {
 // create behaviour for receive and send message to Sender
 addBehaviour(new CyclicBehaviour(this) {
 public void action() {

 ACLMessage msg = receive(); // receive sent message
 if (msg != null) {
 System.out.println(" Message to " + myAgent.getLocalName()
 + " received. Message is : " + msg.getContent());

 // Now create a reply back to the Sender agent
 ACLMessage reply = msg.createReply();
 reply.setPerformative(ACLMessage.INFORM);
 reply.setContent(" Hello reply from Receiver");
 send(reply);
 }
 block();
 }
 });
 }
}

In the Receiver.java cyclic behavior was added so that it can both send and receiver messages. ACLMessage object then has the received message object. The code then prints the message. Using same message object it creates a reply and send it. Sender also mostly does what a Receiver does (listen for messages). In addition it also prepares a message as ACLMessage object and sets the receiver agent id. This specifies which agent the message is intended for (‘a1’ in this case).

Execute

 

Compile the code
javac -cp .:jade.jar Receiver.java Sender.java
Run the Receiver
$ java -cp .:jade.jar jade.Boot -gui a1:Receiver

Here -gui is optional. With -gui option a GUI will pop-up showing different agents that get spawned during execution.

01-jade-gui

Send a message to our a1 agent

$ java -cp .:jade.jar jade.Boot -container main:Sender

02-sender-terminal

Sender’s terminal output

 

Similarly, the Receiver also now shows the message.

03-receiver-terminal

Receiver’s terminal output

What next

As hinted above my group used JADE in our final year project (Intrusion Detection System). You can read more about the project in the documentation “Report (Manual).pdf” inside Documentation directory of our project. This project was done around 2007 (a long time ago). As for the source code of the project we lost most of the code. What we currently have is test code for packet capture and all the payload data that encapsulates the network packets. We don’t have any other code in the project. The reason being, we hosted the code in one online code repository website, which deleted the project (after we finished the project). None of my team members have backup for the code anymore. That code what we have in our git repository is exported from our sorceforge project. If you are seeking to expand on the project, you are more than welcome to use the code (although there is not much there). However, the documentation should give you a pretty good idea on what we did.

Cheers !!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s