/* Agent za primanje i slanje poruka
 */
package Project5;

import java.io.*;
import java.rmi.RemoteException;

import COM.meitca.concordia.*;
import COM.meitca.concordia.collaborate.*;
import COM.meitca.concordia.event.*;
import COM.meitca.concordia.proxy.*;

/**
 * This example illustrates the use of an AgentGroup to pass information between
 * a group of cooperating agents. In this example, we have two agents of class
 * FetchingAgent who travel to a Concordia Server and wait.  A third Agent,
 * of class Monitor, signals the waitng agents that they should wake up and
 * complete their processing.  The Monitor signals the FetchingAgents by
 * posting an AgentTerminateEvent.  All three agents are members of an AgentGroup
 * which provides the means for their communication.
 *
 * The Monitor class signals the two waiting agent that they shoud wake up
 * and terminate their processing.
 */
 public class Monitor
    extends CollaboratorAgent {

    // A reference to the group of which we are a member
    private AgentGroup      agentGroup;

    /**
     * Constructs a Monitor.
     */
    public Monitor(AgentGroup agentGroup)
        throws java.rmi.RemoteException {

        // Calling super with a reference to an AgentGroup causes the
        // newly created  agent to join the group
        super(agentGroup);

        this.agentGroup = agentGroup;

        try {
            makeEventHandler(false);
        } catch (EventHandlerException e) {
            System.err.println("Monitor: Error making event handler "  + e);
            e.printStackTrace(System.err);
        }
    }


    /**
     * This is the method of the agent which appears in its Itinerary.  This
     * agent simply travels to a server and then posts an AgentTerminateEvent
     * to the agent group.
     */
    public void run() {
        try {
             
            // Ceka da drugi agent stigne na novu lokaciju
			int n=5;													//number of iterations
			String[] urls = {"http://localhost/Sareno/riba/fish1.html",	//urls to be checked
							 "http://localhost/Sareno/riba/", 
							 "http://localhost/Sareno/riba/fish.html", 
							 "http://localhost/Sareno/", 
							 "http://localhost/",
							 "http://localhost/Programiranje/", 
							 "http://localhost/Programiranje/PER5UNL/", 
							 "http://localhost/Programiranje/PER5UNL/#Introduction", 
							 "http://localhost/Programiranje/PER5UNL/index.html#Introduction"};
			while(n>0){													//loop
				try {
				    System.out.println("Monitor: salje URL ");
			        postEvent(agentGroup, new PorukaZaFA(urls[n]));		//new task fo FA
			        System.out.println("Monitor: finished posting url");
			        Thread.currentThread().sleep(10000);				//wait 10 secs befor sending next task
		        } catch (InterruptedException e) {
				}
				n--;
			}
			postEvent(agentGroup, new PorukaStopFA("Stop"));			//stop FA
        } catch (Exception e) {
            System.err.println("Monitor: Error posting termination event "  + e);
            e.printStackTrace(System.err);
        }
    }
    public void handleEvent(EventType event)
        throws EventException {

        if (event instanceof PorukaZaMonitor) {
			//if something changed do this
            System.out.println("Monitor: Primio sam sledecu poruku: " + event.getEventDescription());
			// Wake up the primary thread by making a call to
            // Object.notifyAll  The primary thread has been previously
            // suspended by a call to Object.wait.
            synchronized (this) {
                this.notifyAll();
            }

        }
    }

}
