/*
 * MonitorAgent.java
 *
 * Created on December 15, 2004, 4:04 PM
 */

package DDMT.core;

import jade.domain.*;
import jade.lang.acl.*;
import jade.core.AID;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAAgentManagement.*;
import java.awt.*;
import jade.core.Agent;
import jade.lang.acl.ACLMessage;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.MessageTemplate;
import jade.core.behaviours.Behaviour;
import java.util.Vector;
import javax.swing.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*;
import jade.domain.FIPAAgentManagement.*;
import DDMT.Nodes.*;
import java.awt.*;

/**
 *
 * @author  jk3
 */
public class MonitorAgent extends jade.gui.GuiAgent{
    
    //The main program gui.
    MainPanel main1;
    
    //The list of current agents running
    Vector currentAgents;
    
    //The states of currently running agents
    Vector currentStates;
    
    /** Creates a new instance of MonitorAgent */
    public MonitorAgent() {
        
        //Initialize the main program gui
        main1 = new MainPanel(this);
        main1.show();
        
    }
    /**
     *Method to broadcast something to all agents
     */
    void broadcast(ACLMessage message)
    {
        //Send message to all the agents
        if( currentAgents == null )
        {
            System.out.println("Error-No agents!");
        }
        else{
            for( int i = 0; i<currentAgents.size(); i++)
            {
                AID address = new AID();
                address.setLocalName((String)currentAgents.elementAt(i));
                message.addReceiver(address);
                //System.out.println("Adding "+address.getLocalName()+" to recievers");
            }
            System.out.println("Sending message...");
            this.send(message);
        }
    }
    
    public void onGuiEvent(jade.gui.GuiEvent event)
    {
        
    }
    
    /**
     *Method to find which agents are available
     */
    public Vector find()
    {
        //Collect the list of agents
        Vector addresses = new Vector();
        Vector states = new Vector();
        MessageTemplate mt = MessageTemplate.MatchConversationId("ping");
        ACLMessage message = this.receive(mt);
        while( message != null)
        {
            addresses.addElement(message.getSender().getLocalName());
            states.addElement(message.getContent());
            message = this.receive(mt);
        }
        
        //Consoladate the list of agents
        currentAgents = new Vector();
        currentStates = new Vector();
        
        for( int i = 0; i< addresses.size(); i++ )
        {
            boolean found = false;
            for( int j = 0; j< currentAgents.size(); j++)
            {
                if( ((String)addresses.elementAt(i)).equals( 
                        ((String)currentAgents.elementAt(j))))
                {
                    found = true;
                    break;
                }
            }
            
            if(!found)
            {
                currentAgents.add( addresses.elementAt(i) );
                currentStates.add( states.elementAt(i) );
            }
        }
        
        return currentAgents;
    }
    
    public void search()
    {
        //Clear out any residual messages 
        MessageTemplate mt = MessageTemplate.MatchConversationId("ping");
        ACLMessage message = this.receive(mt);
        while( message != null)
        {
            message = this.receive(mt);
        }
        
        //Find the list of agents to check
        DFAgentDescription[] list = null;
        try{
            DFAgentDescription dfd = new DFAgentDescription();
            ServiceDescription sd  = new ServiceDescription();
            sd.setType( "NodeAgent" );
            dfd.addServices(sd);

            list = DFService.search(this, dfd);

        }catch(FIPAException ex)
        {
            ex.printStackTrace();
        }

        //Send out querries to each one
        if( list != null )
        {
            message = new ACLMessage();
            message.setConversationId("monitor");
            message.setEncoding("ping");
            for( int i = 0; i< list.length; i++ )
            {
                message.addReceiver( list[i].getName() );
            }

            this.send(message);
        }else{ System.out.println("Error pinging nodes!");}
    }
    
    /**
     *Class that searches for a specific service on the yellow pages
     */
    private class YellowPagesSearch extends jade.core.behaviours.OneShotBehaviour
    {
        //The service for which the yellow pages should be searched
        String service;
        
        public YellowPagesSearch(String s)
        {
            service = new String(s);
        }
        
        public void action() {
            
        }
        
    }
}
