-
Rafi authored
Refactored how updates are handled, now when there is a change the client is told to update there local copy with the specific change, rather than resend either the whole list of employees information, or the whole list of shift information. This change to updates should make changes to the staff or schedule more efficient especially when there are multiple clients. In coming data from the server is now stored in HashMaps where the key is either the shift or employee IDs, and the value is of Shift or Employee classes. All the methods in this class were made to help with these 2 changes. Cleaned up warnings.
Rafi authoredRefactored how updates are handled, now when there is a change the client is told to update there local copy with the specific change, rather than resend either the whole list of employees information, or the whole list of shift information. This change to updates should make changes to the staff or schedule more efficient especially when there are multiple clients. In coming data from the server is now stored in HashMaps where the key is either the shift or employee IDs, and the value is of Shift or Employee classes. All the methods in this class were made to help with these 2 changes. Cleaned up warnings.
ReceiveThread.java 3.43 KiB
package com.example.schedulerapp;
import java.io.*;
import java.net.*;
public class ReceiveThread extends Thread {
private BufferedReader reader;
private final Model client;
public ReceiveThread(Socket socket, Model client) {
this.client = client;
try {
InputStream input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
} catch (IOException exception) {
System.out.println("Error getting input stream: " + exception.getMessage());
exception.printStackTrace();
}
}
public void run() {
while(true) {
try {
//System.out.println("ReceiveThread.Run.Hello"); why does this run twice.
String response = reader.readLine();
String[] args = response.split("/");
switch (args[0]) {
case "allEmployees" -> allEmployees(args);
case "allShifts" -> allShifts(args);
case "addEmployee" -> addEmployee(args[1]);
case "removeEmployee" -> removeEmployee(args[1]);
case "addShift" -> addShift(args[1]);
case "removeShift" -> removeShiftByIDL(args[1]);
case "editShift" -> editShiftL(args[1]); // fix
}
} catch (IOException exception) {
System.out.println("Dropped connection from server: " + exception.getMessage());
exception.printStackTrace();
break;
}
}
}
private void allEmployees(String[] allEmployees) {
for (String employee : allEmployees) {
if (!employee.equals("allEmployees")){
addEmployee(employee);
}
}
}
private void addEmployee(String employeeData) {
try {
String[] dataSplit = employeeData.split("\\.");
this.client.employees.put(Integer.parseInt(dataSplit[0]), new Employee(employeeData));
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void removeEmployee(String employeeID) {
try {
this.client.employees.remove(Integer.parseInt(employeeID));
} catch (Exception exception) {
exception.printStackTrace();
}
}