package dslab.monitoring; import at.ac.tuwien.dsg.orvell.StopShellException; import at.ac.tuwien.dsg.orvell.annotation.Command; import dslab.ComponentFactory; import dslab.util.ComponentId; import dslab.util.Config; import java.io.InputStream; import java.io.PrintStream; import java.util.logging.Logger; public class MonitoringServer implements IMonitoringServer { private final static Logger LOG = Logger.getLogger(MonitoringServer.class.getSimpleName()); private final InputStream in; private final PrintStream out; private final MonitoringServiceImpl monitoring; private final MonitoringServerStub monitoringServerStub; private final ComponentFactory componentFactory; /** * Creates a new server instance. * * @param componentId the id of the component that corresponds to the Config resource * @param config the component config * @param in the input stream to read console input from * @param out the output stream to write console output to */ public MonitoringServer(String componentId, Config config, InputStream in, PrintStream out) { this.in = in; this.out = out; this.componentFactory = new ComponentFactory(new ComponentId(componentId), config); this.monitoring = componentFactory.getMonitoringService(); try { monitoringServerStub = new MonitoringServerStub(monitoring, config.getInt("udp.port")); } catch (Exception e) { throw new RuntimeException("Failed to initialize Monitoring Server", e); } } @Override public void run() { new Thread(monitoringServerStub).start(); componentFactory.createShell(this, in, out).run(); } @Override @Command public void addresses() { var addresses = monitoring.addresses(); if (addresses.isEmpty()) out.println("empty"); else addresses.forEach((k, v) -> out.println(k + " " + v)); } @Override @Command public void servers() { var servers = monitoring.servers(); if (servers.isEmpty()) out.println("empty"); else monitoring.servers().forEach((k, v) -> out.println(k + " " + v)); } @Override @Command public void shutdown() { LOG.info("Shutting down"); monitoringServerStub.interrupt(); componentFactory.shutdown(); throw new StopShellException(); } public static void main(String[] args) throws Exception { IMonitoringServer server = ComponentFactory.createMonitoringServer(args[0], System.in, System.out); server.run(); } }