예제 #1
0
def cognitive_radio_loop(options, radio, channel_list):
    """
    Program loop here.
    @param options
    @param radio A RadioDevice instance.
    @param channel_list List of Channel objects.
    """

    # Export my server
    command = callback_radio(radio)

    my_rpc = RPCExporter(addr=("143.54.83.30", 8000 + options.my_id))
    my_rpc.register_function('command', command)
    my_rpc.start()

    # Wait broker start
    while not command.run:
        1

    ####
    #Import OTHER RADIOS RPCS
    ####
    rpc_arr = []
    for i in [0, 1, 2, 3]:
            rpc_cli = RPCImporter(addr="http://%s:%d" % (HOSTS_IP[i], 8000 + i))
            rpc_cli.register_function('command')
            rpc_arr.append(rpc_cli)

    # Import PassiveRadio RPC calls
    bs_rpc = RPCImporter(addr="http://%s:9000" % (options.broker_ip))
    bs_rpc.register_function('command')

    # Register parameters for transmission
    Logger.register('radio', ['tx_pkts', 'rx_pkts', 'rx2_pkts', 'channel', 'operation', 'receiver', 'starving',
                              'total_tx', 'total_rx', 'total_starving'])
    # loop
    pkt_len = options.pkt_len
    payload = struct.pack('%sB' % pkt_len, *[options.my_id] * pkt_len)

    print '##### Entering Transmitter loop'
    c_starving = 0

    while command.run:
        """
        ######## FUNCOES:

        ---- BW do canal
        ---- Channel's bandwidth.
        radio.get_bandwidth()

        ---- Num. simbolos na modulacao
        --- Number of symbols in the modulation.
        radio.{tx,rx}.symbols()

        ---- B/S da modulacao
        ---- B/S of the modulation
        radio.{tx,rx}.bits_per_symbol()

        ---- Pkt/s NO ULTIMO SEGUNDO
        ---- Pkt/s in the last second.
        radio.{tx,rx}.counter.get_pkts()

        ---- b/s NO ULTIMO SEGUNDO.
        ---- b/s in the last second.
        radio.{tx,rx}.counter.get_bps()

        ---- Pacotes acumulados desde a ultima chamada.
        ---- Accumulated packages since the last call.
        radio.{tx,rx}.counter.get_pkt_accumulated(clear = False)

        ---- Troca de canal. channel eh um objeto Channel
        ---- Channel changing. channel is a Channel object.
        radio.set_channel(channel)
        #################
        """

        # sense
        while not command.sense and command.run:
            1
        if not command.run:
            break

        sense_data = []

        radio.set_gain(0)
        for channel in channel_list:
            decision, energy = radio.ss.sense_channel(channel, 0.1)
            sense_data.append((decision, float(energy), channel.get_channel()))

        bs_rpc.command([options.my_id, 'sense_data', sense_data])

        # CHOOSE RECEIVER
        #while not command.request_transmission:
        #    1

        # Select a receiver randomly
        #opt  = [0, 1, 2, 3]
        #opt.remove(options.my_id)
        #receiver = random.choice(opt)
        #print '##### DEVICE %d requesting TX to %d' % (options.my_id, receiver)

        #bs_rpc.command([options.my_id, 'request_tx', receiver])
        #radio.set_gain(radios_gain[radio.id])

        # REQUEST CHANNEL
        while not command.request_channel:
            1
        receiver, operation, channel_idx = bs_rpc.command([options.my_id, 'request_channel', 9999])

        # Configure radio. Go to distant frequency if 'idle'
        if channel_idx > -1 or operation == OPERATIONS['idle']:
            if channel_idx > -1:
                radio.set_channel(channel_list[channel_idx])
            else:
                radio.set_channel(CHANNEL_IDLE)

        while not command.transmission:
            1

        # tx pkts is the number of packets transmitted
        # globs.instant_rx is the number of packets received.
        #   Is global cause it is hard to be synchronized with the TX dev when WE are the RX
        #   The easiest way is to let the TX dev control when get this value. And we do it in the RPC server method
        tx_pkts = globs.instant_rx = 0

        if operation == OPERATIONS['tx']:
            # change rx freq to another freq or we will receiver our own packets
            radio.rx.radio.set_channel(CHANNEL_IDLE)
            print "##### ... %d - TRANSMITTING - CHANNEL %d - TO: %d" % (options.my_id, channel_idx, receiver)
            tx_pkts = PktSender.flood_by_time(duration=options.sending_duration,
                                              tx_pkt_arch=radio.tx,
                                              payload=payload)
            globs.instant_rx = rpc_arr[receiver].command((options.my_id, 'get_accumulated', True))

            print "##### ... Transmitted %d/%d pkts in Channel %d" % (tx_pkts, globs.instant_rx, channel_idx)
            globs.total_tx += tx_pkts
            c_starving = 0

        elif operation == OPERATIONS['rx']:
            print "##### ... %d - RECEIVING - CHANNEL %d" % (options.my_id, channel_idx)
            receiver = 10

            globs.wait_for_get = True
            while globs.wait_for_get:
                time.sleep(0.2)

            tx_pkts = 0
            globs.total_rx += globs.instant_rx

            c_starving += 1
            globs.total_starving += 1
        elif operation == OPERATIONS['idle']:
            print '##### ... %d is IDLE' % options.my_id
            receiver = -1
            time.sleep(options.sending_duration)
            c_starving += 1
            globs.total_starving += 1

        bs_rpc.command([options.my_id, 'transmission_res', (tx_pkts, globs.instant_rx)])

        _idle = 0 if operation != OPERATIONS['idle'] else -1
        Logger.append('radio', 'tx_pkts', tx_pkts if _idle > -1 else _idle)
        Logger.append('radio', 'rx_pkts',  globs.instant_rx if operation == OPERATIONS['tx'] else _idle)
        Logger.append('radio', 'rx2_pkts', globs.instant_rx if operation == OPERATIONS['rx'] else _idle)
        Logger.append('radio', 'channel', channel_idx)
        Logger.append('radio', 'operation', operation)
        Logger.append('radio', 'receiver', receiver)
        Logger.append('radio', 'starving', c_starving)

    Logger.set('radio', 'total_tx', globs.total_tx)
    Logger.set('radio', 'total_rx', globs.total_rx)
    Logger.set('radio', 'total_starving', globs.total_starving)
예제 #2
0
def server_loop(options):
    """
    Receiver radios in this loop
    @param options
    """

    # Export my 'command' function to be accessible via RPC
    my_rpc = RPCExporter(addr=("143.54.83.30", 9000))
    command = server_callback(globs.channel_list)
    my_rpc.register_function('command', command)
    my_rpc.start()

    interferer_rpc = RPCImporter(addr="http://143.54.83.30:9001")
    interferer_rpc.register_function('command')

    ####
    #Import OTHER RADIOS RPCS
    ####

    for i in DEVICES:
        rpc_cli = RPCImporter(addr="http://143.54.83.30:%d" % (8000 + i))
        rpc_cli.register_function('command')
        globs.rpc_arr.append(rpc_cli)

    def command_sense(val):
        """
        @param val
        """
        [x.command([BS_ID, 'sense', val]) for x in globs.rpc_arr]

    def command_request_transmission(val):
        """
        @param val
        """
        [x.command([BS_ID, 'request_transmission', val]) for x in globs.rpc_arr]

    def command_request_channel(val):
        """
        @param val
        """
        [x.command([BS_ID, 'request_channel', val]) for x in globs.rpc_arr]

    def command_transmission(val):
        """
        @param val
        """
        [x.command([BS_ID, 'transmission', val]) for x in globs.rpc_arr]

    # Start execution on all RC nodes
    interferer_rpc.command([BS_ID, 'run', True])
    [x.command([BS_ID, 'run', True]) for x in globs.rpc_arr]

    t_fin = time.time() + options.test_duration
    while t_fin > time.time():
        t_it = time.time()

        print "!!!!! SENSING PHASE"
        command_sense(True)
        while not globs.sense_done:
            1
        globs.sense_done = False

        command_sense(False)
        print "!!!!! TRANSMISSION PHASE"
        command_transmission(True)
        globs.p_fin_transmission_time = time.time() + options.sending_duration
        while not globs.transmission_done:
            1
        globs.p_fin_transmission_time = None

        ## moment to evaluate links
        # HERE
        ####
        # clear all used  data
        globs.transmission_done = False

        globs.clear()
        globs.p_total_iterations += 1

        command_transmission(False)
        Logger.append('bs', 'it_dur', time.time() - t_it)

    # Stop execution on all CR nodes
    interferer_rpc.command([BS_ID, 'run', False])
    [x.command([BS_ID, 'run', False]) for x in globs.rpc_arr]
예제 #3
0
def cognitive_radio_loop(options, radio, channel_list):
    """
    Program loop here.
    @param options
    @param radio A RadioDevice instance.
    @param channel_list List of Channel objects.
    """

    # Export my server
    command = callback_radio(radio)

    my_rpc = RPCExporter(addr=("143.54.83.30", 8000 + options.my_id))
    my_rpc.register_function('command', command)
    my_rpc.start()

    # Wait broker start
    while not command.run:
        1

    ####
    #Import OTHER RADIOS RPCS
    ####
    rpc_arr = []
    RADIOS = [0, 3]
    for i in RADIOS:
            rpc_cli = RPCImporter(addr="http://%s:%d" % (HOSTS_IP[i], 8000 + i))
            rpc_cli.register_function('command')
            rpc_arr.append(rpc_cli)

    # Import PassiveRadio RPC calls
    bs_rpc = RPCImporter(addr="http://%s:9000" % (options.broker_ip))
    bs_rpc.register_function('command')

    # Register parameters for transmission
    Logger.register('radio', ['tx_pkts', 'rx_pkts', 'rx2_pkts' , 'channel', 'operation', 'receiver', 'starving',
                              'total_tx', 'total_rx', 'total_starving'])
    # loop
    pkt_len = options.pkt_len
    payload = struct.pack('%sB' % pkt_len, *[options.my_id] * pkt_len)

    print '##### Entering Transmitter loop'
    c_starving = 0

    while command.run:
        """
        ######## FUNCOES:
        ######## Functions:

        ---- BW do canal
        ---- channel's bandwidth.
        radio.get_bandwidth()

        ---- Num. simbolos na modulacao
        ---- Number of symbols in the modulation.
        radio.{tx,rx}.symbols()

        ---- B/S da modulacao
        ---- B/S of the modulation.
        radio.{tx,rx}.bits_per_symbol()

        ---- Pkt/s NO ULTIMO SEGUNDO
        ---- Pkt/s in the last second.
        radio.{tx,rx}.counter.get_pkts()

        ---- b/s NO ULTIMO SEGUNDO.
        ---- b/s in the last second.
        radio.{tx,rx}.counter.get_bps()

        ---- Pacotes acumulados desde a ultima chamada.
        ---- Accumulated packages since the last call.
        radio.{tx,rx}.counter.get_pkt_accumulated(clear = False)

        ---- Troca de canal. channel eh um objeto Channel
        ---- Channel changing. channel is a Channel object
        radio.set_channel(channel)
        #################
        """

        # sense
        while not command.sense and command.run:
            1
        if not command.run:
            break

        sense_data = []

        radio.set_gain(0)
        for channel in channel_list:
            decision, energy = radio.ss.sense_channel(channel, 0.1)
            sense_data.append((decision, float(energy), channel.get_channel()))
            print 'Channel energy: ', energy

        bs_rpc.command([options.my_id, 'sense_data', sense_data])

        while not command.transmission:
            1

        # tx pkts is the number of packets transmitted
        # globs.instant_rx is the number of packets received.
        #   Is global cause it is hard to be synchronized with the TX dev when WE are the RX
        #   The easiest way is to let the TX dev control when get this value. And we do it in the RPC server method
        tx_pkts = globs.instant_rx = 0

        time.sleep(options.sending_duration)
        bs_rpc.command([options.my_id, 'transmission_res', (tx_pkts, globs.instant_rx)])

    Logger.set('radio', 'total_tx', globs.total_tx)
    Logger.set('radio', 'total_rx', globs.total_rx)
    Logger.set('radio', 'total_starving', globs.total_starving)