
package ganglia.gmetric;


/**
 * Implements the Ganglia gmetric command in java
 */
public class GMetric {
	public enum UDPAddressingMode {
		MULTICAST,
		UNICAST
	};
	
    private Protocol protocol ;
    
    /**
     * Constructs a GMetric 
     * @param group the host/group to send the event to
     * @param port the port to send the event to
     * @param mode the mode
     */
    public GMetric( String group, int port, UDPAddressingMode mode, int ttl) {
    	this( group, port, mode, ttl, true );
    }
    /**
     * Constructs a GMetric 
     * @param group the host/group to send the event to
     * @param port the port to send the event to
     */
    public GMetric( String group, int port, UDPAddressingMode mode, int ttl, boolean ganglia311) {
    	if ( ! ganglia311 )
    		this.protocol = new Protocolv30x( group, port, mode, ttl );
    	else
    		this.protocol = new Protocolv31x( group, port, mode, ttl, 5 );
    }
    /**
     * The Ganglia Metric Client (gmetric) announces a metric
     * @param name Name of the metric
     * @param value Value of the metric
     * @param type Type of the metric.  Either string|int8|uint8|int16|uint16|int32|uint32|float|double
     * @param units Unit of measure for the value
     * @param slope Either zero|positive|negative|both
     * @param tmax  The maximum time in seconds between gmetric calls 
     * @param dmax The lifetime in seconds of this metric
     * @param group Group Name of the metric
     * @throws java.lang.Exception
     */
    public void announce( String name, 
            String value, 
            GMetricType type,
            String units,
            GMetricSlope slope,
            int tmax,
            int dmax,
            String group ) throws GangliaException {
        try {
        	protocol.announce( name, value, type, 
        			units, slope, tmax, dmax, group);
        } catch ( Exception ex ) {
            throw new GangliaException( "Exception announcing metric", ex ) ;
        }
    }
    /**
     * Announces a metric
     * @param name Name of the metric 
     * @param value Value of the metric
     * @param group Group Name of the metric
     * @throws ganglia.GangliaException
     */
    public void announce( String name, 
            int value, String group ) throws GangliaException {
        this.announce(name, Integer.toString(value),GMetricType.INT32, "", GMetricSlope.BOTH, 60, 0, group);
    }
    /**
     * Announces a metric
     * @param name Name of the metric 
     * @param value Value of the metric
     * @param group Group Name of the metric
     * @throws ganglia.GangliaException
     */
    public void announce( String name, 
            long value, String group  ) throws GangliaException {
        this.announce(name, Long.toString(value),GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, group );
    }
    /**
     * Announces a metric
     * @param name Name of the metric 
     * @param value Value of the metric
     * @param group Group Name of the metric
     * @throws ganglia.GangliaException
     */
    public void announce( String name, 
            float value, String group ) throws GangliaException {
        this.announce(name, Float.toString(value),GMetricType.FLOAT, "", GMetricSlope.BOTH, 60, 0, group);
    }
    /**
     * Announces a metric
     * @param name Name of the metric 
     * @param value Value of the metric
     * @param group Group Name of the metric
     * @throws ganglia.GangliaException
     */
    public void announce( String name, 
            double value, String group ) throws GangliaException {
        this.announce(name, Double.toString(value),GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, group);
    }
    /**
     * Main method that sends a test metric
     * @param args
     */
    public static void main( String[] args ) {
        try {
            GMetric gm = new GMetric("239.2.11.71", 8649, UDPAddressingMode.MULTICAST, 1) ;
            //gm.announce("heartbeat", "0", GMetricType.UINT32, "", GMetricSlope.ZERO, 0, 0, "core");
            gm.announce("BOILINGPOINT", "100", GMetricType.STRING, 
                    "CELSIUS", GMetricSlope.BOTH, 0,0, "TESTGROUP");
            gm.announce("INTTEST", (int)Integer.MAX_VALUE, "TESTGROUP" ) ;
            gm.announce("LONGTEST", (long)Long.MAX_VALUE, "TESTGROUP" );
            gm.announce("FLOATTEST", (float)Float.MAX_VALUE, "TESTGROUP" ) ;
            gm.announce("DOUBLETEST", (double)Double.MAX_VALUE, "TESTGROUP" ) ;
        } catch ( Exception ex ) {
            ex.printStackTrace() ;
        }
    }
}

@@ -20,16 +20,84 @@
 etric;%0A%0A
+import java.net.InetAddress;%0Aimport java.net.UnknownHostException;%0A%0A
 %0A/**%0A * 
@@ -225,16 +225,254 @@
 %09UNICAST
+;%0A%09%09%0A%09%09public static UDPAddressingMode getModeForAddress(String addr) throws UnknownHostException %7B%0A%09%09%09InetAddress _addr = InetAddress.getByName(addr);%0A%09%09%09%0A%09%09%09if(_addr.isMulticastAddress())%0A%09%09%09%09return MULTICAST;%0A%09%09%09%0A%09%09%09return UNICAST;%0A%09%09%7D
 %0A%09%7D;%0A%09%0A 

Add convenience method to check address types