Skip to content

Commit

Permalink
Reworked some more
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpassauer committed Jun 4, 2021
1 parent 1a08d63 commit 5e31285
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 54 deletions.
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_apache_commons_commons_lang3_3_12_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/swt31-server.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
<version>8.0.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/de/swt/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@
import lombok.Getter;
import lombok.Setter;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Timer;

Expand All @@ -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;
Expand All @@ -41,7 +43,7 @@ public void onEnable() {

server = this;

userCommandMananger = new UserCommandMananger();
userCommandManager = new UserCommandManager();
serverCommandManager = new ServerCommandManager();

dbManager = new DBManager();
Expand All @@ -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();
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/de/swt/database/DBManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +16,11 @@
public class DBManager {

private AsyncMySQL mySQL;
private final Server server;

public DBManager() {
server = Server.getInstance();
}

private void connectToDB() {

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -195,29 +203,33 @@ 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();
}
return groupId;
}

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) {
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/swt/logic/group/GroupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/de/swt/logic/user/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> {
public UserManager(Server server) {
Expand Down Expand Up @@ -34,10 +38,18 @@ public User load(int id) throws SQLException {

@Override
public void cacheAllData() throws SQLException {
ArrayList<User> knownUsers = new ArrayList<>(getHashMap().values());
ArrayList<User> 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<>());
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/de/swt/manager/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> getIds(ResultSet resultSet, String columnName) throws SQLException {
ArrayList<Integer> ids = new ArrayList<>();
while (resultSet.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import java.util.concurrent.LinkedBlockingQueue;

@Getter
public class UserCommandMananger {
public class UserCommandManager {

private final HashMap<Integer, LinkedBlockingQueue<CommandObject>> userCommandQueue;

public UserCommandMananger() {
public UserCommandManager() {
this.userCommandQueue = new HashMap<>();
}

Expand Down
44 changes: 30 additions & 14 deletions src/main/java/de/swt/rmi/RMIServer.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
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;

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<CommandObject> accessCommandQueue(int userid) throws RemoteException {
public LinkedBlockingQueue<CommandObject> accessCommandQueue(int userId) throws RemoteException {
HashMap<Integer, LinkedBlockingQueue<CommandObject>> userCommandQueue = userCommandMananger.getUserCommandQueue();
LinkedBlockingQueue<CommandObject> userCommands = userCommandQueue.get(userid);
LinkedBlockingQueue<CommandObject> 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<Integer, Course> getCourses() throws RemoteException {
return server.getCourseManager().getHashMap();
}

@Override
public HashMap<Integer, User> getUsers() throws RemoteException {
return server.getUserManager().getHashMap();
}

@Override
public HashMap<Integer, Group> getGroups() throws RemoteException {
return server.getGroupManager().getHashMap();
}

@Override
public HashMap<Integer, Session> getSessions() throws RemoteException {
return server.getSessionManager().getHashMap();
}

@Override
public void updateUser(User user) throws RemoteException {
CommandObject serverCommand = new CommandObject();
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/de/swt/rmi/RMIServerInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandObject> accessCommandQueue(int userid) throws RemoteException;
LinkedBlockingQueue<CommandObject> accessCommandQueue(int userId) throws RemoteException;

HashMap<Integer, Course> getCourses() throws RemoteException;

HashMap<Integer, User> getUsers() throws RemoteException;

HashMap<Integer, Group> getGroups() throws RemoteException;

HashMap<Integer, Session> getSessions() throws RemoteException;

void updateUser(User user) throws RemoteException;

Expand Down
Loading

0 comments on commit 5e31285

Please sign in to comment.