I’ve recently been playing with Stanford’s EventHeap and IStuff package for some of the work we are doing here. I am a little to spent to go into a lot of details, but the first task I tackled was trying to write some wrappers to make it easier to work with EventHeap using mDNS. This was far harder than I thought it would be. I kept running into a problem where I would get a serviceAdded event, but the event would not have enough information for me to actually connect to the service.
I am using the jmdns.jar package: http://jmdns.sourceforge.net/
Here is the scenario I was trying to address:
- Server registers it’s self using mdns using service _eheap.
- Sometime after this, a client application listens for services on _eheap.
- Client connects to server.
Unfortunately when I worked in this order I ran into the problem that the event object that mdns passes to serviceAdded did not have enough information to make a connect to the remote host.
I didn’t see any good example of how to do this. I am sure I am probably doing something wrong.
If I did everything in this order it worked fine:
- A client application listens for services on _eheap.
- Server registers it’s self using mdns using service _eheap.
- Client connects to server.
However, for my purposes, I absolutely need the server to be able to start before the clients that are listening for that service.
I also just wanted to be able to write very simple code to lookup a device using mdns. I.e. I wanted to be able to write small quick scripts, that were not event driven to connect to a service advertising itself on mDNS.
After tracing through the jmDNS code I found out that the objects actually had all the information I was trying to get, but were not providing an easy interface to it. (There may be easier interfaces, but there is literally no documentation on jmdns that I could find)
To this end I have written a very simple Jython wrapper for mDNS which provides a few VERY simple interfaces to mDNS / ZeroConf/ Avahi / Bonjour, with the specific goal to provide authors writing very simple scripts, methods to connect to a service that is advertising itself using mDNS.
I have posted this Jython file at: http://umich.edu/~bcx/contrib/jymdns.py
DISCLAIMER: This is the first application I have ever written in Jython, and I have only been playing with mDNS for about 2 days. Thus, this may be completely wasted effort; however, it seems to work well for my purposes.
The interface:
jymdns.py 0.1
AUTHOR:
Ben Congleton, bcx@umich.edu
DESCRIPTION:
Jymdns is a simple jython interface to the mdns library.
REQUIREMENTS:
jython.jar
jmdns.jar
INTRODUCTION:
I could not for the life of me figure out how to use the JAVA mdns library, when I was trying
to listen for hosts that were created before the mdns listener. There is probably a way around this.
This is really just a hack to use the local mDNS cache to find out information about services that
are not responding to the event.getInfo() command. With a few nice interfaces for writing really
quick scripts in jython that need a simple way of finding a host on a network.
REGISTERING:
from jymdns import SimplemDNS
simp = SimplemDNS("_eheap")
simp.register("foo33", 1234)
# do all the real stuff
simp.stop() # stop the mDNS broadcaster
FINDING A MDNS HOST for a service (ignoring the name):
from jymdns import SimplemDNS
res = SimplemDNS.get_service("_eheap")
if(res):
(host, port) = res
FINDING A SPECIFIC HOSTNAME:
from jymdns import SimplemDNS
res = simp.wait_for(None)
if(res):
(host, port) = res
SIMPLE TEST:
java -jar jython.jar jymdns.py
http://umich.edu/~bcx/contrib/jymdns.py