/* * Salamander Transport Layer * * Michael J. Willis (violator@umich.edu) * * $Workfile: SalPacket.java $ * $Author: rmalan $ * $Date: 1997/07/16 02:22:30 $ * $Revision: 1.4 $ */ package Salamander; import java.net.*; import java.io.*; /** * The SalPacket is the basic unit of information transferred from the * server to the SalDataQueue object specified in SalConnection.startDataFlow(). * It contains a SalProperties object, which specifies the properties of the * packet, and a data buffer that can be read from using the getInputStream() * method. */ public class SalPacket { public SalPacket() { m_props = new SalProperties(); setLength("0"); } /** * Header containing all properties of this packet */ SalProperties m_props; /** * Key containing name of service that produced this object */ static final String NAME = "NAME"; /** * Key containing timestamp of creating of this packet */ static final String TIMESTAMP = "TIMESTAMP"; /** * Key containing method name */ static final String METHOD = "COMMAND"; static final String OBJ_SIZE = "OBJ_SIZE"; /** * "Raw" data being sent */ byte m_data[]; /** * Returns an InputStream object that can be used to read the raw data * out of this packet. The length of this InputStream can be obtained * with the getLength() method, though the stream will generate end-of-file * when the data has been read. * @returns An InputStream to the data, or null<\em> if getLength() is 0. */ public InputStream getInputStream() { int length = getLength(); if (length == 0) return null; if (length != m_data.length) // somthing's rotton in the state... return null; return (InputStream) new ByteArrayInputStream(m_data); } /** * Writes to the m_data buffer. Need some way to publish data through the * java interface */ public void setData (byte[] inBytes) { m_data = new byte [inBytes.length]; for (int i = 0; i < inBytes.length; i++) m_data[i] = inBytes[i]; Integer len = new Integer (inBytes.length); m_props.setProperty ("OBJ_SIZE", len.toString()); } /** * Returns number of bytes in the data portion of this packet */ int getLength() { int i = 0; try { String size = (String) m_props.get(OBJ_SIZE); // System.out.println("Size of packet is : "+size); i = Integer.parseInt( size ); } catch (Exception e) { SAL.trace("Caught a sneeze\n"); SAL.trace("Exception: "+e); m_props.serialize(new DataOutputStream(System.err)); return 0; } return i; } /** * Returns a reference to the SalProperties object associated with * this packet. * @returns SalProperties object */ public SalProperties getProperties() { return m_props; } public void setProperties(SalProperties p) { m_props = p; } /** * @returns The name of the service that produced this object */ public String getName() { return (String) m_props.get(NAME); } /** * @returns The timestamp of this packet */ public String getTimeStamp() { return (String) m_props.get(TIMESTAMP); } /** * @returns the method of this packet */ public String getMethod() { return (String) m_props.get(METHOD); } /** * @param name The name of the service producing the related SalPacket */ public void setName(String name) { m_props.setProperty(NAME, name); } public void setLength(String length) { m_props.setProperty(OBJ_SIZE, length); } public int getPropLength() { return (m_props.getLength()); } /** * @param time The timestamp of this packet */ public void setTimeStamp(String time) { m_props.setProperty(TIMESTAMP, time); } /** * @param method The method for this packet */ public void setMethod(String method) { m_props.setProperty(METHOD, method); } public DataOutputStream serialize(DataOutputStream out) throws IOException { m_props.serialize(out); if (null != m_data) out.write(m_data); return out; } public DataInputStream serialize(DataInputStream in) throws IOException { m_props.serialize(in); int len = getLength(); m_data = new byte[len]; SAL.trace("reading in "+len+" bytes..."); for (int i=0;i