return;
}
// Get the ToolNotifier for the indicated debugger (or create a new one
// if not yet there)
ToolNotifier tn = findNotifier(introspectorName);
if(tn == null) { // Need a new notifier
tn = new ToolNotifier(introspectorName);
AID id = new AID(introspectorName.getLocalName() + "-on-" + myID().getName(), AID.ISLOCALNAME);
try {
myContainer.initAgent(id, tn, null, null); // FIXME: Modify to use a proper owner Principal
myContainer.powerUpLocalAgent(id);
if (targetName.equals(myContainer.getAMS())) {
// If we are debugging the AMS, let's wait for the ToolNotifier
// to be ready to avoid deadlock problems. Note also that in
// this case this code is executed by the ams-debug-helper thread and not
// by the AMS thread
tn.waitUntilStarted();
}
// Wait a bit to let the ToolNotifier pass in ACTIVE_STATE
try {Thread.sleep(1000);} catch (Exception e) {};
helper.registerMessageListener(tn);
helper.registerAgentListener(tn);
}
catch (Exception e) {
e.printStackTrace();
}
}
tn.addObservedAgent(targetName);
// Update the map of debuggers currently debugging the targetName agent
synchronized (debuggers) {
List l = (List) debuggers.get(targetName);
if (l == null) {
l = new LinkedList();
debuggers.put(targetName, l);
}
if (!l.contains(introspectorName)) {
l.add(introspectorName);
}
}
Agent a = myContainer.acquireLocalAgent(targetName);
// Activate generation of behaviour-related events on the
// target agent
a.setGenerateBehaviourEvents(true);
// Retrieve the current agent state
AgentState as = a.getAgentState();
// Retrieve the list of pending ACL messages
List messages = new LinkedList();
myContainer.fillListFromMessageQueue(messages, a);
// Retrieve the list of ready and blocked agent behaviour IDs
List readyBehaviours = new LinkedList();
myContainer.fillListFromReadyBehaviours(readyBehaviours, a);
List blockedBehaviours = new LinkedList();
myContainer.fillListFromBlockedBehaviours(blockedBehaviours, a);
myContainer.releaseLocalAgent(targetName);
// Notify all the needed events
fireChangedAgentState(targetName, as, as);
Iterator itReady = readyBehaviours.iterator();
while(itReady.hasNext()) {
BehaviourID bid = (BehaviourID)itReady.next();
AgentEvent ev = new AgentEvent(AgentEvent.ADDED_BEHAVIOUR, targetName, bid, myContainer.getID());
tn.addedBehaviour(ev);
}
Iterator itBlocked = blockedBehaviours.iterator();
while(itBlocked.hasNext()) {
BehaviourID bid = (BehaviourID)itBlocked.next();
AgentEvent ev = new AgentEvent(AgentEvent.ADDED_BEHAVIOUR, targetName, bid, myContainer.getID());
tn.addedBehaviour(ev);
ev = new AgentEvent(AgentEvent.CHANGED_BEHAVIOUR_STATE, targetName, bid, Behaviour.STATE_READY, Behaviour.STATE_BLOCKED, myContainer.getID());
tn.changedBehaviourState(ev);
}
Iterator itMessages = messages.iterator();
while(itMessages.hasNext()) {
ACLMessage msg = (ACLMessage)itMessages.next();
MessageEvent ev = new MessageEvent(MessageEvent.POSTED_MESSAGE, msg, null, targetName, myContainer.getID());
tn.postedMessage(ev);
}
}