public class buffertest { /* make ourselves a 5-slot receiving window */ private static Ringpuffer rbuf = new Ringpuffer(5); /* visualization functions */ private static void show_getbuf (String retval) { System.out.print ("\t\t\t^^^ "+ (retval != null? retval : "[null]" ) + " ^^^"); System.out.println ("\t\t" + rbuf.show() ); } private static void show_delivery (int ack, int s, String data) { System.out.print ("--- ["+ s +": "+ data +"] -->"); System.out.println ("\t\t\t\t" + rbuf.show() ); if (ack >= 0) { System.out.println ("<-- [ACK_"+ ack + "] ---"); } } public static void main(String[] args) throws Exception { int EOT = -1000; String[] data = {"And", "then", "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}; /* Testing sequence: positive values indicate that the corresponding * data element (see above) is delivered, negative values indicate that * the upper layer requests a packet. */ int[] trace = {1, 2, 3, -1, -1, 4, 5, 6, -1, 0, -1, -1, 7, 8, 9, -1, -1, 6, 7, 8, -1, -1, 5, 9, -1, 10, -1, -1, 9, 8, -1, -1, -1, EOT}; // commence testing sequence int i = 0; String message = ""; do { if (trace[i]<0) { /* upper layer requests next packet */ String next = (String)rbuf.getnextpacket(); if (next != null) { message = message + next; } /* visualize on screen */ show_getbuf(next); } else { /* lower layer delivers next packet */ int ack = rbuf.storepacket(trace[i], data[trace[i]]); /* visualize on screen */ show_delivery(ack, trace[i], data[trace[i]]); } i++; } while (trace[i] != EOT); System.out.println(message); } // of main } // of UserAgent class