Beispiel #1
0
  public void viewAccepted(View v) {
    member_size = v.size();
    if (mainFrame != null) setTitle();
    members.clear();
    members.addAll(v.getMembers());

    if (v instanceof MergeView) {
      System.out.println("** MergeView=" + v);

      // This is an example of a simple merge function, which fetches the state from the coordinator
      // on a merge and overwrites all of its own state
      if (use_state && !members.isEmpty()) {
        Address coord = members.get(0);
        Address local_addr = channel.getAddress();
        if (local_addr != null && !local_addr.equals(coord)) {
          try {
            System.out.println("fetching state from " + coord);
            channel.getState(coord, 5000);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    } else System.out.println("** View=" + v);
  }
Beispiel #2
0
  public void go() throws Exception {
    if (!no_channel && !use_state) {
      channel.connect(groupname);
    }
    mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new DrawPanel(use_state);
    panel.setBackground(background_color);
    sub_panel = new JPanel();
    mainFrame.getContentPane().add("Center", panel);
    clear_button = new JButton("Clear");
    clear_button.setFont(default_font);
    clear_button.addActionListener(this);
    leave_button = new JButton("Leave");
    leave_button.setFont(default_font);
    leave_button.addActionListener(this);
    sub_panel.add("South", clear_button);
    sub_panel.add("South", leave_button);
    mainFrame.getContentPane().add("South", sub_panel);
    mainFrame.setBackground(background_color);
    clear_button.setForeground(Color.blue);
    leave_button.setForeground(Color.blue);
    mainFrame.pack();
    mainFrame.setLocation(15, 25);
    mainFrame.setBounds(new Rectangle(250, 250));

    if (!no_channel && use_state) {
      channel.connect(groupname, null, null, state_timeout);
    }
    mainFrame.setVisible(true);
    setTitle();
  }
Beispiel #3
0
 public Draw(Channel channel, boolean use_state, long state_timeout) throws Exception {
   this.channel = channel;
   channel.setReceiver(this);
   channel.addChannelListener(this);
   this.use_state = use_state;
   this.state_timeout = state_timeout;
 }
Beispiel #4
0
  public void start(String props) throws Exception {

    channel = new JChannel(props);
    channel.setReceiver(this);
    channel.connect("ViewDemo");

    while (true) {
      Util.sleep(10000);
    }
  }
Beispiel #5
0
 void setTitle(String title) {
   String tmp = "";
   if (no_channel) {
     mainFrame.setTitle(" Draw Demo ");
     return;
   }
   if (title != null) {
     mainFrame.setTitle(title);
   } else {
     if (channel.getAddress() != null) tmp += channel.getAddress();
     tmp += " (" + member_size + ")";
     mainFrame.setTitle(tmp);
   }
 }
Beispiel #6
0
  public void receive(Message msg) {
    byte[] buf = msg.getRawBuffer();
    if (buf == null) {
      System.err.println(
          "["
              + channel.getAddress()
              + "] received null buffer from "
              + msg.getSrc()
              + ", headers: "
              + msg.printHeaders());
      return;
    }

    try {
      DrawCommand comm =
          (DrawCommand)
              Util.streamableFromByteBuffer(
                  DrawCommand.class, buf, msg.getOffset(), msg.getLength());
      switch (comm.mode) {
        case DrawCommand.DRAW:
          if (panel != null) panel.drawPoint(comm);
          break;
        case DrawCommand.CLEAR:
          clearPanel();
          break;
        default:
          System.err.println("***** received invalid draw command " + comm.mode);
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Beispiel #7
0
 public void stop() {
   if (!no_channel) {
     try {
       channel.close();
     } catch (Exception ex) {
       System.err.println(ex);
     }
   }
   mainFrame.setVisible(false);
   mainFrame.dispose();
 }
Beispiel #8
0
  public void sendClearPanelMsg() {
    DrawCommand comm = new DrawCommand(DrawCommand.CLEAR);

    try {
      byte[] buf = Util.streamableToByteBuffer(comm);
      if (use_unicasts) sendToAll(buf);
      else channel.send(new Message(null, null, buf));
    } catch (Exception ex) {
      System.err.println(ex);
    }
  }
Beispiel #9
0
  public Draw(
      String props,
      boolean no_channel,
      boolean jmx,
      boolean use_state,
      long state_timeout,
      boolean use_blocking,
      boolean use_unicasts,
      String name)
      throws Exception {
    this.no_channel = no_channel;
    this.jmx = jmx;
    this.use_state = use_state;
    this.state_timeout = state_timeout;
    this.use_unicasts = use_unicasts;
    if (no_channel) return;

    channel = new JChannel(props);
    if (name != null) channel.setName(name);
    if (use_blocking) channel.setOpt(Channel.BLOCK, Boolean.TRUE);
    channel.setReceiver(this);
    channel.addChannelListener(this);
  }
Beispiel #10
0
 public Draw(Channel channel) throws Exception {
   this.channel = channel;
   channel.setReceiver(this);
   channel.addChannelListener(this);
 }
Beispiel #11
0
 private void sendToAll(byte[] buf) throws Exception {
   for (Address mbr : members) {
     Message msg = new Message(mbr, null, buf);
     channel.send(msg);
   }
 }