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.utils.*; |
---|
13 | import vnsim.core.*; |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | public class RouteInfo extends Service { |
---|
18 | |
---|
19 | // this is an example of a service |
---|
20 | |
---|
21 | SimulatedCarInfo info; |
---|
22 | |
---|
23 | public RouteInfo(SimulatedCarInfo info) { |
---|
24 | this.info = info; |
---|
25 | } |
---|
26 | |
---|
27 | public VITPRequest satisfy(VITPRequest req, HashMap params) { |
---|
28 | |
---|
29 | // takes the request, reduces the count field in the URI, and adds a |
---|
30 | // line |
---|
31 | // in the message body, of the type "speed=40"; |
---|
32 | // params is a map of parameters of the local node (among them should be |
---|
33 | // 'speed') |
---|
34 | // System.out.println("Route Information modifying a request!"); |
---|
35 | int speed =(int) info.getSpeed(); |
---|
36 | |
---|
37 | try { |
---|
38 | VITPRequest request = (VITPRequest) req; |
---|
39 | req.addToBody("speed=" + speed); |
---|
40 | if (info.getRealPos().currentRoadState != 0) { |
---|
41 | req.addToBody("R" + info.getRoadIdx() + "P" |
---|
42 | + info.getPointIdx() + "C" + info.getRealPos().currentRoadState); |
---|
43 | } |
---|
44 | // reduce cnt by one |
---|
45 | Vector<String> rcs = URIParser.getRCExpressions(request.getURI()); |
---|
46 | if (rcs == null) |
---|
47 | return req; |
---|
48 | for (int i = 0; i < rcs.size(); i++) { |
---|
49 | if (((String) rcs.elementAt(i)).startsWith("cnt")) { |
---|
50 | String cntSir = (String) rcs.elementAt(i); |
---|
51 | int i1 = cntSir.indexOf('='); |
---|
52 | if (i1 == -1) |
---|
53 | return req; |
---|
54 | Integer old = new Integer(cntSir.substring(i1 + 1)); |
---|
55 | int newcnt = old.intValue() - 1; |
---|
56 | String newSir = "cnt=" + newcnt; |
---|
57 | rcs.removeElementAt(i); |
---|
58 | rcs.addElement(newSir); |
---|
59 | |
---|
60 | String oldUri = request.getURI(); |
---|
61 | String newUri = URIParser.getURIFromParts(URIParser |
---|
62 | .getType(oldUri), URIParser.getTag(oldUri), rcs, |
---|
63 | URIParser.getParams(oldUri)); |
---|
64 | request.setURI(newUri); |
---|
65 | return request; |
---|
66 | } |
---|
67 | } |
---|
68 | return req; |
---|
69 | } catch (Exception ex) { |
---|
70 | System.out.println("WARNING! EXCEPTION in VehicleTraffic:" |
---|
71 | + ex.toString()); |
---|
72 | return req; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|