public class RecvBuffer { private Object[] buf; private int MAX_BUF; private int Nfe = 0; public RecvBuffer (int size) { buf = new Object[size]; MAX_BUF = size; } /* return index position in our array for a given sequence number */ private int idx (int s) { return s%MAX_BUF; } /* return data of next packet in sequence */ public Object getnextpacket() { if (buf[idx(Nfe)] != null) { Object data = buf[idx(Nfe)]; buf[idx(Nfe++)] = null; return data; } else { return null; } } /* return sequence number if this packet is acknowledged, * or -1 if no ACK is sent. */ public int storepacket (int s, Object data) { if (s < Nfe) { return s; } if (s >= (Nfe+MAX_BUF)) { return -1; } buf[idx(s)] = data; return s; } /* Optional: return buffer state as string representation */ private int step = 0; public String show () { String status = "("+ step++ +") "; int Nfe_idx = idx(Nfe); for (int i = 0; i < MAX_BUF ; i++) { int offset = (i - Nfe_idx+MAX_BUF)%MAX_BUF; status = status + ((i == Nfe_idx)? "*" : " ")+i + ":"; int s = Nfe+offset; if (buf[idx(i)] != null) { status = status + "["+s+"],"; } else { status = status + " "+s+", "; } } return status; } }