package edu.brown.cs.systems.xtrace.reporting;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.typesafe.config.ConfigFactory;

import edu.brown.cs.systems.pubsub.PubSub;

/**
 * A thread that sends X-Trace reports to the X-Trace server via PubSub
 */
public class PubSubReporter extends Thread implements XTraceReporter {

    protected static final Logger log = LoggerFactory.getLogger(PubSubReporter.class);

    protected final BlockingQueue<XTraceReport> outgoing = new LinkedBlockingQueue<XTraceReport>();

    /**
     * Creates a new PubSub reporter that publishes reports to the X-Trace
     * server via the default PubSub server
     */
    public PubSubReporter() {
        setDaemon(true);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                close();
            }
        });
    }
    
    public void close() {
        try {
            this.interrupt();
            this.join();
        } catch (InterruptedException e) {
            // return;
        }
    }

    /**
     * Enqueues the provided report to be sent by the reporting thread
     * 
     * @param report
     *            The report to send
     */
    public void send(XTraceReport report) {
        outgoing.offer(report);
    }

    @Override
    public void run() {
        // Get the configured topic to publish reports to
        String topic = ConfigFactory.load().getString("xtrace.pubsub.topic");
        log.info("Publishing X-Trace reports on topic {}", topic);

        try {
            while (!Thread.currentThread().isInterrupted()) {
                // Take the next xtrace report
                XTraceReport report = outgoing.take();

                // Build and send
                PubSub.publish(topic, report.builder.build());
            }
        } catch (InterruptedException e) {
            // Do nothing and return
            log.warn("Publisher thread interrupted {}", e);
        }
        
        // Drain reports
        XTraceReport report;
        while ((report = outgoing.poll()) != null) {
            PubSub.publish(topic, report.builder.build());
        }

        log.info("PubSubReporter complete");
    }

}

@@ -1994,68 +1994,8 @@
 urn%0A
-            log.warn(%22Publisher thread interrupted %7B%7D%22, e);%0A
     
@@ -2204,16 +2204,24 @@
 g.info(%22
+X-Trace 
 PubSubRe
@@ -2231,16 +2231,14 @@
 ter 
-complete
+ending
 %22);%0A

Remove some superfluous logging statements