/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.applications.vitp.services; import java.util.*; import vnsim.applications.trafficview.SimulatedCarInfo; import vnsim.applications.vitp.utils.*; import vnsim.core.*; public class RouteInfo extends Service { // this is an example of a service SimulatedCarInfo info; public RouteInfo(SimulatedCarInfo info) { this.info = info; } public VITPRequest satisfy(VITPRequest req, HashMap params) { // takes the request, reduces the count field in the URI, and adds a // line // in the message body, of the type "speed=40"; // params is a map of parameters of the local node (among them should be // 'speed') // System.out.println("Route Information modifying a request!"); int speed =(int) info.getSpeed(); try { VITPRequest request = (VITPRequest) req; req.addToBody("speed=" + speed); if (info.getRealPos().currentRoadState != 0) { req.addToBody("R" + info.getRoadIdx() + "P" + info.getPointIdx() + "C" + info.getRealPos().currentRoadState); } // reduce cnt by one Vector rcs = URIParser.getRCExpressions(request.getURI()); if (rcs == null) return req; for (int i = 0; i < rcs.size(); i++) { if (((String) rcs.elementAt(i)).startsWith("cnt")) { String cntSir = (String) rcs.elementAt(i); int i1 = cntSir.indexOf('='); if (i1 == -1) return req; Integer old = new Integer(cntSir.substring(i1 + 1)); int newcnt = old.intValue() - 1; String newSir = "cnt=" + newcnt; rcs.removeElementAt(i); rcs.addElement(newSir); String oldUri = request.getURI(); String newUri = URIParser.getURIFromParts(URIParser .getType(oldUri), URIParser.getTag(oldUri), rcs, URIParser.getParams(oldUri)); request.setURI(newUri); return request; } } return req; } catch (Exception ex) { System.out.println("WARNING! EXCEPTION in VehicleTraffic:" + ex.toString()); return req; } } }