Revised LoginCredentialProcessor and worked on MessageProcessor

This commit is contained in:
DieGurke 2020-01-25 18:41:44 +01:00
parent 7c7a050c7c
commit 0715e69cd7
3 changed files with 29 additions and 25 deletions

View File

@ -60,7 +60,7 @@ public class ConnectionManager implements ISocketIdListener {
*/
public void registerUser(long userId, long socketId) {
sockets.put(userId, socketId);
pendingSockets.remove(userId);
pendingSockets.remove(socketId);
}
/**

View File

@ -1,6 +1,7 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@ -37,24 +38,14 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
public void process(LoginCredentials input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId));
// Create user
envoy.server.data.User user = new envoy.server.data.User();
envoy.server.data.User user = getUser(input);
if (input.isRegistration()) {
user.setName(input.getName());
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(input.getPasswordHash());
persistenceManager.addUser(user);
}else {
// TODO: Implement error when user does not exist
if (input.getPasswordHash() == user.getPasswordHash()) {
user = persistenceManager.getUserByName(input.getName());
} else {
// TODO: Wrong Password Response
}
// Not logged in successfully
if (user == null) {
return;
}
ConnectionManager.getInstance().registerUser(socketId, user.getId());
ConnectionManager.getInstance().registerUser(user.getId(), socketId);
// Create contacts
List<User> users = PersistenceManager.getPersistenceManager()
@ -79,4 +70,25 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
PersistenceManager.getPersistenceManager().updateMessage(msg);
}
}
private envoy.server.data.User getUser(LoginCredentials credentials) {
envoy.server.data.User user;
if (credentials.isRegistration()) {
user = new envoy.server.data.User();
user.setName(credentials.getName());
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
persistenceManager.addUser(user);
} else {
user = persistenceManager.getUserByName(credentials.getName());
// TODO: Implement error when user does not exist
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
// TODO: Wrong Password Response
return null;
}
}
return user;
}
}

View File

@ -1,10 +1,8 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.Date;
import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent;
import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor;
import envoy.server.database.PersistenceManager;
@ -36,12 +34,6 @@ public class MessageProcessor implements ObjectProcessor<Message> {
System.err.println("Recipient online. Failed to send message" + message.getId());
e.printStackTrace();
}
try {// sender receives confirmation that the server received the message
writeProxy.write(connectionManager.getSocketId(message.getSenderId()),
new MessageStatusChangeEvent(message.getId(), message.getStatus(), new Date()));
} catch (IOException e) {
e.printStackTrace();
}
PersistenceManager.getPersistenceManager().addMessage(new envoy.server.data.Message(message));
}
}