def waitterminate():
    global terminate
    while(not terminate):
        (grp,src,data) = f2f.receive()
        if grp.equals(f2fGroup) and src.equals(f2fInitiator) and data == "terminate":
            terminate = True
        sleep(0.01)
def master():
    global terminate
    resultpointcount = 0L
    resulthitcount = 0L
    starttime=time()
    def showresult():
        print "Master: %s of %s Points."%(resultpointcount, PointsToGather),\
            "Master: Current Pi is:", float(resulthitcount * 4L) / float(resultpointcount)
    while( resultpointcount < PointsToGather ):
        answer = f2f.receive()
        if answer != None:
            (group,src,data) = answer
            if group.equals(f2fGroup):
                if isinstance(data,tuple):
                    (points,hits) = data
                    print "Master: Received from %s: %s"\
                        %(src.getUid(),data)
                    resultpointcount += points
                    resulthitcount += hits
                    showresult()
                elif data == "terminate":
                    terminate = True
    # Send terminate to all clients
    terminate = True # also inform local slave
    for peer in f2fGroup.getPeers():
        peer.send(f2fGroup, "terminate")
    print "Master: Endresult:"
    showresult()
    print "Master: This took %s seconds"%(time()-starttime)
Example #3
0
def master():
    global terminate
    resultpointcount = 0L
    resulthitcount = 0L
    starttime = time()

    def showresult():
        print "Master: %s of %s Points."%(resultpointcount, PointsToGather),\
            "Master: Current Pi is:", float(resulthitcount * 4L) / float(resultpointcount)

    while (resultpointcount < PointsToGather):
        answer = f2f.receive()
        if answer != None:
            (group, src, data) = answer
            if group.equals(f2fGroup):
                if isinstance(data, tuple):
                    (points, hits) = data
                    print "Master: Received from %s: %s"\
                        %(src.getUid(),data)
                    resultpointcount += points
                    resulthitcount += hits
                    showresult()
                elif data == "terminate":
                    terminate = True
    # Send terminate to all clients
    terminate = True  # also inform local slave
    for peer in f2fGroup.getPeers():
        peer.send(f2fGroup, "terminate")
    print "Master: Endresult:"
    showresult()
    print "Master: This took %s seconds" % (time() - starttime)
Example #4
0
def receivethread():
    global terminate
    while(not terminate):
        (grp,src,data) = f2f.receive()
        if grp.equals(f2fGroup) and src.equals(f2fInitiator) and data == "terminate":
            terminate = True
        else:
            print "Received:", data
Example #5
0
def waitterminate():
    global terminate
    while (not terminate):
        (grp, src, data) = f2f.receive()
        if grp.equals(f2fGroup) and src.equals(
                f2fInitiator) and data == "terminate":
            terminate = True
        sleep(0.01)
Example #6
0
def slave():
    global terminate, indexlist
    while True:
        (grp,src,data) = f2f.receive()
        if grp.equals(f2fGroup):
            break;
        print "Got data from wrong group:", grp.getUid(), data 
    (myindex,indexlist) = data
    print "Ok, I have number %s."%myindex
    threading.Thread(target=receivethread).start() # start terminate=thread
    while(not terminate):
        # send to next a message
        nextindex = (myindex + 1) % len(indexlist)
        nextpeer = Peer( id = indexlist[nextindex] )
        #for peer in f2fGroup.getPeers():
        #    if not peer.equals(myself):
        greetings = "Greetings from F2F Id: %s."%myindex
        print "Sending --%s-- to %s."%(greetings,nextpeer.getUid())
        nextpeer.send(f2fGroup, greetings)
        sleep(5.0) # results every second