/* * AudioFileReader.java * * This class reads the encoded audio file at the host active node, * packetizes the audio data into frames, inserts the frame in its * own individual SmartPacket and injects the packet in the network * towards the destination */ package audioapp; import java.io.*; import magician.Node.*; import java.util.*; import magician.PerfTools.*; public class AudioFileReader extends AudioApp implements Sleeper { /* * destination environment variable */ static String DESTINATION = "AUDIO_DEST"; /* * audio fiel path name variable */ static String AUDIOFILEPATH = "AUDIOFILE"; /* * one audio frame size */ static final int MAX_FRAME_SIZE = 40; /* * end-of-file marker */ static final int EOF = -1; /* * buffer for holding one audio frame data */ private byte[] frame; /* * number of audio data bytes actually in frame buffer */ int frameSize; /* * Path of encoded file */ String audioFilePath; /* * destination node of audio packets */ String destn; /* * stream handle to audio file */ BufferedInputStream audStream; /* * random generator for choosing packets to send */ Random rgen; Alarm alrm; int max_time; boolean sendMal; FileInputStream propStream; Properties audProps; /* * constructor */ public AudioFileReader() { try { propStream = new FileInputStream("/VirginieLocal/Platforms/Magician/Applications/audioapp/audioapp.properties"); audProps = new Properties(); loadProperties(); } catch (Exception fie) { fie.printStackTrace(); } /* initialize frame array */ frame = new byte[MAX_FRAME_SIZE]; rgen = new Random(System.currentTimeMillis()); int intvl = getInterval(); alrm = new Alarm(this, intvl); alrm.setPriority(Thread.MAX_PRIORITY - 3); alrm.start(); Thread.currentThread().yield(); } /* * get destination from environment variable * * @return the destination as a string * @exception NoSuchFieldException if environment variable is not defined */ private void getDestination() throws NoSuchFieldException { destn = audProps.getProperty(DESTINATION); if (destn == null) { throw new NoSuchFieldException(); } } /* * reads file a frame at a time * * @return number of bytes read from file or -1 if EOF */ protected int getFrame() { /* * re-initialize the frame buffer */ initFrame(frame); /* read MAX_FRAME_SIZE bytes from audio file * and put it in the frame buffer */ int frameSize = 0; try { frameSize = audStream.read(frame, 0, MAX_FRAME_SIZE); } catch (NullPointerException npe) { try { audStream = new BufferedInputStream(new FileInputStream(audioFilePath)); frameSize = audStream.read(frame, 0, MAX_FRAME_SIZE); } catch (FileNotFoundException fne) { fne.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } catch (IOException ioe) { ioe.printStackTrace(); } return frameSize; } /* * sends audio packet towards destination * @param size size of data in current frame */ protected void sendPacket(int seqnum, int size) { // create new audio packet for frame AudioPacket ap = new AudioPacket(seqnum); //System.err.println("Audio filepath =" + audioFilePath + "xxxx"); ap.setAudioFilePath(audioFilePath + ".out"); ap.setFrameData(frame, size); //System.err.println("Audio packet dest =" + destn + "xxxx"); ap.SendForProcessing(destn); } /* * gets path to the encoded audio file from environment variable * * @exception NoSuchFieldException if environment variable for file path is not set */ private void getAudioFilePath() throws NoSuchFieldException { // get audio file name and path from // environment variable audioFilePath = audProps.getProperty(AUDIOFILEPATH); if (audioFilePath == null) { throw new NoSuchFieldException(); } } /* * main body gets audio file, reads in a frame * at a time, creates audio packet and sends * it to the destination */ public void exec() { /* * get destination from environment variable */ //audioFilePath = "audiofile_en.tdv"; try { getDestination(); getAudioFilePath(); } catch (NoSuchFieldException nsfe) { if (destn == null) { System.err.println("Destination environment variable not set"); } else { System.err.println("Audio file path environment variable not set"); } halt(); return; } System.err.println("File " + audioFilePath + " to be sent to --" + destn+"--"); /* * keep reading from audio file frame by frame until EOF */ int size = 0; // sequence number of packet int seq = 1; int i = 0; while ((size = getFrame()) != EOF) { i = i+1; // create and send packet to destination sendPacket(seq, size); if ((i % 5) == 0) { // sleep(100); // sendMaliciousPacket(); } // sleep a while if (seq == 1) { sleep(1000); } else { sleep(100); } seq++; } alrm.stop(); halt(); } /* * initialize a frame with null data * * @param framebuf the frame to be initialized */ private void initFrame(byte[] framebuf) { int len = framebuf.length; for (int ix = 0; ix < len; ix++) { framebuf[ix] = (byte) 0; } } public void wakeup() { // if (sendMal) { // sendMaliciousPacket(); // } // // loadProperties(); // int intvl = getInterval(); // alrm.setInterval(intvl); } private void sendMaliciousPacket() { // System.err.println("Sending malicious packet at time " + System.currentTimeMillis()); AudioPacket mp = new AudioPacket(0, false); mp.SendForProcessing(destn); } protected void loadProperties() { try { audProps.load(propStream); } catch (Exception e) { e.printStackTrace(); } } protected int getInterval() { int intvl = 0; try { String s_intvl = audProps.getProperty("INTVL"); if (s_intvl == null) { //System.err.println("AudioFileReader: Setting interval to DEFAULT"); sendMal = false; intvl = 5000; } else { sendMal = true; max_time = Integer.parseInt(s_intvl) * 1000; intvl = rgen.nextInt(max_time); } } catch (Exception pe) { pe.printStackTrace(); } return intvl; } }