-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_log.java
111 lines (91 loc) · 3.74 KB
/
server_log.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
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
104
105
106
107
108
109
110
111
import java.io.*;
import java.net.*;
import java.util.logging.Logger;
public class server_log {
public static void main(String[] args) {
try {
final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);
ServerSocket serverSocket = new ServerSocket(6013);
while (true) {
Socket clientSocket = serverSocket.accept();
// Start a new thread for each client connection
Thread clientThread = new ClientThread(clientSocket, output);
Thread secondThread = new secondThread(clientSocket, input);
clientThread.start();
secondThread.start();
System.out.println("Client connected");
System.out.println(serverSocket.getInetAddress());
}
} catch (IOException ioe) {
System.err.println(ioe);
}
}
static class ClientThread extends Thread{
private Socket clientSocket;
private PipedOutputStream output;
public ClientThread(Socket socket, PipedOutputStream output) {
this.clientSocket = socket;
this.output = output;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input = in.readLine(); // Read the input from the client
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
// Call the OpenAIApiCaller method with the input parameter
String response = null;
try {
response = OpenAIApiCaller.callOpenAIApi(input);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
double score = ClaimBusterAPI.getClaimBusterScore(response);
response = response + " | ClaimBuster score: " + score;
// Send to second thread here
try {
output.write(response.getBytes());
} catch (IOException e) {
}
out.println(response); // Send the response back to the client
clientSocket.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
static class secondThread extends Thread{
private Socket clientSocket;
private PipedInputStream input;
public secondThread(Socket socket, PipedInputStream input) {
this.clientSocket = socket;
this.input = input;
}
@Override
public void run() {
final Logger logger = Logger.getLogger(String.valueOf(server_log.class));
StringBuilder modifiedData = null;
try {
int data = input.read();
modifiedData = new StringBuilder();
while (data != -1) {
char character = (char) data;
modifiedData.append(character);
data = input.read();
}
} catch (IOException e) {
}
try (FileWriter f = new FileWriter("log.txt", true);
BufferedWriter b = new BufferedWriter(f);
PrintWriter p = new PrintWriter(b);) {
p.println(modifiedData.toString());
p.println("---------------------------");
} catch (IOException i) {
i.printStackTrace();
}
}
}
}