Package megamek.common.net

Examples of megamek.common.net.Packet


    /**
     * Sends an "add entity" packet
     */
    public void sendAddEntity(Entity entity) {
        checkDuplicateNamesDuringAdd(entity);
        send(new Packet(Packet.COMMAND_ENTITY_ADD, entity));
    }
View Full Code Here


     * Sends an "add squadron" packet
     * This is not working, don't use it
     */
    public void sendAddSquadron(Vector<Entity> fighters) {
        //checkDuplicateNamesDuringAdd(fs);
        send(new Packet(Packet.COMMAND_SQUADRON_ADD, fighters));
    }
View Full Code Here

    /**
     * Sends an "deploy minefields" packet
     */
    public void sendDeployMinefields(Vector<Minefield> minefields) {
        send(new Packet(Packet.COMMAND_DEPLOY_MINEFIELDS, minefields));
    }
View Full Code Here

    /**
     * Sends a "set Artillery Autohit Hexes" packet
     */
    public void sendArtyAutoHitHexes(Vector<Coords> hexes) {
        artilleryAutoHitHexes = hexes; // save for minimap use
        send(new Packet(Packet.COMMAND_SET_ARTYAUTOHITHEXES, hexes));
    }
View Full Code Here

    /**
     * Sends an "update entity" packet
     */
    public void sendUpdateEntity(Entity entity) {
        send(new Packet(Packet.COMMAND_ENTITY_UPDATE, entity));
    }
View Full Code Here

    /**
     * Sends an "update custom initiative" packet
     */
    public void sendCustomInit(Player player) {
        send(new Packet(Packet.COMMAND_CUSTOM_INITIATIVE, player));
    }
View Full Code Here

    /**
     * Sends a "delete entity" packet
     */
    public void sendDeleteEntity(int id) {
        checkDuplicateNamesDuringDelete(id);
        send(new Packet(Packet.COMMAND_ENTITY_REMOVE, new Integer(id)));
    }
View Full Code Here

     */
    public void sendLoadGame(File f) {
        ObjectInputStream ois;
        try {
            ois = new ObjectInputStream(new FileInputStream(f));
            send(new Packet(Packet.COMMAND_LOAD_GAME, new Object[] { f, ois.readObject() }));
        } catch (Exception e) {
            System.out.println("Can't find local savegame "+f);
        }
    }
View Full Code Here

                disconnected();
                connect();
                break;
            case Packet.COMMAND_SERVER_GREETING:
                connected = true;
                send(new Packet(Packet.COMMAND_CLIENT_NAME, name));
                break;
            case Packet.COMMAND_SERVER_CORRECT_NAME:
                correctName(c);
                break;
            case Packet.COMMAND_LOCAL_PN:
View Full Code Here

     *             a valid <code>Packet</code>.
     * @throws <code>NumberFormatException</code> if the value of a numeric
     *             data element is not in a valid format.
     */
    public static Packet decode(ParsedXML node, IGame game) {
        Packet packet = null;
        int command = 0;
        Object[] data = null;

        // Make sure we got a valid packet.
        if (null == node) {
            throw new IllegalArgumentException("The passed node is null.");
        }
        if (!node.getName().equals("packet")) {
            throw new IllegalStateException(
                    "The passed node is not for a packet.");
        }

        // Figure out what type of packet this is.
        String commandStr = node.getAttribute("type");
        if (null == commandStr) {
            throw new IllegalStateException(
                    "Could not determine the packet type.");
        }
        command = Integer.parseInt(commandStr);

        // TODO : perform version checking.

        // Walk the packet node's children. Try to find a "packetData" node.
        Enumeration<?> children = node.elements();
        while (children.hasMoreElements()) {
            ParsedXML subNode = (ParsedXML) children.nextElement();
            if (subNode.getName().equals("packetData")) {

                // How many data elements are in the packet data?
                final int count = Integer.parseInt(subNode
                        .getAttribute("count"));
                data = new Object[count];

                // Do we need to unzip the data elements?
                Enumeration<?> dataElements = null;
                if (subNode.getAttribute("isGzipped").equals("true")) {

                    // Try to find the zipped content.
                    String cdata = subNode.getContent();
                    if (null == cdata) {
                        Enumeration<?> cdataEnum = subNode.elements();
                        while (cdataEnum.hasMoreElements() && null == cdata) {
                            final ParsedXML cdataNode = (ParsedXML) cdataEnum
                                    .nextElement();
                            if (cdataNode.getTypeName().equals("text")) {
                                cdata = cdataNode.getContent();
                            } else if (cdataNode.getTypeName().equals("cdata")) {
                                cdata = cdataNode.getContent();
                            }
                        }
                    } // End look-for-cdata-nodes

                    // Did we find the zipped content?
                    if (null == cdata) {
                        throw new IllegalStateException(
                                "Could not find CDATA for packetData.");
                    }

                    // Yup. Unencode the data from Base64.
                    byte[] unBase64 = Base64.decodeToBytes(cdata);
                    InputStream parseStream;
                    try {
                        // Unzip the data.
                        parseStream = new GZIPInputStream(
                                new ByteArrayInputStream(unBase64));
                    } catch (IOException ioErr) {
                        StringBuffer iobuf = new StringBuffer();
                        iobuf.append("Could not unzip data elements: ").append(
                                ioErr.getMessage());
                        throw new IllegalStateException(iobuf.toString());
                    }

                    try {
                        /*******************************************************
                         * BEGIN debug code try { parseStream.mark(64000); int
                         * inChar = 0; while ( -1 != (inChar =
                         * parseStream.read()) ) { System.out.print( (char)
                         * inChar ); } System.out.println( "" );
                         * parseStream.reset(); } catch ( IOException debugErr ) {
                         * debugErr.printStackTrace(); } END debug code *
                         ******************************************************/

                        // Parse the XML.
                        ParsedXML dummyNode = TinyParser.parseXML(parseStream);
                        dataElements = dummyNode.elements();
                    } catch (ParseException parseErr) {
                        StringBuffer parsebuf = new StringBuffer();
                        parsebuf.append("Could not parse data elements: ")
                                .append(parseErr.getMessage());
                        throw new IllegalStateException(parsebuf.toString());
                    }
                } else {
                    // Nope. Just return the data elements.
                    dataElements = subNode.elements();
                }

                // Walk the children, and decode them into the data array.
                for (int loop = 0; dataElements.hasMoreElements(); loop++) {
                    data[loop] = PacketEncoder.decodeData(
                            (ParsedXML) dataElements.nextElement(), game);
                }

            } // End found-packetData-element

        } // Check the next child of the packet node.

        // Create and return the packet.
        if (null != data) {
            packet = new Packet(command, data);
        } else {
            packet = new Packet(command);
        }
        return packet;
    }
View Full Code Here

TOP

Related Classes of megamek.common.net.Packet

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.