コード例 #1
0
  /**
   * Constructor 1
   *
   * @param props
   * @param no_channel
   * @param jmx
   * @param use_state
   * @param state_timeout
   * @param use_unicasts
   * @param name
   * @param send_own_state_on_merge
   * @param gen
   * @throws Exception
   */
  public JWhiteBoard(
      String props,
      boolean no_channel,
      boolean jmx,
      boolean use_state,
      long state_timeout,
      boolean use_unicasts,
      String name,
      boolean send_own_state_on_merge,
      AddressGenerator gen)
      throws Exception {
    this.noChannel = no_channel;
    this.jmx = jmx;
    this.useState = use_state;
    this.stateTimeout = state_timeout;
    this.use_unicasts = use_unicasts;
    if (no_channel) return;

    channel = new JChannel(props);
    if (gen != null) channel.addAddressGenerator(gen);
    if (name != null) channel.setName(name);
    channel.setReceiver(this);
    channel.addChannelListener(this);
    this.send_own_state_on_merge = send_own_state_on_merge;
  }
コード例 #2
0
  /** Execute when new member join or leave Group */
  public void viewAccepted(View v) {
    memberSize = v.size();
    if (mainFrame != null) setTitle();
    members.clear();
    members.addAll(v.getMembers());

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

      // This is a simple merge function, which fetches the state from the coordinator
      // on a merge and overwrites all of its own state
      if (useState && !members.isEmpty()) {
        Address coord = members.get(0);
        Address local_addr = channel.getAddress();
        if (local_addr != null && !local_addr.equals(coord)) {
          try {

            // make a copy of our state first
            Map<Point, Color> copy = null;
            if (send_own_state_on_merge) {
              synchronized (drawPanel.state) {
                copy = new LinkedHashMap<Point, Color>(drawPanel.state);
              }
            }
            System.out.println("fetching state from " + coord);
            channel.getState(coord, 5000);
            if (copy != null)
              sendOwnState(copy); // multicast my own state so everybody else has it too
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    } else System.out.println("** View=" + v);
  }
コード例 #3
0
  /**
   * Init JWhiteBoard interface
   *
   * @throws Exception
   */
  public void go() throws Exception {
    if (!noChannel && !useState) channel.connect(groupName);
    mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    drawPanel = new DrawPanel(useState);
    drawPanel.setBackground(backgroundColor);
    subPanel = new JPanel();
    mainFrame.getContentPane().add("Center", drawPanel);
    clearButton = new JButton("Clean");
    clearButton.setFont(defaultFont);
    clearButton.addActionListener(this);
    leaveButton = new JButton("Exit");
    leaveButton.setFont(defaultFont);
    leaveButton.addActionListener(this);
    subPanel.add("South", clearButton);
    subPanel.add("South", leaveButton);
    mainFrame.getContentPane().add("South", subPanel);
    mainFrame.setBackground(backgroundColor);
    clearButton.setForeground(Color.blue);
    leaveButton.setForeground(Color.blue);
    mainFrame.pack();
    mainFrame.setLocation(15, 25);
    mainFrame.setBounds(new Rectangle(250, 250));

    if (!noChannel && useState) {
      channel.connect(groupName, null, stateTimeout);
    }
    mainFrame.setVisible(true);
  }
コード例 #4
0
 /**
  * Constructor 3
  *
  * @param channel
  * @param use_state: Save state of Group
  * @param state_timeout: State time out
  * @throws Exception
  */
 public JWhiteBoard(JChannel channel, boolean use_state, long state_timeout) throws Exception {
   this.channel = channel;
   channel.setReceiver(this);
   channel.addChannelListener(this);
   this.useState = use_state;
   this.stateTimeout = state_timeout;
 }
コード例 #5
0
ファイル: TotalTokenDemo.java プロジェクト: shahidminhas/abc
 public void connect() {
   try {
     channel.connect("TOTAL_TOKEN_DEMO_GROUP");
   } catch (ChannelException e) {
     e.printStackTrace();
   }
   receiverThread = new ReceiverThread();
   receiverThread.start();
   Address a = channel.getAddress();
   if (a != null) setTitle(a.toString());
   else setTitle("Not connected");
   control.connected();
 }
コード例 #6
0
 /**
  * Set Frame's title
  *
  * @param title : frame's title
  */
 void setTitle(String title) {
   String tmp = "";
   if (noChannel) {
     mainFrame.setTitle("JWhiteBoard");
     return;
   }
   if (title != null) {
     mainFrame.setTitle(title);
   } else {
     if (channel.getAddress() != null) tmp += channel.getAddress();
     tmp += " (" + memberSize + ")";
     mainFrame.setTitle(tmp);
   }
 }
コード例 #7
0
ファイル: TotalTokenDemo.java プロジェクト: shahidminhas/abc
 public void disconnect() {
   transmitting = false;
   receiverThread.shutDown();
   channel.disconnect();
   control.disconnected();
   setTitle("Not connected");
 }
コード例 #8
0
  /** When receive a message, analyze message content and then execute the command: Draw or Clear */
  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 (drawPanel != null) drawPanel.drawPoint(comm);
          break;
        case DrawCommand.CLEAR:
          clearPanel();
        default:
          System.err.println("***** received invalid draw command " + comm.mode);
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #9
0
 /** Send Clear command to all members in Group */
 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);
   }
 }
コード例 #10
0
 /** Leave Group and close JWhiteBoard */
 public void stop() {
   if (!noChannel) {
     try {
       channel.close();
     } catch (Exception ex) {
       System.err.println(ex);
     }
   }
   mainFrame.setVisible(false);
   mainFrame.dispose();
 }
コード例 #11
0
 /**
  * Send State (content on WhiteBoard) to all members of Group
  *
  * @param copy
  */
 protected void sendOwnState(final Map<Point, Color> copy) {
   if (copy == null) return;
   for (Point point : copy.keySet()) {
     // we don't need the color: it is our draw_color anyway
     DrawCommand comm = new DrawCommand(DrawCommand.DRAW, point.x, point.y, drawColor.getRGB());
     try {
       byte[] buf = Util.streamableToByteBuffer(comm);
       if (use_unicasts) sendToAll(buf);
       else channel.send(new Message(null, buf));
     } catch (Exception ex) {
       System.err.println(ex);
     }
   }
 }
コード例 #12
0
ファイル: TotalTokenDemo.java プロジェクト: shahidminhas/abc
 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);
   }
 }
コード例 #13
0
ファイル: TotalTokenDemo.java プロジェクト: shahidminhas/abc
  public void run() {
    Random r = new Random();

    while (true) {
      Util.sleep(10);
      try {
        if (transmitting) {
          channel.send(new Message(null, null, new TotalPayload(r.nextInt(255))));
        } else {
          Util.sleep(200);
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
コード例 #14
0
 /**
  * Constructor 2
  *
  * @param channel
  * @throws Exception
  */
 public JWhiteBoard(JChannel channel) throws Exception {
   this.channel = channel;
   channel.setReceiver(this);
   channel.addChannelListener(this);
 }
コード例 #15
0
 /**
  * Send message to members in members list only.
  *
  * @param buf
  * @throws Exception
  */
 private void sendToAll(byte[] buf) throws Exception {
   for (Address mbr : members) channel.send(new Message(mbr, buf));
 }