Search in sources :

Example 1 with ClientCallback

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();
            }
        }
    }
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) ChatMessage(mage.view.ChatMessage) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 2 with ClientCallback

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));
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback)

Example 3 with ClientCallback

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();
        }
    }
}
Also used : Callback(org.jboss.remoting.callback.Callback) java.util(java.util) UserData(mage.players.net.UserData) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AsynchInvokerCallbackHandler(org.jboss.remoting.callback.AsynchInvokerCallbackHandler) MageException(mage.MageException) TimeUnit(java.util.concurrent.TimeUnit) UserGroup(mage.players.net.UserGroup) Logger(org.apache.log4j.Logger) GamesRoom(mage.server.game.GamesRoom) ConfigSettings(mage.server.managers.ConfigSettings) ManagerFactory(mage.server.managers.ManagerFactory) Matcher(java.util.regex.Matcher) ClientCallbackMethod(mage.interfaces.callback.ClientCallbackMethod) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LostConnection(mage.server.DisconnectReason.LostConnection) SystemUtil(mage.server.util.SystemUtil) Constants(mage.constants.Constants) Pattern(java.util.regex.Pattern) RandomUtil(mage.util.RandomUtil) HandleCallbackException(org.jboss.remoting.callback.HandleCallbackException) InvokerCallbackHandler(org.jboss.remoting.callback.InvokerCallbackHandler) ClientCallback(mage.interfaces.callback.ClientCallback) Callback(org.jboss.remoting.callback.Callback) ClientCallback(mage.interfaces.callback.ClientCallback) HandleCallbackException(org.jboss.remoting.callback.HandleCallbackException) MageException(mage.MageException) HandleCallbackException(org.jboss.remoting.callback.HandleCallbackException)

Example 4 with ClientCallback

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));
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback)

Example 5 with ClientCallback

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));
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback)

Aggregations

ClientCallback (mage.interfaces.callback.ClientCallback)14 User (mage.server.User)3 ChatMessage (mage.view.ChatMessage)2 DraftClientMessage (mage.view.DraftClientMessage)2 NotBoundException (java.rmi.NotBoundException)1 RemoteException (java.rmi.RemoteException)1 LocateRegistry (java.rmi.registry.LocateRegistry)1 Registry (java.rmi.registry.Registry)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MageException (mage.MageException)1 Constants (mage.constants.Constants)1 MageException (mage.interfaces.MageException)1