Mercurial > hg > human-echolocation-java-webapp
changeset 54:b27106b380d8
Populate and pass location of input dir; handle all parameters from page; use only one echo instance (??)
author | Chris Cannam |
---|---|
date | Mon, 03 Mar 2014 15:31:14 +0000 |
parents | f69b33d895a6 |
children | 2225ef49d9a0 |
files | WebContent/WEB-INF/classes/HumanEcho.properties WebContent/WEB-INF/data/.keep build.xml src/HumanEchoServlet.java |
diffstat | 3 files changed, 54 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/WebContent/WEB-INF/classes/HumanEcho.properties Mon Mar 03 13:17:16 2014 +0000 +++ b/WebContent/WEB-INF/classes/HumanEcho.properties Mon Mar 03 15:31:14 2014 +0000 @@ -1,1 +1,2 @@ +indir=/var/lib/tomcat7/webapps/echoapp/WEB-INF/data outdir=/tmp/wav
--- a/build.xml Mon Mar 03 13:17:16 2014 +0000 +++ b/build.xml Mon Mar 03 15:31:14 2014 +0000 @@ -1,12 +1,14 @@ <project name="EchoWebApp" default="build-war" basedir="."> - <description> - Simple build file for the EchoLocation WebApp. - </description> + <description> + Simple build file for the EchoLocation WebApp. + </description> <!-- set global properties for this build --> <property name="lib" location="WebContent/WEB-INF/lib"/> + <property name="data.dir" location="WebContent/WEB-INF/data"/> + <property name="class.dir" location="WebContent/WEB-INF/classes"/> <property name="src" location="src"/> - <property name="class.dir" location="WebContent/WEB-INF/classes"/> + <property name="mcode.dir" location="mcode"/> <property name="web.dir" location="WebContent"/> <property name="war.file" location="echoapp.war"/> @@ -19,7 +21,11 @@ <javac srcdir="${src}" destdir="${class.dir}" classpath="servlet-api.jar:${lib}/javabuilder.jar:${lib}/isvr.jar:${lib}/log4j-1.2.17.jar" includeantruntime="false"/> </target> - <target name="build-war" depends="build" + <target name="ir"> + <copy file="${mcode.dir}/hrir_final.mat" todir="${data.dir}"/> + </target> + + <target name="build-war" depends="build,ir" description="generate the WAR file" > <war destfile="${war.file}" webxml="${web.dir}/WEB-INF/web.xml"> <fileset dir="${web.dir}">
--- a/src/HumanEchoServlet.java Mon Mar 03 13:17:16 2014 +0000 +++ b/src/HumanEchoServlet.java Mon Mar 03 15:31:14 2014 +0000 @@ -29,10 +29,12 @@ import uk.ac.soton.isvr.*; public class HumanEchoServlet extends HttpServlet { + private HumanEcho echo; private static Logger logger = Logger.getLogger(HumanEchoServlet.class); - private String wavdir = ""; + private String outdir = ""; + private String indir = ""; public void init(ServletConfig config) throws ServletException { super.init(config); @@ -43,15 +45,15 @@ System.getProperty("java.library.path")); try { - // Read directory properties Properties properties = new Properties(); properties.load (Thread.currentThread().getContextClassLoader(). getResourceAsStream("HumanEcho.properties")); - wavdir = properties.getProperty("outdir"); + outdir = properties.getProperty("outdir"); + indir = properties.getProperty("indir"); - logger.info("WAV file directory is " + wavdir); + logger.info("WAV file directory is " + outdir); } catch (Exception e) { logger.error("Failed to load app properties: " + e.getMessage()); @@ -85,36 +87,41 @@ logger.info("In doGet"); + logger.info("Distance (as string) is: " + request.getParameter("dist")); + // todo: validate/normalise distance - int dist = Integer.parseInt(request.getParameter("dist")); + Double dist = Double.parseDouble(request.getParameter("dist")); - logger.info("String azim is: " + request.getParameter("azim")); - Float azim = Float.parseFloat(request.getParameter("azim")); + Double azim = Double.parseDouble(request.getParameter("azim")); + + String orientation = request.getParameter("orient"); + + Double dirweight = Double.parseDouble(request.getParameter("dirweight")); MWStructArray Input = null; - HumanEcho echo; String outputfname = new String(); - logger.info("Got dist and azim"); + logger.info("Got parameters"); try { - //!!! erch, shouldn't be creating a new one on each request as well as a global one - echo = new HumanEcho(); - logger.info("Property catalina.base is " + System.getProperty("catalina.base")); // Matlab structure: - // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav') + // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav', 'indir', '/path/to/hrir') - String[] InputStructFields = {"dist", "azim", "orient", "dirweight", "outdir", "outname"}; + String[] InputStructFields = { + "dist", "azim", "orient", "dirweight", + "outdir", "outname", "indir" + }; Input = new MWStructArray(1, 1, InputStructFields); Input.set("dist", 1, Double.valueOf(dist)); Input.set("azim", 1, Double.valueOf(azim)); - Input.set("orient", 1, "horz"); - Input.set("dirweight", 1, Double.valueOf(0.2)); - Input.set("outdir", 1, wavdir); + Input.set("orient", 1, orientation); + Input.set("dirweight", 1, Double.valueOf(dirweight)); + Input.set("outdir", 1, outdir); + Input.set("indir", 1, indir); // the ofname should depend on the parameters StringBuilder sb = new StringBuilder(); @@ -140,7 +147,11 @@ try{ stream = response.getOutputStream(); - File wavfile = new File(outputfname); + + String filename = outdir + "/" + outputfname + ".wav"; + logger.info("About to read WAV data from \"" + filename + "\""); + + File wavfile = new File(filename); //set response headers response.setContentType("audio/x-wav"); @@ -149,24 +160,24 @@ FileInputStream input = new FileInputStream(wavfile); buf = new BufferedInputStream(input); - int readBytes = 0; - //read from the file; write to the ServletOutputStream - while((readBytes = buf.read( )) != -1) - stream.write(readBytes); + int b = 0; + int total = 0; + + // read from the file; write to the ServletOutputStream + while ((b = buf.read()) != -1) { + stream.write(b); + total += 1; + } + + logger.info("Successfully wrote " + total + " byte(s) to client"); } catch (IOException ioe){ logger.error("Failed to read wav data: " + ioe.getMessage()); throw new ServletException(ioe.getMessage( )); } finally { - //close the input/output streams - if(stream != null) - stream.close( ); - - if(buf != null) - buf.close( ); - } - - logger.info("Wrote file: " + outputfname); + if (stream != null) stream.close(); + if (buf != null) buf.close(); + } } }