source: proiecte/ptvs/src/vnsim/applications/vitp/services/RouteTime.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 2.5 KB
Line 
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 ***********************************************************************************/
6package vnsim.applications.vitp.services;
7
8
9import java.util.*;
10
11import vnsim.applications.trafficview.SimulatedCarInfo;
12import vnsim.applications.vitp.utils.*;
13import vnsim.core.*;
14import vnsim.map.object.Globals;
15
16
17
18
19public class RouteTime extends Service {
20
21        // this is an example of a service
22
23        SimulatedCarInfo info;
24
25        public RouteTime(SimulatedCarInfo info) {
26                this.info = info;
27        }
28
29        public VITPRequest satisfy(VITPRequest req, HashMap params) {
30
31                // takes the request, reduces the count field in the URI, and adds a
32                // line
33                // in the message body, of the type "speed=40";
34                // params is a map of parameters of the local node (among them should be
35                // 'speed')
36//              System.out.println("Route Information modifying a request!");
37//int speed=(int) info.getSpeed();
38       
39                long speed =(Globals.engine.crtTime * 1000) / Globals.engine.fps;
40                       
41                speed =speed-info.getRealPos().timeOnSegment;
42                try {
43                        VITPRequest request = (VITPRequest) req;
44                        req.addToBody("speed=" +((int)speed));
45                        if (info.getRealPos().currentRoadState != 0) {
46                                req.addToBody("R" + info.getRoadIdx() + "P"
47                                                + info.getPointIdx() + "C" + info.getRealPos().currentRoadState);
48                        }
49                        // reduce cnt by one
50                        Vector<String> rcs = URIParser.getRCExpressions(request.getURI());
51                        if (rcs == null)
52                                return req;
53                        for (int i = 0; i < rcs.size(); i++) {
54                                if (((String) rcs.elementAt(i)).startsWith("cnt")) {
55                                        String cntSir = (String) rcs.elementAt(i);
56                                        int i1 = cntSir.indexOf('=');
57                                        if (i1 == -1)
58                                                return req;
59                                        Integer old = new Integer(cntSir.substring(i1 + 1));
60                                        int newcnt = old.intValue() - 1;
61                                        String newSir = "cnt=" + newcnt;
62                                        rcs.removeElementAt(i);
63                                        rcs.addElement(newSir);
64
65                                        String oldUri = request.getURI();
66                                        String newUri = URIParser.getURIFromParts(URIParser
67                                                        .getType(oldUri), URIParser.getTag(oldUri), rcs,
68                                                        URIParser.getParams(oldUri));
69                                        request.setURI(newUri);
70                                        return request;
71                                }
72                        }
73                        return req;
74                } catch (Exception ex) {
75                        System.out.println("WARNING! EXCEPTION in VehicleTraffic:"
76                                        + ex.toString());
77                        return req;
78                }
79        }
80}
Note: See TracBrowser for help on using the repository browser.