Rob Ottaway
Rob Ottaway

Reputation: 614

Extending DefaultChannelGroup

I would like to create a class that works much like a DefaultChannelGroup, but with the one difference that the message being written belongs to a connection and the channel associated with it will not have the message written back to it.

Think of the chat application where we should write to all other channels other than the one belonging to the user who wrote the message.

Looking at the implementation of the DefaultChannelGroup it seems I could add a new method named write that expects a given channel and the message, and will iterate the non-server channels and skip a channel that is equals to the given channel.

Upvotes: 0

Views: 242

Answers (1)

Nicholas
Nicholas

Reputation: 16066

You could extend the DefaultChannelGroup to do as you outlined, but a channel group is already an iterator and set of channels already. If you already have a channel, you can perform ther write directly to it (i.e. you don't need to get it from the ChannelGroup), or, if for some reason, you really wanted to get it from the channel group, you could call ChannelGroup.find(channel.getId()).

I guess if you are doing this for the pruposes of narrowing down to a single channel, it's an issue of cosmetics. I am not pannning it.... personal preference ! If it makes it better for you, go for it.

The more interesting scenario, which would be a truly useful extension to the DefautChannelGroup, would be to assign individual channels a group of attributes encoded as a bit-mask. Then you might be able to do something like tell the BitMaskChannelGroup to write this message to all channels with a provided bit-mask argument, which might be the encoding for all chat-room users over the age of 21 living in New Jersey, or all routing devices where the manufacturer is Cisco.

Upvotes: 2

Related Questions