From 387153bc6d8c0700ade59943683cdb5e4d14fe88 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 18 Jan 2020 09:55:12 +0100 Subject: [PATCH 01/12] Fixed EventProcessor and LoginCredentialProcessor --- src/main/java/envoy/server/Startup.java | 6 +++++- .../envoy/server/processors/EventProcessor.java | 17 ++++++----------- .../processors/LoginCredentialProcessor.java | 9 +++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index e2edb6a..a31c094 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -6,6 +6,7 @@ import java.util.Set; 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; @@ -36,8 +37,11 @@ public class Startup { processors.add(new LoginCredentialProcessor()); processors.add(new MessageProcessor()); processors.add(new EventProcessor()); - // new PersistenceManager(); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); + + // TODO: Prevent lazy DB initialization + PersistenceManager.getPersistenceManager(); + server.start(); server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance()); } diff --git a/src/main/java/envoy/server/processors/EventProcessor.java b/src/main/java/envoy/server/processors/EventProcessor.java index 58c769f..8c75278 100644 --- a/src/main/java/envoy/server/processors/EventProcessor.java +++ b/src/main/java/envoy/server/processors/EventProcessor.java @@ -20,9 +20,8 @@ import envoy.server.net.ObjectWriteProxy; * @author Leon Hofmeister * @since Envoy Server Standalone v0.1-alpha */ -public class EventProcessor implements ObjectProcessor> { - - private Event event; +@SuppressWarnings("rawtypes") +public class EventProcessor implements ObjectProcessor { /** * Creates an instance of @link{EventProcessor}. @@ -31,15 +30,13 @@ public class EventProcessor implements ObjectProcessor> { */ public EventProcessor() {} - @SuppressWarnings("unchecked") @Override - public Class> getInputClass() { return (Class>) event.getClass(); } + public Class getInputClass() { return Event.class; } @Override - public void process(Event input, long socketId, ObjectWriteProxy writeProxy) throws IOException { - event = input; - if (event instanceof MessageStatusChangeEvent) try { - applyMessageStatusChange((MessageStatusChangeEvent) event, writeProxy); + public void process(Event input, long socketId, ObjectWriteProxy writeProxy) throws IOException { + if (input instanceof MessageStatusChangeEvent) try { + applyMessageStatusChange((MessageStatusChangeEvent) input, writeProxy); } catch (EnvoyException e) { e.printStackTrace(); } @@ -71,7 +68,5 @@ public class EventProcessor implements ObjectProcessor> { e.printStackTrace(); } perMan.updateMessage(msg); - } - } diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index e94ce91..3196382 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -49,13 +49,14 @@ public class LoginCredentialProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(new envoy.server.data.User(user)); - pendingMessages.forEach((msg) -> { + for (Message msg : pendingMessages) { + System.out.println("Sending message " + msg.toString()); + writeProxy.write(socketId, msg); msg.setReceivedDate(new Date()); msg.setStatus(MessageStatus.RECEIVED); PersistenceManager.getPersistenceManager().updateMessage(msg); - }); - writeProxy.write(socketId, pendingMessages); + } } } From 6ee56bd635b551212c9ef3f08c72c40115444e30 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 18 Jan 2020 13:28:54 +0100 Subject: [PATCH 02/12] Added method to get contacts, although as of now all Users are returned --- src/main/java/envoy/server/data/User.java | 7 ++++++- .../envoy/server/database/PersistenceManager.java | 12 +++++++++++- .../server/processors/LoginCredentialProcessor.java | 9 +++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 5f4e28e..e596cca 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -8,6 +8,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.Temporal; @@ -28,7 +29,11 @@ import javax.persistence.TemporalType; */ @Entity @Table(name = "users") -@NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById") +@NamedQueries( + { @NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById"), + @NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested + } +) public class User { @Id diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index fb38a9e..a61275f 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -98,7 +98,17 @@ public class PersistenceManager { */ @SuppressWarnings("unchecked") public List getUnreadMessages(User user) { - // TODO may need to be changed to clientId return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList(); } + + /** + * @param user the User whose contacts should be retrieved + * @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", + // user).getResultList(); } \ No newline at end of file diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 3196382..3574f0e 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -1,9 +1,9 @@ package envoy.server.processors; import java.io.IOException; -import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import envoy.data.Contacts; import envoy.data.LoginCredentials; @@ -42,7 +42,12 @@ public class LoginCredentialProcessor implements ObjectProcessor()); + List users = PersistenceManager.getPersistenceManager() + .getContacts(new envoy.server.data.User(user)) + .stream() + .map(envoy.server.data.User::toCommonUser) + .collect(Collectors.toList()); + Contacts contacts = new Contacts(user.getId(), users); // Complete handshake System.out.println("Sending user..."); From 5f6f94502ad490ca869cbf37363954bb4a097767 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 18 Jan 2020 14:11:27 +0100 Subject: [PATCH 03/12] pom.xml contains now the commit id upon which Envoy Common is selected --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c0ed70b..047f246 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,8 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + e5c67b8 + com.github.informatik-ag-ngl From 8161c5fd287e09c7b0dda2c4ddaa430859f92b19 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 18 Jan 2020 23:50:41 +0100 Subject: [PATCH 04/12] implemented login and registration (unfinished) --- src/main/java/envoy/server/data/User.java | 8 +++++-- .../server/database/PersistenceManager.java | 3 +++ .../processors/LoginCredentialProcessor.java | 24 ++++++++++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index e596cca..c71e0ac 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -3,13 +3,15 @@ package envoy.server.data; import java.util.Date; import java.util.List; -import javax.persistence.ElementCollection; +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; @@ -31,6 +33,7 @@ import javax.persistence.TemporalType; @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.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested } ) @@ -46,7 +49,8 @@ public class User { private Date lastSeen; private envoy.data.User.UserStatus status; - @ElementCollection + // TODO: Fix Error + @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "contacts") private List contacts; /** diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index a61275f..0339be6 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -78,6 +78,9 @@ public class PersistenceManager { */ public User getUserById(long id) { return (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); } + public User getUserByName(String name) { + return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult(); + } /** * Searches for a {@link Message} with a specific id. * diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 3574f0e..b658445 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -23,12 +23,12 @@ import envoy.server.net.ObjectWriteProxy; * Created: 30.12.2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-alpha */ public class LoginCredentialProcessor implements ObjectProcessor { - // TODO: Acquire user IDs from database - private static long currentUserId = 1; + private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager(); @Override public Class getInputClass() { return LoginCredentials.class; } @@ -38,7 +38,25 @@ public class LoginCredentialProcessor implements ObjectProcessor Date: Tue, 21 Jan 2020 17:51:50 +0100 Subject: [PATCH 05/12] Interface Communication Login Credentials --- src/main/java/envoy/server/data/Message.java | 8 +++--- src/main/java/envoy/server/data/User.java | 3 +-- .../server/database/PersistenceManager.java | 7 +++++- .../processors/LoginCredentialProcessor.java | 25 ++++++++----------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 575da6a..83d7aeb 100644 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -2,6 +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; @@ -33,9 +34,6 @@ 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.sender =:sender AND m.status = :status", - name = "find read messages"// TODO do we need this namedQuery? ), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "getMessageById") } ) public class Message { @@ -43,10 +41,10 @@ public class Message { @Id private long id; - @ManyToOne + @ManyToOne(cascade = { CascadeType.PERSIST }) private User sender; - @ManyToOne + @ManyToOne(cascade = { CascadeType.PERSIST }) private User recipient; @Temporal(TemporalType.TIMESTAMP) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index c71e0ac..2f629dc 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -49,8 +49,7 @@ public class User { private Date lastSeen; private envoy.data.User.UserStatus status; - // TODO: Fix Error - @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "contacts") + @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true) private List contacts; /** diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 0339be6..aa2d008 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -43,7 +43,12 @@ public class PersistenceManager { * @param User the {@link User} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void addUser(User User) { entityManager.persist(User); } + public void addUser(User User) { + entityManager.getTransaction().begin(); + entityManager.persist(User); + entityManager.flush(); + entityManager.getTransaction().commit(); + } /** * Adds a {@link Message} to the database. diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index b658445..71a55dc 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -38,21 +38,18 @@ public class LoginCredentialProcessor implements ObjectProcessor users = PersistenceManager.getPersistenceManager() - .getContacts(new envoy.server.data.User(user)) + .getContacts(user) .stream() .map(envoy.server.data.User::toCommonUser) .collect(Collectors.toList()); @@ -69,11 +66,11 @@ public class LoginCredentialProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(new envoy.server.data.User(user)); + List pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(user); for (Message msg : pendingMessages) { System.out.println("Sending message " + msg.toString()); writeProxy.write(socketId, msg); From 0715e69cd7f0ca0beb68a61195034e7c60e483bd Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 25 Jan 2020 18:41:44 +0100 Subject: [PATCH 06/12] Revised LoginCredentialProcessor and worked on MessageProcessor --- .../java/envoy/server/ConnectionManager.java | 2 +- .../processors/LoginCredentialProcessor.java | 44 ++++++++++++------- .../server/processors/MessageProcessor.java | 8 ---- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/main/java/envoy/server/ConnectionManager.java b/src/main/java/envoy/server/ConnectionManager.java index 0ae8fcc..202334f 100644 --- a/src/main/java/envoy/server/ConnectionManager.java +++ b/src/main/java/envoy/server/ConnectionManager.java @@ -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); } /** diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 71a55dc..376989a 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -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 users = PersistenceManager.getPersistenceManager() @@ -79,4 +70,25 @@ public class LoginCredentialProcessor implements ObjectProcessor { 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)); } } From 6ab15547ec65281e851a0843d2804155961c4f87 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Mon, 27 Jan 2020 18:36:13 +0100 Subject: [PATCH 07/12] Fixes error multiple login, removed src/test/resources Fixes #12 --- .classpath | 77 +++++++++---------- pom.xml | 5 ++ .../envoy/server/net/ObjectMessageReader.java | 9 ++- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/.classpath b/.classpath index b468bc4..37e1a73 100644 --- a/.classpath +++ b/.classpath @@ -1,39 +1,38 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 047f246..39582f6 100644 --- a/pom.xml +++ b/pom.xml @@ -50,5 +50,10 @@ envoy-server-standalone + + + src/main/resources + + \ No newline at end of file diff --git a/src/main/java/envoy/server/net/ObjectMessageReader.java b/src/main/java/envoy/server/net/ObjectMessageReader.java index 51fa3df..1206cd6 100644 --- a/src/main/java/envoy/server/net/ObjectMessageReader.java +++ b/src/main/java/envoy/server/net/ObjectMessageReader.java @@ -5,7 +5,10 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; -import com.jenkov.nioserver.*; +import com.jenkov.nioserver.IMessageReader; +import com.jenkov.nioserver.Message; +import com.jenkov.nioserver.MessageBuffer; +import com.jenkov.nioserver.Socket; import envoy.util.SerializationUtils; @@ -47,10 +50,10 @@ public class ObjectMessageReader implements IMessageReader { nextMessage.writeToMessage(buffer); // Get message length - if (nextMessage.length - nextMessage.offset < 4) return; + if (nextMessage.length < 4) return; int length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4; - if (nextMessage.length - nextMessage.offset >= length) { + if (nextMessage.length >= length) { Message message = messageBuffer.getMessage(); message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); completeMessages.add(nextMessage); From 01d110fe1cd2a10e2c946077d93f26773b581ac3 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 28 Jan 2020 19:25:30 +0100 Subject: [PATCH 08/12] 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(); From 1a83b5ca50bc636a78fb576f8fcb56ddb88b0e3e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 28 Jan 2020 19:57:40 +0100 Subject: [PATCH 09/12] Fixed typo --- src/main/java/envoy/server/data/User.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 2e22518..5cb4b9e 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -37,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, orphanRemoval = true) private List contacts; /** From f1a86068255ce13e39807c32768644b72fd5133d Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 28 Jan 2020 20:46:27 +0100 Subject: [PATCH 10/12] Persisting the current message ID in a configuration table --- pom.xml | 2 +- src/main/java/envoy/server/Startup.java | 9 ++- .../java/envoy/server/data/ConfigItem.java | 65 +++++++++++++++++++ .../server/database/PersistenceManager.java | 39 ++++++++++- .../IdGeneratorRequestProcessor.java | 11 ++-- 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/main/java/envoy/server/data/ConfigItem.java diff --git a/pom.xml b/pom.xml index 3d271ff..db5b8da 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - f~login_or_registration-SNAPSHOT + develop-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 31aa072..98e8b3d 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -6,6 +6,7 @@ import java.util.Set; import com.jenkov.nioserver.Server; +import envoy.server.data.ConfigItem; import envoy.server.database.PersistenceManager; import envoy.server.net.ObjectMessageProcessor; import envoy.server.net.ObjectMessageReader; @@ -38,10 +39,14 @@ public class Startup { processors.add(new IdGeneratorRequestProcessor()); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); - // TODO: Prevent lazy DB initialization - PersistenceManager.getPersistenceManager(); + initializeCurrentMessageId(); server.start(); server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance()); } + + private static void initializeCurrentMessageId() { + PersistenceManager persMan = PersistenceManager.getPersistenceManager(); + if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0")); + } } \ No newline at end of file diff --git a/src/main/java/envoy/server/data/ConfigItem.java b/src/main/java/envoy/server/data/ConfigItem.java new file mode 100644 index 0000000..2fdb8ba --- /dev/null +++ b/src/main/java/envoy/server/data/ConfigItem.java @@ -0,0 +1,65 @@ +package envoy.server.data; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * Project: envoy-server-standalone
+ * File: ConfigItem.java
+ * Created: 28 Jan 2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server Standalone v0.1-alpha + */ +@Entity +@Table(name = "configuration") +public class ConfigItem { + + @Id + private String key; + private String value; + + /** + * Creates an instance of @link{ConfigItem}. + * + * @since Envoy Server Standalone v0.1-alpha + */ + public ConfigItem() {} + + /** + * Creates an instance of @link{ConfigItem}. + * + * @param key + * @param value + * @since Envoy Server Standalone v0.1-alpha + */ + public ConfigItem(String key, String value) { + this.key = key; + this.value = value; + } + + /** + * @return the key + * @since Envoy Server Standalone v0.1-alpha + */ + public String getKey() { return key; } + + /** + * @param key the key to set + * @since Envoy Server Standalone v0.1-alpha + */ + public void setKey(String key) { this.key = key; } + + /** + * @return the value + * @since Envoy Server Standalone v0.1-alpha + */ + public String getValue() { return value; } + + /** + * @param value the value to set + * @since Envoy Server Standalone v0.1-alpha + */ + public void setValue(String value) { this.value = value; } +} diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 9330296..97d2a3a 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -5,6 +5,7 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Persistence; +import envoy.server.data.ConfigItem; import envoy.server.data.Message; import envoy.server.data.User; @@ -59,13 +60,29 @@ public class PersistenceManager { entityManager.getTransaction().commit(); } + /** + * Adds a {@link ConfigItem} to the database. + * + * @param configItem the {@link ConfigItem} to add to the database + * @since Envoy Server Standalone v0.1-alpha + */ + public void addConfigItem(ConfigItem configItem) { + entityManager.getTransaction().begin(); + entityManager.persist(configItem); + entityManager.getTransaction().commit(); + } + /** * Updates a {@link User} in the database * * @param user the {@link User} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void updateUser(User user) { entityManager.merge(user); } + public void updateUser(User user) { + entityManager.getTransaction().begin(); + entityManager.merge(user); + entityManager.getTransaction().commit(); + } /** * Updates a {@link Message} in the database. @@ -73,7 +90,23 @@ public class PersistenceManager { * @param message the message to update * @since Envoy Server Standalone v0.1-alpha */ - public void updateMessage(Message message) { entityManager.merge(message); } + public void updateMessage(Message message) { + entityManager.getTransaction().begin(); + entityManager.merge(message); + entityManager.getTransaction().commit(); + } + + /** + * Updates a {@link ConfigItem} in the database. + * + * @param configItem the configItem to update + * @since Envoy Server Standalone v0.1-alpha + */ + public void updateConfigItem(ConfigItem configItem) { + entityManager.getTransaction().begin(); + entityManager.merge(configItem); + entityManager.getTransaction().commit(); + } /** * Searches for a {@link User} with a specific id. @@ -104,6 +137,8 @@ public class PersistenceManager { */ public Message getMessageById(long id) { return entityManager.find(Message.class, id); } + public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); } + /** * Returns all messages received while being offline. * diff --git a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java index fb958e8..31ba1c0 100644 --- a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java +++ b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java @@ -5,6 +5,8 @@ import java.io.IOException; import envoy.data.IdGenerator; import envoy.event.IdGeneratorRequest; import envoy.server.ObjectProcessor; +import envoy.server.data.ConfigItem; +import envoy.server.database.PersistenceManager; import envoy.server.net.ObjectWriteProxy; /** @@ -17,8 +19,7 @@ import envoy.server.net.ObjectWriteProxy; */ public class IdGeneratorRequestProcessor implements ObjectProcessor { - private static long currentId = 0; - private static final long ID_RANGE = 2; + private static final long ID_RANGE = 2; @Override public Class getInputClass() { return IdGeneratorRequest.class; } @@ -27,8 +28,10 @@ public class IdGeneratorRequestProcessor implements ObjectProcessor Date: Wed, 29 Jan 2020 07:45:59 +0100 Subject: [PATCH 11/12] Working on reading multiple messages at once The ObjectMessageReader does function normally for single messages but will deliver corrupted objects when a message consisting of multiple objects is received. --- src/main/java/envoy/server/data/User.java | 2 +- .../envoy/server/net/ObjectMessageReader.java | 27 +++++++++++-------- .../IdGeneratorRequestProcessor.java | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 5cb4b9e..877a699 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -86,7 +86,7 @@ public class User { public String getName() { return name; } /** - * @param name the user name to set + * @param name the username to set * @since Envoy Server Standalone v0.1-alpha * @see User#getName() */ diff --git a/src/main/java/envoy/server/net/ObjectMessageReader.java b/src/main/java/envoy/server/net/ObjectMessageReader.java index 1206cd6..1ae8e9a 100644 --- a/src/main/java/envoy/server/net/ObjectMessageReader.java +++ b/src/main/java/envoy/server/net/ObjectMessageReader.java @@ -5,10 +5,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; -import com.jenkov.nioserver.IMessageReader; -import com.jenkov.nioserver.Message; -import com.jenkov.nioserver.MessageBuffer; -import com.jenkov.nioserver.Socket; +import com.jenkov.nioserver.*; import envoy.util.SerializationUtils; @@ -48,18 +45,26 @@ public class ObjectMessageReader implements IMessageReader { } nextMessage.writeToMessage(buffer); + buffer.clear(); // Get message length if (nextMessage.length < 4) return; int length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4; + do { - if (nextMessage.length >= length) { - Message message = messageBuffer.getMessage(); - message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); - completeMessages.add(nextMessage); - nextMessage = message; - } + // Separate first complete message + if (nextMessage.length >= length) { + Message message = messageBuffer.getMessage(); + message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); + message.length = nextMessage.length - length; + completeMessages.add(nextMessage); + nextMessage = message; + } - buffer.clear(); + // Get message length + if (nextMessage.length < 4) return; + length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4; + + } while (nextMessage.length >= length); } } \ No newline at end of file diff --git a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java index 31ba1c0..e12fbd2 100644 --- a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java +++ b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java @@ -14,7 +14,7 @@ import envoy.server.net.ObjectWriteProxy; * File: IdGeneratorRequestProcessor.java
* Created: 28 Jan 2020
* - * @author KSKE + * @author Kai S. K. Engelbart * @since Envoy Server Standalone v0.1-alpha */ public class IdGeneratorRequestProcessor implements ObjectProcessor { From a95eb07dae5d616029bc746213e4ff2472518ef4 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 29 Jan 2020 16:20:57 +0100 Subject: [PATCH 12/12] Fixed reading multiple objects from one message --- .../java/envoy/server/net/ObjectMessageReader.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/server/net/ObjectMessageReader.java b/src/main/java/envoy/server/net/ObjectMessageReader.java index 1ae8e9a..db0fee7 100644 --- a/src/main/java/envoy/server/net/ObjectMessageReader.java +++ b/src/main/java/envoy/server/net/ObjectMessageReader.java @@ -5,7 +5,10 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; -import com.jenkov.nioserver.*; +import com.jenkov.nioserver.IMessageReader; +import com.jenkov.nioserver.Message; +import com.jenkov.nioserver.MessageBuffer; +import com.jenkov.nioserver.Socket; import envoy.util.SerializationUtils; @@ -55,8 +58,9 @@ public class ObjectMessageReader implements IMessageReader { // Separate first complete message if (nextMessage.length >= length) { Message message = messageBuffer.getMessage(); - message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); - message.length = nextMessage.length - length; + message.writePartialMessageToMessage(nextMessage, length); + message.length = nextMessage.length - length; + nextMessage.length = length; completeMessages.add(nextMessage); nextMessage = message; }