Replaces print statements by logger calls

Closes #34
This commit is contained in:
Kai S. K. Engelbart 2020-06-11 12:06:06 +02:00
parent 2f0fccb536
commit bf60e9675d
9 changed files with 56 additions and 27 deletions

View File

@ -40,7 +40,7 @@ public class Startup {
items.put("homeDirectory", items.put("homeDirectory",
new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true)); new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.SEVERE, true)); items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.SEVERE, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.INFO, true)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINE, true));
final var config = new Config(); final var config = new Config();
config.load(items); config.load(items);

View File

@ -4,12 +4,14 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger;
import com.jenkov.nioserver.IMessageProcessor; import com.jenkov.nioserver.IMessageProcessor;
import com.jenkov.nioserver.Message; import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.WriteProxy; import com.jenkov.nioserver.WriteProxy;
import envoy.server.processors.ObjectProcessor; import envoy.server.processors.ObjectProcessor;
import envoy.util.EnvoyLog;
/** /**
* Handles incoming objects.<br> * Handles incoming objects.<br>
@ -25,6 +27,8 @@ public class ObjectMessageProcessor implements IMessageProcessor {
private final Set<ObjectProcessor<?>> processors; private final Set<ObjectProcessor<?>> processors;
private static final Logger logger = EnvoyLog.getLogger(ObjectMessageProcessor.class);
/** /**
* The constructor to set the {@link ObjectProcessor}s. * The constructor to set the {@link ObjectProcessor}s.
* *
@ -39,11 +43,11 @@ public class ObjectMessageProcessor implements IMessageProcessor {
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
Object obj = in.readObject(); Object obj = in.readObject();
if (obj == null) { if (obj == null) {
System.out.println("received a null object"); logger.warning("Received a null object");
return; return;
} }
System.out.println("Read object: " + obj); logger.fine("Received " + obj);
// Process object // Process object
processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> { processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {

View File

@ -1,10 +1,12 @@
package envoy.server.net; package envoy.server.net;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger;
import com.jenkov.nioserver.Message; import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.WriteProxy; import com.jenkov.nioserver.WriteProxy;
import envoy.util.EnvoyLog;
import envoy.util.SerializationUtils; import envoy.util.SerializationUtils;
/** /**
@ -21,6 +23,8 @@ public class ObjectWriteProxy {
private final WriteProxy writeProxy; private final WriteProxy writeProxy;
private static final Logger logger = EnvoyLog.getLogger(ObjectWriteProxy.class);
/** /**
* Creates an instance of {@link ObjectWriteProxy}. * Creates an instance of {@link ObjectWriteProxy}.
* *
@ -40,7 +44,7 @@ public class ObjectWriteProxy {
final Message response = writeProxy.getMessage(); final Message response = writeProxy.getMessage();
response.socketId = recipientSocketID; response.socketId = recipientSocketID;
System.out.println("Sending object " + obj); logger.fine("Sending " + obj);
// Serialize object to byte array // Serialize object to byte array
final byte[] objBytes = SerializationUtils.writeToByteArray(obj); final byte[] objBytes = SerializationUtils.writeToByteArray(obj);

View File

@ -1,12 +1,14 @@
package envoy.server.processors; package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger;
import envoy.event.ElementOperation; import envoy.event.ElementOperation;
import envoy.event.contact.ContactOperationEvent; import envoy.event.contact.ContactOperationEvent;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
@ -19,6 +21,7 @@ import envoy.server.net.ObjectWriteProxy;
public class ContactOperationProcessor implements ObjectProcessor<ContactOperationEvent> { public class ContactOperationProcessor implements ObjectProcessor<ContactOperationEvent> {
private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class);
@Override @Override
public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException { public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException {
@ -27,7 +30,7 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
final long userID = ConnectionManager.getInstance().getUserIdBySocketId(socketId); final long userID = ConnectionManager.getInstance().getUserIdBySocketId(socketId);
final long contactId = evt.get().getID(); final long contactId = evt.get().getID();
System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userID); logger.fine(String.format("Adding user %s to the contact list of user %d.%n", evt.get(), userID));
PersistenceManager.getInstance().addUserContact(userID, contactId); PersistenceManager.getInstance().addUserContact(userID, contactId);
// Notify the contact if online // Notify the contact if online
@ -36,7 +39,7 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
new ContactOperationEvent(PersistenceManager.getInstance().getUserByID(userID).toCommon(), ElementOperation.ADD)); new ContactOperationEvent(PersistenceManager.getInstance().getUserByID(userID).toCommon(), ElementOperation.ADD));
break; break;
default: default:
System.err.printf("Received %s with an unsupported operation.%n", evt); logger.warning(String.format("Received %s with an unsupported operation.%n", evt));
} }
} }

View File

@ -2,6 +2,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException; import javax.persistence.EntityExistsException;
@ -11,6 +12,7 @@ import envoy.event.MessageStatusChangeEvent;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
@ -22,6 +24,8 @@ import envoy.server.net.ObjectWriteProxy;
*/ */
public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> { public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
private static final Logger logger = EnvoyLog.getLogger(GroupCreationProcessor.class);
@Override @Override
public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) { public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) {
groupMessage.nextStatus(); groupMessage.nextStatus();
@ -29,7 +33,7 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
try { try {
writeProxy.write(socketID, new MessageStatusChangeEvent(groupMessage)); writeProxy.write(socketID, new MessageStatusChangeEvent(groupMessage));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Could not send messageStatusChangeEvent to the sender of this groupMessage with ID: " + groupMessage.getID()); logger.warning("Could not send messageStatusChangeEvent to the sender of this groupMessage with ID: " + groupMessage.getID());
e.printStackTrace(); e.printStackTrace();
} }
@ -48,7 +52,7 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
try { try {
PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, new Date())); PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, new Date()));
} catch (EntityExistsException e) { } catch (EntityExistsException e) {
System.err.println("Received a groupMessage with an ID that already exists"); logger.warning("Received a groupMessage with an ID that already exists");
} }
} }
@ -57,7 +61,7 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
// If recipient is online, send the groupMessage directly // If recipient is online, send the groupMessage directly
writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); writeProxy.write(connectionManager.getSocketId(memberID), groupMessage);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Recipient online. Failed to send message" + groupMessage.getID()); logger.warning("Recipient online. Failed to send message" + groupMessage.getID());
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -1,12 +1,14 @@
package envoy.server.processors; package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger;
import envoy.data.IDGenerator; import envoy.data.IDGenerator;
import envoy.event.IDGeneratorRequest; import envoy.event.IDGeneratorRequest;
import envoy.server.data.ConfigItem; import envoy.server.data.ConfigItem;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
@ -20,15 +22,16 @@ import envoy.server.net.ObjectWriteProxy;
public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> { public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
private static final long ID_RANGE = 200; private static final long ID_RANGE = 200;
private static final Logger logger = EnvoyLog.getLogger(IDGeneratorRequestProcessor.class);
@Override @Override
public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; } public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; }
@Override @Override
public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
System.out.println("Received id generation request."); logger.fine("Received id generation request.");
var generator = createIDGenerator(); var generator = createIDGenerator();
System.out.println("Sending new id generator " + generator); logger.info("Sending new id generator " + generator);
writeProxy.write(socketID, generator); writeProxy.write(socketID, generator);
} }

View File

@ -2,6 +2,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.logging.Logger;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
@ -15,6 +16,7 @@ import envoy.server.data.Message;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* This {@link ObjectProcessor} handles {@link LoginCredentials}.<br> * This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
@ -32,16 +34,18 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
@Override @Override
public void process(LoginCredentials input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(LoginCredentials input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
UserStatusChangeProcessor.setWriteProxy(writeProxy); UserStatusChangeProcessor.setWriteProxy(writeProxy);
System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketID)); logger.info(String.format("Received login credentials %s from socket ID %d", input, socketID));
envoy.server.data.User user = getUser(input, socketID, writeProxy); envoy.server.data.User user = getUser(input, socketID, writeProxy);
// Not logged in successfully // Not logged in successfully
if (user == null) { if (user == null) {
System.out.println("Rejecting handshake on socket " + socketID); logger.info("Rejecting handshake on socket " + socketID);
return; return;
} }
connectionManager.registerUser(user.getID(), socketID); connectionManager.registerUser(user.getID(), socketID);
@ -52,25 +56,25 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
UserStatusChangeProcessor.updateUserStatus(user); UserStatusChangeProcessor.updateUserStatus(user);
// Complete handshake // Complete handshake
System.out.println("Sending user..."); logger.fine("Sending user...");
writeProxy.write(socketID, user.toCommon()); writeProxy.write(socketID, user.toCommon());
System.out.println("Acquiring pending messages for the client..."); logger.fine("Acquiring pending messages for the client...");
List<Message> pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); List<Message> pendingMessages = PersistenceManager.getInstance().getPendingMessages(user);
for (Message msg : pendingMessages) for (Message msg : pendingMessages)
if (msg.getStatus() == MessageStatus.SENT) { if (msg.getStatus() == MessageStatus.SENT) {
System.out.println("Sending message " + msg.toCommon()); logger.info("Sending message " + msg.toCommon());
writeProxy.write(socketID, msg.toCommon()); writeProxy.write(socketID, msg.toCommon());
msg.setReceivedDate(new Date()); msg.setReceivedDate(new Date());
msg.setStatus(MessageStatus.RECEIVED); msg.setStatus(MessageStatus.RECEIVED);
if (connectionManager.isOnline(msg.getSender().getID())) { if (connectionManager.isOnline(msg.getSender().getID())) {
var evt = new MessageStatusChangeEvent(msg.toCommon()); var evt = new MessageStatusChangeEvent(msg.toCommon());
System.out.println("Sending messageStatusChangeEvent to sender " + evt); logger.info("Sending messageStatusChangeEvent to sender " + evt);
writeProxy.write(connectionManager.getSocketId(msg.getSender().getID()), evt); writeProxy.write(connectionManager.getSocketId(msg.getSender().getID()), evt);
} }
PersistenceManager.getInstance().updateMessage(msg); PersistenceManager.getInstance().updateMessage(msg);
} else { } else {
var evt = new MessageStatusChangeEvent(msg.toCommon()); var evt = new MessageStatusChangeEvent(msg.toCommon());
System.out.println("Sending messageStatusChangeEvent " + evt); logger.info("Sending messageStatusChangeEvent " + evt);
writeProxy.write(socketID, evt); writeProxy.write(socketID, evt);
} }
} }

View File

@ -2,6 +2,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException; import javax.persistence.EntityExistsException;
@ -11,6 +12,7 @@ import envoy.event.MessageStatusChangeEvent;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* This {@link ObjectProcessor} handles incoming {@link Message}s.<br> * This {@link ObjectProcessor} handles incoming {@link Message}s.<br>
@ -25,6 +27,8 @@ import envoy.server.net.ObjectWriteProxy;
*/ */
public class MessageProcessor implements ObjectProcessor<Message> { public class MessageProcessor implements ObjectProcessor<Message> {
private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class);
@Override @Override
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { public void process(Message message, long socketID, ObjectWriteProxy writeProxy) {
message.nextStatus(); message.nextStatus();
@ -36,14 +40,14 @@ public class MessageProcessor implements ObjectProcessor<Message> {
try { try {
writeProxy.write(socketID, new MessageStatusChangeEvent(message)); writeProxy.write(socketID, new MessageStatusChangeEvent(message));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID()); logger.warning("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID());
e.printStackTrace(); e.printStackTrace();
} }
} }
try { try {
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message)); PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
} catch (EntityExistsException e) { } catch (EntityExistsException e) {
System.err.println("Received a message with an id that already exists"); logger.warning("Received a message with an id that already exists");
} }
} }
@ -54,9 +58,8 @@ public class MessageProcessor implements ObjectProcessor<Message> {
// Update the message status to RECEIVED // Update the message status to RECEIVED
message.setReceivedDate(new Date()); message.setReceivedDate(new Date());
message.nextStatus(); message.nextStatus();
} catch (IOException e) { } catch (IOException e) {
System.err.println("Recipient online. Failed to send message" + message.getID()); logger.warning("Recipient online. Failed to send message" + message.getID());
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -1,6 +1,7 @@
package envoy.server.processors; package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.event.UserStatusChangeEvent; import envoy.event.UserStatusChangeEvent;
@ -8,6 +9,7 @@ import envoy.server.data.PersistenceManager;
import envoy.server.data.User; import envoy.server.data.User;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* This processor handles incoming {@link UserStatusChangeEvent}.<br> * This processor handles incoming {@link UserStatusChangeEvent}.<br>
@ -24,6 +26,8 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
private static ObjectWriteProxy writeProxy; private static ObjectWriteProxy writeProxy;
private static PersistenceManager persistenceManager = PersistenceManager.getInstance(); private static PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class);
@Override @Override
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; } public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; }
@ -31,11 +35,10 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
public void process(UserStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(UserStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
// new status should not equal old status // new status should not equal old status
if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) { if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) {
System.out.println("Received an unnecessary UserStatusChangeEvent"); logger.warning("Received an unnecessary UserStatusChangeEvent");
return; return;
} }
updateUserStatus(input); updateUserStatus(input);
} }
/** /**
@ -47,10 +50,11 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
*/ */
public static void updateUserStatus(User user) { public static void updateUserStatus(User user) {
// handling for newly logged in clients
// Handling for newly logged in clients
persistenceManager.updateContact(user); persistenceManager.updateContact(user);
// handling for contacts that are already online // Handling for contacts that are already online
notifyContacts(user); notifyContacts(user);
} }
@ -75,7 +79,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
if (connectionManager.isOnline(contact.getID())) writeProxy.write(connectionManager.getSocketId(contact.getID()), evt); if (connectionManager.isOnline(contact.getID())) writeProxy.write(connectionManager.getSocketId(contact.getID()), evt);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.err.println("Could not notify online contacts of user " + evt.getID() + " that his status has been changed"); logger.warning("Could not notify online contacts of user " + evt.getID() + " that his status has been changed");
} }
} }