#!/usr/local/bin/python import socket import struct import sys import os import subprocess import commands import shlex multicast_group = '224.3.29.71' server_address = ('', 8989) meshpointnum = os.environ['MYNUM'] mesh = "none" beat = False # Create the socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Bind to the server address sock.bind(server_address) # Tell the operating system to add the socket to the multicast group # on all interfaces. group = socket.inet_aton(multicast_group) mreq = struct.pack('4sL', group, socket.INADDR_ANY) print >>sys.stderr, 'made it here' sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) while True: data, address = sock.recvfrom(1024) print >>sys.stderr, address cmd = data.rsplit(' ') print >>sys.stderr, 'cmd recieved: ', cmd print >>sys.stderr, 'received %s bytes from %s' % (len(data), address) print >>sys.stderr, data if len(cmd) == 0: pass elif cmd[0] == "heartbeatup": if len(cmd) == 1: if beat: # should be able to call heartbeatup and everyone that is down goes up, everyone up is fine sock.sendto(meshpointnum+' '+cmd[0]+' heartbeat is already running in '+mesh+' mode', address) continue if mesh == "bat": args = shlex.split('heartbeatup -B') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) elif mesh == "802": args = shlex.split('heartbeatup -i mesh0') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) elif mesh == "none": sock.sendto(meshpointnum+' '+cmd[0]+' bring up bat, 802, or olsr interface first', address) continue else: args = shlex.split('heartbeatup') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) beat = True sock.sendto(meshpointnum+' '+cmd[0], address) elif meshpointnum in cmd[1:]: if beat: sock.sendto(meshpointnum+' '+cmd[0]+' heartbeat is already running in '+mesh+' mode', address) continue if mesh == "bat": args = shlex.split('heartbeatup -B') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) elif mesh == "802": args = shlex.split('heartbeatup -i mesh0') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) elif mesh == "none": sock.sendto(meshpointnum+' '+cmd[0]+' bring up bat, 802, or olsr interface first', address) else: args = shlex.split('heartbeatup') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) beat = True sock.sendto(meshpointnum+' '+cmd[0], address) else: pass elif cmd[0] == "heartbeatdown": if len(cmd) == 1: response = commands.getoutput('heartbeatdown') if len(response) == 0: response = meshpointnum+' '+cmd[0] beat = False else: response = meshpointnum+' heartbeat was not running' sock.sendto(response, address) elif meshpointnum in cmd[1:]: response = commands.getoutput('heartbeatdown') if len(response) == 0: response = meshpointnum+' '+cmd[0] beat = False else: response = meshpointnum+' heartbeat was not running' sock.sendto(response, address) else: pass elif cmd[0] == "batup": # send acknowledgement if len(cmd) == 1: commands.getoutput('batup') mesh = "bat" elif meshpointnum in cmd[1:]: commands.getoutput('batup') mesh = "bat" else: pass sock.sendto(meshpointnum+' '+cmd[0],address) elif cmd[0] == "batdown": # send acknowledgement if len(cmd) == 1: commands.getoutput('batdown') mesh = "none" elif meshpointnum in cmd[1:]: commands.getoutput('batdown') mesh = "none" else: pass sock.sendto(meshpointnum+' '+cmd[0],address) elif cmd[0] == "802up": # send acknowledgement if len(cmd) == 1: commands.getoutput('802up') mesh = "802" elif meshpointnum in cmd[1:]: commands.getoutput('802up') mesh = "802" else: pass sock.sendto(meshpointnum+' '+cmd[0],address) elif cmd[0] == "802down": # send acknowledgement if len(cmd) == 1: commands.getoutput('802down') mesh = "none" elif meshpointnum in cmd[1:]: commands.getoutput('802down') mesh = "none" else: pass sock.sendto(meshpointnum+' '+cmd[0],address) elif cmd[0] == "olsrup": # send acknowledgement if len(cmd) == 1: commands.getoutput('olsrup') mesh = "olsr" elif meshpointnum in cmd[1:]: commands.getoutput('olsrup') mesh = "olsr" else: pass sock.sendto(meshpointnum+' '+cmd[0],address) elif cmd[0] == "olsrdown": # send acknowledgement if len(cmd) == 1: commands.getoutput('olsrdown') mesh = "none" elif meshpointnum in cmd[1:]: commands.getoutput('olsrdown') mesh = "none" else: pass sock.sendto(meshpointnum+' '+cmd[0],address) elif cmd[0] == "hello": if mesh != "none": sock.sendto(meshpointnum+' is here!', (os.environ['MESHROOTIP'],address.__getitem__(1))) else: sock.sendto(meshpointnum+' mesh is down', address) elif cmd[0] == "testconverge": if len(cmd) > 1 and meshpointnum in cmd[1:] : args = shlex.split('testconverge') p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) while 1: line = p.stdout.readline() print >> sys.stderr , line else: pass