|
1 | 1 | package org.jgroups.protocols;
|
2 | 2 |
|
3 | 3 | import org.jgroups.Message;
|
4 |
| -import org.jgroups.annotations.Property; |
| 4 | +import org.jgroups.annotations.MBean; |
| 5 | +import org.jgroups.annotations.ManagedAttribute; |
| 6 | +import org.jgroups.annotations.ManagedOperation; |
5 | 7 | import org.jgroups.stack.Protocol;
|
| 8 | +import org.jgroups.util.MessageBatch; |
| 9 | + |
| 10 | +import java.util.Set; |
| 11 | +import java.util.concurrent.ConcurrentSkipListSet; |
| 12 | +import java.util.stream.Collectors; |
6 | 13 |
|
7 | 14 | /**
|
8 |
| - * Protocol which clears the given flags in the down direction for all messages |
| 15 | + * Protocol which clears a set of flags in the down or up direction for all messages |
9 | 16 | * @author Bela Ban
|
10 | 17 | * @since 4.0.4
|
11 | 18 | */
|
| 19 | +@MBean(description="Clears a given set of flags from up or down messages") |
12 | 20 | public class CLEAR_FLAGS extends Protocol {
|
13 |
| - @Property(description="clear OOB flags") |
14 |
| - protected boolean oob=true; |
| 21 | + protected final Set<Message.Flag> down_flags=new ConcurrentSkipListSet<>(); |
| 22 | + protected final Set<Message.Flag> up_flags=new ConcurrentSkipListSet<>(); |
15 | 23 |
|
16 |
| - @Override |
17 |
| - public Object down(Message msg) { |
18 |
| - if(oob) |
19 |
| - msg.clearFlag(Message.Flag.OOB); |
| 24 | + @ManagedAttribute(description="Shows up and down flags") |
| 25 | + public String flags() { |
| 26 | + return String.format("down flags: %s\nup_flags: %s", downFlags(), upFlags()); |
| 27 | + } |
| 28 | + |
| 29 | + @ManagedOperation(description="Adds a flag to the up or down set") |
| 30 | + public CLEAR_FLAGS addFlag(boolean down, String flag) { |
| 31 | + Message.Flag fl=Message.Flag.valueOf(flag); |
| 32 | + return fl != null? addFlag(down, fl) : this; |
| 33 | + } |
| 34 | + |
| 35 | + public CLEAR_FLAGS addFlag(boolean down, Message.Flag flag) { |
| 36 | + Set<Message.Flag> s=down? down_flags : up_flags; |
| 37 | + s.add(flag); |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + @ManagedOperation(description="Removes a flag from the up or down set") |
| 42 | + public CLEAR_FLAGS removeFlag(boolean down, String flag) { |
| 43 | + Message.Flag fl=Message.Flag.valueOf(flag); |
| 44 | + return fl != null? removeFlag(down, fl) : this; |
| 45 | + } |
| 46 | + |
| 47 | + public CLEAR_FLAGS removeFlag(boolean down, Message.Flag flag) { |
| 48 | + Set<Message.Flag> s=down? down_flags : up_flags; |
| 49 | + s.remove(flag); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + @ManagedOperation(description="Removes all up or down flags") |
| 54 | + public CLEAR_FLAGS removeAllFlags() { |
| 55 | + down_flags.clear(); |
| 56 | + up_flags.clear(); |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + @Override public Object down(Message msg) { |
| 61 | + if(!down_flags.isEmpty()) { |
| 62 | + for(Message.Flag flag: down_flags) |
| 63 | + msg.clearFlag(flag); |
| 64 | + } |
20 | 65 | return down_prot.down(msg);
|
21 | 66 | }
|
| 67 | + |
| 68 | + @Override public Object up(Message msg) { |
| 69 | + if(!up_flags.isEmpty()) { |
| 70 | + for(Message.Flag fl: up_flags) |
| 71 | + msg.clearFlag(fl); |
| 72 | + } |
| 73 | + return up_prot.up(msg); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void up(MessageBatch batch) { |
| 78 | + if(!up_flags.isEmpty()) { |
| 79 | + for(Message.Flag fl: up_flags) |
| 80 | + for(Message msg: batch) |
| 81 | + msg.clearFlag(fl); |
| 82 | + } |
| 83 | + up_prot.up(batch); |
| 84 | + } |
| 85 | + |
| 86 | + public String downFlags() { |
| 87 | + return down_flags.stream().map(Enum::toString).collect(Collectors.joining(", ")); |
| 88 | + } |
| 89 | + |
| 90 | + public String upFlags() { |
| 91 | + return up_flags.stream().map(Enum::toString).collect(Collectors.joining(", ")); |
| 92 | + } |
22 | 93 | }
|
0 commit comments