I should add that the errors that I've posted are appearing on the server 
side, not the client side.  I've attached my Webserver and WebserverHandler 
code.

On Tuesday, April 17, 2012 11:56:41 PM UTC-4, Kiran wrote:
>
> Hi All,
>
> I was wondering if you had any thoughts to some errors I am getting with 
> Java I/O.
>
> Here is my setup:  I have 4 Android phones in an Ad-Hoc WIFI network. 
>  Each of these Android phones is running a Jetty Webserver (
> http://jetty.codehaus.org/jetty/).  Jetty has an app specifically for 
> Android, but I believe that is not what I want, so what I did was actually 
> just download the Jetty JAR, and put it in the build-path of my Android 
> project.  So.., each of the 4 phones is running a Jetty Webserver.  The 
> Webserver is capable of processing HTTP GET and HTTP POST commands.  The 
> HTTP GET is used by phones to get files from other phones, and the POST is 
> used to POST XML commands that essentially command a phone to do something. 
>  So.., this all seems to be working fine.
>
> Now, I'm starting to transfer a lot of files between phones.  More 
> specifically in my setup, 1 phone is serving 450 image files that the other 
> 3 phones are trying to get using the HTTP GET commands.  Each phone that is 
> requesting files basically has a list of URL's that it does HTTP GET on to 
> get files, and there is no delay (like Thread.sleep) between when a HTTP 
> GET ends and the next one is processed.  The reason for stating the point 
> of there being no delay between when the first HTTP GET ends and the next 
> HTTP GET starts is that the phone that is serving the 450 files is getting 
> pegged pretty hard by HTTP GET requests.  
>
> I've noticed the following JavaI/O errors while doing this:
>
> *04-17 22:48:59.183: W/System.err(31530): java.io.IOException: Closed
> 04-17 22:48:59.183: W/System.err(31530): at 
> org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:152)
> 04-17 22:48:59.183: W/System.err(31530): at 
> org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:101)
> 04-17 22:48:59.187: W/System.err(31530): at 
> java.io.BufferedOutputStream.write(BufferedOutputStream.java:129)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.kiran.ms.core.WebserverHandler.processHTTPGet(WebserverHandler.java:125)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.kiran.ms.core.WebserverHandler.handle(WebserverHandler.java:86)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.server.Server.handle(Server.java:346)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:442)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:924)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:582)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:218)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)
> 04-17 22:48:59.187: W/System.err(31530): at 
> org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)
> 04-17 22:48:59.187: W/System.err(31530): at 
> java.lang.Thread.run(Thread.java:1019)*
>
> *04-17 22:49:36.722: W/System.err(31530): 
> java.lang.IllegalStateException: State==HEADER*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.http.HttpGenerator.flushBuffer(HttpGenerator.java:824)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.http.AbstractGenerator.blockForOutput(AbstractGenerator.java:541)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:159)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:101)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> java.io.BufferedOutputStream.write(BufferedOutputStream.java:129)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.kiran.ms.core.WebserverHandler.processHTTPGet(WebserverHandler.java:125)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.kiran.ms.core.WebserverHandler.handle(WebserverHandler.java:86)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.Server.handle(Server.java:346)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:442)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:924)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:582)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:218)*
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)
> *
> *04-17 22:49:36.722: W/System.err(31530):  at 
> java.lang.Thread.run(Thread.java:1019)*
>
> These errors are popping up occasionally while Jetty is trying to serve a 
> specific file but I don't think the error is file specific (i.e. the same 
> errors pop up with different files at different times), and I'm thinking 
> the errors have something to do with the OS not being able to read these 
> files fast enough?
>
> The code I'm using to process execute a HTTP GET is (the relavent portion 
> of the code):
>
> BufferedInputStream bis = null;
> OutputStream outStream = null;
> try {
> HttpClient client = new DefaultHttpClient();
>         HttpGet request = new HttpGet();
>         request.setURI(new URI(getURL));
>         HttpResponse response = client.execute(request);
>         
>         bis = new BufferedInputStream(response.getEntity().getContent());
>         outStream = new FileOutputStream(fullPath);
>         
>         bytesRead = bis.read(buf1, 0, bufLen);
> while (bytesRead != -1) {
> outStream.write(buf1, 0, bytesRead);
> bytesRead = bis.read(buf1, 0, bufLen);
> }
> }
>
> Thanks in advance,
> Kiran
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
package org.kiran.ms.core;

import org.eclipse.jetty.server.Server;

import android.content.Context;
import android.util.Log;

public class Webserver implements Runnable {

	private int serverPort;
	
	private static final String TAG = Webserver.class.getSimpleName();
	private Server server;
	
	public Webserver(int port, Context context) {
		serverPort = port;
		server = null;
		
		server = new Server(serverPort);
		server.setHandler(new WebserverHandler(context));
		
	}
	
	public void stop() {
		String methodname = "stop";
		try {
			server.stop();
			
			Log.i(TAG, "Stopped successfully!");
		} catch (Exception e) {
			Log.e(TAG, methodname, e);
		}
	}

	@Override
	public void run() {
		try {
			server.start();
			server.join();
			
			Log.i(TAG, "Started successfully!");
			
		} catch (Exception e) {
			Log.i(TAG, "Webserver thread stopped");
		}
	}
	
	
}
package org.kiran.ms.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.kiran.ms.WDCConstants;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class WebserverHandler extends AbstractHandler {

	private static final String TAG = WebserverHandler.class.getSimpleName();
	
	//private String target;
	private Request baseRequest;
	//private HttpServletRequest request;
	private HttpServletResponse response;
	private String queryString;
	
	private static final String webserverDir = "/mnt/sdcard/data/";
	
	private Context context;
	
	public WebserverHandler(Context context) {
		this.context = context;
	}
	
	@Override
	public void handle(String target, Request baseRequest, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
			
		//this.target = target;
		this.baseRequest = baseRequest;
		//this.request = request;
		this.response = response;
		this.queryString = request.getQueryString();
		
		String methodRequested = request.getMethod();
		
		if(methodRequested.equals("GET")) {
			processHTTPGet();
		}
		else if(methodRequested.equals("POST")) {
			processHTTPPost();
		}
		// we don't allow any other HTTP Verbs
		
	}
	
	/**
	 * Gets a file specified by the path in the URL and returns it as an
	 * application/octet-stream
	 * URL's must be in the format: http://<ip>:<port>/fileXfer?filename=<pathToFile>
	 * <pathToFile> is relative to the base webserver directory -- which is /mnt/sdcard/data
	 * For example, if the requested file is in the directory: /mnt/sdcard/data/faces/s1/1.pgm, then
	 * the URL request must be: http://<ip>:<port>/fileXfer?filename=faces/s1/1.pgm
	 * 
	 * @throws IOException
	 */
	private void processHTTPGet() throws IOException {
		String filename = getFilenameFromQueryString();
		Log.i(TAG, "filenameFromQueryString=" + filename);
		// look for the file in the directory of files
		File f = new File(webserverDir+filename);
		int            		 BUFLEN = 2048;
        byte[]               buf1 = new byte[BUFLEN];
        int                  bytes_read = 0;
        FileInputStream      inStream = null;
        
        response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment, filename="+filename);
		
		if(f.exists()) {
			OutputStream oos = response.getOutputStream();
			inStream = new FileInputStream(webserverDir+filename);
			while ((bytes_read = inStream.read(buf1)) != -1) {
                oos.write(buf1, 0, bytes_read);
            }
			oos.close();
			response.setStatus(HttpServletResponse.SC_OK);
		}
		else {
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
		}
		baseRequest.setHandled(true);
	}
	
	/**
	 * This function handles HTTP POST requests.  Only XML strings are
	 * processed.  The XML contains a list of URL's, which point to files that
	 * the receive processor needs in order to do a task.  
	 * TODO: pass the XML string to a call-back
	 *
	 * @throws IOException
	 */
	private void processHTTPPost() throws IOException {	
		ServletInputStream sis = baseRequest.getInputStream();
		int            		 BUFLEN = 2048;
        byte[]               buf = new byte[BUFLEN];
        int bytesRead;
        int offset = 0;
        StringBuilder xml = new StringBuilder();
        
        while ((bytesRead = sis.read(buf, 0, BUFLEN)) != -1) {
        	xml.append(new String(buf).toCharArray(), offset, bytesRead);
        	offset+=bytesRead;
        }
        
        // return code of OK
        response.setStatus(HttpServletResponse.SC_OK);
		baseRequest.setHandled(true);
		
		Intent i = new Intent(WDCConstants.RX_HTTP_POST);
		i.addCategory(Intent.CATEGORY_DEFAULT);
		i.putExtra("xml", xml.toString());
		context.sendBroadcast(i);
	}
	
	private static Map<String, List<String>> getUrlParameters(String query)
	        throws UnsupportedEncodingException {
	    Map<String, List<String>> params = new HashMap<String, List<String>>();
        for (String param : query.split("&")) {
            String pair[] = param.split("=");
            String key = URLDecoder.decode(pair[0], "UTF-8");
            String value = "";
            if (pair.length > 1) {
                value = URLDecoder.decode(pair[1], "UTF-8");
            }
            List<String> values = params.get(key);
            if (values == null) {
                values = new ArrayList<String>();
                params.put(key, values);
            }
            values.add(value);
        }
	    return params;
	}
	
	private String getFilenameFromQueryString() throws UnsupportedEncodingException {
		// process the query string for filename
        HashMap<String, List<String>> params = (HashMap<String, List<String>>) getUrlParameters(queryString);
        //Set<String> keySet = params.keySet();
        
        List<String> filename = params.get("filename");
        return filename.get(0);
        
	}

}

Reply via email to