Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 7:56 pm

Hello, Berries.

I'm continuing systematic development of my distributed synthesizer mesh program! It's going steadily...

At some point, I'm going to want to be able to discover other RPs on the local network - so when I turn an RP on and start running my program, it's able to find all other RPs that are also running my program.

I understand all too well that discovery in general is a fairly tricky problem but I'm certainly willing to hack and take shortcuts.

My current plan requires me to know the range of possible IP addresses (which is perfectly practical).

When an instance of the program starts up, it opens a server socket on a "known port" - then it goes out to all possible IP addresses and tries to connect to a socket on that known port.

This seems quite doable, a little slow (even if I set the timeouts pretty low on the socket connections), but practical.

Any other ideas?

(I'm writing most of the program in Python but chunks will have to be in C++ anyway, so dipping into a compiled language is quite conceivable...)

Thanks in advance!

User avatar
tedhale
Posts: 114
Joined: Thu Sep 20, 2012 4:52 pm
Location: Williamsburg, VA, USA
Contact: Website

Re: Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 8:16 pm

The way I have done discovery in the past is something like this:

Do a UDP broadcast of a "who's there?" message on a specific port.
A broadcast goes to all addresses on the LAN as defined by the netmask.
Then just listen to the responses come in.

To make those responses happen, have your program listen to that UDP port.
When the program sees the "Who's There" message come in, have it reply on the same port using UDP to the address that sent the message.
The recvfrom function returns the senders address.
I tend to include other info the system might need such as host name and system capabilities.

I did a quick google search and found several examples of doing UDP broadcast using C.
Use those and adapt as needed.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com

Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Re: Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 8:44 pm

UDP, interesting idea!

You can certainly do UDP in Python, it's basically trivial. I'll give it a whirl and send a link to the code when I'm done...

brs
Posts: 70
Joined: Fri Aug 17, 2012 9:27 pm
Location: Zurich

Re: Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 10:28 pm

http://en.wikipedia.org/wiki/Avahi_(software) is one of the common solutions for advertising and discovering network services. It supposedly has Python bindings to query the local server for the available service listings for the local network(s).

Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Re: Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 10:34 pm

UDP broadcast/receive works well in Python, and you can see an example of how to do it in these files, TestReceiveBroadcast.py and TestSendBroadcast.py.

lb
Posts: 285
Joined: Sat Jan 28, 2012 8:07 pm

Re: Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 10:35 pm

yup, Avahi is the way to go. By default, Avahi already broadcasts the hostname via mDNS. This might be good enough already if you use a common hostname prefix.

Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Re: Easiest way to discover other RPs on the network?

Mon Oct 08, 2012 10:54 pm

I looked at Avahi, but it seems far too heavy for what I want to do.

I'm going to use the handrolled UDP mechanism and not worry about it.

Thanks!

Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Re: Easiest way to discover other RPs on the network?

Tue Oct 09, 2012 7:04 am

All right, I have a nice discovery class in Python and you can see it here.

Works on RP and/or on the Mac.

I'll be tightening this up in the coming weeks of course but all the idea is there.

Twinkletoes
Posts: 210
Joined: Fri May 25, 2012 9:44 pm

Re: Easiest way to discover other RPs on the network?

Tue Oct 09, 2012 8:49 am

Avahi (apple call it bonjour) does various useful things:

It works when you don't have DHCP
It works when you just have a reversed network cable
It enables named lookups (http://mydevice/index.html)
Names can be automatically generated so they're unique on the network, or can be fixed "myPi". If you have two myPis on the LAN you'll see myPi and myPi1.
You can discover all connected devices on the LAN and what their names are
You can tag services and search for them (all raspberry Pis for example - most printers have avahi built in)

The inventor does a fantastic lecture online (can't remember the title but try googling bonjour lecture) where he says "when was the last time you couldn't turn the light on because you didn't know how to construct a subnet mask?"

Bryan

Tom Swirly
Posts: 168
Joined: Tue Sep 25, 2012 1:08 am

Re: Easiest way to discover other RPs on the network?

Tue Oct 09, 2012 4:16 pm

Avahi is not bonjour: source.

Return to “General programming discussion”