package ubc.cs.cpsc210.sustainabilityapp.routing;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.util.Log;
import ubc.cs.cpsc210.sustainabilityapp.model.LatLong;
/**
* Wrapper around a service which calculates routes between geographic
* locations. This class may be called concurrently from multiple threads -- it
* is thread-safe.
*
* Currently, this class wraps the www.yournavigation.org API (http://wiki.openstreetmap.org/wiki/YOURS#Routing_API).
*/
public class RoutingService {
/**
* Caches routes retrieved by their endpoints. Access to this map must be
* synchronized on the map.
*/
private Map routeCache = new HashMap();
/**
* Client for making HTTP requests to the API of the service.
*/
private HttpClient client;
public RoutingService() {
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be accessing the HttpClient.
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
params, schemeRegistry);
client = new DefaultHttpClient(cm, params);
}
public void shutdown() {
if (client != null) {
client.getConnectionManager().shutdown();
}
}
/**
* Calculate route for given start point and end point. An internet
* connection must be available. See {@link getRouteFromService} for further
* information on route generation.
*
* @param start
* The start point of the route.
* @param end
* The end point of the route.
* @param useCache
* Indicates whether the service should return a cached route, if
* one exists. If this flag is set to true, and a cached route is
* not available, then the new route obtained from the server
* will be cached.
* @return Information on the route calculated, including the waypoints.
* @throws IOException
* If an error occurs while retrieving the route from the
* server.
*/
public RouteInfo getRoute(LatLong start, LatLong end, boolean useCache)
throws IOException {
RouteEndpoints endpoints = new RouteEndpoints(start,end);
RouteInfo ri;
try {
ri=getRouteFromService(endpoints);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* Calculate route for given endpoints. Currently, we use the
* www.yournavigation.org API (http://wiki.openstreetmap.org/wiki/YOURS#Routing_API), with result
* format set to geojson, vehicle set to foot and route type set to shortest
* (rather than fastest). Using route type of fastest can result in
* different routes between the same two points, depending on the direction
* traveled.
*
* Subclasses can override this method to connect to alternate routing
* services.
*
* @param endpoints
* Endpoints of the route.
* @return Information on the route calculated, including waypoints.
* @throws IOException
* If an error occurs while retrieving the route from the
* server.
* @throws URISyntaxException
*/
protected RouteInfo getRouteFromService(RouteEndpoints endpoints) throws IOException, URISyntaxException {
String requestString, returnedString;
HttpGet hg;
URI uri = null;
List waypoints = new ArrayList();
requestString = "http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson";
requestString += "&flat=" + endpoints.getStart().getLatitude();
requestString += "&flon=" + endpoints.getStart().getLongitude();
requestString += "&tlat=" + endpoints.getEnd().getLatitude();
requestString += "&tlon=" + endpoints.getEnd().getLongitude();
requestString += "&v=foot";
requestString += "&fast=0";
requestString += "&layer=mapnik";
System.out.println(requestString);
try {
uri = new URI(requestString);
}
catch (URISyntaxException e)
{
// handle exception
}
hg = new HttpGet(uri);
hg.addHeader("X-Yours-client", "UBC CPSC 210");
/* ResponseHandler handler = new ResponseHandler() {
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
} else {
return null;
}
}
}; */
ResponseHandler responseHandler = new BasicResponseHandler();
returnedString = client.execute(hg, responseHandler);
System.out.println(returnedString);
JSONObject json = new JSONObject();
return null;
}
private RouteInfo getCachedRoute(RouteEndpoints endpoints) {
synchronized (routeCache) {
return routeCache.get(endpoints);
}
}
private void addRouteToCache(RouteEndpoints endpoints, RouteInfo routeInfo) {
synchronized (routeCache) {
routeCache.put(endpoints, routeInfo);
}
}
}