/************************************************************************************ * 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.peer.*; import vnsim.applications.vitp.utils.*; import vnsim.map.object.*; public class MessageProcessServices { //this module keeps a list of all registered services (modules that know how //to serve a particular VITPRequest); this module just dispatches the VITP Request //to the appropriate module HashMap servicesMap; SimulatedCarInfo car; public MessageProcessServices(SimulatedCarInfo car) { this.car=car; servicesMap=new HashMap(); servicesMap.put("/vehicle/traffic", new VehicleTraffic(car)); servicesMap.put("/Road/Info", new RouteInfo(car)); servicesMap.put("/Route/Time", new RouteTime(car)); } public void addService(String name, Service s) { servicesMap.put(name,s); } public void remove(String name){ servicesMap.remove(name); } public VITPMessage satisfy(VITPRequest req){ //tries to find an appropriate Service, in order to serve //the request; returns the modified message (it could be a //modified request, or a new reply, built as a result of the //complete fulfilling of a request) String serviceName="/"+URIParser.getType(req.getURI())+"/"+URIParser.getTag(req.getURI()); Service s=(Service)(servicesMap.get(serviceName)); if(s==null) { System.out.println("MessageProcessServices - no service found for:"+serviceName); return req; } HashMap params=new HashMap(); params.put("speed", ""+car.getSpeed()); VITPRequest ret=s.satisfy(req, params); //check for the completion of the return condition; if true, build a reply if(!checkForCompletion(ret)) { //System.out.println("MPS-sending:"+ret); return ret; } else { //change it into a reply = invert source and destination; set REPLY VITPReply repret=ret.getReversedVITP(); repret.setStatus("OK"); return repret; } } private boolean checkForCompletion(VITPRequest req) { //check if the cnt field is 0 Vector rcs=URIParser.getRCExpressions(req.getURI()); if(rcs==null) return true; //no cnt field; consider completed for(int i=0;i