package csx55.overlay.util; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; /** * This class is used by messaging nodes to store the statistics of messages whenever * it sends, receives, or relays messages. Each operation is done atomically to make * sure that whenever messaging nodes access these variables, they are fully commited * and not in intermediate states. This is necessary because each node can receive many * messages at once from all of its peers. This is passed in to the TaskSummaryResponse * wireformat and the values are sent to the registry. */ public class StatisticsCollector { private AtomicInteger messagesSent; private AtomicLong sentSummation; private AtomicInteger messagesRec; private AtomicLong recSummation; private AtomicInteger messagesRelayed; public StatisticsCollector() { this.messagesSent = new AtomicInteger(0); this.sentSummation = new AtomicLong(0); this.messagesRec = new AtomicInteger(0); this.recSummation = new AtomicLong(0); this.messagesRelayed = new AtomicInteger(0); } //getters for accessing private variables public AtomicInteger getMessagesSent() { return this.messagesSent; } public AtomicLong getSentSummation() { return this.sentSummation; } public AtomicInteger getMessagesRec() { return this.messagesRec; } public AtomicLong getRecSummation() { return this.recSummation; } public AtomicInteger getMessagesRelayed() { return this.messagesRelayed; } //sets all values to 0 so that start can be used again without errors public void setToZero() { this.messagesSent = new AtomicInteger(0); this.sentSummation = new AtomicLong(0); this.messagesRec = new AtomicInteger(0); this.recSummation = new AtomicLong(0); this.messagesRelayed = new AtomicInteger(0); } //incerements the value for sent and adds to the summation atomically public void sendInfo(int payload) { this.sentSummation.addAndGet(payload); this.messagesSent.getAndIncrement(); } //incerements the value for received and adds to the summation atomically public void receiveInfo(int payload) { this.recSummation.addAndGet(payload); this.messagesRec.getAndIncrement(); } //incerements the value for relayed message atomically public void relayInfo() { this.messagesRelayed.getAndIncrement(); } }