Search in sources :

Example 1 with SOCKeyedMessage

use of soc.message.SOCKeyedMessage in project JSettlers2 by jdmonin.

the class SOCGameHandler method sendGamePendingMessages.

/**
 * Sends the contents of this game's {@link SOCGame#pendingMessagesOut}, then empties that list.
 * To avoid unnecessary work here, check if the list is empty before calling this method.
 *<P>
 * <B>I18N:</B> Checks {@code pendingMessagesOut} for {@link SOCKeyedMessage}s and handles them accordingly.
 * Currently this is the only method that checks for those, because other places send text messages
 * immediately instead of queueing them and localizing/sending later.
 * Also checks for {@link UnlocalizedString}s, to be localized and sent with
 * {@link SOCServer#messageToGameKeyed(SOCGame, boolean, String, Object...)}
 * or {@link SOCServer#messageToGameKeyedSpecial(SOCGame, boolean, String, Object...)}.
 *<P>
 * <B>Locks:</B> If {@code takeMon} is true, takes and releases
 * {@link SOCGameList#takeMonitorForGame(String) gameList.takeMonitorForGame(gameName)}.
 * Otherwise call {@link SOCGameList#takeMonitorForGame(String) gameList.takeMonitorForGame(gameName)}
 * before calling this method.
 * @param ga  game with pending messages
 * @param takeMon Should this method take and release game's monitor via
 *     {@link SOCGameList#takeMonitorForGame(String) gameList.takeMonitorForGame(gameName)}?
 *     True unless caller already holds that monitor.
 * @see #updatePlayerSVPPendingMessage(SOCGame, SOCPlayer, int, String)
 * @since 2.0.00
 */
void sendGamePendingMessages(SOCGame ga, final boolean takeMon) {
    final String gaName = ga.getName();
    if (takeMon)
        srv.gameList.takeMonitorForGame(gaName);
    for (final Object msg : ga.pendingMessagesOut) {
        if (msg instanceof SOCKeyedMessage)
            srv.messageToGameKeyedType(ga, (SOCKeyedMessage) msg, false);
        else if (msg instanceof SOCMessage)
            srv.messageToGameWithMon(gaName, (SOCMessage) msg);
        else if (msg instanceof UnlocalizedString) {
            final UnlocalizedString us = (UnlocalizedString) msg;
            if (us.isSpecial)
                srv.messageToGameKeyedSpecial(ga, false, us.key, us.params);
            else
                srv.messageToGameKeyed(ga, false, us.key, us.params);
        }
    // else: ignore
    }
    ga.pendingMessagesOut.clear();
    for (SOCPlayer p : ga.getPlayers()) {
        final List<Object> pq = p.pendingMessagesOut;
        final int L = pq.size();
        if (L >= 0) {
            final Connection c = srv.getConnection(p.getName());
            if (c != null)
                for (int i = 0; i < L; ++i) c.put(((SOCMessage) pq.get(i)).toCmd());
            pq.clear();
        }
    }
    if (takeMon)
        srv.gameList.releaseMonitorForGame(gaName);
}
Also used : SOCMessage(soc.message.SOCMessage) SOCKeyedMessage(soc.message.SOCKeyedMessage) Connection(soc.server.genericServer.Connection)

Aggregations

SOCKeyedMessage (soc.message.SOCKeyedMessage)1 SOCMessage (soc.message.SOCMessage)1 Connection (soc.server.genericServer.Connection)1