📄 packettool.java
字号:
/* * MegaMek - Copyright (C) 2003,2004 Ben Mazur (bmazur@sev.org) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */package megamek.test;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.Socket;import java.net.ServerSocket;import megamek.common.Board;import megamek.common.net.Connection;import megamek.common.net.ConnectionFactory;import megamek.common.net.ConnectionListenerAdapter;import megamek.common.net.DisconnectedEvent;import megamek.common.net.Packet;import megamek.common.net.PacketReceivedEvent;/** * This class provides an AWT GUI for testing the transmission * and reception of <code>Packet</code>s. * * @author James Damour <suvarov454@users.sourceforge.net> */public class PacketTool extends Frame implements Runnable { /** * The currently-loaded <code>Board</code>. May be <code>null</code>. */ private Board board = null; /** * The panel containing the connection controls. */ private Panel panConnect = null; /** * The panel containing the transimission controls. */ private Panel panXmit = null; /** * The text control where the host name is entered. */ private TextField hostName = null; /** * The text control where the host port is entered. */ private TextField hostPort = null; /** * The label where the board name is displayed. */ private Label boardName = null; /** * The name of the most-recently-created board file. * May be <code>null</code>. */ private String fileName = null; /** * The button that sends the loaded board. */ private Button butSend = null; /** * The connection to the other peer. */ private Connection conn = null; /** * Display a window for testing the transmission of boards. */ public static void main( String[] args ) { Frame frame = new PacketTool(); // set visible on middle of screen Dimension screenSize = frame.getToolkit().getScreenSize(); frame.pack(); frame.setLocation( screenSize.width / 2 - frame.getSize().width / 2, screenSize.height / 2 - frame.getSize().height / 2); // Show the window. frame.setVisible(true); } /** * Create a window for testing the transmission of boards. * The window will <em>not</em> be displayed by this constructor; * calling functions should call <code>setVisible(true)</code>. */ public PacketTool() { super( "Board Transmition" ); Button button = null; Panel main = null; // Handle the frame stuff. this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { quit(); } } ); // Create the panels. main = new Panel(); main.setLayout( new GridLayout(0,1) ); panConnect = new Panel(); panConnect.setLayout( new GridLayout(0,2) ); main.add( panConnect ); panXmit = new Panel(); panXmit.setLayout( new GridLayout(0,1) ); panXmit.setEnabled( false ); main.add( panXmit ); this.add( main ); // Populate the connection panel. panConnect.add( new Label(" Connect To:") ); hostName = new TextField( "localhost", 10 ); panConnect.add( hostName ); panConnect.add( new Label("Port Number:") ); hostPort = new TextField( "2346", 10 ); panConnect.add( hostPort ); button = new Button( "Listen" ); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { (new Thread(PacketTool.this, "Packet Reader")).start(); } } ); panConnect.add( button ); button = new Button( "Connect" ); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { connect(); } } ); panConnect.add( button ); // Populate the transmission panel. button = new Button( "Load Board" ); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { boardLoad(); } } ); panXmit.add( button ); boardName = new Label(); boardName.setAlignment( Label.CENTER ); panXmit.add( boardName ); butSend = new Button( "Send" ); butSend.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { send(); } } ); butSend.setEnabled( false ); panXmit.add( butSend ); } /** * Close this window. */ public synchronized void quit() { if ( null != this.conn ) { conn.close(); } System.exit(0); } /** * Connect to the specified host. */ public void connect() { String host = hostName.getText(); int port = 0; try { port = Integer.parseInt( hostPort.getText() ); conn = ConnectionFactory.getInstance().createServerConnection(new Socket(host, port), 1 ); //conn = new XmlConnection( this, new Socket(host, port), 1 ); System.out.println( "Connected to peer." ); conn.addConnectionListener(connectionListener); board = new Board(); panConnect.setEnabled( false ); panXmit.setEnabled( true ); } catch ( Throwable err ) { err.printStackTrace(); } } /** * Load a board from a file. */ public void boardLoad() { FileDialog fd = new FileDialog(this, "Load Board...", FileDialog.LOAD); fd.setDirectory("data" + File.separator + "boards"); if ( boardName.getText().length() > 0 ) { fd.setFile( boardName.getText() ); } fd.setLocation(this.getLocation().x + 150, this.getLocation().y + 100); fd.setVisible(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -