Search in sources :

Example 41 with Connection

use of soc.server.genericServer.Connection in project JSettlers2 by jdmonin.

the class SOCServer method nameConnection.

// newConnection2
/**
 * Name a current connection to the system, which may replace an older connection.
 * Call c.setData(name) just before calling this method.
 * Calls {@link Server#nameConnection(Connection, boolean)} to move the connection
 * from the unnamed to the named connection list.  Increments {@link #numberOfUsers}.
 *<P>
 * If {@code isReplacing}:
 *<UL>
 * <LI> Replaces the old connection with the new one in all its games and channels
 * <LI> Calls {@link SOCClientData#copyClientPlayerStats(SOCClientData)}
 *      for win/loss record and current game and channel count
 * <LI> Sends the old connection an informational disconnect {@link SOCServerPing SOCServerPing(-1)}
 *</UL>
 *
 * @param c  Connected client; its name key ({@link Connection#getData()}) must not be null
 * @param isReplacing  Are we replacing / taking over a current connection?
 * @throws IllegalArgumentException If c isn't already connected, if c.getData() returns null,
 *          or if nameConnection has previously been called for this connection.
 * @since 1.1.08
 */
@Override
public void nameConnection(Connection c, boolean isReplacing) throws IllegalArgumentException {
    Connection oldConn = null;
    if (isReplacing) {
        String cKey = c.getData();
        if (cKey == null)
            throw new IllegalArgumentException("null c.getData");
        oldConn = conns.get(cKey);
        if (oldConn == null)
            // shouldn't happen, but fail gracefully
            isReplacing = false;
    }
    super.nameConnection(c, isReplacing);
    if (isReplacing) {
        gameList.replaceMemberAllGames(oldConn, c);
        channelList.replaceMemberAllChannels(oldConn, c);
        SOCClientData scdNew = (SOCClientData) (c.getAppData());
        SOCClientData scdOld = (SOCClientData) (oldConn.getAppData());
        if ((scdNew != null) && (scdOld != null))
            scdNew.copyClientPlayerStats(scdOld);
        // in case it ever does get its connection back.
        if (oldConn.getVersion() >= 1108)
            oldConn.put(SOCServerPing.toCmd(-1));
    }
    numberOfUsers++;
}
Also used : StringConnection(soc.server.genericServer.StringConnection) Connection(soc.server.genericServer.Connection)

Example 42 with Connection

use of soc.server.genericServer.Connection in project JSettlers2 by jdmonin.

the class SOCServer method destroyChannel.

/**
 * Destroy a channel and then clean up related data, such as the owner's count of
 * {@link SOCClientData#getcurrentCreatedChannels()}.
 * Calls {@link SOCChannelList#deleteChannel(String)}.
 *<P>
 * <B>Note:</B> Caller must send {@link SOCDeleteChannel} message, this method does not do so.
 *<P>
 * <B>Locks:</B> Must have {@link #channelList}{@link SOCChannelList#takeMonitor() .takeMonitor()}
 * before calling this method.
 *
 * @param ch  Name of the channel to destroy
 * @see #leaveChannel(Connection, String, boolean, boolean)
 * @since 1.1.20
 */
protected final void destroyChannel(final String ch) {
    channelList.deleteChannel(ch);
    // Reduce the owner's channels-active count
    Connection oConn = conns.get(channelList.getOwner(ch));
    if (oConn != null)
        ((SOCClientData) oConn.getAppData()).deletedChannel();
}
Also used : StringConnection(soc.server.genericServer.StringConnection) Connection(soc.server.genericServer.Connection)

Example 43 with Connection

use of soc.server.genericServer.Connection in project JSettlers2 by jdmonin.

the class SOCServer method messageToGameKeyedSpecialExcept.

/**
 * Send a localized {@link SOCGameServerText} game text message (with parameters) to a game,
 * optionally with special formatting like <tt>{0,rsrcs}</tt>, optionally excluding some connections.
 * Same as {@link #messageToGame(String, String)} but calls each member connection's
 * {@link Connection#getLocalizedSpecial(SOCGame, String, Object...) c.getLocalizedSpecial(...)} for the
 * localized text to send.
 *<P>
 * Game members with null locale (such as robots) will not be sent the message.
 *
 * @param ga  the game object
 * @param takeMon Should this method take and release
 *                game's monitor via {@link SOCGameList#takeMonitorForGame(String)} ?
 *                True unless caller already holds that monitor.
 * @param ex  the excluded connections, or {@code null}
 * @param key the message localization key, from {@link SOCStringManager#get(String)}, to look up and send text of.
 *            If its localized text begins with ">>>", the client should consider this
 *            an urgent message, and draw the user's attention in some way.
 *            (See {@link #messageToGameUrgent(String, String)})
 * @param params  Objects to use with <tt>{0}</tt>, <tt>{1}</tt>, etc in the localized string
 *             by calling {@link MessageFormat#format(String, Object...)}.
 *             <P>
 *             These objects can include {@link SOCResourceSet} or pairs of
 *             Integers for a resource count and type; see {@link SOCStringManager#getSpecial(SOCGame, String, Object...)}.
 * @throws MissingResourceException if no string can be found for {@code key}; this is a RuntimeException
 * @throws IllegalArgumentException if the localized pattern string has a parse error (closing '}' brace without opening '{' brace, etc)
 * @see #messageToGameKeyedSpecialExcept(SOCGame, boolean, Connection, String, Object...)
 * @see #messageToGameKeyed(SOCGame, boolean, String)
 * @see #messageToGame(String, String)
 * @since 2.0.00
 */
public final void messageToGameKeyedSpecialExcept(SOCGame ga, final boolean takeMon, List<Connection> ex, final String key, final Object... params) throws MissingResourceException, IllegalArgumentException {
    List<Connection> sendTo = gameList.getMembers(ga.getName());
    if ((ex != null) && !ex.isEmpty()) {
        // Copy the members list, then remove the excluded connections.
        // This method isn't called for many situations, so this is efficient enough.
        sendTo = new ArrayList<Connection>(sendTo);
        for (Connection excl : ex) sendTo.remove(excl);
    }
    impl_messageToGameKeyedSpecial(ga, takeMon, sendTo, null, true, key, params);
}
Also used : StringConnection(soc.server.genericServer.StringConnection) Connection(soc.server.genericServer.Connection)

Example 44 with Connection

use of soc.server.genericServer.Connection in project JSettlers2 by jdmonin.

the class SOCGameBoardReset method sortPlayerConnections.

/**
 * Grab connection information for this game's humans and robots.
 * memberConns is from _old_ game, so robots are included.
 * Robots aren't copied to the new game, and must re-join.
 *<P>
 * Two modes:
 *<P>
 * If currently copying a game, assumes newGame is from oldGame via {@link SOCGame#resetAsCopy()},
 * and newGame contains only the human players, oldGame contains all human and robot players.
 *<P>
 * If not copying a game, only inspecting one, then oldGame is null, and assumes newGame has all
 * players (both human and robot).
 *
 * @param newGame New game (if resetting), or only game
 * @param oldGame Old game (if resetting), or null
 * @param memberConns Members of old game, from {@link soc.server.SOCGameListAtServer#getMembers(String)};
 *                   a Vector of {@link Connection}s
 * @param humanConns New array to fill with human players; indexed 0 to SOCGame.MAXPLAYERS-1.
 *                   humanConns[pn] will be the human player at position pn, or null.
 * @param robotConns New array to fill with robot players; indexed 0 to SOCGame.MAXPLAYERS-1.
 *                   robotConns[pn] will be the robot player at position pn, or null.
 *
 * @return The number of human players in newGame
 */
public static int sortPlayerConnections(SOCGame newGame, SOCGame oldGame, Vector<Connection> memberConns, Connection[] humanConns, Connection[] robotConns) {
    // This enum is easier than enumerating all connected clients;
    // there is no server-wide mapping of clientname -> connection.
    int numHuman = 0;
    Enumeration<Connection> playersEnum = memberConns.elements();
    while (playersEnum.hasMoreElements()) {
        Connection pCon = playersEnum.nextElement();
        String pname = pCon.getData();
        SOCPlayer p = newGame.getPlayer(pname);
        if (p != null) {
            int pn = p.getPlayerNumber();
            if (p.isRobot())
                robotConns[pn] = pCon;
            else {
                humanConns[pn] = pCon;
                ++numHuman;
            }
        } else if (oldGame != null) {
            // No such player in new game.
            // Assume is robot player in old game.
            p = oldGame.getPlayer(pname);
            if (p != null) {
                int pn = p.getPlayerNumber();
                if (p.isRobot())
                    robotConns[pn] = pCon;
                else
                    // should not happen
                    D.ebugPrintln("findPlayerConnections assert failed: human player not copied: " + pn);
            }
        }
    }
    // Check all player positions after enum
    for (int pn = 0; pn < newGame.maxPlayers; ++pn) {
        if (!newGame.isSeatVacant(pn)) {
            if ((humanConns[pn] == null) && (robotConns[pn] == null))
                D.ebugPrintln("findPlayerConnections assert failed: did not find player " + pn);
        } else {
            if ((humanConns[pn] != null) || ((robotConns[pn] != null) && ((oldGame == null) || oldGame.isSeatVacant(pn))))
                D.ebugPrintln("findPlayerConnections assert failed: memberlist had vacant player " + pn);
        }
    }
    return numHuman;
}
Also used : Connection(soc.server.genericServer.Connection) SOCPlayer(soc.game.SOCPlayer)

Aggregations

Connection (soc.server.genericServer.Connection)44 StringConnection (soc.server.genericServer.StringConnection)24 SQLException (java.sql.SQLException)8 MissingResourceException (java.util.MissingResourceException)8 SOCGame (soc.game.SOCGame)7 EOFException (java.io.EOFException)5 IOException (java.io.IOException)5 SocketException (java.net.SocketException)5 ArrayList (java.util.ArrayList)5 SOCPlayerElement (soc.message.SOCPlayerElement)5 DBSettingMismatchException (soc.server.database.DBSettingMismatchException)5 SOCPlayer (soc.game.SOCPlayer)4 Date (java.util.Date)3 SOCGameElements (soc.message.SOCGameElements)3 HashSet (java.util.HashSet)2 Hashtable (java.util.Hashtable)2 TimerTask (java.util.TimerTask)2 Vector (java.util.Vector)2 SOCDevCardAction (soc.message.SOCDevCardAction)2 SOCInventoryItemAction (soc.message.SOCInventoryItemAction)2