#!/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" # 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) 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": # send acknowledgement if len(cmd) == 1: 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": print >>sys.stderr, 'bring up bat, 802, or olsr interface first' else: args = shlex.split('heartbeatup') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) elif meshpointnum in cmd[1:]: 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": print >>sys.stderr, 'bring up bat, 802, or olsr interface first' else: args = shlex.split('heartbeatup') subprocess.Popen(args, stdout=subprocess.PIPE, shell=True) else: pass elif cmd[0] == "heartbeatdown": # send acknowledgement if len(cmd) == 1: commands.getoutput('heartbeatdown') elif meshpointnum in cmd[1:]: commands.getoutput('heartbeatdown') 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 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 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 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 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 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 else: pass