1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.applications.vitp.services; |
---|
7 | |
---|
8 | |
---|
9 | import java.util.*; |
---|
10 | |
---|
11 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
12 | import vnsim.applications.vitp.peer.*; |
---|
13 | import vnsim.applications.vitp.utils.*; |
---|
14 | import vnsim.map.object.*; |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | public class MessageProcessServices { |
---|
19 | |
---|
20 | //this module keeps a list of all registered services (modules that know how |
---|
21 | //to serve a particular VITPRequest); this module just dispatches the VITP Request |
---|
22 | //to the appropriate module |
---|
23 | |
---|
24 | HashMap<String, Service> servicesMap; |
---|
25 | SimulatedCarInfo car; |
---|
26 | |
---|
27 | public MessageProcessServices(SimulatedCarInfo car) { |
---|
28 | this.car=car; |
---|
29 | servicesMap=new HashMap<String, Service>(); |
---|
30 | servicesMap.put("/vehicle/traffic", new VehicleTraffic(car)); |
---|
31 | servicesMap.put("/Road/Info", new RouteInfo(car)); |
---|
32 | servicesMap.put("/Route/Time", new RouteTime(car)); |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | } |
---|
37 | |
---|
38 | public void addService(String name, Service s) { |
---|
39 | servicesMap.put(name,s); |
---|
40 | } |
---|
41 | |
---|
42 | public void remove(String name){ |
---|
43 | servicesMap.remove(name); |
---|
44 | } |
---|
45 | |
---|
46 | public VITPMessage satisfy(VITPRequest req){ |
---|
47 | //tries to find an appropriate Service, in order to serve |
---|
48 | //the request; returns the modified message (it could be a |
---|
49 | //modified request, or a new reply, built as a result of the |
---|
50 | //complete fulfilling of a request) |
---|
51 | String serviceName="/"+URIParser.getType(req.getURI())+"/"+URIParser.getTag(req.getURI()); |
---|
52 | Service s=(Service)(servicesMap.get(serviceName)); |
---|
53 | if(s==null) { |
---|
54 | System.out.println("MessageProcessServices - no service found for:"+serviceName); |
---|
55 | return req; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | HashMap<String, String> params=new HashMap<String, String>(); |
---|
60 | params.put("speed", ""+car.getSpeed()); |
---|
61 | |
---|
62 | |
---|
63 | VITPRequest ret=s.satisfy(req, params); |
---|
64 | //check for the completion of the return condition; if true, build a reply |
---|
65 | if(!checkForCompletion(ret)) { |
---|
66 | //System.out.println("MPS-sending:"+ret); |
---|
67 | return ret; |
---|
68 | } else { |
---|
69 | //change it into a reply = invert source and destination; set REPLY |
---|
70 | VITPReply repret=ret.getReversedVITP(); |
---|
71 | repret.setStatus("OK"); |
---|
72 | return repret; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | private boolean checkForCompletion(VITPRequest req) { |
---|
77 | //check if the cnt field is 0 |
---|
78 | Vector rcs=URIParser.getRCExpressions(req.getURI()); |
---|
79 | if(rcs==null) return true; //no cnt field; consider completed |
---|
80 | for(int i=0;i<rcs.size();i++){ |
---|
81 | if(((String)rcs.elementAt(i)).startsWith("cnt")) { |
---|
82 | String cntSir=(String)rcs.elementAt(i); |
---|
83 | int i1=cntSir.indexOf('='); |
---|
84 | if(i1==-1) return false; |
---|
85 | Integer n=new Integer(cntSir.substring(i1+1)); |
---|
86 | if(n.intValue()==0) |
---|
87 | return true; |
---|
88 | else |
---|
89 | return false; |
---|
90 | } |
---|
91 | } |
---|
92 | return false; |
---|
93 | } |
---|
94 | } |
---|