import socket, sys
from time import sleep

# ew...
def getIP():
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	s.connect(('rjdj.me', 80))
	addr = s.getsockname()[0] 
	s.close()
	return addr

class PdLANPoller:
	"""
		Broadcasts our IP address every two seconds.
		It's the poor GNU programmer's Bonjour! :)
	"""
	def Launch(self):
		dest = ('255.255.255.255', 10314)
		
		s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
		
		if len(sys.argv) == 2:
			addr = sys.argv[1]
		else:
			addr = getIP()
		
		while 1:
			# broadcast the server IP address
			print 'broacasting ip...'
			s.sendto("ip %s;\n" % addr, dest)
			sleep(2)

