Skip to content
Snippets Groups Projects
Commit a545ce6a authored by Rafi's avatar Rafi
Browse files

Fixed bugs with that caused in consistencies with message sending between...

Fixed bugs with that caused in consistencies with message sending between client and server. All employee data should be updated to all clients when employee data is modified in the database. The client should have an updated copy of employee information.
parent 7b169e04
No related branches found
No related tags found
No related merge requests found
......@@ -23,4 +23,14 @@ public class Model {
return "Successfully connected to database.";
}
public static void main(String[] args) {
Model model = new Model();
/* Testing stuff that should be cleaned up for the alpha
String s = model.staff.getEmployees();
System.out.println();
*/
}
}
......@@ -2,12 +2,16 @@ package com.example.schedulerapp;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
public class ReceiveThread {
public class ReceiveThread extends Thread {
private BufferedReader reader;
private Socket socket;
private ScheduleClient client;
public ReceiveThread(Socket socket) {
public ReceiveThread(Socket socket, ScheduleClient client) {
this.client = client;
this.socket = socket;
try {
......@@ -22,8 +26,18 @@ public class ReceiveThread {
public void run() {
while(true) {
try {
//System.out.println("ReceiveThread.Run.Hello"); why does this run twice.
String response = reader.readLine();
// Split response by delimiter '/' then find out what it means.
String[] args = response.split("/");
//System.out.println(args[0]); testing
switch (args[0]) {
case "allEmployees":
this.client.employees = new ArrayList<String>(Arrays.asList(args));
break;
case "Shifts":
this.client.shifts = new ArrayList<String>(Arrays.asList(args));
break;
}
} catch (IOException error) {
System.out.println("Error reading from server: " + error.getMessage());
......
......@@ -9,4 +9,8 @@ public class Schedule {
protected Schedule(Connection c) {
this.dbConnection = c;
}
protected String allShifts(){
return "All Shifts";
}
}
......@@ -2,13 +2,15 @@ package com.example.schedulerapp;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class ScheduleClient {
private String hostname;
private int port;
private String userName;
private Socket socket;
private PrintWriter writer;
protected ArrayList<String> shifts = new ArrayList<>();
protected ArrayList<String> employees = new ArrayList<>();
public ScheduleClient(String hostname, int port) {
this.hostname = hostname;
......@@ -17,7 +19,7 @@ public class ScheduleClient {
this.socket = new Socket (hostname, port);
System.out.println("Connected to the scheduling server");
//new ReceiveThread(socket, this).start();
new ReceiveThread(socket, this).start();
OutputStream output = this.socket.getOutputStream();
this.writer = new PrintWriter(output, true);
......@@ -29,6 +31,14 @@ public class ScheduleClient {
}
}
void getAllShifts(){
writer.println("allEmployees");
}
void getAllEmployees(){
writer.println("allShifts");
}
void addEmployee(String first_name, String last_name) {
writer.println("addEmployee/" + first_name + "/" + last_name);
}
......@@ -37,9 +47,42 @@ public class ScheduleClient {
writer.println("removeEmployee/" + first_name + "/" + last_name);
}
void printAllEmployees(){
System.out.println(this.employees);
}
String getEmployeeByID(int id) {
if (!this.employees.isEmpty()) {
for(String e: this.employees){
if(!e.equals("allEmployees")){
String[] eSplit = e.split("\\.");
if (eSplit[0].equals(id)){
return eSplit[1] + " " + eSplit[2];
}
}
}
return "No Employee with that ID";
}
return "No Employees";
}
void logOut() {
writer.println("logout");
}
public static void main(String[] args) {
ScheduleClient client = new ScheduleClient("localhost", 8989);
client.addEmployee("Rafi", "Zereselasie");
client.removeEmployee("Rafi", "Zereselasie");
/* Testing stuff that should be cleaned up for the alpha.
System.out.println("1");
client.printAllEmployees();
System.out.println("2");
System.out.println(client.getEmployeeByID(3));
System.out.println("3");
client.addEmployee("John", "Peter");
System.out.println("4");
System.out.println(client.getEmployeeByID(3));
client.printAllEmployees();
*/
}
}
......@@ -65,18 +65,19 @@ public class Staff {
Description: prints the id, and names of each employee
Return: none
*/
protected String printEmployees(){
protected String getEmployees(){
String response = "allEmployees";
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("select * from Employees");
while (myRs.next()) {
System.out.println("id: " + myRs.getString("employee_ID") + ", " + myRs.getString("last_name") + ", " + myRs.getString("first_name"));
response = response + "/" + myRs.getString("employee_ID") + "." + myRs.getString("first_name") + "." + myRs.getString("last_name");;
}
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
return "fail";
}
return "success";
return response;
}
}
......@@ -23,6 +23,7 @@ public class UserThread extends Thread {
public void run() {
try {
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
......@@ -32,6 +33,9 @@ public class UserThread extends Thread {
String serverMessage;
String clientMessage;
serverMessage = model.staff.getEmployees();
sendMessage(serverMessage);
do {
clientMessage = reader.readLine();
String[] args = clientMessage.split("/");
......@@ -39,15 +43,29 @@ public class UserThread extends Thread {
case "addEmployee":
if(args.length == 3){
model.staff.addEmployee(args[1], args[2]);
serverMessage = model.staff.getEmployees();
server.broadcast(serverMessage);
}
break;
case "removeEmployee":
try {
model.staff.removeEmployee(Integer.parseInt(args[1]));
serverMessage = model.staff.getEmployees();
server.broadcast(serverMessage);
} catch (NumberFormatException e) {
System.out.println("Client Message Error: " + clientMessage);
}
break;
case "allEmployees":
//Returns all Employees
serverMessage = model.staff.getEmployees();
sendMessage(serverMessage);
break;
case "allShifts":
serverMessage = model.schedule.allShifts();
sendMessage(serverMessage);
break;
}
} while (!clientMessage.equals("logout"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment