-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServerThread.java
More file actions
84 lines (74 loc) · 2.71 KB
/
ServerThread.java
File metadata and controls
84 lines (74 loc) · 2.71 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
/**
* This is class receives the Message and updates the DB via the inctance DPUpdate .
* It will communicate with the agents and update the DB.
* @author Michael Ekdal
* @version 2021-05-11
*/
package Jvakt;
/*
* 2024-06-05 V.56 Michael Ekdal Added time stamp in the cartch statements println.
* 2023-02-27 V.55 Michael Ekdal Added client.close() i ServerThread to the end of code trying avoid CLOSE_WAIT.
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.*;
import java.net.*;
import java.util.Date;
class ServerThread extends Thread {
/**
* The class is started in a new thread by the Server class.
* It receives the Message object via the session and sends it to DBupdate to update the DB.
*/
Socket client;
DBupdate dt;
String version = "ServerThread 2.4.56";
boolean swData;
String line;
Message jm = new Message();
ServerThread(Socket client, DBupdate dt) { this.client = client; this.dt = dt; }
public void run() {
try {
swData = false;
client.setSoTimeout(15000);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter ut = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
// dt.getStatus();
ut.println(dt.getStatus() + " " +version );
ut.flush();
// String line;
// Message jm = new Message();
while((line = in.readLine()) != null ) {
// System.out.println("ServerThread #1: "+line +" " + client.getInetAddress() );
swData = false;
if (line.length() == 0) break;
if (line.startsWith("SendMsg")) continue ;
String[] tab = line.split("<;>");
// System.out.println("ServerThread #2: " + tab.length+" "+line +" " + client.getInetAddress() );
if (tab.length < 6) break;
swData = true;
jm.setType(tab[0]);
jm.setId(tab[1]);
jm.setRptsts(tab[2]);
jm.setBody(tab[3]);
jm.setAgent(tab[4]);
jm.setPrio(Integer.parseInt(tab[5]));
ut.println("okay " );
ut.flush();
break;
}
in.close();
ut.close();
// client.close();
// System.out.println("ServerThread #2: " + client.getInetAddress() + " " + jm.getType() + " " + jm.getId() + " " +jm.getRptsts() + " " + jm.getBody() + " " +jm.getAgent() + " " +jm.getPrio());
}
catch (IOException e1) {
System.out.println(new Date()+" ServerThread IOexception:>> " + client.getInetAddress() + " " + e1 );
}
if (swData) dt.dbWrite(jm); // update DB
try {
client.close();
}
catch (Exception e2) {
System.out.println(new Date()+" ServerThread close exception:>> " + client.getInetAddress() + " " + e2 );
}
}
}