The project is built using Netbeans (i am using Netbeans 6.9.1). Please download the SimpleJadeLeap.zip and extract them to a folder to build.
In addition, the project use Customized LEAP library, therefore the LeapMIDP.jar is need to.
SimpleAgent.java : The JADE Agent
SimpleAgentGUIDesign.java: This is an interesting part of make use of Netbeans to design the UI, flows. Then everything is copied into SimpleAgentGUI.java :-) In theory, this file is not needed in the project.
SimpleAgentGUI.java: The graphic user interface for Agent. You can manually design the GUI, but with the help of SimpleAgentGUIDesign.java and netbeans, things much easier
SimpleAgentGUIInterface.java: is a java interface, for communications between agent and the UI. Because, its name is an interface :-)
Start.java: is a starting point of JADE/Leap midlet. In the original Jade/Leap from Tilab, once the midlet start, it actually start the container, and you are online immediately. In my case, I hack into the MicroBoot class. Therefore, I can first create a container (for hosting the mobile agent) then start the agent.
To much to explain, let's start the platform:
At the command prompt, start the JADE/LEAP Platform using the JadeLeap.jar
java -cp JadeLeap.jar jade.Boot -gui
Start the J2ME:
c:\wtk2.5.2\bin\emulator.exe -Xdescriptor SimpleJadeLeap.jar
I hope you enjoy this post !
Looking into details
public class Start extends MicroBootLight implements CommandListener {
...
...
...
public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == form) {
if (command == CreateContainer) {
// This is actual create container on the JADE Platform
Thread t = new Thread() {
public void run() {
myStartGoOnline();
}
};
t.start();
// write post-action user code here
} else if (command == StartAgent) {
// Then, we start the agent
String name = AgentName.getString();
try {
MicroRuntime.startAgent(name, "example.SimpleAgent", null);
// write post-action user code here
} catch (Exception ex) {
ex.printStackTrace();
}
// write post-action user code here
} else if (command == exitCommand) {
// write pre-action user code here
exitMIDlet();
// write post-action user code here
}
}
// write post-action user code here
}
public void myStartGoOnline(){
try {
super.startGoOnline("192.168.0.15");
} catch (MIDletStateChangeException ex) {
ex.printStackTrace();
}
System.out.print("Join Agent Platform Done");
}
}
public class SimpleAgentGUI implements SimpleAgentGUIInterface, CommandListener {
private SimpleAgent myAgent;
// This is the constructor,
SimpleAgentGUI (SimpleAgent mAgent){
this.myAgent = mAgent;
// myAgent use for communication between agent and its graphic interface
}
....
}
Full code of Simple Agent
public class SimpleAgent extends Agent {
private SimpleAgentGUI myAgentGUI;
public String sLocalName;
public String sAgentFullName;
// Create constructor
protected void takeDown() {
if (myAgentGUI != null) {
myAgentGUI.dispose();
}
}
protected void setup() {
// Printout a welcome message
sLocalName=getAID().getLocalName();
sAgentFullName=getAID().getName();
System.out.println("Hello World. I'm an agent!");
System.out.println("My local-name is "+sLocalName);
System.out.println("My full Name is "+sAgentFullName);
Iterator it = getAID().getAllAddresses();
int i=0;
while (it.hasNext()){
System.out.println("Address ["+i+"]="+it.next().toString());
i++;
}
// Activate the GUI
try{
myAgentGUI = new SimpleAgentGUI(this);
}catch (Exception e){
System.out.println("Start GUI failed!");
}
myAgentGUI.getAgentInformation(sAgentFullName, sLocalName);
// Initials behaviours.
addBehaviour(new handleActivities(this));
// takeDown(); // Take down the agent
}
void sendMsg(String sAgentName, String sMsgContent) {
System.out.println("Sender: "+sAgentFullName);
System.out.println("Receiver: "+sAgentName);
System.out.println("Message: "+sMsgContent);
ACLMessage msg = new ACLMessage (ACLMessage.INFORM);
AID agentID=new AID(sAgentName,AID.ISLOCALNAME);
msg.addReceiver(agentID);
msg.setContent(sMsgContent);
send(msg);
}
///////////////////////////////////////////////////////////////////////////
public class handleActivities extends CyclicBehaviour {
handleActivities(Agent a) {
super(a);
}
public void action() {
ACLMessage msg= receive();
if (msg!=null){
String sContent= msg.getContent();
String sSenderName = msg.getSender().getLocalName();
//System.out.println("Received messange: "+sContent+ " from: "+sSenderName);
myAgentGUI.updateMessage(sSenderName, sContent);
}else {
System.out.println("Bloking");
block();
}
System.out.println("EndAction");
}// End action()
}
}