-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
82 lines (74 loc) · 2.48 KB
/
Server.java
File metadata and controls
82 lines (74 loc) · 2.48 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
import java.awt.Point;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import java.net.*;
public class Server implements Runnable {
private Blackboard blackboard;
private Timer timer;
private Socket socket;
private ObjectOutputStream objectOutputStream;
private boolean isSendingData = false;
public Server(Blackboard blackboard) {
this.blackboard = blackboard;
try {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server is waiting for client connection...");
socket = serverSocket.accept();
System.out.println("Client connected.");
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
System.out.println("Server is running...");
}
public void startTransmission() {
if (!isSendingData) {
System.out.println("Starting data transmission...");
isSendingData = true;
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
sendClickData();
}
}, 0, 1000 ); // removed division by transmission speed
}
}
public void stopTransmission() {
if (isSendingData && timer != null) {
System.out.println("Stopping data transmission...");
timer.cancel();
isSendingData = false;
}
}
private void sendClickData() {
List<Point> clicks = blackboard.getClickPositions();
if (!clicks.isEmpty()) {
try {
objectOutputStream.writeObject(clicks);
objectOutputStream.flush();
blackboard.clearClicks(); // Clear clicks after sending
System.out.println("Sent clicks to client.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stopServer() {
stopTransmission();
try {
if (socket != null) {
objectOutputStream.close();
socket.close();
}
System.out.println("Server stopped.");
} catch (IOException e) {
e.printStackTrace();
}
}
}