6 |
import java.util.logging.Level; |
import java.util.logging.Level; |
7 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
8 |
|
|
9 |
|
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.List; |
12 |
|
import java.util.concurrent.Future; |
13 |
|
|
14 |
|
import org.apache.http.HttpHost; |
15 |
|
import org.apache.http.HttpRequest; |
16 |
|
import org.apache.http.HttpResponse; |
17 |
|
import org.apache.http.client.methods.HttpGet; |
18 |
|
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient; |
19 |
|
import org.apache.http.impl.nio.client.HttpAsyncClients; |
20 |
|
import org.apache.http.util.EntityUtils; |
21 |
|
|
22 |
import com.google.gson.Gson; |
import com.google.gson.Gson; |
23 |
|
|
24 |
import dk.daoas.fulddaekning.Adresse; |
import dk.daoas.fulddaekning.Adresse; |
31 |
|
|
32 |
Gson gson = new Gson(); |
Gson gson = new Gson(); |
33 |
|
|
34 |
final String base_url = "http://10.30.2.61:5000"; |
|
35 |
|
final String host = "10.30.2.61"; |
36 |
|
final int port = 5000; |
37 |
|
final String base_url = "http://" + host + ":" + port; |
38 |
|
|
39 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) { |
40 |
|
|
80 |
|
|
81 |
return bedsteAdresse; |
return bedsteAdresse; |
82 |
} |
} |
83 |
|
|
84 |
|
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) { |
85 |
|
|
86 |
|
int bedsteAfstand = Integer.MAX_VALUE; |
87 |
|
Adresse bedsteAdresse = null; |
88 |
|
|
89 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
90 |
|
haystack.toArray(hayArray); |
91 |
|
|
92 |
|
|
93 |
|
try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) { |
94 |
|
httpclient.start(); |
95 |
|
|
96 |
|
HttpHost targetHost = new HttpHost(host, port); |
97 |
|
|
98 |
|
System.out.println("1>" + targetHost.toString() ); |
99 |
|
System.out.println("2>" + targetHost.toURI() ); |
100 |
|
|
101 |
|
List<HttpRequest> requests = new ArrayList<HttpRequest>(); |
102 |
|
|
103 |
|
String loc1 = a1.latitude + "," + a1.longitude; |
104 |
|
|
105 |
|
//for (int i=0; i<hayArray.length; i++) { |
106 |
|
for (int i=0; i<2; i++) { |
107 |
|
Adresse a2 = hayArray[i]; |
108 |
|
String loc2 = a2.latitude + "," + a2.longitude; |
109 |
|
|
110 |
|
String uri = "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
111 |
|
System.out.println( uri ); |
112 |
|
|
113 |
|
requests.add( new HttpGet(uri) ); |
114 |
|
|
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null); |
119 |
|
List<HttpResponse> responses = future.get(); |
120 |
|
|
121 |
|
HttpResponse respArr[] = new HttpResponse[ responses.size() ]; |
122 |
|
responses.toArray( respArr ); |
123 |
|
|
124 |
|
for (int i=0; i<respArr.length; i++) { |
125 |
|
String jsonBody = EntityUtils.toString( respArr[i].getEntity() ); |
126 |
|
OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class); |
127 |
|
|
128 |
|
if (res.route_summary.total_distance < bedsteAfstand) { |
129 |
|
bedsteAfstand = res.route_summary.total_distance; |
130 |
|
bedsteAdresse = hayArray[i]; |
131 |
|
} |
132 |
|
|
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
System.out.println(responses); |
137 |
|
|
138 |
|
} catch(Exception e) { |
139 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
140 |
|
|
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
return bedsteAdresse; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
/* |
/* |
149 |
* Denne virker men table opslaget er 10 gange hurtigere |
* Denne virker men table opslaget er 10 gange hurtigere |