totaltokendemo.java

来自「JGRoups源码」· Java 代码 · 共 566 行 · 第 1/2 页

JAVA
566
字号
                catch (TimeoutException e)                {                    e.printStackTrace();                }            }        }    }    public static class TotalPayload implements Serializable    {        private final int seqRandom;        public TotalPayload(int random)        {            seqRandom = random;        }        public int getRandomSequence()        {            return seqRandom;        }    }    class TransmitAction extends AbstractAction    {        private static final String TRANSMIT_OFF = "transmit off";        private static final String TRANSMIT_ON = "transmit on";        TransmitAction()        {            putValue(NAME, TRANSMIT_OFF);        }        public void actionPerformed(ActionEvent e)        {            if (getValue(NAME) == TRANSMIT_OFF)            {                putValue(NAME, TRANSMIT_ON);                transmitting = true;            }            else            {                putValue(NAME, TRANSMIT_OFF);                transmitting = false;            }        }    }    class ControlPanel extends JPanel    {        private static final String DISCONNECT = "Disconnect";        private static final String CONNECT = "Connect";        final JTextField numMessagesInLastView;        final JTextField throughput;        final JTextField viewNumber;        final JTextField messageSize;        final JTextField state;        final JButton transmit;        final JButton connectButton;        JTabbedPane pane;        public ControlPanel()        {            super();            //Layout the labels in a panel            JPanel labelPane = new JPanel();            labelPane.setLayout(new GridLayout(0, 1));            labelPane.add(new JLabel("Message size"));            labelPane.add(new JLabel("Current view"));            labelPane.add(new JLabel("Throughput"));            labelPane.add(new JLabel("Last view count"));            colorPanel = new ColorPanel();            connectButton = new JButton(DISCONNECT);            connectButton.addActionListener(new ActionListener()            {                public void actionPerformed(ActionEvent e)                {                    JButton b = (JButton) e.getSource();                    String current_state = b.getText();                    if (CONNECT.equals(current_state))                    {                        connect();                    }                    else if (DISCONNECT.equals(current_state))                    {                        disconnect();                    }                }            });            transmit = new JButton(new TransmitAction());            labelPane.add(connectButton);            labelPane.add(transmit);            int size = 10;            messageSize = new JTextField(size);            messageSize.setText("" + mSize);            messageSize.addActionListener(new ActionListener()            {                /**                 * Invoked when an action occurs.                 */                public void actionPerformed(ActionEvent e)                {                    mSize = Integer.parseInt(messageSize.getText());                }            });            viewNumber = new JTextField(size);            viewNumber.setEditable(false);            throughput = new JTextField(size);            throughput.setEditable(false);            numMessagesInLastView = new JTextField(size);            numMessagesInLastView.setEditable(false);            state = new JTextField(size);            state.setEditable(false);            //Layout the text fields in a panel            JPanel fieldPane = new JPanel();            fieldPane.setLayout(new GridLayout(0, 1));            fieldPane.add(messageSize);            fieldPane.add(viewNumber);            fieldPane.add(throughput);            fieldPane.add(numMessagesInLastView);            fieldPane.add(state);            fieldPane.add(colorPanel);            //Put the panels in another panel, labels on left,            //text fields on right            JPanel contentPane = new JPanel();            contentPane.setBorder(BorderFactory.createTitledBorder("Control"));            contentPane.setLayout(new BorderLayout());            contentPane.add(labelPane, BorderLayout.CENTER);            contentPane.add(fieldPane, BorderLayout.EAST);            this.setLayout(new BorderLayout());            this.add(contentPane);        }        public void connected()        {            connectButton.setText(DISCONNECT);            state.setText("connected ok");        }        public void disconnected()        {            connectButton.setText(CONNECT);            state.setText("disconnected ok");        }    }    class ColorPanel extends JPanel    {        long seq1,seq2,seq3;        public ColorPanel()        {            super();            setOpaque(false);            this.setLayout(new BorderLayout());            //setBorder(BorderFactory.createLineBorder(Color.black));        }        public Dimension getPreferredSize()        {            Dimension layoutSize = super.getPreferredSize();            int max = Math.max(layoutSize.width, layoutSize.height);            return new Dimension(max + 20, max + 20);        }        public void setSeq(long seq1, long seq2, long seq3)        {            this.seq1 = seq1;            this.seq2 = seq2;            this.seq3 = seq3;            this.repaint();        }        protected void paintComponent(Graphics g)        {            Dimension size = this.getSize();            int x = 0;            int y = 0;            g.setColor(new Color((int) seq1, (int) seq2, (int) seq3));            g.fillRect(x, y, size.width, size.height);        }    }    class StackPanel extends JPanel    {        final ProtocolStack stack;        public StackPanel(JChannel channel)        {            super();            setBorder(BorderFactory.createTitledBorder("ProtocolStack"));            this.setLayout(new GridLayout(0, 2));            this.stack = channel.getProtocolStack();            Iterator iter = stack.getProtocols().iterator();            String debugLevels [] = new String[]{"DEBUG","INFO","ERROR"};            while (iter.hasNext())            {                Protocol p = (Protocol) iter.next();                JLabel field = new JLabel(p.getName());                JComboBox pane = new JComboBox(debugLevels);                this.add(field);                this.add(pane);            }        }    }    static void help()    {        System.out.println("\nTotalTokenDemo [-help] [-props <protocol stack definition>]");        System.out.println("-props: argument can be an old-style protocol stack specification, or it can be " +                           "a URL. In the latter case, the protocol specification will be read from the URL\n");    }    public static void main(String args[])    {        String props = null;        for (int i = 0; i < args.length; i++)        {            if ("-help".equals(args[i]))            {                help();                return;            }            if ("-props".equals(args[i]))            {                props = args[++i];                continue;            }            help();            return;        }        if (props == null)        {            props = "UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +                    "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +                    "PING(timeout=2000;num_initial_members=5):" +                    "FD_SOCK:" +                    "VERIFY_SUSPECT(timeout=1500):" +                    "UNICAST(timeout=5000):" +                    "FRAG(frag_size=8192;down_thread=false;up_thread=false):" +                    "TOTAL_TOKEN(block_sending=50;unblock_sending=10):" +                    "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +                    "shun=false;print_local_addr=true)";        }        TotalTokenDemo ttd = new TotalTokenDemo(props);        //StackPanel not_done_yet = new StackPanel(ttd.getChannel());        //ttd.addPanel("Debug", not_done_yet);        ttd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        ttd.pack();        ttd.show();        new Thread(ttd).start();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?