Adding user to their own contacts, reusing one database transaction
Fixes #15
This commit is contained in:
parent
5ae067a305
commit
c7bb65818e
@ -44,6 +44,10 @@ public class Startup {
|
||||
|
||||
server.start();
|
||||
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
|
||||
|
||||
System.out.println("Press any key to stop the server...");
|
||||
System.in.read();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static void initializeCurrentMessageId() {
|
||||
|
@ -3,6 +3,7 @@ package envoy.server.database;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import envoy.server.data.ConfigItem;
|
||||
@ -19,6 +20,9 @@ import envoy.server.data.User;
|
||||
*/
|
||||
public class PersistenceManager {
|
||||
|
||||
private final EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
||||
private final EntityTransaction transaction = entityManager.getTransaction();
|
||||
|
||||
private static final PersistenceManager persistenceManager = new PersistenceManager();
|
||||
|
||||
/**
|
||||
@ -26,7 +30,10 @@ public class PersistenceManager {
|
||||
*
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
private PersistenceManager() {}
|
||||
private PersistenceManager() {
|
||||
transaction.begin();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> transaction.commit()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link PersistenceManager} singleton
|
||||
@ -34,8 +41,6 @@ public class PersistenceManager {
|
||||
*/
|
||||
public static PersistenceManager getPersistenceManager() { return persistenceManager; }
|
||||
|
||||
private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
||||
|
||||
/**
|
||||
* Adds a {@link User} to the database.
|
||||
*
|
||||
@ -43,9 +48,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addUser(User User) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(User);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,9 +59,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addMessage(Message message) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(message);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,9 +70,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addConfigItem(ConfigItem configItem) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(configItem);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,9 +81,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateUser(User user) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(user);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,9 +92,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateMessage(Message message) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(message);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,9 +103,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateConfigItem(ConfigItem configItem) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(configItem);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,9 +114,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void deleteUser(User user) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(user);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,9 +125,8 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void deleteMessage(Message message) {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(message);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -33,6 +33,7 @@ public class ObjectMessageProcessor implements IMessageProcessor {
|
||||
*/
|
||||
public ObjectMessageProcessor(Set<ObjectProcessor<?>> processors) { this.processors = processors; }
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void process(Message message, WriteProxy writeProxy) {
|
||||
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
|
||||
|
@ -54,6 +54,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
// Create contacts
|
||||
Contacts contacts = new Contacts(user.getId(),
|
||||
user.getContacts().stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
|
||||
contacts.getContacts().add(user.toCommonUser());
|
||||
|
||||
// Complete handshake
|
||||
System.out.println("Sending user...");
|
||||
|
Reference in New Issue
Block a user