GangliaContext extends AbstractMetricsContext { @Override @InterfaceAudience.Private public void init(String contextName, ContextFactory factory) { super.init(contextName, factory); parseAndSetPeriod(PERIOD_PROPERTY); metricsServers = Util.parse(getAttribute(SERVERS_PROPERTY), DEFAULT_PORT); unitsTable = getAttributeTable(UNITS_PROPERTY); slopeTable = getAttributeTable(SLOPE_PROPERTY); tmaxTable = getAttributeTable(TMAX_PROPERTY); dmaxTable = getAttributeTable(DMAX_PROPERTY); multicastEnabled = Boolean.parseBoolean(getAttribute(MULTICAST_PROPERTY)); String multicastTtlValue = getAttribute(MULTICAST_TTL_PROPERTY); if (multicastEnabled) { if (multicastTtlValue == null) { multicastTtl = DEFAULT_MULTICAST_TTL; } else { multicastTtl = Integer.parseInt(multicastTtlValue); } } try { if (multicastEnabled) { LOG.info("Enabling multicast for Ganglia with TTL " + multicastTtl); datagramSocket = new MulticastSocket(); ((MulticastSocket) datagramSocket).setTimeToLive(multicastTtl); } else { datagramSocket = new DatagramSocket(); } } catch (IOException e) { LOG.error(e); } } @InterfaceAudience.Private  GangliaContext(); @Override @InterfaceAudience.Private void init(String contextName, ContextFactory factory); @Override void close(); @Override @InterfaceAudience.Private void emitRecord(String contextName, String recordName,
    OutputRecord outRec);  }
@Test public void testShouldCreateDatagramSocketByDefault() throws Exception { GangliaContext context = new GangliaContext(); context.init("gangliaContext", ContextFactory.getFactory()); assertFalse("Created MulticastSocket", context.datagramSocket instanceof MulticastSocket); }
@Test public void testShouldCreateDatagramSocketIfMulticastIsDisabled() throws Exception { GangliaContext context = new GangliaContext(); ContextFactory factory = ContextFactory.getFactory(); factory.setAttribute("gangliaContext.multicast", "false"); context.init("gangliaContext", factory); assertFalse("Created MulticastSocket", context.datagramSocket instanceof MulticastSocket); }
@Test public void testShouldCreateMulticastSocket() throws Exception { GangliaContext context = new GangliaContext(); ContextFactory factory = ContextFactory.getFactory(); factory.setAttribute("gangliaContext.multicast", "true"); context.init("gangliaContext", factory); assertTrue("Did not create MulticastSocket", context.datagramSocket instanceof MulticastSocket); MulticastSocket multicastSocket = (MulticastSocket) context.datagramSocket; assertEquals("Did not set default TTL", multicastSocket.getTimeToLive(), 1); }
@Test public void testShouldSetMulticastSocketTtl() throws Exception { GangliaContext context = new GangliaContext(); ContextFactory factory = ContextFactory.getFactory(); factory.setAttribute("gangliaContext.multicast", "true"); factory.setAttribute("gangliaContext.multicast.ttl", "10"); context.init("gangliaContext", factory); MulticastSocket multicastSocket = (MulticastSocket) context.datagramSocket; assertEquals("Did not set TTL", multicastSocket.getTimeToLive(), 10); }