def exchange_info(ID, data, controllerIP, gain, udp_port):

    freq_control = 3000e6
    file2send = '/root/total/discovery_phase/my_info.txt'
    file_received = '/root/total/discovery_phase/received_info.txt'
    ready_file = '/root/total/discovery_phase/ready.txt'
    ok_file = '/root/total/discovery_phase/ok.txt'
    sec = 4

    f = open(file2send, 'w')

    if not isinstance(data, basestring):
        data = l2str(data)

    str2fil = str(ID) + ' ' + data + '\n'
    f.write(str2fil)
    f.close()

    myIP = socket.gethostbyname(socket.gethostname())
    with open('/root/total/ipnet.json') as jf:
        diction = json.load(jf)
        ips = diction['net']

    nodeList = [int(x) for x in ips.keys()]
    num_of_ch = len(nodeList) - 1
    print 'num_of_channels ', num_of_ch
    f = open('/root/total/discovery_phase/ready.txt', 'w')
    f.write('Ready ' + str(ID) + ' To Exchange\n')
    f.close()
    udp_transmit.transmit(controllerIP, udp_port + int(ID), ready_file)

    udp_receive.receive(myIP, udp_port, ok_file)

    receive_transmit.transceive(freq_control + int(ID) * 100e6,
                                freq_control + 100e6 * min(nodeList),
                                file2send, file_received, sec, num_of_ch, gain)

    winners = getVectors(file_received, 2, nodeList)
    os.remove(file2send)
    os.remove(file_received)
    os.remove(ok_file)
    os.remove(ready_file)

    return winners
            for t in thread_array:
                t.start()

            #global counter
            while (counter < num_nodes):
                pass

            # at this point all nodes have notified the controller of their  readiness(completion)

            print "READY!!!"
            nodes = [i for i in ips.keys() if i != controller_id]

            # instruct PUs to start transmitting
            for pu_ip in pus_ips:
                udp_transmit.transmit(pu_ip, udp_port, 'startPU.txt')

            for n in nodes:
                print 'Sending at N1-' + str(n)
                filename2 = open('node' + str(n), 'w+')
                filename2.write('Node ' + str(n) + ' send!')
                filename2.close()
                udp_transmit.transmit(ips[n], udp_port, 'node' + str(n))
            print "Terminated iteration"

            for t in thread_array:
                t.join()

            #global counter
            counter = 0
            os.remove(filename)
def exchange(ID, vectorsSend, nCh, gain, sec):
    """ Vectors' exchange within the neighborhood Gs.
        vectorsSend: contains the filename with the data to be exchanged """

    udp_port = 11000
    freq_control = 3000e6
    ready_file = '/root/total/SIG_exchange/ready.txt'
    ok_file = '/root/total/SIG_exchange/ok.txt'
    file_received = '/root/total/SIG_exchange/dataReceived.txt'
    file_received2 = '/root/total/SIG_exchange/dataReceived2.txt'
    file2Send = '/root/total/SIG_exchange/tsdata.txt'

    with open(ready_file, 'w') as f:
        f.write("Ready to start phase!")

    # exchange vectors within transmission range Ts
    # winners is a list that contains the lines corresponding to each neighbor data
    # tsA, tsN, tsC, tsU are lists of the 1-hop neighbors respective vectors

    # read configuration file
    with open('/root/total/ipnet.json') as jf:
        diction = json.load(jf)
        ips = diction['net']

    # dictionary ips contains the IP addresses of the nodes
    ips = {int(x): y.encode('utf8') for x, y in ips.items()}

    nodeList = ips.keys()  # list of nodes
    c_id = max(ips.keys())  # controller always takes the largest id

    udp_transmit.transmit(ips[c_id], udp_port + int(ID), ready_file)
    udp_receive.receive(ips[ID], udp_port, ok_file)

    receive_transmit.transceive(freq_control + int(ID) * 100e6,
                                freq_control + 100e6 * min(nodeList),
                                vectorsSend, file_received, sec, c_id - 3,
                                gain)
    copyfile(file_received, '/root/total/kostacito.txt')
    winners, tsA, tsN, tsC, tsU = rv.getVectors(file_received, nCh, nodeList)
    tsA = [l for l in tsA if l[0] != ID]
    tsN = [l for l in tsN if l[0] != ID]
    tsC = [l for l in tsC if l[0] != ID]
    tsU = [l for l in tsU if l[0] != ID]

    print "First step completed! -- Signaling Exchange"

    # write in the file the data of your 1-hop neighbors
    f2 = open(file2Send, 'w')
    for w in winners:
        f2.write(w)
    f2.close()

    # exchange within your transmission range the above file
    receive_transmit.transceive(freq_control + int(ID) * 100e6,
                                freq_control + 100e6 * min(nodeList),
                                file2Send, file_received2, sec, c_id - 3, gain)
    _, gA, gN, gC, gU = rv.getVectors(file_received2, nCh, nodeList)

    print "Second step completed! -- Signaling Exchange"

    # GA, GN, GC, GU are lists of the neighbors respective vectors
    GA = rem_dupl(
        tsA + [l for l in gA if l[0] != ID])  # remove duplicates and yourself
    GN = rem_dupl(tsN + [l for l in gN if l[0] != ID])
    GC = rem_dupl(tsC + [l for l in gC if l[0] != ID])
    GU = rem_dupl(tsU + [l for l in gU if l[0] != ID])

    print "\nTRANSMISSION AREA"
    print tsA
    print tsN
    print tsC
    print tsU

    print "\nNEIGHBORHOOD"
    print GA
    print GN
    print GC
    print GU

    os.remove(file2Send)
    os.remove(file_received)
    os.remove(ok_file)
    os.remove(ready_file)
    os.remove(file_received2)
    os.remove(vectorsSend)

    return (tsU, GA, GN, GU)
def gibbs_samp(ID, ips, sweeps, TsU, GsU, GsA, GsN, myA, myC, myU, rs,
               convergence_filename):

    for w in range(1, sweeps + 1):

        T = temperature(w, 2)
        winners = list()
        # sequential scheme = {3,4,5,6,...}
        # port used for 8000
        nodeList = [int(x) for x in ips.keys()]
        if not (w == 1 and ID == 3):

            # new info is of the format:
            # ID * neighID1 neighID2 ... * new_u_id * old_u_id
            # every time new info is added to the file
            # before sending file is cleaned from possible repetitions
            udp_receive.receive(
                ips[ID], 8000, '/root/total/final_gibbs_sampling/new_info.txt')
            winners = read_new_info(
                '/root/total/final_gibbs_sampling/new_info.txt', nodeList)
            data = get_new_vectors(winners)
            for n_vectors in data:
                ancestor_id = n_vectors[0]
                if ancestor_id in [i[0] for i in TsU]:  # ancestor_id in TsU
                    if n_vectors[2] != n_vectors[3]:  # change has happened
                        index = TsU.index(
                            [ancestor_id] + n_vectors[2]
                        )  # find index of u vector of the right node
                        TsU[index] = [ancestor_id
                                      ] + n_vectors[3]  # replace new value
                if ancestor_id in [i[0] for i in GsU]:
                    if n_vectors[2] != n_vectors[3]:  # change has happened
                        index = GsU.index(
                            [ancestor_id] + n_vectors[2]
                        )  # find index of u vector of the right node
                        GsU[index] = [ancestor_id
                                      ] + n_vectors[3]  # replace new value
                for j, node in enumerate([i[0] for i in GsN]):
                    if node in n_vectors[1]:
                        GsN[j] = [
                            k - g for k, g in zip(GsN[j], [ancestor_id] +
                                                  n_vectors[2])
                        ]  # subtract old u
                        GsN[j] = [
                            k + g for k, g in zip(GsN[j], [ancestor_id] +
                                                  n_vectors[3])
                        ]  # add new u

        else:
            time.sleep(1.5)
        u_star, prob = draw_sample(T, TsU, GsU, GsA, GsN, myA, myC, rs, myU)
        loc_energy = local_energy.energy_calc(TsU, GsU, GsA, GsN, u_star, myA,
                                              myC, rs, myU)
        c_f = open(convergence_filename, 'a')
        c_f.write(
            str(w) + '    ' + lst2str(u_star) + '    ' +
            str(round(loc_energy, 3)) + '    ' + str(round(prob, 3)) + '\n')
        c_f.close()

        affected = [i[0] for i in TsU]
        length = 6 + len(affected) + len(myU) * 2
        MYstring = str(ID) + ' * ' + lst2str(affected) + '* ' + lst2str(
            myU) + '* ' + lst2str(u_star) + '* ' + str(length)
        myU = u_star
        if w != 1:
            os.remove('/root/total/final_gibbs_sampling/new_info.txt')
        f = open('/root/total/final_gibbs_sampling/new_info.txt',
                 'w+')  ## must overwrite
        for w in winners:
            if int(w.split()[0]) != ID:
                f.write(w)
        f.write(MYstring)
        f.write('\n\n')
        f.close()
        copyfile('/root/total/final_gibbs_sampling/new_info.txt',
                 '/root/total/final_gibbs_sampling/myNew_info.txt')

        target = ID + 1  ## VISITING SCHEME
        tls = ips.keys()
        mx = max(ips.keys())
        tls.remove(mx)
        if target not in tls:
            target = sorted(ips.keys())[0]
        udp_transmit.transmit(ips[target], 8000,
                              '/root/total/final_gibbs_sampling/new_info.txt')
        os.remove('/root/total/final_gibbs_sampling/new_info.txt')

    return u_star
                dataread = f.readlines()
                for l in dataread:
                    f2.write(l)
                f.close()
            f2.close()
            winners = get_new_Us(fname, ips.keys())

            #construct filename2
            filename2 = open('new_data.txt', 'w+')
            print 'winnners = ', winners
            for w in winners:
               filename2.write(w)
            filename2.close()
        

            for n in nodes:
                print 'Sending at N1-' + str(n)
                udp_transmit.transmit(ips[n], udp_port, 'new_data.txt')
            print "Terminated iteration"
            
            for t in thread_array:
                t.join()
            
            #global counter
            counter = 0
            #os.remove(filename)
            os.remove('new_data.txt')
            os.remove('all_msgs.txt')
            for i in ids:
                os.remove(filename[:-4]+str(i)+'.txt')
def gibbs_samp(ID, ips, sweeps, TsU, GsU, GsA, GsN, myA, myC, myU, rs,
               convergence_filename):
    nodeList = [int(x) for x in ips.keys()]
    Ts_nodes = [n[0] for n in TsU]
    Gs_nodes = [n[0] for n in GsU]
    prob = 0
    for w in range(1, sweeps + 1):
        if (w != 1):
            winners = list()
            udp_receive.receive(
                ips[ID], 8000, '/root/total/final_gibbs_sampling/new_info.txt')
            winners = read_new_info(
                '/root/total/final_gibbs_sampling/new_info.txt', nodeList)
            data = get_new_vectors(winners)
            for n_vectors in data:
                node_id = n_vectors[0]
                if node_id in Ts_nodes:
                    if n_vectors[2] != n_vectors[3]:  # change has happened
                        index = TsU.index(
                            [node_id] + n_vectors[2])  # find index of u vector
                        TsU[index] = [node_id
                                      ] + n_vectors[3]  # replace new value
                if node_id in Gs_nodes:
                    if n_vectors[2] != n_vectors[3]:  # change has happened
                        index = GsU.index(
                            [node_id] + n_vectors[2])  # find index of u vector
                        GsU[index] = [node_id
                                      ] + n_vectors[3]  # replace new value
                for j, node in enumerate([i[0] for i in GsN]):
                    if node in n_vectors[1]:
                        GsN[j] = [
                            k - g
                            for k, g in zip(GsN[j], [node_id] + n_vectors[2])
                        ]
                        GsN[j] = [
                            k + g
                            for k, g in zip(GsN[j], [node_id] + n_vectors[3])
                        ]

        T = temperature(w, 2)
        tau = 0.2  # may need fine-tuning
        temp = bernoulli.rvs(tau, size=1)

        if (temp[0] == True):
            print "I will update my state!"
            u_star, prob = draw_sample(T, TsU, GsU, GsA, GsN, myA, myC, rs,
                                       myU)
        else:
            print "I will NOT update my state!"
            u_star = myU

        loc_energy = local_energy.energy_calc(TsU, GsU, GsA, GsN, u_star, myA,
                                              myC, rs, myU)
        c_f = open(convergence_filename, 'a')
        c_f.write(
            str(w) + '    ' + lst2str(u_star) + '    ' +
            str(round(loc_energy, 3)) + '    ' + str(round(prob, 3)) + '\n')
        c_f.close()
        print 'i picked u: ', u_star
        affected = Ts_nodes
        length = 6 + len(affected) + len(myU) * 2
        MYstring = str(ID) + ' * ' + lst2str(affected) + '* ' + lst2str(
            myU) + '* ' + lst2str(u_star) + '* ' + str(length)
        myU = u_star
        if w != 1:
            os.remove('/root/total/final_gibbs_sampling/new_info.txt')
        f = open('/root/total/final_gibbs_sampling/new_info.txt',
                 'w+')  ## must overwrite
        f.write(MYstring)
        f.write('\n\n')
        f.close()
        copyfile('/root/total/final_gibbs_sampling/new_info.txt',
                 '/root/total/final_gibbs_sampling/myNew_info.txt')
        mx = max(ips.keys())  #ID of controller
        time.sleep(1.9)
        udp_transmit.transmit(ips[mx], 8000 + int(ID),
                              '/root/total/final_gibbs_sampling/new_info.txt')
        os.remove('/root/total/final_gibbs_sampling/new_info.txt')

    return u_star
def one_iteration(filename, filename1, filename2, filename3, ID, nCh, neigh):

    with open('/root/total/ipnet.json') as jf:
        diction = json.load(jf)
        ips = diction['net']

    ips = {int(x): y.encode('utf8') for x, y in ips.items()}

    nodeList = [int(x) for x in ips.keys()]

    threadR_array = []
    threadS_array = []

    # in a topology a node opens up as many receivers
    # and transmitters as its neighbours number. Each is listening/transmitting
    # in a carefully selected port.

    # open receivers in threads
    for ip in neigh:
        snd = rev_lookup(ips, ip)
        port = int(snd) * 1000 + int(ID)
        # open a receiver
        temp = threading.Thread(target=udp_listen,
                                args=[
                                    ips[ID],
                                    port,
                                    filename2,
                                ])
        threadR_array.append(temp)
        # open a sender
        port = int(ID) * 1000 + int(snd)
        temp = threading.Thread(target=udp_send, args=[
            ip,
            port,
            filename3,
        ])
        threadS_array.append(temp)

    for t in threadR_array:
        t.start()

    # controller always has the maximum ID
    c_id = max(ips.keys())
    udp_transmit.transmit(ips[c_id], 13000 + ID, filename)
    udp_receive.receive(ips[ID], 13000, filename1)

    for t in threadS_array:
        t.start()

    global counter
    while (counter < (len(threadR_array) + len(threadS_array))):
        pass

    counter = 0

    winners, lA, lN, lC, lU = rv.getVectors(
        '/root/total/final_gibbs_sampling/dataReceived.txt', nCh, nodeList)

    os.remove(filename1)
    os.remove(filename2)
    os.remove(filename3)

    for t in threadR_array:
        t.join()

    for t in threadS_array:
        t.join()

    return (winners, lA, lN, lC, lU)
def udp_send(IP_address, udp_port, filename):
    udp_transmit.transmit(IP_address, udp_port, filename)
    global counter
    counter = counter + 1