package com.barchart.udt;
import java.lang.reflect.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MonitorUDT {
private static final Logger log = LoggerFactory.getLogger(MonitorUDT.class);
protected final SocketUDT socketUDT;
protected MonitorUDT(final SocketUDT socketUDT) {
this.socketUDT = socketUDT;
}
protected volatile long msTimeStamp;
public long millisSinceStart() {
return msTimeStamp;
}
protected volatile long pktSentTotal;
public long globalSentTotal() {
return pktSentTotal;
}
protected volatile long pktRecvTotal;
public long globalReceivedTotal() {
return pktRecvTotal;
}
protected volatile int pktSndLossTotal;
public int globalSenderLost() {
return pktSndLossTotal;
}
protected volatile int pktRcvLossTotal;
public int globalReceiverLost() {
return pktRcvLossTotal;
}
protected volatile int pktRetransTotal;
public int globalRetransmittedTotal() {
return pktRetransTotal;
}
protected volatile int pktSentACKTotal;
public int globalSentAckTotal() {
return pktSentACKTotal;
}
protected volatile int pktRecvACKTotal;
public int globalReceivedAckTotal() {
return pktRecvACKTotal;
}
protected volatile int pktSentNAKTotal;
public int globalSentNakTotal() {
return pktSentNAKTotal;
}
protected volatile int pktRecvNAKTotal;
public int globalReceivedNakTotal() {
return pktRecvNAKTotal;
}
protected volatile long usSndDurationTotal;
public long globalMicrosSendDurationTotal() {
return usSndDurationTotal;
}
protected volatile long pktSent;
public long localPacketsSent() {
return pktSent;
}
protected volatile long pktRecv;
public long localPacketsReceived() {
return pktRecv;
}
protected volatile int pktSndLoss;
public int localSenderLost() {
return pktSndLoss;
}
protected volatile int pktRcvLoss;
public int localReceiverLost() {
return pktRcvLoss;
}
protected volatile int pktRetrans;
public int localRetransmitted() {
return pktRetrans;
}
protected volatile int pktSentACK;
public int localSentAck() {
return pktSentACK;
}
protected volatile int pktRecvACK;
public int localReceivedAck() {
return pktRecvACK;
}
protected volatile int pktSentNAK;
public int localSentNak() {
return pktSentNAK;
}
protected volatile int pktRecvNAK;
public int localReceivedNak() {
return pktRecvNAK;
}
protected volatile double mbpsSendRate;
public double mbpsSendRate() {
return mbpsSendRate;
}
protected volatile double mbpsRecvRate;
public double mbpsReceiveRate() {
return mbpsRecvRate;
}
protected volatile long usSndDuration;
public long microsSendTime() {
return usSndDuration;
}
protected volatile double usPktSndPeriod;
public double currentSendPeriod() {
return usPktSndPeriod;
}
protected volatile int pktFlowWindow;
public int currentFlowWindow() {
return pktFlowWindow;
}
protected volatile int pktCongestionWindow;
public int currentCongestionWindow() {
return pktCongestionWindow;
}
protected volatile int pktFlightSize;
public int currentFlightSize() {
return pktFlightSize;
}
protected volatile double msRTT;
public double currentMillisRTT() {
return msRTT;
}
protected volatile double mbpsBandwidth;
public double currentMbpsBandwidth() {
return mbpsBandwidth;
}
protected volatile int byteAvailSndBuf;
public int currentAvailableInSender() {
return byteAvailSndBuf;
}
protected volatile int byteAvailRcvBuf;
public int currentAvailableInReceiver() {
return byteAvailRcvBuf;
}
public void appendSnapshot(final StringBuilder text) {
text.append("\n\t");
text.append(String.format("[id: 0x%08x]", socketUDT.id()));
final Field fieldArray[] = MonitorUDT.class.getDeclaredFields();
for (final Field field : fieldArray) {
if (!isNumeric(field)) {
continue;
}
try {
field.setAccessible(true);
final String fieldName = field.getName();
final String fieldValue = field.get(this).toString();
text.append("\n\t");
text.append(fieldName);
text.append(" = ");
text.append(fieldValue);
} catch (final Exception e) {
log.error("unexpected", e);
}
}
final double localSendLoss = 100.0 * pktSndLoss / pktSent;
text.append("\n\t% localSendLoss = ");
text.append(localSendLoss);
final double localReceiveLoss = 100.0 * pktRcvLoss / pktRecv;
text.append("\n\t% localReceiveLoss = ");
text.append(localReceiveLoss);
}
protected boolean isNumeric(final Field field) {
final Class<?> fieledType = field.getType();
return fieledType == int.class || fieledType == long.class
|| fieledType == double.class;
}
@Override
public String toString() {
final StringBuilder text = new StringBuilder(1024);
appendSnapshot(text);
return text.toString();
}
}