From 01d110fe1cd2a10e2c946077d93f26773b581ac3 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 28 Jan 2020 19:25:30 +0100 Subject: [PATCH] Added IdGenerationRequestProcessor, fixed bugs in PersistenceManager * Removed get*ById queries * Removed src/test/resources from pom.xml --- .classpath | 6 ---- pom.xml | 3 +- src/main/java/envoy/server/Startup.java | 5 ++- src/main/java/envoy/server/data/Message.java | 14 ++------ src/main/java/envoy/server/data/User.java | 22 +++--------- .../server/database/PersistenceManager.java | 33 +++++++++-------- .../server/processors/EventProcessor.java | 3 +- .../IdGeneratorRequestProcessor.java | 36 +++++++++++++++++++ .../server/processors/MessageProcessor.java | 8 ++++- 9 files changed, 75 insertions(+), 55 deletions(-) create mode 100644 src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java diff --git a/.classpath b/.classpath index 37e1a73..fab7589 100644 --- a/.classpath +++ b/.classpath @@ -15,12 +15,6 @@ - - - - - - diff --git a/pom.xml b/pom.xml index 39582f6..3d271ff 100644 --- a/pom.xml +++ b/pom.xml @@ -28,8 +28,7 @@ com.github.informatik-ag-ngl envoy-common - e5c67b8 - + f~login_or_registration-SNAPSHOT com.github.informatik-ag-ngl diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index a31c094..31aa072 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -9,9 +9,7 @@ import com.jenkov.nioserver.Server; import envoy.server.database.PersistenceManager; import envoy.server.net.ObjectMessageProcessor; import envoy.server.net.ObjectMessageReader; -import envoy.server.processors.EventProcessor; -import envoy.server.processors.LoginCredentialProcessor; -import envoy.server.processors.MessageProcessor; +import envoy.server.processors.*; /** * Starts the server.
@@ -37,6 +35,7 @@ public class Startup { processors.add(new LoginCredentialProcessor()); processors.add(new MessageProcessor()); processors.add(new EventProcessor()); + processors.add(new IdGeneratorRequestProcessor()); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); // TODO: Prevent lazy DB initialization diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 83d7aeb..e5d1fce 100644 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -2,15 +2,7 @@ package envoy.server.data; import java.util.Date; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToOne; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import javax.persistence.*; import envoy.data.MessageBuilder; import envoy.server.database.PersistenceManager; @@ -34,7 +26,7 @@ import envoy.server.database.PersistenceManager; { @NamedQuery( query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT", name = "getUnreadMessages" - ), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "getMessageById") } + ) } ) public class Message { @@ -91,7 +83,7 @@ public class Message { */ public envoy.data.Message toCommonMessage() { // TODO: Attachment, dates - return new MessageBuilder(sender.getId(), recipient.getId()).setText(text).setDate(creationDate).setStatus(status).build(); + return new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text).setDate(creationDate).setStatus(status).build(); } /** diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 2f629dc..2e22518 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -3,18 +3,7 @@ package envoy.server.data; import java.util.Date; import java.util.List; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import javax.persistence.*; /** * This class serves as a way to let Hibernate communicate with the server @@ -32,8 +21,7 @@ import javax.persistence.TemporalType; @Entity @Table(name = "users") @NamedQueries( - { @NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById"), - @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), + { @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), @NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested } ) @@ -49,7 +37,7 @@ public class User { private Date lastSeen; private envoy.data.User.UserStatus status; - @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true) + @OneToMany(targetEntity = User.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) private List contacts; /** @@ -76,7 +64,7 @@ public class User { * @return a database {@link User} converted into an {@link envoy.data.User} * @since Envoy Server Standalone v0.1-alpha */ - public envoy.data.User toCommonUser() { return new envoy.data.User(this.id, this.name); } + public envoy.data.User toCommonUser() { return new envoy.data.User(id, name); } /** * @return the id of a {link envoy.data.User} @@ -98,7 +86,7 @@ public class User { public String getName() { return name; } /** - * @param name the username to set + * @param name the user name to set * @since Envoy Server Standalone v0.1-alpha * @see User#getName() */ diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index aa2d008..9330296 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -5,8 +5,6 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Persistence; -import org.hibernate.Session; - import envoy.server.data.Message; import envoy.server.data.User; @@ -46,7 +44,6 @@ public class PersistenceManager { public void addUser(User User) { entityManager.getTransaction().begin(); entityManager.persist(User); - entityManager.flush(); entityManager.getTransaction().commit(); } @@ -56,7 +53,11 @@ public class PersistenceManager { * @param message the {@link Message} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void addMessage(Message message) { entityManager.persist(message); } + public void addMessage(Message message) { + entityManager.getTransaction().begin(); + entityManager.persist(message); + entityManager.getTransaction().commit(); + } /** * Updates a {@link User} in the database @@ -64,7 +65,7 @@ public class PersistenceManager { * @param user the {@link User} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void updateUser(User user) { entityManager.unwrap(Session.class).merge(user); } + public void updateUser(User user) { entityManager.merge(user); } /** * Updates a {@link Message} in the database. @@ -72,30 +73,36 @@ public class PersistenceManager { * @param message the message to update * @since Envoy Server Standalone v0.1-alpha */ - public void updateMessage(Message message) { entityManager.unwrap(Session.class).merge(message); } + public void updateMessage(Message message) { entityManager.merge(message); } /** * Searches for a {@link User} with a specific id. * - * @param id - the id to search for + * @param id the id to search for * @return the user with the specified id * @since Envoy Server Standalone v0.1-alpha */ - public User getUserById(long id) { return (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); } + public User getUserById(long id) { return entityManager.find(User.class, id); } + /** + * Searched for a {@link User} with a specific name. + * + * @param name the name of the user + * @return the user with the specified name + * @since Envoy Server Standalone v0.1-alpha + */ public User getUserByName(String name) { return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult(); } + /** * Searches for a {@link Message} with a specific id. * - * @param id - the id to search for + * @param id the id to search for * @return the message with the specified id * @since Envoy Server Standalone v0.1-alpha */ - public Message getMessageById(long id) { - return (Message) entityManager.createNamedQuery("getMessageById").setParameter("id", id).getSingleResult(); - } + public Message getMessageById(long id) { return entityManager.find(Message.class, id); } /** * Returns all messages received while being offline. @@ -104,7 +111,6 @@ public class PersistenceManager { * @return all messages that the client does not yet have (unread messages) * @since Envoy Server Standalone v0.1-alpha */ - @SuppressWarnings("unchecked") public List getUnreadMessages(User user) { return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList(); } @@ -114,7 +120,6 @@ public class PersistenceManager { * @return the contacts of this User - currently everyone using Envoy * @since Envoy Server Standalone v0.1-alpha */ - @SuppressWarnings("unchecked") public List getContacts(User user) { return entityManager.createQuery("FROM User").getResultList(); } // TODO current solution gets all users, not just contacts. Should be changed to // entityManager.createNamedQuery("getContactsOfUser").setParameter("user", diff --git a/src/main/java/envoy/server/processors/EventProcessor.java b/src/main/java/envoy/server/processors/EventProcessor.java index 8c75278..024d22b 100644 --- a/src/main/java/envoy/server/processors/EventProcessor.java +++ b/src/main/java/envoy/server/processors/EventProcessor.java @@ -46,7 +46,8 @@ public class EventProcessor implements ObjectProcessor { * Redirects messageStatus changes to the database and to the recipient of the * {@link Message}. * - * @param event the {@link MessageStatusChangeEvent} to adjust + * @param event the {@link MessageStatusChangeEvent} to adjust + * @param writeProxy allows sending objects to clients * @throws EnvoyException if the {@link Message} has an invalid state * @since Envoy Server Standalone v0.1-alpha */ diff --git a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java new file mode 100644 index 0000000..fb958e8 --- /dev/null +++ b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java @@ -0,0 +1,36 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.data.IdGenerator; +import envoy.event.IdGeneratorRequest; +import envoy.server.ObjectProcessor; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: IdGeneratorRequestProcessor.java
+ * Created: 28 Jan 2020
+ * + * @author KSKE + * @since Envoy Server Standalone v0.1-alpha + */ +public class IdGeneratorRequestProcessor implements ObjectProcessor { + + private static long currentId = 0; + private static final long ID_RANGE = 2; + + @Override + public Class getInputClass() { return IdGeneratorRequest.class; } + + @Override + public void process(IdGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException { + System.out.println("Received id generation request."); + + IdGenerator generator = new IdGenerator(currentId, ID_RANGE); + currentId += ID_RANGE; + + System.out.println("Sending new id generator " + generator); + writeProxy.write(socketId, generator); + } +} diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index 2cb6abc..ad3bc54 100644 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -1,6 +1,7 @@ package envoy.server.processors; import java.io.IOException; +import java.util.Date; import envoy.data.Message; import envoy.server.ConnectionManager; @@ -28,8 +29,13 @@ public class MessageProcessor implements ObjectProcessor { ConnectionManager connectionManager = ConnectionManager.getInstance(); message.nextStatus(); - if (connectionManager.isOnline(message.getRecipientId())) try {// if recipient is online, he receives the message directly + if (connectionManager.isOnline(message.getRecipientId())) try { + // If recipient is online, send the message directly writeProxy.write(connectionManager.getSocketId(message.getRecipientId()), message); + + // Update the message status to RECEIVED + message.setReceivedDate(new Date()); + message.nextStatus(); } catch (IOException e) { System.err.println("Recipient online. Failed to send message" + message.getId()); e.printStackTrace();