#!/usr/bin/python

import bluetooth
import md5
import random

ssk = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
		
ssk.bind(("", 9))
ssk.listen(1)
# bluetooth.advertise_service( ssk, "Boo", provider="EPx", description="Test for Boo", profiles=[ bluetooth.SERIAL_PORT_PROFILE ], service_classes = [ bluetooth.SERIAL_PORT_CLASS ] )

f = open("/dev/urandom")

while 1:
	(sk, addr) = ssk.accept()
	exit = 0
	while not exit:
		buf = ""
		
		while buf.find("abracadabra") < 0:
			# print "Reading...", 
			nowbuf = sk.recv(4096)
			if not nowbuf:
				exit = 1
			# print "Received %d bytes " % len(nowbuf),
			buf += nowbuf
	
		print "RECV: Len = %d, MD5 = %s" % (len(buf), md5.new(buf).hexdigest())
		
		buf = ""
		lbuf = random.randint(8888,32000)
		while len(buf) < lbuf:
			buf += f.read(lbuf - len(buf))

		buf += "abracadabra"
		
		print "TO SEND: Len = %d, MD5 = %s" % (len(buf), md5.new(buf).hexdigest())

		while buf:
			# print "Sending... ",
			# xbuf = buf[0:500]
			xbuf = buf
			sent = sk.send(xbuf)
			# print "Sent %d bytes " % sent,
			buf = buf[sent:]

	print "Closing"
	sk.close()






