package eu.choreos.monitoring.daemon;

import java.util.ArrayList;
import java.util.List;

import eu.choreos.monitoring.GmondDataReader;

public class ThresholdEvalDaemon {

	private static String host;
	private static int port;
	private AnomalyNotifier notifier;

	public void setDataReader(GmondDataReader dataReader) {
		this.notifier = new AnomalyNotifier(dataReader);
	}

	public static void main(String[] args) throws InterruptedException {
		parseArgs(args);

		ThresholdEvalDaemon daemon = new ThresholdEvalDaemon();

		daemon.setDataReader(new GmondDataReader(host, port));
		
		List<Threshold> thresholds = new ArrayList<Threshold>();
		thresholds.add(new Threshold("load_one", Threshold.MAX, 3));
		
		daemon.setThresholdList(thresholds);
		

		while (true) {
			daemon.evaluateThresholds();
			Thread.sleep(600000);
		}
	}
	
	public void setThresholdList(List<Threshold> thresholds) {
	    notifier.setThresholds(thresholds);
	}

	private void evaluateThresholds() {
	    
	    for(Threshold threshold: notifier.evaluateAllThresholds()) {
		System.out.println("Threshold triggered for " + threshold);
	    }
	    
	}

	private static void parseArgs(String[] args) {
	    host = "http://localhost/";
	    port = 8649;
		
	    switch (args.length) {
		case 0:
			break;
		case 2:
		    	port = Integer.parseInt(args[1]);
		case 1:
			host = args[0];
			
			break;
		default:
			System.out
					.println("USAGE: ThresholdEvalDaemon [hostLocation] [port]");
			System.out
					.println("Default values: hostLocation = 'http://localhost/'");
			System.out.println("                port = 8649");host = args[0];
			
			System.out
					.println("Note: to set a port, the hostLocation must also be present");
			break;
		}
	}

}

@@ -258,16 +258,166 @@
 ifier;%0A%0A
+%09public ThresholdEvalDaemon()%7B%0A%09%09%0A%09%7D%0A%09%0A%09public ThresholdEvalDaemon(String host,int port)%7B%0A%09%09this.setDataReader(new GmondDataReader(host, port));%0A%09%7D%0A%09%0A
 %09public 
@@ -609,17 +609,16 @@
 (args);%0A
-%0A
 %09%09Thresh
@@ -668,71 +668,21 @@
 mon(
-);%0A%0A%09%09daemon.setDataReader(new GmondDataReader(
 host,
- 
 port)
-)
 ;%0A%09
-%09
 %0A%09%09L
@@ -1040,19 +1040,29 @@
 %0A%0A%09p
-rivate void
+ublic List%3CThreshold%3E
  eva
@@ -1096,12 +1096,13 @@
     
-for(
+List%3C
 Thre
@@ -1106,26 +1106,38 @@
 hreshold
- t
+%3E evaluateAllT
 hreshold
 : notifi
@@ -1128,17 +1128,19 @@
 hreshold
-:
+s =
  notifie
@@ -1164,16 +1164,66 @@
 sholds()
+;%0A%09%09for(Threshold threshold: evaluateAllThresholds
 ) %7B%0A%09%09Sy
@@ -1292,16 +1292,45 @@
  %7D%0A%09    
+return evaluateAllThresholds;
 %0A%09%7D%0A%0A%09pr

update contructor and evaluate threshold