Message6792

Author amak
Recipients amak, hulettbh
Date 2012-03-11.22:54:13
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1331506455.05.0.0738022845138.issue1846@psf.upfronthosting.co.za>
In-reply-to
Content
I didn't have any plans to implement multicast functionality, primarily because I wasn't aware that there was need for it. But you are using it, so let's have a think about it.

Problem number one is that there is no support for multicast sockets under java.nio, i.e. although java.net.MulticastSocket is a subclass of java.net.DatagramSocket, there is no corresponding java.nio equivalent subclass. This is still true on java 6, which is the target for the next version of jython.

http://docs.oracle.com/javase/6/docs/api/index.html?java/nio/channels/DatagramChannel.html
https://blogs.oracle.com/alanb/entry/multicasting_with_nio

Note that there is a multicast channel in java 7. 

http://docs.oracle.com/javase/7/docs/api/java/nio/channels/MulticastChannel.html

But we currently have no plans to target java 7, so if support for multicast sockets were to be added to jython, it could only ever work on java 7 and later.

As you may be aware, all sockets created with the jython socket module have a dual nature: they consist of a java.nio.channels.* object, which is used for all asynchronous operations and 'select'ing, and a java.net.* object, which is used for all other operations. There is no other choice than to work it this way, because neither of the java.net or java.nio APIs contain all of the functionality required.

You can see this if you look at the declaration of the "_datagram_socket_impl" class in the socket module. It creates both a "jchannel" and a "jsocket" attribute. In the case of multicast sockets, there is no way to create a "jchannel" object.

Looking at the API for the java MulticastSocket class, I see other potential problems. Some of the methods take an "Interface" parameter, i.e. they allow you to specify a particular interface on which to send and receive multicasts. At the moment, there is no support in the jython socket module for modelling individual network interfaces: dealing with a specific interface requires specifying the IP address of the interface. (As far as I can tell, cpython doesn't permit modelling network interfaces either).

(As an aside, this support for network interfaces will have to added at some stage, since support IPv6 scopes will require it)

So, some questions.

1. Would you require non-blocking support for multicast sockets? This can not be implemented on jython until we hit java 7.
2. Would you require using select or poll on multicast sockets? Again, this would require java 7.
3. Does the cpython code that you are working with specify particular interfaces? How does it do that?

If you could paste some sample code for your use case, that would be really helpful.

Lastly, if your answer to all three questions is "no", then there should be a straightforward way to monkeypatch an UDP socket to be a multicast socket, by simply

A: Creating a java.net.MulticastSocket object, through the java APIs.
B: Creating a jython UDP socket using socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
C: Assiging the "jsocket" attribute of the UDP socket to be the java Multicast socket.
D: Only ever using the socket in blocking mode, and never using it in a select or poll call.

Sample code

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

from java.net import MulticastSocket, InetSocketAddress
import socket

ip, port = "192.168.1.1", 6789

jmultisock = MulticastSocket(InetSocketAdddress(ip, port))
mysock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
mysock.sock_impl.jsocket = jmultisock

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
History
Date User Action Args
2012-03-11 22:54:15amaksetmessageid: <1331506455.05.0.0738022845138.issue1846@psf.upfronthosting.co.za>
2012-03-11 22:54:15amaksetrecipients: + amak, hulettbh
2012-03-11 22:54:14amaklinkissue1846 messages
2012-03-11 22:54:13amakcreate