package edu.vt.marian.uip; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; //-------------------------------------------------------------------------------------------------------- // Class name: packet_input_stream // Class description: kind of wrapper of DatagramPacket and the DatagramSocket, used // for udp communication // Author: Jianxin Zhao // Finished time: June 2, 1998 // Known bugs: none // Platform: jdk1.1.5 under UNIX //-------------------------------------------------------------------------------------------------------- public class packet_input_stream { // used for debugging private Debug debug = null; // this is the sender's address private InetAddress address = null; private int port = -1; // this is used to read data private String data = null; private int start_index = 0; // this constructor will create a packet_input_stream object public packet_input_stream(DatagramSocket socket, Debug debug) { this.debug = debug; try { // first receive a packet from the sender byte[] buffer = new byte[9000]; DatagramPacket dp = new DatagramPacket(buffer, 9000); socket.receive(dp); // get the data from the packet data = new String(dp.getData(), 0, dp.getLength()); // get number of packets start_index = data.indexOf('\n'); if (start_index == -1) { debug.dumpTrace("class packet_input_stream, constructor, no number of packets"); return; } int number_of_packets = 0; try { number_of_packets = Integer.parseInt(data.substring(0, start_index)); } catch (Exception e) { debug.dumpTrace("class packet_input_stream, constructor, number_of_packets not integer"); return; } if (number_of_packets < 1) { debug.dumpTrace("class packet_input_stream, constructor, number_of_packets not valid"); return; } // read out all the remaining packets if possible for (int i = 1; i < number_of_packets; i++) { buffer = new byte[9000]; dp = new DatagramPacket(buffer, 9000); socket.receive(dp); data += new String(dp.getData(), 0, dp.getLength()); } socket.close(); // jump over the '\n' start_index++; // get the address of the sender address = dp.getAddress(); port = dp.getPort(); //debug.dumpTrace("class packet input stream, constructor, data is "); //debug.dumpTrace(data); } catch (Exception e) { debug.dumpTrace("class packet_input_stream, constructor, error create socket or read from it"); } } // this constructor will create a packet_input_stream object public packet_input_stream(DatagramPacket dp, Debug debug) { this.debug = debug; if (dp == null) { debug.dumpTrace("class packet_input_stream, constructor, null parameter identified"); return; } // first get the data from the packet data = new String(dp.getData(), 0, dp.getLength()); // set the start index to jump over the first line start_index = data.indexOf('\n'); start_index++; // get the address of the sender address = dp.getAddress(); port = dp.getPort(); } // this method will read a line from the data of this stream public String readLine() { if (start_index == data.length()) { // we reached the end of the data return null; } int end_index = data.indexOf('\n', start_index); if (end_index == -1) { // we just reached the end of the data after this read String return_string = data.substring(start_index); start_index = data.length(); return return_string; } String return_string = data.substring(start_index, end_index); start_index = end_index + 1; return return_string; } // the method will return the address of the sender public InetAddress get_ip_address() { return address; } // the method will return the address of the sender public int get_port() { return port; } }