forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 31
/
DefaultRoutingService.java
84 lines (73 loc) · 3.12 KB
/
DefaultRoutingService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.opentripplanner.routing.service;
import java.time.ZoneId;
import org.opentripplanner.framework.application.OTPRequestTimeoutException;
import org.opentripplanner.framework.time.ZoneIdFallback;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.algorithm.RoutingWorker;
import org.opentripplanner.routing.algorithm.via.ViaRoutingWorker;
import org.opentripplanner.routing.api.RoutingService;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.RouteViaRequest;
import org.opentripplanner.routing.api.response.RoutingResponse;
import org.opentripplanner.routing.api.response.ViaRoutingResponse;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.utils.tostring.MultiLineToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO VIA: 2022-08-29 javadocs
/**
* Entry point for requests towards the routing API.
*/
public class DefaultRoutingService implements RoutingService {
private static final Logger LOG = LoggerFactory.getLogger(DefaultRoutingService.class);
private final OtpServerRequestContext serverContext;
private final ZoneId timeZone;
public DefaultRoutingService(OtpServerRequestContext serverContext) {
this.serverContext = serverContext;
this.timeZone = ZoneIdFallback.zoneId(serverContext.transitService().getTimeZone());
}
@Override
public RoutingResponse route(RouteRequest request) {
LOG.debug("Request: {}", request);
OTPRequestTimeoutException.checkForTimeout();
request.validateOriginAndDestination();
var worker = new RoutingWorker(serverContext, request, timeZone);
var response = worker.route();
logResponse(response);
return response;
}
@Override
public ViaRoutingResponse route(RouteViaRequest request) {
LOG.debug("Request: {}", request);
OTPRequestTimeoutException.checkForTimeout();
var viaRoutingWorker = new ViaRoutingWorker(
request,
req ->
new RoutingWorker(serverContext, req, serverContext.transitService().getTimeZone()).route()
);
// TODO: Add output logging here, see route(..) method
return viaRoutingWorker.route();
}
private void logResponse(RoutingResponse response) {
if (response.getTripPlan().itineraries.isEmpty() && response.getRoutingErrors().isEmpty()) {
// We should provide an error if there is no results, this is important for the client so
// it knows if it can page or abort.
LOG.warn("The routing result is empty, but there is no errors...");
}
if (LOG.isDebugEnabled()) {
var m = response.getMetadata();
var text = MultiLineToStringBuilder
.of("Response")
.addDuration("SearchWindowUsed", m == null ? null : m.searchWindowUsed)
.add("NextPage", response.getNextPageCursor())
.add("PreviousPage", response.getPreviousPageCursor())
.addColNl(
"Itineraries",
response.getTripPlan().itineraries.stream().map(Itinerary::toStr).toList()
)
.addColNl("Errors", response.getRoutingErrors())
.toString();
LOG.debug(text);
}
}
}