⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gossip.java

📁 JGRoups源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        clearPanel();                        continue;                    default:                        System.err.println("***** Gossip.run(): received invalid draw command " + comm.mode);                        break;                }            }            catch(ChannelNotConnectedException not) {                System.err.println("Gossip: " + not);                break;            }            catch(ChannelClosedException closed) {                System.err.println("Gossip: channel was closed");                break;            }            catch(Exception e) {                System.err.println(e);                continue; // break;            }        }    }    /* --------------- Callbacks --------------- */    public void mouseMoved(MouseEvent e) {    }    public void clearPanel() {        Rectangle bounds=null;        if(panel == null || graphics == null)            return;        bounds=panel.getBounds();        graphics.clearRect(0, 0, bounds.width, bounds.height);    }    public void colorPanel(int r, int g, int b) {        if(graphics != null) {            red=r;            green=g;            blue=b;            graphics.setColor(new Color(red, green, blue));            Rectangle bounds=panel.getBounds();            graphics.fillRect(0, 0, bounds.width, bounds.height);            graphics.setColor(default_color);        }    }    void sendGossip() {        int tmp[]=new int[1];        tmp[0]=0;        Command comm;        ObjectOutputStream os;        Vector dests=(Vector)members.clone();        try {            selectColor();  // set a new randomly chosen color            dests.removeElement(local_addr);            dests=Util.pickSubset(dests, subset);            if(dests == null || dests.size() == 0) {  // only apply new color locally                // System.out.println("-- local");                colorPanel(red, green, blue);                return;            }            colorPanel(red, green, blue);            comm=new Command(Command.GOSSIP, red, green, blue);            comm.not_seen=(Vector)members.clone();            comm.not_seen.removeElement(local_addr);            out.reset();            os=new ObjectOutputStream(out);            os.writeObject(comm);            os.flush();            for(int i=0; i < dests.size(); i++) {                channel.send(new Message((Address)dests.elementAt(i), null, out.toByteArray()));            }        }        catch(Exception ex) {            System.err.println(ex);        }    }    public void sendClearPanelMsg() {        int tmp[]=new int[1];        tmp[0]=0;        Command comm=new Command(Command.CLEAR);        ObjectOutputStream os;        try {            out.reset();            os=new ObjectOutputStream(out);            os.writeObject(comm);            os.flush();            channel.send(new Message(null, null, out.toByteArray()));        }        catch(Exception ex) {            System.err.println(ex);        }    }    public void windowActivated(WindowEvent e) {    }    public void windowClosed(WindowEvent e) {    }    public void windowClosing(WindowEvent e) {        System.exit(0);  // exit the dirty way ...    }    public void windowDeactivated(WindowEvent e) {    }    public void windowDeiconified(WindowEvent e) {    }    public void windowIconified(WindowEvent e) {    }    public void windowOpened(WindowEvent e) {    }    public void actionPerformed(ActionEvent e) {        String command=e.getActionCommand();        if("Gossip".equals(command)) {            sendGossip();        }        else            if("Clear".equals(command))                sendClearPanelMsg();            else                if("Leave & Exit".equals(command)) {                    try {                        channel.disconnect();                        channel.close();                    }                    catch(Exception ex) {                        System.err.println(ex);                    }                    mainFrame.setVisible(false);                    System.exit(0);                }                else                    System.out.println("Unknown action");    }    public void channelConnected(Channel channel) {        if(first)            first=false;        else            startThread();    }    public void channelDisconnected(Channel channel) {        // System.out.println("----> channelDisconnected()");    }    public void channelClosed(Channel channel) {        // System.out.println("----> channelClosed()");    }    public void channelShunned() {        System.out.println("----> channelShunned()");    }    public void channelReconnected(Address new_addr) {        System.out.println("----> channelReconnected(" + new_addr + ')');        local_addr=new_addr;    }    private static class Command implements Serializable {        static final int GOSSIP=1;        static final int CLEAR=2;        final int mode;        int r=0;        int g=0;        int b=0;        Vector not_seen=new Vector();        Command(int mode) {            this.mode=mode;        }        Command(int mode, int r, int g, int b) {            this.mode=mode;            this.r=r;            this.g=g;            this.b=b;        }        public String toString() {            StringBuffer ret=new StringBuffer();            switch(mode) {                case GOSSIP:                    ret.append("GOSSIP(" + r + '|' + g + '|' + b);                    break;                case CLEAR:                    ret.append("CLEAR");                    break;                default:                    return "<undefined>";            }            ret.append(", not_seen=" + not_seen);            return ret.toString();        }    }    private class TrafficGenerator implements Runnable {        Thread generator=null;        public void start() {            if(generator == null) {                generator=new Thread(this, "TrafficGeneratorThread");                generator.start();            }        }        public void stop() {            if(generator != null)                generator=null;            generator=null;        }        public void run() {            while(generator != null) {                Util.sleep(traffic_interval);                if(generator != null)                    sendGossip();            }        }    }    private class MyPanel extends JPanel {        final Dimension preferred_size=new Dimension(200, 200);        public Dimension getPreferredSize() {            return preferred_size;        }    }}

⌨️ 快捷键说明

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