-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocketTest.java
54 lines (50 loc) · 1.84 KB
/
SocketTest.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
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
ServerSocket server;
Socket lidar;
server = new ServerSocket(15550);
System.out.println("STARTING PYTHON");
ProcessBuilder pb = new ProcessBuilder("python3","/home/tic-tac/projects/lidar/main.py");
pb.directory(new File("/home/tic-tac/projects/lidar/"));
pb.inheritIO();
Process pr = pb.start();
System.out.println("Waiting for lidar");
lidar=server.accept();
System.out.println("Lidar Connected");
long moy=0;
long n=0;
Runtime.getRuntime().addShutdownHook(
new Thread(()->{
try {
System.out.println("STOPPING BECAUSE OF SIGINT");
lidar.close();
server.close();
Runtime.getRuntime().exec("kill -f -SIGINT "+Integer.toString((int)pr.pid()));
} catch (IOException e) {
e.printStackTrace();
}
})
);
BufferedReader input = new BufferedReader(new InputStreamReader(lidar.getInputStream()));
String message="";
long last = System.currentTimeMillis();
while(true){
if(input.ready()){
message=input.readLine();
n++;
long now = System.currentTimeMillis();
moy+=now-last;
System.out.println(now-last+" "+message);
System.out.println("Temps moyen d'echantillonnage:"+moy/n);
last=now;
}
}
}
}