Skip to content

Commit 3cc255e

Browse files
author
NoteFox
committed
added ControllerEndpoint for ohdmServiceController
1 parent c5dfbb2 commit 3cc255e

17 files changed

Lines changed: 444 additions & 122 deletions

File tree

Server/db_rendering.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

Server/runner-0.5-SNAPSHOT.jar

-17 MB
Binary file not shown.

init.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// '[init]' says this is an init file,
2+
// just so it doesn't read an random file out of nothing
3+
//
4+
// Standart config
5+
// 'variable' 'name' = 'value'
6+
// '//' Comment
7+
8+
[init]
9+
logDefaultDir = log/
10+
mapsDefaultDir = maps/
11+
sftpDefaultDir = sftp/
12+
13+
renderingParameterFilePath = db_rendering.txt
14+
ohdmConverterFilePath = OHDMConverter.jar
15+
jdbcDriverFilePath = postgresql-42.1.1.jar
16+
17+
javaJdkPath = java
18+
19+
webPort = 5001
20+
sftpPort = 5002
21+
22+
standardUserName = ohdmOffViewer
23+
standardUserPassword = H!3r0glyph Sat3llite Era$er

pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>ohdm</groupId>
1414
<artifactId>runner</artifactId>
15-
<version>0.5.2-SNAPSHOT</version>
15+
<version>0.9.2-SNAPSHOT</version>
1616
<name>runner</name>
1717
<description>mapDownloadService for Android App</description>
1818
<!-- lookup parent from repository -->
@@ -72,7 +72,11 @@
7272
<version>0.1.55</version>
7373
</dependency>
7474

75-
75+
<dependency>
76+
<groupId>com.google.code.gson</groupId>
77+
<artifactId>gson</artifactId>
78+
<version>2.8.6</version>
79+
</dependency>
7680

7781
</dependencies>
7882

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package Server.ControllerEndpoint;
2+
3+
import Server.CustomObjects.QueryRequest;
4+
import Server.LogService.Logger;
5+
import Server.SpringClass;
6+
7+
import java.io.*;
8+
import java.text.SimpleDateFormat;
9+
10+
public class ControllerEndpoint extends Thread {
11+
12+
private String msgPath;
13+
14+
public ControllerEndpoint(String msgPath) {
15+
this.msgPath = msgPath;
16+
}
17+
18+
private synchronized File fetchNewFile() throws InterruptedException {
19+
File msgDir = new File(msgPath);
20+
if (!msgDir.exists()) {
21+
msgDir.mkdir();
22+
}
23+
24+
File[] fileList;
25+
26+
while (true) {
27+
fileList = msgDir.listFiles();
28+
if (fileList.length == 0)
29+
wait(1000);
30+
else
31+
for (File f : fileList) {
32+
if (!f.getName().endsWith(".req") && !f.getName().endsWith(".ans"))
33+
f.delete();
34+
else {
35+
if (f.getName().endsWith(".ans")) {
36+
// kindly ignore
37+
} else {
38+
return f;
39+
}
40+
}
41+
42+
wait(1000);
43+
}
44+
}
45+
}
46+
47+
private ControllerRequest readContent(File file) throws IOException {
48+
String id = file.getName().replace(".req", "");
49+
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
50+
51+
if (!br.ready())
52+
return null;
53+
54+
String value = br.readLine();
55+
br.close();
56+
return new ControllerRequest(id, value);
57+
}
58+
59+
private BufferedWriter writeResponse(File file, ControllerRequest r) throws IOException {
60+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
61+
bw.write(r.returnValue);
62+
bw.flush();
63+
return bw;
64+
}
65+
66+
private String processRequest(String request) throws IOException {
67+
String answer = "";
68+
BufferedReader br;
69+
switch (request) {
70+
case "help" :
71+
answer += "currently implemented : " + "\n";
72+
answer += " status - giving back the status the service is currently in \n";
73+
answer += " log - gives back the current log entries\n";
74+
answer += " daemonLog - gives back the daemon log entries\n";
75+
answer += " reload webservice - restarts the service with new read init values (all buffered requests will be saved)\n";
76+
answer += " reload log - restarts log with new read init values\n";
77+
answer += " restart - restarts the whole Service with new read init values\n";
78+
return answer;
79+
80+
81+
case "status" :
82+
answer += "Service running : " + SpringClass.serviceInstance.isRunning() + "\n";
83+
answer += "Service currently working : " + SpringClass.serviceInstance.isActive() + "\n";
84+
answer += "Requests currently waiting : " + SpringClass.serviceInstance.getBUFFER_LIST().size() + "\n";
85+
answer += "Logger active : " + Logger.instance.isRunning + "\n";
86+
answer += "-----------------------------------------------------------------------------\n";
87+
answer += "current working Requests : \n";
88+
for (QueryRequest r:
89+
SpringClass.serviceInstance.getWORKER_LIST()) {
90+
answer += "ID: " + r.getRequestedByID() + "\n";
91+
answer += "requestName: " + r.getMapName() + "\n";
92+
answer += "status: " + r.getStatus() + "\n\n";
93+
answer += "time passed since start" + new SimpleDateFormat("yyyy-MM-dd at HH-mm-ss").format(System.currentTimeMillis() - r.getStartTime());
94+
}
95+
return answer;
96+
97+
case "current log" :
98+
File loggerFile = Logger.instance.currentWritingFile;
99+
br = new BufferedReader(new InputStreamReader(new FileInputStream(loggerFile)));
100+
while (br.ready())
101+
answer += br.readLine() + "\n";
102+
103+
return answer;
104+
105+
case "daemon log" :
106+
File daemonLogFile = new File("daemonLog.log");
107+
br = new BufferedReader(new InputStreamReader(new FileInputStream(daemonLogFile)));
108+
while (br.ready())
109+
answer += br.readLine() + "\n";
110+
111+
return answer;
112+
113+
case "reload webservice":
114+
return SpringClass.reloadWebService();
115+
116+
case "reload log":
117+
return SpringClass.reloadLog();
118+
119+
case "restart":
120+
return SpringClass.restart();
121+
122+
default:
123+
return "couldn't process request " + request;
124+
}
125+
}
126+
127+
private class ControllerRequest {
128+
String id;
129+
String value;
130+
String returnValue;
131+
132+
public ControllerRequest(String id, String value) {
133+
this.id = id;
134+
this.value = value;
135+
}
136+
137+
public void setReturnValue(String returnValue) {
138+
this.returnValue = returnValue;
139+
}
140+
141+
public String getReturnValue() {
142+
return returnValue;
143+
}
144+
}
145+
146+
@Override
147+
public synchronized void run() {
148+
while (true) {
149+
File f = null;
150+
try {
151+
f = fetchNewFile();
152+
System.out.println("new File found");
153+
} catch (InterruptedException e) {
154+
e.printStackTrace();
155+
}
156+
157+
try {
158+
ControllerRequest request = readContent(f);
159+
160+
assert request != null;
161+
request.setReturnValue(processRequest(request.value));
162+
163+
f.delete();
164+
165+
File returnFile = new File(msgPath + request.id + ".ans");
166+
writeResponse(returnFile, request).close();
167+
168+
} catch (IOException e) {
169+
e.printStackTrace();
170+
}
171+
}
172+
}
173+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Server.CustomObjects;
2+
3+
public class LogEntry {
4+
5+
public LogType type;
6+
public String TAG;
7+
public String message;
8+
9+
public LogEntry(LogType type, String TAG, String message) {
10+
this.type = type;
11+
this.TAG = TAG;
12+
this.message = message;
13+
}
14+
}

src/java/Server/CustomObjects/QueryRequest.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ public class QueryRequest extends Thread {
2121
private String osmDir;
2222
private String mapDir;
2323

24-
public QueryRequest(List<Coords> coordinates, String date, String mapName, int id, String osmDir, String mapDir) {
24+
private String renderingParameter;
25+
private String ohdmConverter;
26+
27+
private String javaJdkPath;
28+
private String jdbcDriverPath;
29+
30+
private long startTime = System.currentTimeMillis();
31+
32+
public QueryRequest(List<Coords> coordinates, String date, String mapName, int id, String osmDir, String mapDir, String renderingParameter, String ohdmConverter, String javaJdkPath, String jdbcDriverPath) {
2533
this.osmDir = osmDir;
2634
this.mapDir = mapDir;
2735

36+
this.renderingParameter = renderingParameter;
37+
this.ohdmConverter = ohdmConverter;
38+
39+
this.javaJdkPath = javaJdkPath;
40+
this.jdbcDriverPath = jdbcDriverPath;
41+
2842
this.coordinates = coordinates;
2943
this.date = date;
3044
this.mapName = mapName;
@@ -64,6 +78,10 @@ public String getErrorMessage() {
6478
return errorMessage;
6579
}
6680

81+
public long getStartTime() {
82+
return startTime;
83+
}
84+
6785
/**
6886
* calls a script to convert an .osm to a .map file.
6987
*
@@ -95,20 +113,20 @@ private CommandReturn convert_map() {
95113
*
96114
* @return 0 on success, anything else on failure.
97115
*/
98-
private CommandReturn download_map() throws Exception {
99-
String[] template = new String[]{"java", "-jar", "OHDMConverter.jar", "-o", osmDir + "/" + mapName + ".osm", "-r", "db_rendering.txt", "-p", rearrangeCoordsForScript(getCoordinates()), "-t", date};
116+
private CommandReturn download_map() {
117+
String[] template = new String[]{javaJdkPath, "-classpath", jdbcDriverPath, "-jar", ohdmConverter, "-o", osmDir + "/" + mapName + ".osm", "-r", renderingParameter, "-p", rearrangeCoordsForScript(getCoordinates()), "-t", date};
100118

101119
String tmpl = "";
102-
for (String s:
103-
template) {
120+
for (String s :
121+
template) {
104122
tmpl += s + " ";
105123
}
106124

107-
Logger.instance.addLogEntry(LogType.INFO, TAG,"running: " + tmpl);
125+
Logger.instance.addLogEntry(LogType.INFO, TAG, "running: " + tmpl);
108126

109127
CommandReturn result;
110-
result = excCommand(template);
111128

129+
result = excCommand(template);
112130
return result;
113131
}
114132

src/java/Server/CustomPRNG.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Server;
2+
3+
// some rand
4+
public class CustomPRNG {
5+
private static long next = 1;
6+
public static long random() {
7+
next = next * 1103515245 / 12345;
8+
return (next*665536)%32768;
9+
}
10+
11+
static void seeding(long seed) {
12+
next = seed;
13+
}
14+
}

src/java/Server/FileService/FileServerClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface FileServerClient<RemoteFile, RemoteDirectory> {
5353
* @return FTPFiles in current dir
5454
* @throws IOException couldn't read from current dir
5555
*/
56-
ArrayList<RemoteFile> getAllFileList(String path) throws IOException;
56+
//ArrayList<RemoteFile> getAllFileList(String path) throws IOException;
5757

5858
/**
5959
* call to download a File from current dir

0 commit comments

Comments
 (0)