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 VehicleTraffic extends Service { |
---|
18 | |
---|
19 | //this is an example of a service |
---|
20 | |
---|
21 | SimulatedCarInfo info; |
---|
22 | |
---|
23 | public VehicleTraffic(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 line |
---|
30 | //in the message body, of the type "speed=40"; |
---|
31 | //params is a map of parameters of the local node (among them should be 'speed') |
---|
32 | System.out.println("VehicleTraffic modifying a request!"); |
---|
33 | double speed=info.getSpeed(); |
---|
34 | |
---|
35 | try{ |
---|
36 | VITPRequest request=(VITPRequest) req; |
---|
37 | req.addToBody("speed="+speed); |
---|
38 | //reduce cnt by one |
---|
39 | Vector<String> rcs=URIParser.getRCExpressions(request.getURI()); |
---|
40 | if(rcs==null) return req; |
---|
41 | for(int i=0;i<rcs.size();i++){ |
---|
42 | if(((String)rcs.elementAt(i)).startsWith("cnt")) { |
---|
43 | String cntSir=(String)rcs.elementAt(i); |
---|
44 | int i1=cntSir.indexOf('='); |
---|
45 | if(i1==-1) return req; |
---|
46 | Integer old=new Integer(cntSir.substring(i1+1)); |
---|
47 | int newcnt=old.intValue()-1; |
---|
48 | String newSir="cnt="+newcnt; |
---|
49 | rcs.removeElementAt(i); |
---|
50 | rcs.addElement(newSir); |
---|
51 | |
---|
52 | String oldUri=request.getURI(); |
---|
53 | String newUri=URIParser.getURIFromParts(URIParser.getType(oldUri), URIParser.getTag(oldUri), |
---|
54 | rcs, URIParser.getParams(oldUri)); |
---|
55 | request.setURI(newUri); |
---|
56 | return request; |
---|
57 | } |
---|
58 | } |
---|
59 | return req; |
---|
60 | } catch (Exception ex){ |
---|
61 | System.out.println("WARNING! EXCEPTION in VehicleTraffic:"+ex.toString()); |
---|
62 | return req; |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|