Multicast question

32 views
Skip to first unread message

newbie

unread,
Sep 13, 2010, 5:00:50 AM9/13/10
to kilimthreads
Is multicast supported in kilim?

In the example dir, it seems only SocketChannel is provided (in
Ping.java).

Can I simple use something e.g. DatagramPacket to achieve multicast
feature? Or additional work is required?

I appreciate any suggestion.

Many thanks.


Sriram Srinivasan

unread,
Sep 13, 2010, 4:03:08 PM9/13/10
to kilimt...@googlegroups.com

On Sep 13, 2010, at 2:00 AM, newbie wrote:

> Is multicast supported in kilim?
>

No, there is no multicast support in kilim/nio.

As you observed, I use SocketChannel, and I believe one should be able
to get away with using SelectableChannel, in order to accomodate both
DatagramChannel and SocketChannel. If you do this refactoring, I'd be
immensely grateful and can roll it in.

--sriram.


newbie

unread,
Sep 21, 2010, 6:40:48 AM9/21/10
to kilimthreads
Hi Sriram,

I am happy to do that. But whilst testing to implement MulticastSocket
function, I encountered a problem.

At receiving side (e.g. MulticastSocket.receive()), it keeps throwing
message e.g.

java.nio.channels.IllegalSelectorException
at sun.nio.ch.SelectorImpl.register(SelectorImpl.java:111)
at
java.nio.channels.spi.AbstractSelectableChannel.register(AbstractSelectableChannel.java:
180)
at
java.nio.channels.SelectableChannel.register(SelectableChannel.java:
254)
at kilim.nio.NioSelectorScheduler
$RegistrationTask.execute(NioSelectorScheduler.java:324)
at kilim.Task._runExecute(Task.java:432)
at kilim.nio.NioSelectorScheduler
$SelectorThread.run(NioSelectorScheduler.java:198)

when receiving packet from the client.

After checking the code, looks like it is issued by SelectorImpl.java
in which

protected final SelectionKey register(AbstractSelectableChannel ch,
int ops,
Object attachment)
{
if (!(ch instanceof SelChImpl))
throw new IllegalSelectorException();
...
}

it checks the channel passed in required to implement SelChImpl.
Unfortunately, SelChImpl is not public, resulting
in the compiler complains `... <subclass that extends > ... cannot
access its superinterface sun.nio.ch.SelChImpl,'
if trying to implements SelChImpl class.

This message comes from my code (PollSelectorProvider) where to open
the Multicast Socket function, e.g.

public static AbstractSelectableChannel open(int port) throws
IOException{
return new AbstractMulticastSocket(new
sun.nio.ch.PollSelectorProvider(), port);
}

However, I think because I am not familiar with this I do not have any
good idea on how to get around it. Is there any chance
to provide any hint or advice? Or point me to where there may contain
related resource/ doc?

I appreciate any suggestion.

Testing code:
MulticastSocketWrapper - http://pastebin.com/u8SA3rMG
NioSelectorScheduler - http://pastebin.com/2CK6nwHR

Sriram Srinivasan

unread,
Sep 21, 2010, 9:54:53 AM9/21/10
to kilimt...@googlegroups.com
I see. This will require more digging then.

The basic issue is that you need to receive multicast sockets and work
with Kilim tasks. Most likely you have just the single multicast
socket, one reasonable alternative -- at least for now -- is to have
a separate thread blocked on that multicast socket? Whenever you
receive a packet, put it in some task's mailbox and return to blocking
on that socket.

--sriram.

newbie

unread,
Sep 23, 2010, 4:56:11 AM9/23/10
to kilimthreads
I switch to use a separate Task receiving incoming packet and
basically looks like it works. But an message is thrown once the
socket received a packet saying

java.net.SocketException: Address already in use
at java.net.PlainDatagramSocketImpl.join(Native Method)
at java.net.PlainDatagramSocketImpl.join(PlainDatagramSocketImpl.java:
172)
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:277)
at
kilim.nio.MulticastSocketWrapper.joinGroup(MulticastSocketWrapper.java:
61)
at kilim.nio.NioSelectorScheduler
$MulticastListenTask.execute(NioSelectorScheduler.java:245)
at kilim.Task._runExecute(Task.java:432)
at kilim.nio.NioSelectorScheduler
$SelectorThread.run(NioSelectorScheduler.java:198)

And this cause the server would only receive packet once only.

My guess is because RegistrationTask registers channel with a
SlectionKey returned; therefore, the SelectorThread can select channel
with different key. Unfortunately,
the separate task uses MulticastSocket instead of Channel instance so
the register() function in RegistrationTask won't actually perform
register procedure as channel
passed in is null.

I notice that seems can be modified by customizing
SelectorImpl.register() function, but again a similar issue as
previous one would happen again (The default implementation
can not be reused if I am correct). Before doing this, I would like to
double check if this is the right direction (because some more detail
I may not be aware of, though I've dug a
bit into the source), or there is an alternative way to get around
this issue.

Modified code:
http://pastebin.com/3TTq2pAX
http://pastebin.com/T4FWyDXQ
http://pastebin.com/VUCYgEmP

I appreciate any advice.

Thank you.
> > NioSelectorScheduler -http://pastebin.com/2CK6nwHR

Sriram Srinivasan

unread,
Sep 23, 2010, 10:06:20 AM9/23/10
to kilimt...@googlegroups.com
Hi,

From a _very_ cursory look at the source, I'm puzzled by one
conspicuous omission: there is no import of DatagramChannel.
Perhaps that is the key to the solution.

See also Alan Bateman's blog entry on this topic:

http://blogs.sun.com/alanb/entry/multicasting_with_nio

--sriram.


newbie

unread,
Oct 11, 2010, 12:48:51 PM10/11/10
to kilimthreads
I just submitted a patch through github, but not very sure if that's
the formal way (I can't find something e.g. issue tracking to create
an issue and add patch as attachment.)

Please let me know if ant issue relates.

Many thanks.

Sriram Srinivasan

unread,
Oct 11, 2010, 10:20:40 PM10/11/10
to kilimt...@googlegroups.com

On Oct 11, 2010, at 9:48 AM, newbie wrote:

> I just submitted a patch through github, but not very sure if that's
> the formal way (I can't find something e.g. issue tracking to create
> an issue and add patch as attachment.)
>
> Please let me know if ant issue relates.


thanks much. I'll take a look at the patch.

Reply all
Reply to author
Forward
0 new messages