package edu.vt.marian.uip; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; //-------------------------------------------------------------------------------------------------------- // Class name: packet_output_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_output_stream { // used for debugging private Debug debug = null; // this is the address of the receiver private InetAddress address = null; private int port = -1; // this variable contains the data to be sented private StringBuffer sb = new StringBuffer(); // this is the udp fragment size private final static int MAX_PACKET_SIZE = 8000; // this is the socket associated with the stream private DatagramSocket socket = null; // this constructor will create a packet_input_stream object public packet_output_stream(InetAddress address, int port, Debug debug) { this.debug = debug; this.address = address; this.port = port; // create the socket in the constructor try { socket = new DatagramSocket(); } catch (Exception e) { debug.dumpTrace("class packet_output_stream, constructor, create socket error"); } } // this constructor will create a packet_input_stream object public packet_output_stream(String host_name, int port, Debug debug) { this.debug = debug; try { address = InetAddress.getByName(host_name); this.port = port; } catch (Exception e) { debug.dumpTrace("class packet_output_stream, constructor, error get ip address from the hostname"); } // create the socket in the constructor try { socket = new DatagramSocket(); } catch (Exception e) { debug.dumpTrace("class packet_output_stream, constructor, create socket error"); } } // this method will write one line to the data of this object public void println(String data) { sb.append(data + "\n"); } // the method will really send the data to the receiver public void flush() { try { // caculate the number of packets need to be sent int length = sb.length(); int number_of_packets = length / MAX_PACKET_SIZE + 1; sb.insert(0, Integer.toString(number_of_packets) + "\n"); String s = sb.toString(); //debug.dumpTrace("class packet_output_stream, method flush, data is "); //debug.dumpTrace(s); // send the packets int i; for (i = 0; i < number_of_packets - 1; i++) { byte[] buffer = (s.substring(i * MAX_PACKET_SIZE, (i + 1) * MAX_PACKET_SIZE)).getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, address, port); socket.send(dp); } // don't forget the last one, there is the possibility // that this one will be a little longer than 8000 because // we added the packet number byte[] buffer = (s.substring(i * MAX_PACKET_SIZE)).getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, address, port); socket.send(dp); } catch (Exception e) { debug.dumpTrace("class packet_output_stream, method flush, error create socket or send data"); } } // this method will return the socket of this stream public DatagramSocket get_socket() { return socket; } // this method will return the local port number of this stream public void close() { socket.close(); } }