-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler.java
More file actions
95 lines (71 loc) · 3.09 KB
/
ClientHandler.java
File metadata and controls
95 lines (71 loc) · 3.09 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
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import org.json.JSONObject;
/*
This class handles each connection. It stops since my json returns null :( , but it can be persistent if you send back a simple string instead.
Uses gson for parsing.
*/
public class ClientHandler extends Thread {
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
final String validReq1 = "<AVL><vehicles>all</vehicles></AVL>";
final String validReq2 = "<AVL><vehicles>active</vehicles></AVL>";
List<Object> list;
public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos, List<Object> list) {
this.s = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run()
{
String received;
String sendme;
//String trythis = sendme.toString();
while (true)
{
try{
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(dis));
received = inFromClient.readLine();
System.out.println("Received(TCP): " + received);
DataOutputStream outToClient = new DataOutputStream(dos);
//TRY TO LOAD THE PAYLOAD WITH LIST DATA
Gson gson = new Gson();
String jsonPayload = gson.toJson(list);
if(received.equals(validReq1)||received.equals(validReq2)){
// sendme = received.toUpperCase() + '\n'; // ------> You can use this if you want to see a persistent connection
//outToClient.writeBytes(sendme);
System.out.println("Trying to send json.." +jsonPayload);
outToClient.writeBytes(jsonPayload);
/* switch(received){
case validReq1: //return JSON for all vehicles(V) break;
outToClient.writeBytes(list.toString());
break;
case validReq2: //return JSON for all vehicles flagged active(A) break;
outToClient.writeBytes(trythis);
break;
default:
System.out.println("FFFFUUUUU!!1! WHY GOD");
}
*/
}
else
System.out.println(received + "is not valid sry."); //discards invalid requests
}
//TOCHANGE
catch (IOException ex) {
Logger.getLogger(UDPHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}