/* * Load monitor rev 0.0.2 for FIPA-OS 2.1.0 * ( http://fipa-os.sourceforge.net ) * * Author: Ognen Duzlevski (ogd116@mail.usask.ca) * MADMUC lab at http://bistrica.usask.ca/MADMUC * University of Saskatchewan, Saskatoon, Canada * * This agent calculates a load balance and averages it over course of * time. It will return the overall load average, the load average over last * five runs and the last average calculated as a responce to a request * performative * * The average is calculated by iteratively obtaining the first 50 * Fibonacci numbers, 100000 times. The number of iterations can be * adjusted through the FIBMAXTIMES constant. * * Start with: java LoadMonitor /platform.profile * */ import fipaos.agent.*; import fipaos.agent.conversation.*; import fipaos.agent.task.*; import fipaos.mts.*; import fipaos.ont.fipa.*; import fipaos.ont.fipa.fipaman.*; import fipaos.parser.acl.ACLMessage; // Import registration exception classes import fipaos.platform.ams.AMSRegistrationException; import fipaos.platform.df.DFRegistrationException; import java.util.*; import java.io.*; public class LoadMonitor extends FIPAOSAgent { private final String ATYPE = "load-balance-agent"; // the number of times to calculate Fibonacci numbers private final int FIBMAXTIMES = 100000; private Vector Loads = new Vector(); // loads added periodically public LoadMonitor(String profile, String name, String owner) { super(profile, name, owner); // now go on with the setup System.out.println("Initializing agent."); // set the listener task setListenerTask(new IdleTask()); // here we have to cater for the registration exceptions in 2.1.0 try { // register with the AMS first registerWithAMS(); // if we are here than the registration succeded System.out.println("Agent registered with AMS."); } catch (AMSRegistrationException amsre) { System.out.println("Problem registering load-balance agent with the AMS."); String reason = amsre.getExceptionReason(); if (reason != null) System.out.println("Reason: " + reason); shutdown(); } // try to register with the DF try { registerWithDF(ATYPE); // same drill as above System.out.println("Agent registered with DF."); } catch (DFRegistrationException dfre) { System.out.println("Problem registering load-balance agent with theDF."); String reason = dfre.getExceptionReason(); if (reason != null) System.out.println("Reason: " + reason); shutdown(); } } /* the communication handler follows */ public class IdleTask extends Task { public IdleTask() { // do nothing for now } public void startTask() { // we will start a LoadMonitorTask here newTask(new LoadMonitorTask()); } public void doneLoadMonitor_LoadMonitorTask( Task t ) { // here we initiate a new LoadMonitorTask, when the previous one is done newTask(new LoadMonitorTask()); } public void handleOther(Conversation conv) { System.out.println("handleOther()"); } public void handleAgree(Conversation conv) { System.out.println("handleAgree()"); } public void handleInform(Conversation conv) { System.out.println("handleInform()"); } public void handleRequest(Conversation conv) { long sum = 0; long sumLastFive = 0; long val = 0; float avg; float avgLastFive = 0.0f; // get overall averege load synchronized (Loads) { for (int i=0; i < Loads.size(); i++) { Long l = (Long)Loads.elementAt(i); val = l.longValue(); sum += val; // calculate average of last five if (i > Loads.size() - 5) sumLastFive += val; } } // synchronized avg = (float)sum / (float)Loads.size(); if (sumLastFive > 0) avgLastFive = (float)sumLastFive / 5.0f; // get last message sent ACL msg = conv.getACL(conv.getLatestMessageIndex()); // send agree first ACL agree = new ACL(); agree.setPerformative(FIPACONSTANTS.AGREE); agree.setSenderAID(getAID()); agree.setReceiverAID(msg.getSenderAID()); agree.setInReplyTo( msg.getReplyWith() ); agree.setReplyWith( null ); forward(agree); System.out.println("Sent agree to: " + msg.getSenderAID().getName()); // now send the inform ACL inform = new ACL(); inform.setPerformative(FIPACONSTANTS.INFORM); inform.setSenderAID(getAID()); inform.setReceiverAID(msg.getSenderAID()); String sAvg = ":average " + (new Float(avg)).toString(); String sAvgLF = ":average-over-five " + (new Float(avgLastFive)).toString(); String sVal = ":last-value "+ (new Long(val)).toString(); String cnt = "( " + sAvg + " " + sAvgLF + " " + sVal + " )"; inform.setContentObject(cnt); forward(inform); System.out.println("Sent inform to: " + msg.getSenderAID().getName()); } } public class LoadMonitorTask extends Task { public LoadMonitorTask() { // do nothing for now } public void startTask() { // get time now Calendar calStart = Calendar.getInstance(); Date dateStart = calStart.getTime(); long tmStart = dateStart.getTime(); // calculate the Fibonacci sequence a constant number of times calcFibonacci(FIBMAXTIMES); // get end time Calendar calEnd = Calendar.getInstance(); Date dateEnd = calEnd.getTime(); long tmEnd = dateEnd.getTime(); // calculate elapsed time long timeElapsed = tmEnd - tmStart; // add it to the vector Loads.add(new Long(timeElapsed)); // signal that the task is done done(); } // LoadMonitorTask.startTask() public void calcFibonacci(int limit) { // calculates the first 50 Fibonacci numbers limit times // the calculation is iterative to avoid stack-overflows long fib, fibm1, fibm2; for (int j=1; j < limit; j++) { fibm1 = 1; fibm2 = 1; for (int i=3; i < 50; i++) { fib = fibm1 + fibm2; fibm2 = fibm1; fibm1 = fib; } // for i } // for j } // calcFibonacci() } // LoadMonitorTask class public static void main(String args[]) { new LoadMonitor(args[0], args[1], "FIPA-OS"); } }