diff --git a/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_12_0.xml b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_12_0.xml new file mode 100644 index 0000000..28b8682 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_12_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/swt31-server.iml b/.idea/swt31-server.iml index c94af3e..b74f8bf 100644 --- a/.idea/swt31-server.iml +++ b/.idea/swt31-server.iml @@ -13,6 +13,7 @@ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 075e020..eae2873 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,14 @@ 8.0.13 + + + org.apache.commons + commons-lang3 + 3.12.0 + + + org.projectlombok diff --git a/src/main/java/de/swt/Server.java b/src/main/java/de/swt/Server.java index 14fd508..ae1beee 100644 --- a/src/main/java/de/swt/Server.java +++ b/src/main/java/de/swt/Server.java @@ -7,7 +7,8 @@ import de.swt.logic.session.SessionManager; import de.swt.logic.user.UserManager; import de.swt.manager.ServerCommandManager; -import de.swt.manager.UserCommandMananger; +import de.swt.manager.UserCommandManager; +import de.swt.manager.UserCommandManager; import de.swt.manager.CommandObject; import de.swt.rmi.InitRMIServer; import de.swt.util.SGCheck; @@ -16,6 +17,7 @@ import lombok.Getter; import lombok.Setter; +import java.sql.SQLException; import java.util.HashMap; import java.util.Timer; @@ -26,7 +28,7 @@ public class Server { private static Server server; private DBManager dbManager; private AsyncMySQL mySQL; - private UserCommandMananger userCommandMananger; + private UserCommandManager userCommandManager; private ServerCommandManager serverCommandManager; private CourseManager courseManager; private UserManager userManager; @@ -41,7 +43,7 @@ public void onEnable() { server = this; - userCommandMananger = new UserCommandMananger(); + userCommandManager = new UserCommandManager(); serverCommandManager = new ServerCommandManager(); dbManager = new DBManager(); @@ -55,10 +57,21 @@ public void onEnable() { InitRMIServer initRMIServer = new InitRMIServer(); initRMIServer.initRMIServer(); + try { + courseManager.cacheAllData(); + userManager.cacheAllData(); + groupManager.cacheAllData(); + sessionManager.cacheAllData(); + } catch (SQLException exception) { + exception.printStackTrace(); + } + + /* new Thread(() -> { Timer syncTimer = new Timer(); - syncTimer.schedule(new Synchronizer(), 0, 1000); + syncTimer.schedule(new Synchronizer(), 0, 10); }).start(); + */ new Thread(()->{ Timer timeCheckTimer = new Timer(); diff --git a/src/main/java/de/swt/database/DBManager.java b/src/main/java/de/swt/database/DBManager.java index 70802d8..8387361 100644 --- a/src/main/java/de/swt/database/DBManager.java +++ b/src/main/java/de/swt/database/DBManager.java @@ -1,9 +1,11 @@ package de.swt.database; +import de.swt.Server; import de.swt.logic.course.Course; import de.swt.logic.group.Group; import de.swt.logic.session.Session; import de.swt.logic.user.User; +import de.swt.manager.CommandObject; import de.swt.util.AccountType; import java.sql.ResultSet; @@ -14,6 +16,11 @@ public class DBManager { private AsyncMySQL mySQL; + private final Server server; + + public DBManager() { + server = Server.getInstance(); + } private void connectToDB() { @@ -121,6 +128,7 @@ private void resetServer() { mySQL.update("DELETE FROM groupInSession;"); mySQL.update("DELETE FROM sessions;"); mySQL.update("DELETE FROM groups;"); + mySQL.update("UPDATE users SET active = false"); } public int updateUser(User user) { @@ -195,9 +203,11 @@ public int updateGroup(Group group) { } else { mySQL.update("INSERT INTO groups (timeTillTermination, maxGroupSize) VALUES (" + timeTillTermination + ", " + maxGroupSize + ");"); groupId = addID(); + mySQL.update("INSERT INTO groupInSession (sessionId, groupId) VALUES (" + group.getSessionId() + ", " + groupId + ");"); } - for (int userId : participants) + for (int userId : participants) { mySQL.update("INSERT INTO userInGroup (userId, groupId) VALUES (" + userId + ", " + groupId + ");"); + } } catch (SQLException e) { e.printStackTrace(); } @@ -205,19 +215,21 @@ public int updateGroup(Group group) { } public void deleteGroup(int groupId) { + mySQL.update("DELETE FROM userInGroup WHERE groupId = " + groupId + ";"); + mySQL.update("DELETE FROM groupInSession WHERE groupId = " + groupId + ";"); mySQL.update("DELETE FROM groups WHERE groupId = " + groupId + ";"); - mySQL.update("DELETE FROM userInGroups WHERE groupId = " + groupId + ";"); } public void deleteSession(int sessionId) { - mySQL.update("DELETE FROM sessions WHERE sessionId = " + sessionId + ";"); mySQL.update("DELETE FROM userInSession WHERE sessionId = " + sessionId + ";"); + mySQL.update("DELETE FROM masterInSession WHERE sessionId = " + sessionId + ";"); + mySQL.update("DELETE FROM sessions WHERE sessionId = " + sessionId + ";"); } public void deleteCourse(int courseId) { - mySQL.update("DELETE FROM courses WHERE courseId = " + courseId + ";"); mySQL.update("DELETE FROM userInCourse WHERE courseId = " + courseId + ";"); mySQL.update("DELETE FROM dateInCourse WHERE courseId = " + courseId + ";"); + mySQL.update("DELETE FROM courses WHERE courseId = " + courseId + ";"); } public int updateSession(Session session) { @@ -235,7 +247,6 @@ public int updateSession(Session session) { mySQL.update("UPDATE sessions SET remainingTime = " + remainingTime + " WHERE sessionId = " + sessionId + ";"); mySQL.update("DELETE FROM userInSession WHERE sessionId = " + sessionId + ";"); mySQL.update("DELETE FROM masterInSession WHERE sessionId = " + sessionId + ";"); - mySQL.update("DELETE FROM groupInSession WHERE sessionId = " + sessionId + ";"); } else { mySQL.update("INSERT INTO sessions (remainingTime) VALUES (" + remainingTime + ");"); sessionId = addID(); @@ -244,8 +255,6 @@ public int updateSession(Session session) { mySQL.update("INSERT INTO userInSession (sessionId, userId) VALUES (" + sessionId + ", " + participant + ");"); for (int master : masters) mySQL.update("INSERT INTO masterInSession (sessionId, userId) VALUES (" + sessionId + ", " + master + ");"); - for (int group : groups) - mySQL.update("INSERT INTO groupInSession (sessionId, groupId) VALUES (" + sessionId + ", " + group + ");"); } catch (SQLException throwables) { throwables.printStackTrace(); } diff --git a/src/main/java/de/swt/logic/group/GroupManager.java b/src/main/java/de/swt/logic/group/GroupManager.java index 5c914f0..fd7ba17 100644 --- a/src/main/java/de/swt/logic/group/GroupManager.java +++ b/src/main/java/de/swt/logic/group/GroupManager.java @@ -25,8 +25,9 @@ public Group load(int id) throws SQLException { resultSet = getMySQL().query("SELECT userId FROM userInGroup WHERE groupId = " + id + ";"); newGroup.setUserIds(getIds(resultSet, "userId")); resultSet = getMySQL().query("SELECT sessionId FROM groupInSession WHERE groupId = " + id + ";"); - resultSet.next(); - newGroup.setSessionId(resultSet.getInt("sessionId")); + if (resultSet.next()) { + newGroup.setSessionId(resultSet.getInt("sessionId")); + } getHashMap().put(id, newGroup); diff --git a/src/main/java/de/swt/logic/user/UserManager.java b/src/main/java/de/swt/logic/user/UserManager.java index f4eddc7..82e67c3 100644 --- a/src/main/java/de/swt/logic/user/UserManager.java +++ b/src/main/java/de/swt/logic/user/UserManager.java @@ -3,9 +3,13 @@ import de.swt.Server; import de.swt.manager.Manager; import de.swt.util.AccountType; +import org.apache.commons.lang3.SerializationUtils; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.LinkedBlockingQueue; public class UserManager extends Manager { public UserManager(Server server) { @@ -34,10 +38,18 @@ public User load(int id) throws SQLException { @Override public void cacheAllData() throws SQLException { + ArrayList knownUsers = new ArrayList<>(getHashMap().values()); + ArrayList newUsers = new ArrayList<>(); getHashMap().clear(); ResultSet resultSet = getMySQL().query("SELECT userId FROM users;"); while (resultSet.next()) { - load(resultSet.getInt("userId")); + User newUser = load(resultSet.getInt("userId")); + if (!knownUsers.contains(newUser)){ + newUsers.add(newUser); + }; + } + for (User user: newUsers){ + getServer().getUserCommandManager().getUserCommandQueue().put(user.getUserId(),new LinkedBlockingQueue<>()); } } } diff --git a/src/main/java/de/swt/manager/Manager.java b/src/main/java/de/swt/manager/Manager.java index 7d28946..de061b0 100644 --- a/src/main/java/de/swt/manager/Manager.java +++ b/src/main/java/de/swt/manager/Manager.java @@ -26,6 +26,19 @@ public Manager(Server server) { public abstract void cacheAllData() throws SQLException; + public void cacheSingleData(int dataId){ + getHashMap().remove(dataId); + try { + load(dataId); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + + public void deleteSingleData(int dataId){ + getHashMap().remove(dataId); + } + public ArrayList getIds(ResultSet resultSet, String columnName) throws SQLException { ArrayList ids = new ArrayList<>(); while (resultSet.next()) { diff --git a/src/main/java/de/swt/manager/UserCommandMananger.java b/src/main/java/de/swt/manager/UserCommandManager.java similarity index 80% rename from src/main/java/de/swt/manager/UserCommandMananger.java rename to src/main/java/de/swt/manager/UserCommandManager.java index fe8abcf..b42084c 100644 --- a/src/main/java/de/swt/manager/UserCommandMananger.java +++ b/src/main/java/de/swt/manager/UserCommandManager.java @@ -6,11 +6,11 @@ import java.util.concurrent.LinkedBlockingQueue; @Getter -public class UserCommandMananger { +public class UserCommandManager { private final HashMap> userCommandQueue; - public UserCommandMananger() { + public UserCommandManager() { this.userCommandQueue = new HashMap<>(); } diff --git a/src/main/java/de/swt/rmi/RMIServer.java b/src/main/java/de/swt/rmi/RMIServer.java index 65e37de..2afbaca 100644 --- a/src/main/java/de/swt/rmi/RMIServer.java +++ b/src/main/java/de/swt/rmi/RMIServer.java @@ -1,22 +1,17 @@ package de.swt.rmi; import de.swt.Server; -import de.swt.database.DBManager; import de.swt.logic.course.Course; -import de.swt.logic.course.CourseManager; import de.swt.logic.group.Group; -import de.swt.logic.group.GroupManager; import de.swt.logic.session.Session; -import de.swt.logic.session.SessionManager; import de.swt.logic.user.User; -import de.swt.logic.user.UserManager; import de.swt.manager.ServerCommandManager; -import de.swt.manager.UserCommandMananger; +import de.swt.manager.UserCommandManager; import de.swt.manager.CommandObject; +import org.apache.commons.lang3.SerializationUtils; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; -import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.LinkedBlockingQueue; @@ -24,25 +19,46 @@ public class RMIServer extends UnicastRemoteObject implements RMIServerInterface public int port; private final ServerCommandManager serverCommandManager; - private final UserCommandMananger userCommandMananger; + private final UserCommandManager userCommandMananger; + private final Server server; public RMIServer() throws RemoteException { this.port = 1099; this.serverCommandManager = Server.getInstance().getServerCommandManager(); - this.userCommandMananger = Server.getInstance().getUserCommandMananger(); + this.userCommandMananger = Server.getInstance().getUserCommandManager(); + this.server = Server.getInstance(); } @Override - public LinkedBlockingQueue accessCommandQueue(int userid) throws RemoteException { + public LinkedBlockingQueue accessCommandQueue(int userId) throws RemoteException { HashMap> userCommandQueue = userCommandMananger.getUserCommandQueue(); - LinkedBlockingQueue userCommands = userCommandQueue.get(userid); + LinkedBlockingQueue userCommands = new LinkedBlockingQueue<>(userCommandQueue.get(userId)); // reset command list - userCommandQueue.remove(userid); - userCommandQueue.put(userid, new LinkedBlockingQueue<>()); + userCommandQueue.get(userId).clear(); return userCommands; } + @Override + public HashMap getCourses() throws RemoteException { + return server.getCourseManager().getHashMap(); + } + + @Override + public HashMap getUsers() throws RemoteException { + return server.getUserManager().getHashMap(); + } + + @Override + public HashMap getGroups() throws RemoteException { + return server.getGroupManager().getHashMap(); + } + + @Override + public HashMap getSessions() throws RemoteException { + return server.getSessionManager().getHashMap(); + } + @Override public void updateUser(User user) throws RemoteException { CommandObject serverCommand = new CommandObject(); @@ -115,7 +131,7 @@ public void deleteSession(int sessionId) { } @Override - public void deleteCourse(int groupId){ + public void deleteCourse(int groupId) { CommandObject serverCommand = new CommandObject(); serverCommand.setCommand("DG:" + groupId); serverCommandManager.getServerCommandQueue().add(serverCommand); diff --git a/src/main/java/de/swt/rmi/RMIServerInterface.java b/src/main/java/de/swt/rmi/RMIServerInterface.java index ea52708..fb9160d 100644 --- a/src/main/java/de/swt/rmi/RMIServerInterface.java +++ b/src/main/java/de/swt/rmi/RMIServerInterface.java @@ -9,11 +9,20 @@ import java.rmi.Remote; import java.rmi.RemoteException; import java.util.ArrayList; +import java.util.HashMap; import java.util.concurrent.LinkedBlockingQueue; public interface RMIServerInterface extends Remote { - LinkedBlockingQueue accessCommandQueue(int userid) throws RemoteException; + LinkedBlockingQueue accessCommandQueue(int userId) throws RemoteException; + + HashMap getCourses() throws RemoteException; + + HashMap getUsers() throws RemoteException; + + HashMap getGroups() throws RemoteException; + + HashMap getSessions() throws RemoteException; void updateUser(User user) throws RemoteException; diff --git a/src/main/java/de/swt/util/SGCheck.java b/src/main/java/de/swt/util/SGCheck.java index 519b0af..0cf820b 100644 --- a/src/main/java/de/swt/util/SGCheck.java +++ b/src/main/java/de/swt/util/SGCheck.java @@ -8,9 +8,11 @@ import de.swt.logic.session.SessionManager; import de.swt.logic.user.User; import de.swt.logic.user.UserManager; -import de.swt.manager.UserCommandMananger; +import de.swt.manager.UserCommandManager; import de.swt.manager.CommandObject; +import org.apache.commons.lang3.SerializationUtils; +import java.util.ArrayList; import java.util.TimerTask; public class SGCheck extends TimerTask { @@ -22,17 +24,27 @@ public void run() { GroupManager groupManager = Server.getInstance().getGroupManager(); DBManager dbManager = Server.getInstance().getDbManager(); - for (Session session : sessionManager.getHashMap().values()) { + ArrayList sessionsToDelete = new ArrayList<>(); + ArrayList sessionsCopy = SerializationUtils.clone(new ArrayList<>(sessionManager.getHashMap().values())); + for (Session session : sessionsCopy) { if (session.getRemainingTime() <= System.currentTimeMillis()) { - dbManager.deleteSession(session.getSessionId()); + sessionsToDelete.add(session); } } + for (Session session : sessionsToDelete){ + dbManager.deleteSession(session.getSessionId()); + } - for (Group group : groupManager.getHashMap().values()) { + ArrayList groupsToDelete = new ArrayList<>(); + ArrayList groupsCopy = SerializationUtils.clone(new ArrayList<>(groupManager.getHashMap().values())); + for (Group group : groupsCopy) { if (group.getTimeTillTermination() <= System.currentTimeMillis()) { - dbManager.deleteGroup(group.getGroupId()); + groupsToDelete.add(group);dbManager.deleteGroup(group.getGroupId()); } } + for (Group group: groupsToDelete){ + dbManager.deleteGroup(group.getGroupId()); + } } } diff --git a/src/main/java/de/swt/util/ServerCommandWorker.java b/src/main/java/de/swt/util/ServerCommandWorker.java index 6c5e346..e24fc4e 100644 --- a/src/main/java/de/swt/util/ServerCommandWorker.java +++ b/src/main/java/de/swt/util/ServerCommandWorker.java @@ -11,12 +11,11 @@ import de.swt.logic.user.User; import de.swt.logic.user.UserManager; import de.swt.manager.CommandObject; -import de.swt.manager.UserCommandMananger; +import de.swt.manager.UserCommandManager; import lombok.Getter; import lombok.Setter; import java.sql.SQLException; -import java.util.ArrayList; import java.util.HashMap; import java.util.TimerTask; import java.util.concurrent.LinkedBlockingQueue; @@ -31,7 +30,7 @@ public class ServerCommandWorker extends TimerTask { private final GroupManager groupManager; private final SessionManager sessionManager; private final DBManager dbManager; - private final UserCommandMananger userCommandMananger; + private final UserCommandManager userCommandMananger; private LinkedBlockingQueue serverCommandQueue; @@ -43,13 +42,12 @@ public ServerCommandWorker(LinkedBlockingQueue serverCommandQueue this.groupManager = server.getGroupManager(); this.sessionManager = server.getSessionManager(); this.dbManager = server.getDbManager(); - this.userCommandMananger = server.getUserCommandMananger(); + this.userCommandMananger = server.getUserCommandManager(); } @Override public void run() { - while (serverCommandQueue.peek() != null){ - System.out.println(serverCommandQueue); + while (serverCommandQueue.peek() != null) { evaluateCommand(serverCommandQueue.poll()); } } @@ -70,16 +68,16 @@ private void evaluateCommand(CommandObject command) { switch (keyArgs[0]) { case "UU" -> { User user = (User) updatedObject; - dbManager.updateUser(user); + userManager.cacheSingleData(dbManager.updateUser(user)); } case "CU" -> { Course course = (Course) updatedObject; - dbManager.updateCourse(course); + courseManager.cacheSingleData(dbManager.updateCourse(course)); } case "SU" -> { Session session = (Session) updatedObject; if (session.getMasterIds().size() > 0) { - dbManager.updateSession(session); + sessionManager.cacheSingleData(dbManager.updateSession(session)); } else { CommandObject serverCommand = new CommandObject(); serverCommand.setCommand("DS:" + session.getSessionId()); @@ -88,11 +86,9 @@ private void evaluateCommand(CommandObject command) { } case "GU" -> { Group group = (Group) updatedObject; - int groupId = dbManager.updateGroup(group); + groupManager.cacheSingleData(dbManager.updateGroup(group)); try { - Session session = server.getSessionManager().load(group.getSessionId()); - session.getGroupIds().add(groupId); - dbManager.updateSession(session); + sessionManager.cacheSingleData(dbManager.updateSession(sessionManager.load(group.getSessionId()))); } catch (SQLException exception) { exception.printStackTrace(); } @@ -108,7 +104,11 @@ private void evaluateCommand(CommandObject command) { System.out.println("[" + originId + "]: workspace update group ping."); commandObject.setCommand("FU:"); commandObject.setWorkspaceFileBytes(workspaceBytes); - userCommandQueue.get(ids).add(commandObject); + try { + userCommandQueue.get(ids).put(commandObject); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } return; @@ -122,7 +122,11 @@ private void evaluateCommand(CommandObject command) { System.out.println("[" + originId + "]: workspace update session ping."); commandObject.setCommand("FU:"); commandObject.setWorkspaceFileBytes(workspaceBytes); - userCommandQueue.get(ids).add(commandObject); + try { + userCommandQueue.get(ids).put(commandObject); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } } @@ -148,23 +152,35 @@ private void evaluateCommand(CommandObject command) { if (userManager.getHashMap().containsKey(destinationId)) { System.out.println("[" + originId + ", " + destinationId + "]: teacher to user accept ping."); userCommand.setCommand("AN:" + answer); - userCommandQueue.get(originId).add(userCommand); + userCommand.setOriginId(originId); + userCommandQueue.get(destinationId).add(userCommand); } } case "DG" -> { int groupId = Integer.parseInt(args[0]); dbManager.deleteGroup(groupId); + groupManager.deleteSingleData(groupId); } case "DS" -> { int sessionId = Integer.parseInt(args[0]); - + try { + Session session = sessionManager.load(sessionId); + for (int groupId : session.getGroupIds()) { + dbManager.deleteGroup(groupId); + groupManager.deleteSingleData(groupId); + } + } catch (SQLException exception) { + exception.printStackTrace(); + } dbManager.deleteSession(sessionId); + sessionManager.deleteSingleData(sessionId); } case "DC" -> { int courseId = Integer.parseInt(args[0]); dbManager.deleteCourse(courseId); + courseManager.deleteSingleData(courseId); } case "ST" -> { HashMap> userCommandQueue = userCommandMananger.getUserCommandQueue(); @@ -174,7 +190,7 @@ private void evaluateCommand(CommandObject command) { if (userId == originId) { continue; } - System.out.println("[" + originId + "]: sending task ping."); + System.out.println("[" + originId + ", " + userId + "]: sending task ping."); userCommand.setCommand("ST:"); userCommand.setWorkspaceFileBytes(workspaceBytes); userCommand.setTaskBytes(taskBytes);