def send_ident(actTime):
    '''update hash and broadcast info msg regularly'''

    msg = flpconfig.SEP.join([flpconfig.PROG,flpconfig.INFOMSG,flpconfig.FLPGROUP,str(flpconfig.TIMESTAMP),str(flpconfig.SUPERHASH)])    

    if time.time() - actTime > flpconfig.BC_TIME:
        actTime = time.time()
        ourHash = getHashDir(flpconfig.FLPGROUP)
        if ourHash != flpconfig.SUPERHASH:
            flpconfig.LOGGER.info('detected change in files, updating hash and timestamp..')
            flpconfig.DIRTREE = flputil.get_dict()
            flpconfig.TIMESTAMP = time.time()
            flpconfig.SUPERHASH = ourHash
        flpconfig.LOGGER.info('sending udp info broadcast: ' + msg)
        flpconfig.UDPSOCK.sendto(msg,("<broadcast>",flpconfig.UDPPORT))

    return actTime
def send_ident(actTime):
    '''update hash and broadcast info msg regularly'''

    msg = flpconfig.SEP.join([
        flpconfig.PROG, flpconfig.INFOMSG, flpconfig.FLPGROUP,
        str(flpconfig.TIMESTAMP),
        str(flpconfig.SUPERHASH)
    ])

    if time.time() - actTime > flpconfig.BC_TIME:
        actTime = time.time()
        ourHash = getHashDir(flpconfig.FLPGROUP)
        if ourHash != flpconfig.SUPERHASH:
            flpconfig.LOGGER.info(
                'detected change in files, updating hash and timestamp..')
            flpconfig.DIRTREE = flputil.get_dict()
            flpconfig.TIMESTAMP = time.time()
            flpconfig.SUPERHASH = ourHash
        flpconfig.LOGGER.info('sending udp info broadcast: ' + msg)
        flpconfig.UDPSOCK.sendto(msg, ("<broadcast>", flpconfig.UDPPORT))

    return actTime
def flpbox(flpGroup):

    #general
    flpconfig.RUNNING = 1
    flpconfig.FLPGROUP = flpGroup
    try:
        flpconfig.PEERGROUP = pickle.load(open(flpconfig.PEERFILE, "rb"))
    except IOError:
        flpconfig.PEERGROUP = {}
        flpconfig.INITIAL   = True
    flpconfig.JOBS  = {}
    flpconfig.LANIP = flputil.get_LAN_IP()
    flpconfig.MYTREEFILE = flpconfig.TREEFILE + flpconfig.LANIP

    #start logging
    logging.basicConfig(filename=flpconfig.LOGFILE, level=logging.INFO)
    #logging.basicConfig(level=logging.INFO)
    flpconfig.LOGGER = logging.getLogger(__name__)
    flpconfig.LOGGER.info('started')

    #create broadcast and unicast sockets
    flpconfig.UDPSOCK = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    flpconfig.UDPSOCK.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
    flpconfig.UDPSOCK.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,True)
    flpconfig.TCPSOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    flpconfig.TCPSOCK.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)

    #bind both sockets then broadcast
    flpconfig.UDPSOCK.bind((flpconfig.INTERFC,flpconfig.UDPPORT))
    flpconfig.TCPSOCK.bind((flpconfig.INTERFC,flpconfig.TCPPORT))
    flpconfig.TCPSOCK.listen(flpconfig.BACKLOG)

    # broadcast first info msg
    flpconfig.TIMESTAMP = actTime = time.time()
    flpconfig.SUPERHASH = getHashDir(flpconfig.FLPGROUP)
    flpconfig.DIRTREE = flputil.get_dict()
    pickle.dump(flpconfig.DIRTREE,open(flpconfig.MYTREEFILE,'wb'))
    flpconfig.LOGGER.info('initial hash: ' + str(flpconfig.SUPERHASH))
    flpconfig.LISTEN = [flpconfig.UDPSOCK, flpconfig.TCPSOCK]
    flpconfig.SEND   = []
    flpconfig.LOGGER.info('entering Main Loop')

    while flpconfig.RUNNING:

        # call select and broadcast info
        manage_jobs()
        readable, writeable, _ = select.select(flpconfig.LISTEN,flpconfig.SEND,[],flpconfig.BC_TIME)
        actTime = send_ident(actTime)

        for s in readable:

            # did we receive a broadcast msg?
            if s == flpconfig.UDPSOCK:
                data, address = s.recvfrom(flpconfig.SIZE)
                # ignore own broadcast messages
                if address[0] != flpconfig.LANIP:
                    handle_udp_broadcast(data,address[0])

            # did someone connect to our peer?
            elif s == flpconfig.TCPSOCK:
                peer, address = s.accept()
                handle_tcp(peer,address[0])

            # is it a active job?
            elif s in flpconfig.JOBS:
                job = flpconfig.JOBS[s]
                if job.readable and not job.finished:
                    job.do()
        
        
        for s in writeable:
            # is a job in a writeable state?
            
            if s in flpconfig.JOBS:
                job = flpconfig.JOBS[s]
                if job.writeable and not job.finished:
                    job.do()

    # clean up after select loop terminates
    flpconfig.UDPSOCK.close()
    flpconfig.TCPSOCK.close()
    logging.info('finished')
def flpbox(flpGroup):

    #general
    flpconfig.RUNNING = 1
    flpconfig.FLPGROUP = flpGroup
    try:
        flpconfig.PEERGROUP = pickle.load(open(flpconfig.PEERFILE, "rb"))
    except IOError:
        flpconfig.PEERGROUP = {}
        flpconfig.INITIAL = True
    flpconfig.JOBS = {}
    flpconfig.LANIP = flputil.get_LAN_IP()
    flpconfig.MYTREEFILE = flpconfig.TREEFILE + flpconfig.LANIP

    #start logging
    logging.basicConfig(filename=flpconfig.LOGFILE, level=logging.INFO)
    #logging.basicConfig(level=logging.INFO)
    flpconfig.LOGGER = logging.getLogger(__name__)
    flpconfig.LOGGER.info('started')

    #create broadcast and unicast sockets
    flpconfig.UDPSOCK = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    flpconfig.UDPSOCK.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
    flpconfig.UDPSOCK.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    flpconfig.TCPSOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    flpconfig.TCPSOCK.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)

    #bind both sockets then broadcast
    flpconfig.UDPSOCK.bind((flpconfig.INTERFC, flpconfig.UDPPORT))
    flpconfig.TCPSOCK.bind((flpconfig.INTERFC, flpconfig.TCPPORT))
    flpconfig.TCPSOCK.listen(flpconfig.BACKLOG)

    # broadcast first info msg
    flpconfig.TIMESTAMP = actTime = time.time()
    flpconfig.SUPERHASH = getHashDir(flpconfig.FLPGROUP)
    flpconfig.DIRTREE = flputil.get_dict()
    pickle.dump(flpconfig.DIRTREE, open(flpconfig.MYTREEFILE, 'wb'))
    flpconfig.LOGGER.info('initial hash: ' + str(flpconfig.SUPERHASH))
    flpconfig.LISTEN = [flpconfig.UDPSOCK, flpconfig.TCPSOCK]
    flpconfig.SEND = []
    flpconfig.LOGGER.info('entering Main Loop')

    while flpconfig.RUNNING:

        # call select and broadcast info
        manage_jobs()
        readable, writeable, _ = select.select(flpconfig.LISTEN,
                                               flpconfig.SEND, [],
                                               flpconfig.BC_TIME)
        actTime = send_ident(actTime)

        for s in readable:

            # did we receive a broadcast msg?
            if s == flpconfig.UDPSOCK:
                data, address = s.recvfrom(flpconfig.SIZE)
                # ignore own broadcast messages
                if address[0] != flpconfig.LANIP:
                    handle_udp_broadcast(data, address[0])

            # did someone connect to our peer?
            elif s == flpconfig.TCPSOCK:
                peer, address = s.accept()
                handle_tcp(peer, address[0])

            # is it a active job?
            elif s in flpconfig.JOBS:
                job = flpconfig.JOBS[s]
                if job.readable and not job.finished:
                    job.do()

        for s in writeable:
            # is a job in a writeable state?

            if s in flpconfig.JOBS:
                job = flpconfig.JOBS[s]
                if job.writeable and not job.finished:
                    job.do()

    # clean up after select loop terminates
    flpconfig.UDPSOCK.close()
    flpconfig.TCPSOCK.close()
    logging.info('finished')