public static void main(String args[]) { Bandwidth np; boolean sending = false; if (args.length != 4) usage(); if (args[0].equals("send")) sending = true; MSG_SIZE = Integer.decode(args[2]).intValue(); WINDOW_SIZE = Integer.decode(args[3]).intValue(); try { SandstormConfig cfg = new SandstormConfig(); if (USE_NIO) cfg.putString("global.aSocket.provider", "NIO"); Sandstorm ss = new Sandstorm(cfg); System.err.println( "Bandwidth: message size=" + MSG_SIZE + ", burst size=" + WINDOW_SIZE + ", rx block=" + BLOCKING_DEQUEUE); np = new Bandwidth(args[1], sending); np.setup(); np.doIt(); System.exit(0); } catch (Exception e) { if (VERBOSE) System.err.println("Bandwidth.main() got exception: " + e); if (VERBOSE) e.printStackTrace(); } }
public void doIt() throws SinkClosedException, IOException { int i; int n = 0; if (DEBUG) System.err.println("Bandwidth: doIt called"); byte barr[] = new byte[MSG_SIZE]; if (sending) { for (i = 0; i < MSG_SIZE; i++) { barr[i] = (byte) (i & 0xff); } } BufferElement buf; if (sending) { buf = new BufferElement(barr, 0, MSG_SIZE); } else { buf = new BufferElement(barr, 0, ACK_SIZE); } QueueElementIF fetched[]; long before, after; /* Wait for connection */ if (DEBUG) System.err.println("Bandwidth: Waiting for connection to complete"); while (true) { while ((fetched = comp_q.blocking_dequeue_all(0)) == null) ; if (fetched.length != 1) throw new IOException("Got more than one event on initial fetch?"); if (!(fetched[0] instanceof ATcpConnection)) continue; break; } conn = (ATcpConnection) fetched[0]; System.err.println("Got connection from " + conn.getAddress() + ":" + conn.getPort()); conn.startReader(comp_q); sink = (SinkIF) conn; before = System.currentTimeMillis(); if (sending && !SEND_ACKS) { /* Just blast the data! */ while (true) { while (!sink.enqueue_lossy(buf)) ; n++; if (n % 500 == 0) { after = System.currentTimeMillis(); printTime(before, after, 500, MSG_SIZE); before = after; } } } if (sending) { for (int m = 0; m < WINDOW_SIZE; m++) { sink.enqueue_lossy(buf); } } int total_size = 0; boolean first_window = true; while (true) { if (DEBUG) System.err.println("Bandwidth: Waiting for dequeue..."); if (BLOCKING_DEQUEUE) { while ((fetched = comp_q.blocking_dequeue_all(0)) == null) ; } else { while ((fetched = comp_q.dequeue_all()) == null) ; } for (i = 0; i < fetched.length; i++) { if (fetched[i] instanceof ATcpInPacket) { ATcpInPacket pkt = (ATcpInPacket) fetched[i]; int size = pkt.size(); if (DEBUG) System.err.println("GOT PACKET: " + size + " bytes"); if (!sending) { total_size += size; if (total_size == MSG_SIZE * WINDOW_SIZE) { if (first_window == true) { System.err.println("Finished receiving first burst"); first_window = false; } n += WINDOW_SIZE; if ((n % (WINDOW_SIZE * 100)) == 0) { after = System.currentTimeMillis(); if (VERBOSE) printTime(before, after, WINDOW_SIZE * 100, MSG_SIZE); before = after; } if (SEND_ACKS) { if (DEBUG) System.err.println("SENDING AN ACK"); sink.enqueue_lossy(buf); } total_size = 0; } else if (total_size > MSG_SIZE * WINDOW_SIZE) { throw new IOException( "Huh?? Got total_size " + total_size + ", should be no more than " + MSG_SIZE * WINDOW_SIZE); } } else { if (size != ACK_SIZE) { throw new IOException("Huh? Got ack size " + size); } n += WINDOW_SIZE; if (DEBUG) System.err.println("n/WINDOW_SIZE=" + (n / WINDOW_SIZE)); if ((n % (WINDOW_SIZE * 100)) == 0) { after = System.currentTimeMillis(); if (VERBOSE) printTime(before, after, WINDOW_SIZE * 100, MSG_SIZE); before = after; } if (DEBUG) System.err.println("SENDING NEXT BURST"); for (int m = 0; m < WINDOW_SIZE; m++) { sink.enqueue_lossy(buf); } } } else if (fetched[i] instanceof SinkDrainedEvent) { if (DEBUG) System.err.println("Got NetworkWriteDrainedEvent!"); } else { throw new IOException("Sender got unknown comp_q event: " + fetched[i].toString()); } } } }
private static void usage() { System.err.println("usage: Bandwidth [send|recv] <remote_hostname> <msgsize> <burstsize>"); System.exit(1); }