-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackerReader.java
More file actions
104 lines (87 loc) · 3.13 KB
/
Copy pathTrackerReader.java
File metadata and controls
104 lines (87 loc) · 3.13 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//http://www.binarytides.com/java-socket-programming-tutorial/
import java.io.*;
import java.util.Map;
import java.net.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class TrackerReader extends Thread
{
public volatile double x;
public volatile double y;
public volatile double a;
public volatile double theta;
public volatile double targetx;
public volatile double targety;
//public static void main(String args[])
public void run()
{
x = 0;
y = 0;
System.out.println("LISTENER");
ServerSocket s = null;
Socket conn = null;
try
{
//1. creating a server socket - 1st parameter is port number and 2nd is the backlog
s = new ServerSocket(5000 , 10);
//2. Wait for an incoming connection
echo("Server socket created.Waiting for connection...");
conn = s.accept();
//print the hostname and port number of the connection
echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());
String line , input = "";
JSONParser parser=new JSONParser();
try
{
//get socket writing and reading streams
DataInputStream in = new DataInputStream(conn.getInputStream());
PrintStream out = new PrintStream(conn.getOutputStream());
//Send welcome message to client
//out.println("Welcome to the Server");
//Now start reading input from client
while((line = in.readLine()) != null && !line.equals("."))
{
try{
//System.out.println(line);
Object obj=parser.parse(line);
JSONObject jsonObject = (JSONObject) obj;
x = (Double) jsonObject.get("x");
y = (Double) jsonObject.get("y");
a = (Double) jsonObject.get("a");
theta = (Double) jsonObject.get("theta");
targetx = (Double) jsonObject.get("targetx");
targety = (Double) jsonObject.get("targety");
}catch (ParseException e) {
e.printStackTrace();
}
}
//client disconnected, so close socket
conn.close();
}
catch (IOException e)
{
System.out.println("IOException on socket : " + e);
e.printStackTrace();
}
}
catch(IOException e)
{
System.err.println("IOException");
}
//5. close the connections and stream
try
{
s.close();
}
catch(IOException ioException)
{
System.err.println("Unable to close. IOexception");
}
}
public static void echo(String msg)
{
System.out.println(msg);
}
}