forked from External/mage
add some Java8 magic to the server
This commit is contained in:
parent
9bea7c7df2
commit
50f28a2bf7
7 changed files with 46 additions and 56 deletions
|
|
@ -1,14 +1,15 @@
|
|||
|
||||
package mage.server;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import javax.annotation.Nonnull;
|
||||
import mage.MageException;
|
||||
import mage.players.net.UserData;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jboss.remoting.callback.InvokerCallbackHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
|
|
@ -88,18 +89,15 @@ public enum SessionManager {
|
|||
}
|
||||
|
||||
public boolean setUserData(String userName, String sessionId, UserData userData, String clientVersion, String userIdStr) throws MageException {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
session.setUserData(userName, userData, clientVersion, userIdStr);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return getSession(sessionId)
|
||||
.map(session -> session.setUserData(userName,userData, clientVersion, userIdStr))
|
||||
.orElse(false);
|
||||
|
||||
}
|
||||
|
||||
public void disconnect(String sessionId, DisconnectReason reason) {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
if (!sessions.containsKey(sessionId)) {
|
||||
getSession(sessionId).ifPresent(session -> {
|
||||
if (!isValidSession(sessionId)) {
|
||||
// session was removed meanwhile by another thread so we can return
|
||||
return;
|
||||
}
|
||||
|
|
@ -122,11 +120,11 @@ public enum SessionManager {
|
|||
default:
|
||||
logger.trace("endSession: unexpected reason " + reason.toString() + " - sessionId: " + sessionId);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Admin requested the disconnect of a user
|
||||
*
|
||||
|
|
@ -150,11 +148,9 @@ public enum SessionManager {
|
|||
}
|
||||
|
||||
private Optional<User> getUserFromSession(String sessionId) {
|
||||
Optional<Session> session = getSession(sessionId);
|
||||
if (!session.isPresent()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return UserManager.instance.getUser(session.get().getUserId());
|
||||
return getSession(sessionId)
|
||||
.flatMap(s -> UserManager.instance.getUser(s.getUserId()));
|
||||
|
||||
}
|
||||
|
||||
public void endUserSession(String sessionId, String userSessionId) {
|
||||
|
|
@ -164,11 +160,8 @@ public enum SessionManager {
|
|||
}
|
||||
|
||||
public boolean isAdmin(String sessionId) {
|
||||
Session admin = sessions.get(sessionId);
|
||||
if (admin != null) {
|
||||
return admin.isAdmin();
|
||||
}
|
||||
return false;
|
||||
return getSession(sessionId).map(Session::isAdmin).orElse(false);
|
||||
|
||||
}
|
||||
|
||||
public boolean isValidSession(@Nonnull String sessionId) {
|
||||
|
|
@ -185,11 +178,9 @@ public enum SessionManager {
|
|||
}
|
||||
|
||||
public boolean extendUserSession(String sessionId, String pingInfo) {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
return UserManager.instance.extendUserSession(session.getUserId(), pingInfo);
|
||||
}
|
||||
return false;
|
||||
return getSession(sessionId)
|
||||
.map(session -> UserManager.instance.extendUserSession(session.getUserId(), pingInfo))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
public void sendErrorMessageToClient(String sessionId, String message) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue