Ejemplo n.º 1
0
 public static void main(String[] args) {
   try {
     new UnicastChannelTest().start(args);
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Ejemplo n.º 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);
  }
Ejemplo n.º 3
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();
    }
  }
Ejemplo n.º 4
0
 /**
  * used to reconstitute public key sent in byte form from peer
  *
  * @param encodedKey
  * @return PublicKey
  */
 private PublicKey generatePubKey(byte[] encodedKey) {
   PublicKey pubKey = null;
   try {
     KeyFactory KeyFac = KeyFactory.getInstance(getAlgorithm(asymAlgorithm));
     X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encodedKey);
     pubKey = KeyFac.generatePublic(x509KeySpec);
   } catch (Exception e) {
     e.printStackTrace();
   }
   return pubKey;
 }
Ejemplo n.º 5
0
 /** Execute when disconnected from Group */
 public void channelDisconnected(Channel channel) {
   if (jmx) {
     MBeanServer server = Util.getMBeanServer();
     if (server != null) {
       try {
         JmxConfigurator.unregisterChannel((JChannel) channel, server, groupName);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
 }
Ejemplo n.º 6
0
 public void run() {
   for (int i = 1; i <= number_of_msgs; i++) {
     try {
       Message msg = new Message(destination, buf);
       if (oob) msg.setFlag(Message.Flag.OOB);
       if (dont_bundle) msg.setFlag(Message.Flag.DONT_BUNDLE);
       if (i > 0 && print > 0 && i % print == 0) System.out.println("-- sent " + i);
       channel.send(msg);
       if (sleep_time > 0) Util.sleep(sleep_time);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
Ejemplo n.º 7
0
 public void run() {
   try {
     latch.await();
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   for (int i = 0; i < NUM_MSGS; i++) {
     try {
       Message msg = new Message(dest, null, buf);
       ch.send(msg);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
Ejemplo n.º 8
0
 public void receive(Message msg) {
   Message reply = new Message(msg.getSrc());
   try {
     System.out.println(
         "-- MySimpleReplier["
             + channel.getAddress()
             + "]: received message from "
             + msg.getSrc());
     if (handle_requests) {
       System.out.println(", sending reply");
       channel.send(reply);
     } else System.out.println("\n");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 9
0
  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();
      }
    }
  }