package sb.behaviours;

import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import sb.agents.DefaultAgent;
import sb.helpers.ClassificationHelper;
import sb.helpers.ECategoryHelper;
import sb.helpers.ETypeHelper;

public class AgentDiscoveryBehaviour extends TickerBehaviour {
	
	private ECategoryHelper _category;
	private ETypeHelper 	_type;
	private int				_roomNumber;
	private DefaultAgent	_agent;
	private Agent 			_jadeAgent;

	public AgentDiscoveryBehaviour(Agent a, ECategoryHelper category, ETypeHelper type, int roomNumber) {
		super(a, 5000);
		_category = category;
		_type = type;
		_roomNumber = roomNumber;
		_agent = (DefaultAgent)a;
	}

	@Override
	protected void onTick() {
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType(ClassificationHelper.getCategoryCode(_category, _type));
		template.addServices(sd);
		try {
			DFAgentDescription[] result = DFService.search(_jadeAgent, template); 
			for (DFAgentDescription dfAgentDescription : result) {
				_agent.receivers.add(dfAgentDescription.getName());
			}			
		}
		catch (FIPAException fe) {
			System.err.println("AGT-DISC-BHR Err : "+fe.getMessage());
		}
	}

}

@@ -406,16 +406,79 @@
 elper;%0A%0A
+/**%0A * This behavior update the Agent %0A * @author Herv%C3%A9%0A *%0A */%0A
 public c
@@ -969,17 +969,25 @@
 ription 
-t
+researchT
 emplate 
@@ -1014,16 +1014,48 @@
 tion();%0A
+%09%09// Classification Description%0A
 %09%09Servic
@@ -1067,18 +1067,31 @@
 ription 
-sd
+typeDescription
  = new S
@@ -1113,18 +1113,31 @@
 on();%0A%09%09
-sd
+typeDescription
 .setType
@@ -1200,31 +1200,247 @@
 ;%0A%09%09
-template.addServices(sd
+// Room Description%0A%09%09ServiceDescription roomDescription = new ServiceDescription();%0A%09%09roomDescription.setType(String.valueOf(_roomNumber));%0A%09%09%0A%09%09researchTemplate.addServices(typeDescription);%0A%09%09researchTemplate.addServices(roomDescription
 );%0A%09
@@ -1508,17 +1508,25 @@
 eAgent, 
-t
+researchT
 emplate)
@@ -1586,16 +1586,72 @@
 sult) %7B%0A
+%09%09%09%09if(!_agent.receivers.contains(dfAgentDescription))%0A%09
 %09%09%09%09_age
@@ -1702,19 +1702,16 @@
 ));%0A%09%09%09%7D
-%09%09%09
 %0A%09%09%7D%0A%09%09c

Update DiscoveryBehaviour