Added IdGenerationRequestProcessor, fixed bugs in PersistenceManager
* Removed get*ById queries * Removed src/test/resources from pom.xml
This commit is contained in:
parent
3ff1ac4c66
commit
01d110fe1c
@ -15,12 +15,6 @@
|
|||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
|
||||||
<attributes>
|
|
||||||
<attribute name="test" value="true"/>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="optional" value="true"/>
|
<attribute name="optional" value="true"/>
|
||||||
|
3
pom.xml
3
pom.xml
@ -28,8 +28,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.informatik-ag-ngl</groupId>
|
<groupId>com.github.informatik-ag-ngl</groupId>
|
||||||
<artifactId>envoy-common</artifactId>
|
<artifactId>envoy-common</artifactId>
|
||||||
<version>e5c67b8</version>
|
<version>f~login_or_registration-SNAPSHOT</version>
|
||||||
<!--<version>develop-SNAPSHOT</version> <version>0.2-alpha</version> -->
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.informatik-ag-ngl</groupId>
|
<groupId>com.github.informatik-ag-ngl</groupId>
|
||||||
|
@ -9,9 +9,7 @@ import com.jenkov.nioserver.Server;
|
|||||||
import envoy.server.database.PersistenceManager;
|
import envoy.server.database.PersistenceManager;
|
||||||
import envoy.server.net.ObjectMessageProcessor;
|
import envoy.server.net.ObjectMessageProcessor;
|
||||||
import envoy.server.net.ObjectMessageReader;
|
import envoy.server.net.ObjectMessageReader;
|
||||||
import envoy.server.processors.EventProcessor;
|
import envoy.server.processors.*;
|
||||||
import envoy.server.processors.LoginCredentialProcessor;
|
|
||||||
import envoy.server.processors.MessageProcessor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the server.<br>
|
* Starts the server.<br>
|
||||||
@ -37,6 +35,7 @@ public class Startup {
|
|||||||
processors.add(new LoginCredentialProcessor());
|
processors.add(new LoginCredentialProcessor());
|
||||||
processors.add(new MessageProcessor());
|
processors.add(new MessageProcessor());
|
||||||
processors.add(new EventProcessor());
|
processors.add(new EventProcessor());
|
||||||
|
processors.add(new IdGeneratorRequestProcessor());
|
||||||
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
|
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
|
||||||
|
|
||||||
// TODO: Prevent lazy DB initialization
|
// TODO: Prevent lazy DB initialization
|
||||||
|
@ -2,15 +2,7 @@ package envoy.server.data;
|
|||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.*;
|
||||||
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 envoy.data.MessageBuilder;
|
import envoy.data.MessageBuilder;
|
||||||
import envoy.server.database.PersistenceManager;
|
import envoy.server.database.PersistenceManager;
|
||||||
@ -34,7 +26,7 @@ import envoy.server.database.PersistenceManager;
|
|||||||
{ @NamedQuery(
|
{ @NamedQuery(
|
||||||
query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT",
|
query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT",
|
||||||
name = "getUnreadMessages"
|
name = "getUnreadMessages"
|
||||||
), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "getMessageById") }
|
) }
|
||||||
)
|
)
|
||||||
public class Message {
|
public class Message {
|
||||||
|
|
||||||
@ -91,7 +83,7 @@ public class Message {
|
|||||||
*/
|
*/
|
||||||
public envoy.data.Message toCommonMessage() {
|
public envoy.data.Message toCommonMessage() {
|
||||||
// TODO: Attachment, dates
|
// 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,18 +3,7 @@ package envoy.server.data;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.*;
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class serves as a way to let Hibernate communicate with the server
|
* This class serves as a way to let Hibernate communicate with the server
|
||||||
@ -32,8 +21,7 @@ import javax.persistence.TemporalType;
|
|||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@NamedQueries(
|
@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
|
@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 Date lastSeen;
|
||||||
private envoy.data.User.UserStatus status;
|
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<User> contacts;
|
private List<User> contacts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,7 +64,7 @@ public class User {
|
|||||||
* @return a database {@link User} converted into an {@link envoy.data.User}
|
* @return a database {@link User} converted into an {@link envoy.data.User}
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @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}
|
* @return the id of a {link envoy.data.User}
|
||||||
@ -98,7 +86,7 @@ public class User {
|
|||||||
public String getName() { return name; }
|
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
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
* @see User#getName()
|
* @see User#getName()
|
||||||
*/
|
*/
|
||||||
|
@ -5,8 +5,6 @@ import java.util.List;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
|
|
||||||
import envoy.server.data.Message;
|
import envoy.server.data.Message;
|
||||||
import envoy.server.data.User;
|
import envoy.server.data.User;
|
||||||
|
|
||||||
@ -46,7 +44,6 @@ public class PersistenceManager {
|
|||||||
public void addUser(User User) {
|
public void addUser(User User) {
|
||||||
entityManager.getTransaction().begin();
|
entityManager.getTransaction().begin();
|
||||||
entityManager.persist(User);
|
entityManager.persist(User);
|
||||||
entityManager.flush();
|
|
||||||
entityManager.getTransaction().commit();
|
entityManager.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +53,11 @@ public class PersistenceManager {
|
|||||||
* @param message the {@link Message} to add to the database
|
* @param message the {@link Message} to add to the database
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @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
|
* Updates a {@link User} in the database
|
||||||
@ -64,7 +65,7 @@ public class PersistenceManager {
|
|||||||
* @param user the {@link User} to add to the database
|
* @param user the {@link User} to add to the database
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @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.
|
* Updates a {@link Message} in the database.
|
||||||
@ -72,30 +73,36 @@ public class PersistenceManager {
|
|||||||
* @param message the message to update
|
* @param message the message to update
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @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.
|
* 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
|
* @return the user with the specified id
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @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) {
|
public User getUserByName(String name) {
|
||||||
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
|
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches for a {@link Message} with a specific id.
|
* 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
|
* @return the message with the specified id
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public Message getMessageById(long id) {
|
public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
|
||||||
return (Message) entityManager.createNamedQuery("getMessageById").setParameter("id", id).getSingleResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all messages received while being offline.
|
* 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)
|
* @return all messages that the client does not yet have (unread messages)
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public List<Message> getUnreadMessages(User user) {
|
public List<Message> getUnreadMessages(User user) {
|
||||||
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
|
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
|
* @return the contacts of this User - currently everyone using Envoy
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public List<User> getContacts(User user) { return entityManager.createQuery("FROM User").getResultList(); }
|
public List<User> getContacts(User user) { return entityManager.createQuery("FROM User").getResultList(); }
|
||||||
// TODO current solution gets all users, not just contacts. Should be changed to
|
// TODO current solution gets all users, not just contacts. Should be changed to
|
||||||
// entityManager.createNamedQuery("getContactsOfUser").setParameter("user",
|
// entityManager.createNamedQuery("getContactsOfUser").setParameter("user",
|
||||||
|
@ -46,7 +46,8 @@ public class EventProcessor implements ObjectProcessor<Event> {
|
|||||||
* Redirects messageStatus changes to the database and to the recipient of the
|
* Redirects messageStatus changes to the database and to the recipient of the
|
||||||
* {@link Message}.
|
* {@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
|
* @throws EnvoyException if the {@link Message} has an invalid state
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
|
@ -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: <strong>envoy-server-standalone</strong><br>
|
||||||
|
* File: <strong>IdGeneratorRequestProcessor.java</strong><br>
|
||||||
|
* Created: <strong>28 Jan 2020</strong><br>
|
||||||
|
*
|
||||||
|
* @author KSKE
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorRequest> {
|
||||||
|
|
||||||
|
private static long currentId = 0;
|
||||||
|
private static final long ID_RANGE = 2;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<IdGeneratorRequest> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package envoy.server.processors;
|
package envoy.server.processors;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import envoy.data.Message;
|
import envoy.data.Message;
|
||||||
import envoy.server.ConnectionManager;
|
import envoy.server.ConnectionManager;
|
||||||
@ -28,8 +29,13 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
|||||||
|
|
||||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||||
message.nextStatus();
|
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);
|
writeProxy.write(connectionManager.getSocketId(message.getRecipientId()), message);
|
||||||
|
|
||||||
|
// Update the message status to RECEIVED
|
||||||
|
message.setReceivedDate(new Date());
|
||||||
|
message.nextStatus();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Recipient online. Failed to send message" + message.getId());
|
System.err.println("Recipient online. Failed to send message" + message.getId());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Reference in New Issue
Block a user