/* * Sample Java Salamander Suppler code. * * G. Robert Malan * 7/14/97 * * $Id: SampleJavaSalamanderSupplier.java,v 1.3 1997/07/16 03:08:51 rmalan Exp $ */ import Salamander.*; // the Salamander package contains all necessary classes import java.util.*; import java.io.*; /** * This SampleJavaSalamanderSupplier class is a driver to demonstrate the * instantiation and use of a SalConnection object for the purpose of * connecting a simple supplier to the Salamander server. */ public class SampleJavaSalamanderSupplier implements SalDataQueue { SalConnection salConnection; /** * The first of two methods defined in the SalDataQueue interface. * * enqueue() is called by the SalConnection object internally whenever * a new SalPacket addressed to this data queue has arrived. * @param p The SalPacket object that has arrived */ public void enqueue(SalPacket p) { // Grab the properties out of this packet (for demonstration) SalProperties props = p.getProperties(); Enumeration e = props.keys(); // print out all the headers (properties) for this packet while (e.hasMoreElements()) { String key = (String) e.nextElement(); System.out.println(key+": "+(String)props.get(key)); } // If there was any meaningful data, we could use it like this // InputStream in = p.getInputStream(); // read out the raw data... // in.readInt(); //....use data here.... } /** * The second of two methods in the SalDataQueue interface. */ public int getCurrentQueueLength() { // Not implemented. return 0; } /** * supplierMethod: * * Main body of the simple supplier. */ public void supplierMethod(String[] arg) { String queueName; int i; // turn on internal diagnostics SAL.enableTrace(); // The first argument is the queue name to supply. if (arg.length < 1) { queueName = "test"; }else{ queueName = arg[0]; } System.out.println("Queue Name = "+queueName); // Obtain a SalConnection object. Note that the constructor // of SalConnection is not public, thus this is the only way // to obtain a SalConnection object. // In this simple example, we pass no parameters to getSalConnection() // so that defaults of "lorax.eecs.umich.edu" and 8086 will be used. salConnection = SalConnection.getSalConnection(); if (null == salConnection) { System.err.println("SalClientTest: Could not get SalConnection()!"); System.exit(1); } // Request a dataflow for any salamander feedback to this object. // By passing this object as the second parameter, // we have specified that we will get the feedback in our enqueue // method. try { salConnection.startDataFlow(SalProperties.ALL_SALAMANDER_DATA, this); } catch (SalException e) { SAL.error("SalClientTest: Could not start data flow!"); } SAL.trace("Started data flow..."); // Keep sending simple packets to the Salamander server. for (i = 1; true; i++) { // now send the image to salamander, one image per packet. SalPacket outboundPacket = new SalPacket(); Date now = new Date (); Long timeval = new Long (now.getTime()); outboundPacket.setTimeStamp (timeval.toString()); SalProperties prop = outboundPacket.getProperties(); prop.setProperty(SalProperties.COMMAND_PROPERTY, SalProperties.PUBLISH_COMMAND); prop.setProperty(SalProperties.NAME_PROPERTY, queueName); prop.setProperty("SUBNAME", (new Integer(i)).toString()); outboundPacket.setProperties(prop); // Set the data payload with the setData method, eg: // outboundPacket.setData (byteArr); // Dump out the properties of the packet we're about to send. SalProperties props = outboundPacket.getProperties(); Enumeration en = props.keys(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); System.out.println (key + ": " + props.get(key)); } // Actually send the packet. Blocking send. salConnection.dispatch(outboundPacket); // Slight pause before the next iteration... try { Thread.currentThread().sleep(5); } catch (Exception e) { System.out.println("Sleep failed."); } } } /** * main is declared so that this example can be run directly * as an application */ public static void main(String[] arg) { SampleJavaSalamanderSupplier me = new SampleJavaSalamanderSupplier(); // Pass the command line to the supplier method. me.supplierMethod(arg); } }