-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
99 lines (84 loc) · 3.84 KB
/
Server.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Server {
private static ServerSocket serverSocket;
public Server(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public void start() throws IOException {
try {
while (!serverSocket.isClosed()) {
System.out.println("Waiting for a client to connect");
Socket clientSocket = serverSocket.accept();
System.out.println("A new client has connected");
ClientHandler clientHandler = new ClientHandler(clientSocket);
Thread clientThread = new Thread(clientHandler);
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static Queue<String> messageQueue = new ConcurrentLinkedQueue<String>();
private static Queue<String> responseQueue = new ConcurrentLinkedQueue<String>();
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(6013);
Server server = new Server(serverSocket);
server.start();
while (true) {
Socket socket = new Socket("localhost", 6013);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String input = in.readLine(); // Read the input from the client
//add clients input to queue
messageQueue.add(input);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Call the OpenAIApiCaller method with the input parameter
out.println("Message received by server.");
String response = null;
try {
response = OpenAIApiCaller.callOpenAIApi(input);
} catch (URISyntaxException | InterruptedException e) {
throw new RuntimeException(e);
}
double score = ClaimBusterAPI.getClaimBusterScore(response);
String score_string = "";
String scoreString = (score >= 0.4) ? "%This response may not need to be fact checked." : "%This response may need to be fact checked.";
String formattedScore = String.format("%.2f", score);
String formattedResponse = response + "%ClaimBuster score: " + formattedScore + " " + scoreString + "%";
responseQueue.add(formattedResponse);
String format = String.format("%.2f", score);
format = "%ClaimBuster score: " + format + " " + score_string + "%";
String new_response = response + format;
out.println(new_response); // Send the response back to the client
for (String message : messageQueue) {
if (!message.equals(formattedResponse)) {
out.println("Received message: " + message);
}
}
for (String responseMessage : responseQueue) {
out.println("Received response: " + responseMessage);
}
close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}