use of mage.interfaces.callback.ClientCallback in project mage by magefree.
the class ChatSession method broadcast.
public void broadcast(String userName, String message, MessageColor color, boolean withTime, Game game, MessageType messageType, SoundToPlay soundToPlay) {
if (!message.isEmpty()) {
Set<UUID> clientsToRemove = new HashSet<>();
ClientCallback clientCallback = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage(userName, message, (withTime ? new Date() : null), game, color, messageType, soundToPlay));
List<UUID> chatUserIds = new ArrayList<>();
final Lock r = lock.readLock();
r.lock();
try {
chatUserIds.addAll(clients.keySet());
} finally {
r.unlock();
}
for (UUID userId : chatUserIds) {
Optional<User> user = managerFactory.userManager().getUser(userId);
if (user.isPresent()) {
user.get().fireCallback(clientCallback);
} else {
clientsToRemove.add(userId);
}
}
if (!clientsToRemove.isEmpty()) {
final Lock w = lock.readLock();
w.lock();
try {
clients.keySet().removeAll(clientsToRemove);
} finally {
w.unlock();
}
}
}
}
use of mage.interfaces.callback.ClientCallback in project mage by magefree.
the class RandomString method sendInfoMessageToClient.
public void sendInfoMessageToClient(String message) {
List<String> messageData = new LinkedList<>();
messageData.add("Information about connecting to the server");
messageData.add(message);
fireCallback(new ClientCallback(ClientCallbackMethod.SHOW_USERMESSAGE, null, messageData));
}
use of mage.interfaces.callback.ClientCallback in project mage by magefree.
the class RandomString method fireCallback.
public void fireCallback(final ClientCallback call) {
boolean lockSet = false;
try {
if (valid && callBackLock.tryLock(50, TimeUnit.MILLISECONDS)) {
call.setMessageId(messageId.incrementAndGet());
lockSet = true;
Callback callback = new Callback(call);
callbackHandler.handleCallbackOneway(callback);
}
} catch (InterruptedException ex) {
logger.warn("SESSION LOCK - fireCallback - userId: " + userId + " messageId: " + call.getMessageId(), ex);
} catch (HandleCallbackException ex) {
this.valid = false;
managerFactory.userManager().getUser(userId).ifPresent(user -> {
user.setUserState(User.UserState.Disconnected);
logger.warn("SESSION CALLBACK EXCEPTION - " + user.getName() + " userId " + userId + " messageId: " + call.getMessageId() + " - cause: " + getBasicCause(ex).toString());
logger.trace("Stack trace:", ex);
managerFactory.sessionManager().disconnect(sessionId, LostConnection);
});
} catch (Exception ex) {
logger.warn("Unspecific exception:", ex);
} finally {
if (lockSet) {
callBackLock.unlock();
}
}
}
use of mage.interfaces.callback.ClientCallback in project mage by magefree.
the class RandomString method sendErrorMessageToClient.
public void sendErrorMessageToClient(String message) {
List<String> messageData = new LinkedList<>();
messageData.add("Error while connecting to server");
messageData.add(message);
fireCallback(new ClientCallback(ClientCallbackMethod.SHOW_USERMESSAGE, null, messageData));
}
use of mage.interfaces.callback.ClientCallback in project mage by magefree.
the class User method showUserMessage.
public void showUserMessage(final String titel, String message) {
List<String> messageData = new LinkedList<>();
messageData.add(titel);
messageData.add(message);
fireCallback(new ClientCallback(ClientCallbackMethod.SHOW_USERMESSAGE, null, messageData));
}
Aggregations