Clean-up, disabled SQL logging

This commit is contained in:
2020-02-12 07:10:33 +01:00
parent 66de88029f
commit c4b60dfab7
13 changed files with 143 additions and 44 deletions

View File

@ -1,12 +1,6 @@
package envoy.server;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import com.jenkov.nioserver.ISocketIdListener;
@ -53,7 +47,7 @@ public class ConnectionManager implements ISocketIdListener {
public void socketCancelled(long socketId) {
if (!pendingSockets.remove(socketId)) {
// notifying contacts of this users offline-going
envoy.server.data.User user = PersistenceManager.getPersistenceManager().getUserById(getUserIdBySocketId(socketId));
envoy.server.data.User user = PersistenceManager.getInstance().getUserById(getUserIdBySocketId(socketId));
user.setStatus(UserStatus.OFFLINE);
user.setLastSeen(new Date());
UserStatusChangeProcessor.updateUserStatus(user);
@ -105,9 +99,5 @@ public class ConnectionManager implements ISocketIdListener {
* @return the userId of all users who are currently online
* @since Envoy Server Standalone v0.1-alpha
*/
public List<Long> getOnlineUsers() {
List<Long> onlineUsers = new ArrayList<>();
sockets.forEach((userId, unimportant) -> onlineUsers.add(userId));
return onlineUsers;
}
public Set<Long> getOnlineUsers() { return sockets.keySet(); }
}

View File

@ -53,7 +53,7 @@ public class Startup {
}
private static void initializeCurrentMessageId() {
PersistenceManager persMan = PersistenceManager.getPersistenceManager();
PersistenceManager persMan = PersistenceManager.getInstance();
if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0"));
}
}

View File

@ -66,7 +66,7 @@ public class Message {
* @since Envoy Server Standalone v0.1-alpha
*/
public Message(envoy.data.Message message) {
PersistenceManager persMan = PersistenceManager.getPersistenceManager();
PersistenceManager persMan = PersistenceManager.getInstance();
id = message.getId();
status = message.getStatus();
text = message.getText();

View File

@ -49,7 +49,7 @@ public class PersistenceManager {
* @return the {@link PersistenceManager} singleton
* @since Envoy Server Standalone v0.1-alpha
*/
public static PersistenceManager getPersistenceManager() { return persistenceManager; }
public static PersistenceManager getInstance() { return persistenceManager; }
/**
* Adds a {@link User} to the database.

View File

@ -30,11 +30,11 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
final long contactId = evt.get().getId();
System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userId);
PersistenceManager.getPersistenceManager().addContact(userId, contactId);
PersistenceManager.getInstance().addContact(userId, contactId);
// Notify the contact if online
if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId),
new Contacts(Arrays.asList(PersistenceManager.getPersistenceManager().getUserById(userId).toCommonUser())));
new Contacts(Arrays.asList(PersistenceManager.getInstance().getUserById(userId).toCommonUser())));
break;
default:
System.err.printf("Received %s with an unsupported operation.%n", evt);

View File

@ -33,7 +33,7 @@ public class ContactsRequestEventProcessor implements ObjectProcessor<ContactSea
@Override
public void process(ContactSearchRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException {
writeProxy.write(socketId,
new ContactSearchResult(PersistenceManager.getPersistenceManager()
new ContactSearchResult(PersistenceManager.getInstance()
.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketId))
.stream()
.map(User::toCommonUser)

View File

@ -28,10 +28,10 @@ public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorR
public void process(IdGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
System.out.println("Received id generation request.");
ConfigItem currentId = PersistenceManager.getPersistenceManager().getConfigItemById("currentMessageId");
ConfigItem currentId = PersistenceManager.getInstance().getConfigItemById("currentMessageId");
IdGenerator generator = new IdGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + ID_RANGE));
PersistenceManager.getPersistenceManager().updateConfigItem(currentId);
PersistenceManager.getInstance().updateConfigItem(currentId);
System.out.println("Sending new id generator " + generator);
writeProxy.write(socketId, generator);

View File

@ -31,10 +31,8 @@ import envoy.server.net.ObjectWriteProxy;
*/
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager();
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override
public void process(LoginCredentials input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
@ -44,10 +42,13 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
envoy.server.data.User user = getUser(input, socketId, writeProxy);
// Not logged in successfully
if (user == null) return;
ConnectionManager.getInstance().registerUser(user.getId(), socketId);
if (user == null) {
System.out.println("Rejecting handshake on socket " + socketId);
return;
}
connectionManager.registerUser(user.getId(), socketId);
// notifies contacts of this users online-going and updates his status in the
// Notifies contacts of this users online-going and updates his status in the
// database
user.setStatus(UserStatus.ONLINE);
UserStatusChangeProcessor.updateUserStatus(user);
@ -62,42 +63,47 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
System.out.println("Sending contacts...");
writeProxy.write(socketId, contacts);
System.out.println("Acquiring pending messages for the client...");
List<Message> pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(user);
List<Message> pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user);
for (Message msg : pendingMessages) {
System.out.println("Sending message " + msg.toCommonMessage().toString());
System.out.println("Sending message " + msg.toCommonMessage());
writeProxy.write(socketId, msg.toCommonMessage());
msg.setReceivedDate(new Date());
msg.setStatus(MessageStatus.RECEIVED);
PersistenceManager.getPersistenceManager().updateMessage(msg);
PersistenceManager.getInstance().updateMessage(msg);
}
}
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
private envoy.server.data.User getUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
return credentials.isRegistration() ? newUser(credentials, socketId, writeProxy) : checkForExistingUser(credentials, socketId, writeProxy);
}
/**
* @param credentials the input to evaluate
* @param socketId the socket ID at which the client performing the handshake
* is connected
* @param writeProxy the {@link ObjectWriteProxy} to use if login was not
* successful
* @return the database user matching the login credentials
* @throws IOException if sending the failed login back to the client failed
* @since Envoy Server Standalone v0.1-alpha
*/
private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.User user;
ConnectionManager connectionManager = ConnectionManager.getInstance();
String userIdentifier = credentials.getIdentifier();
try {
// TODO will need to be replaced with the Identifier once implemented
user = persistenceManager.getUserByName(userIdentifier);
envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier());
// Checking if user is already online
if (connectionManager.isOnline(user.getId())) {
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
return null;
}
// Evaluating the correctness of the password hash
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash()))
throw new InputMismatchException("User " + credentials.getIdentifier() + "tried logging in using a wrong password");
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD));
return null;
}
return user;
} catch (NoResultException e) {
// Checking if user exists
@ -111,6 +117,10 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
/**
* @param credentials the credentials upon which to create the new {@link User}
* @param socketId the socketID at which the client performing the handshake
* is connected
* @param writeProxy the write proxy used to notify the client about handshake
* rejection
* @return the newly created {@link User}
* @throws IOException if sending the failed login back to the client failed
* @since Envoy Server Standalone v0.1-alpha
@ -118,7 +128,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
private envoy.server.data.User newUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
try {
// Checking that no user already has this identifier
PersistenceManager.getPersistenceManager().getUserByName(credentials.getIdentifier());
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
// this code only gets executed if this user already exists
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_EXISTS_ALREADY));
return null;
@ -130,6 +140,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new ArrayList<>());
persistenceManager.addUser(user);
return user;
}

View File

@ -42,6 +42,6 @@ public class MessageProcessor implements ObjectProcessor<Message> {
System.err.println("Recipient online. Failed to send message" + message.getId());
e.printStackTrace();
}
PersistenceManager.getPersistenceManager().addMessage(new envoy.server.data.Message(message));
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
}
}

View File

@ -32,7 +32,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStat
throw new IOException(e);
}
ConnectionManager conMan = ConnectionManager.getInstance();
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
PersistenceManager perMan = PersistenceManager.getInstance();
envoy.server.data.Message msg = perMan.getMessageById(input.getId());
msg.setStatus(input.get());

View File

@ -23,7 +23,7 @@ import envoy.server.net.ObjectWriteProxy;
public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChangeEvent> {
private static ObjectWriteProxy writeProxy;
private static PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager();
private static PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; }

View File

@ -20,8 +20,6 @@
value="org.hibernate.dialect.PostgreSQL95Dialect" /> <!-- DB Dialect -->
<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
<property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
<property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
</properties>
</persistence-unit>