#!/usr/bin/python import socket import sys import getopt from threading import Thread import bson import time SEND_IP="127.0.0.1" SEND_PORT=8123 IFACE='wlan0' PERIOD=1 BATMAN=False def debugOut(str): DEBUG = True if DEBUG: print str ''' takes a dictionary and sends the BSON representation to SEND_IP:SEND_PORT ''' def sendHeartBeat(dict, sock): c = bson.dumps(dict) b = bson.loads(c) debugOut('\nsending to %s:%s' % (SEND_IP, SEND_PORT)) debugOut('packet: %s' % b) sock.sendto(c,(SEND_IP, SEND_PORT)) # Send periodic heartbeat packets class HeartbeatSenderThread(Thread): def __init__(self, iface=IFACE, ip = SEND_IP, port = SEND_PORT, period = PERIOD): Thread.__init__(self, name='Sender') self.iface = iface self.ip = ip self.port = port self.period = period self.done = False def stop(self): self.done = 1; def run(self): debugOut("Monitoring nodes from %s" % self.iface) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while not self.done: sendHeartBeat({"source_mac":1234, "source_ip":623}, sock) time.sleep(self.period) def parseCmd(cmd): global SEND_IP global SEND_PORT global IFACE global PERIOD def usage(): print "usage: %s [OPTIONS]" % cmd[0] print "" print " Options:" print " -h,--help - This usage" print "" print " Sender Options:" print " -o,--ocu - IP address of OCU on mesh (default=%s)" % SEND_IP print " -p, --port - Port to send/recv heartbeats on (default=%d)" % SEND_PORT print " -i,--interface - Wireless Interface to monitor neighbors of (default=%s)" % IFACE print " --period - Number of seconds between each heartbeat packet send (default=%s)" % PERIOD print " -b, --batman - Uses batman debugfs/sysfs file structure (default=%s)" % BATMAN print "" try: opts, args = getopt.getopt(cmd[1:], "h:o:p:i:b", ["help", "port=", "host=", "interface=", "ocu=", "period=", "batman"]); except getopt.GetoptError, err: print str(err) usage() sys.exit(2) for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() elif o in ("-o", "--ocu"): SEND_IP = a elif o in ("-p", "--port"): SEND_PORT = int(a) elif o in ("-i", "--interface"): IFACE = a elif o in ("--period"): PERIOD = int(a) elif o in ("-b", "--batman"): BATMAN = True def main(): parseCmd(sys.argv) sender = HeartbeatSenderThread() sender.start() #stay in a while loop until killed try: while 1: pass except KeyboardInterrupt: pass sender.stop() if __name__ == '__main__': main()