Example #1
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();
    }
  }
 public void run() {
   while (!Thread.currentThread().isInterrupted()) {
     lock.lock();
     try {
       for (Iterator<Entry<Address, V>> it = conns.entrySet().iterator(); it.hasNext(); ) {
         Entry<Address, V> entry = it.next();
         V c = entry.getValue();
         if (c.isExpired(System.currentTimeMillis())) {
           log.info("Connection " + c.toString() + " reaped");
           Util.close(c);
           it.remove();
         }
       }
     } finally {
       lock.unlock();
     }
     Util.sleep(reaperInterval);
   }
 }
 public void addConnection(Address address, V conn) {
   lock.lock();
   try {
     V previous = conns.put(address, conn);
     Util.close(previous);
   } finally {
     lock.unlock();
   }
   notifyConnectionOpened(address, conn);
 }
 public void removeConnection(Address address) {
   Connection conn = null;
   lock.lock();
   try {
     conn = conns.remove(address);
   } finally {
     lock.unlock();
   }
   Util.close(conn);
 }
Example #5
0
  public void start(String props) throws Exception {

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

    while (true) {
      Util.sleep(10000);
    }
  }
Example #6
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);
    }
  }
Example #7
0
 public void setState(InputStream istream) {
   try {
     try {
       panel.readState(istream);
     } catch (IOException e) {
       e.printStackTrace();
     }
   } finally {
     Util.close(istream);
   }
 }
Example #8
0
 public void getState(OutputStream ostream) {
   try {
     try {
       panel.writeState(ostream);
     } catch (IOException e) {
       e.printStackTrace();
     }
   } finally {
     Util.close(ostream);
   }
 }
Example #9
0
 public byte[] getState() {
   byte[] retval = null;
   if (state == null) return null;
   synchronized (state) {
     try {
       retval = Util.objectToByteBuffer(state);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return retval;
 }
Example #10
0
 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();
       }
     }
   }
 }
Example #11
0
 @SuppressWarnings("unchecked")
 public void setState(byte[] buf) {
   synchronized (state) {
     try {
       Map<Point, Color> tmp = (Map<Point, Color>) Util.objectFromByteBuffer(buf);
       state.clear();
       state.putAll(tmp);
       System.out.println(
           "received state: " + buf.length + " bytes, " + state.size() + " entries");
       createOffscreenImage(true);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
 public void stop() {
   if (reaper != null) {
     reaper.stop();
   }
   lock.lock();
   try {
     for (Iterator<Entry<Address, V>> i = conns.entrySet().iterator(); i.hasNext(); ) {
       Entry<Address, V> e = i.next();
       Util.close(e.getValue());
     }
     clear();
   } finally {
     lock.unlock();
   }
   conn_listeners.clear();
 }
  /**
   * Removes all connections from ConnectionTable which are not in current_mbrs
   *
   * @param current_mbrs
   */
  public void retainAll(Collection<Address> current_mbrs) {
    if (current_mbrs == null) return;

    Map<Address, V> copy = null;
    lock.lock();
    try {
      copy = new HashMap<Address, V>(conns);
      conns.keySet().retainAll(current_mbrs);
    } finally {
      lock.unlock();
    }
    copy.keySet().removeAll(current_mbrs);

    for (Iterator<Entry<Address, V>> i = copy.entrySet().iterator(); i.hasNext(); ) {
      Entry<Address, V> e = i.next();
      Util.close(e.getValue());
    }
    copy.clear();
  }
Example #14
0
    public void mouseDragged(MouseEvent e) {
      int x = e.getX(), y = e.getY();
      DrawCommand comm =
          new DrawCommand(
              DrawCommand.DRAW,
              x,
              y,
              draw_color.getRed(),
              draw_color.getGreen(),
              draw_color.getBlue());

      if (no_channel) {
        drawPoint(comm);
        return;
      }

      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);
      }
    }
Example #15
0
 public void writeTo(DataOutputStream out) throws IOException {
   Util.writeStreamable(view, out);
   Util.writeStreamable(digest, out);
   Util.writeString(fail_reason, out);
 }
Example #16
0
 public void channelConnected(Channel channel) {
   if (jmx) {
     Util.registerChannel((JChannel) channel, "jgroups");
   }
 }
Example #17
0
 public void readFrom(DataInputStream in)
     throws IOException, IllegalAccessException, InstantiationException {
   view = (View) Util.readStreamable(View.class, in);
   digest = (Digest) Util.readStreamable(Digest.class, in);
   fail_reason = Util.readString(in);
 }
Example #18
0
    public static void main(String[] args) throws Exception {
        String props="udp.xml";
        boolean migrate_data=false;

        for(int i=0; i < args.length; i++) {
            if(args[i].equals("-props")) {
                props=args[++i];
                continue;
            }
            if(args[i].equals("-migrate_data")) {
                migrate_data=true;
                continue;
            }
            help();
            return;
        }



        PartitionedHashMap<String,String> map=new PartitionedHashMap<String,String>(props, "demo-cluster");
        Cache<String,String> l1_cache=new Cache<String,String>();
        l1_cache.setMaxNumberOfEntries(5);
        l1_cache.disableReaping();
        map.setL1Cache(l1_cache);
        Cache<String, String> l2_cache=map.getL2Cache();
        l2_cache.enableReaping(10000);
        map.setMigrateData(migrate_data);
        map.start();

        while(true) {
            int ch=Util.keyPress("[1] put [2] get [3] remove [4] print [q] quit");

            switch(ch) {
                case '1':
                    String key=readLine("key: ");
                    String val=readLine("val: ");
                    String caching_time=readLine("ttl: ");
                    map.put(key, val, Long.parseLong(caching_time));
                    break;
                case '2':
                    key=readLine("key: ");
                    val=map.get(key);
                    System.out.println("val = " + val);
                    break;
                case '3':
                    key=readLine("key: ");
                    map.remove(key);
                    break;
                case '4':
                    System.out.println("address: " + map.getLocalAddress());
                    System.out.println("L1 cache:\n" + map.getL1Cache());
                    System.out.println("L2 cache:\n" + map.getL2Cache());
                    break;
                case 'q':
                    l1_cache.stop();
                    map.stop();
                    return;
            }
        }

    }