Package megamek.common.net

Examples of megamek.common.net.IConnection


        } catch (IOException ex) {
        }

        // kill pending connnections
        for (Enumeration<IConnection> connEnum = connectionsPending.elements();connEnum.hasMoreElements();) {
            IConnection conn = connEnum.nextElement();
            conn.close();
        }
        connectionsPending.removeAllElements();

        // Send "kill" commands to all connections
        // N.B. I may be starting a race here.
        for (Enumeration<IConnection> connEnum = connections.elements();connEnum.hasMoreElements();) {
            IConnection conn = connEnum.nextElement();
            send(conn.getId(), new Packet(Packet.COMMAND_CLOSE_CONNECTION));
        }

        // kill active connnections
        for (Enumeration<IConnection> connEnum = connections.elements();connEnum.hasMoreElements();) {
            IConnection conn = connEnum.nextElement();
            conn.close();
        }

        connections.removeAllElements();
        connectionIds.clear();
        System.out.flush();
View Full Code Here


    /**
     * Recieves a player name, sent from a pending connection, and connects that
     * connection.
     */
    private void receivePlayerName(Packet packet, int connId) {
        final IConnection conn = getPendingConnection(connId);
        String name = (String) packet.getObject(0);
        boolean returning = false;

        // this had better be from a pending connection
        if (conn == null) {
            System.out.println("server: got a client name from a non-pending" + " connection");
            return;
        }

        // check if they're connecting with the same name as a ghost player
        for (Enumeration<Player> i = game.getPlayers(); i.hasMoreElements();) {
            Player player = i.nextElement();
            if (player.getName().equals(name)) {
                if (player.isGhost()) {
                    returning = true;
                    player.setGhost(false);
                    // switch id
                    connId = player.getId();
                    conn.setId(connId);
                }
            }
        }

        if (!returning) {
            // Check to avoid duplicate names...
            name = correctDupeName(name);
            send(connId, new Packet(Packet.COMMAND_SERVER_CORRECT_NAME, name));
        }

        // right, switch the connection into the "active" bin
        connectionsPending.removeElement(conn);
        connections.addElement(conn);
        connectionIds.put(new Integer(conn.getId()), conn);

        // add and validate the player info
        if (!returning) {
            int team = Player.TEAM_NONE;
            for (Player p : game.getPlayersVector()) {
View Full Code Here

    private void send(Packet packet) {
        if (connections == null) {
            return;
        }
        for (Enumeration<IConnection> connEnum = connections.elements();connEnum.hasMoreElements();) {
            IConnection conn = connEnum.nextElement();
            conn.send(packet);
        }
    }
View Full Code Here

        if (connections == null) {
            return;
        }

        for (Enumeration<IConnection> connEnum = connections.elements();connEnum.hasMoreElements();) {
            IConnection conn = connEnum.nextElement();
            Player p = game.getPlayer(conn.getId());
            Packet packet;
            if (tacticalGeniusReport) {
                packet = createTacticalGeniusReportPacket();
            } else {
                packet = createReportPacket(p);
            }
            conn.send(packet);
        }
    }
View Full Code Here

        }
        // act on it
        switch (packet.getCommand()) {
        case Packet.COMMAND_CLOSE_CONNECTION:
            // We have a client going down!
            IConnection c = getConnection(connId);
            if (c != null) {
                c.close();
            }
            break;
        case Packet.COMMAND_CLIENT_NAME:
            receivePlayerName(packet, connId);
            break;
View Full Code Here

                Socket s = serverSocket.accept();

                int id = getFreeConnectionId();
                System.out.println("s: accepting player connection #" + id + " ...");

                IConnection c = ConnectionFactory.getInstance().createServerConnection(s, id);
                c.addConnectionListener(connectionListener);
                c.open();
                connectionsPending.addElement(c);

                greeting(id);
                ConnectionWatchdog w = new ConnectionWatchdog(this, id);
                timer.schedule(w, 1000, 500);
View Full Code Here

     * Resets all the connections. The only use of this right now is when games are loaded after
     * clients have connected
     */
    public void resetConnections() {
        for (Enumeration<IConnection> connEnum = connections.elements();connEnum.hasMoreElements();) {
            IConnection conn = connEnum.nextElement();
            send(conn.getId(), new Packet(Packet.COMMAND_RESET_CONNECTION));
        }
    }
View Full Code Here

                message.append(" ");
                message.append(args[pos]);
            }

            for (Enumeration<IConnection> i = server.getConnections(); i.hasMoreElements();) {
                IConnection conn = i.nextElement();

                if (server.getPlayer(conn.getId()).getTeam() == team)
                    server.sendChat(conn.getId(), origin, message.toString());
            }
        }
    }
View Full Code Here

            conn = ConnectionFactory.getInstance().createServerConnection(
                    new Socket(host, port), 1);
            Timer t = new Timer(true);
            final Runnable packetUpdate = new Runnable() {
                public void run() {
                    IConnection connection = PacketTool.this.conn;
                    if (connection != null)
                        connection.update();
                }
            };
            final TimerTask packetUpdate2 = new TimerTask() {
                public void run() {
                    try {
View Full Code Here

        server.sendServerChat(connId, "Listing all connections...");
        server
                .sendServerChat(connId,
                        "[id#] : [name], [address], [pending], [bytes sent], [bytes received]");
        for (Enumeration<IConnection> i = server.getConnections(); i.hasMoreElements();) {
            IConnection conn = i.nextElement();
            StringBuffer cb = new StringBuffer();
            cb.append(conn.getId()).append(" : ");
            cb.append(server.getPlayer(conn.getId()).getName()).append(", ");
            cb.append(conn.getInetAddress());
            cb.append(", ").append(conn.hasPending()).append(", ");
            cb.append(conn.bytesSent());
            cb.append(", ").append(conn.bytesReceived());
            server.sendServerChat(connId, cb.toString());
        }
        server.sendServerChat(connId, "end list");
    }
View Full Code Here

TOP

Related Classes of megamek.common.net.IConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.