def test():
    """ Sanity check. """
    
    proxy_client = StateProxyClient(FLEXI_CONTROLLER_HOST)
    proxy_client.reset()
    send_trigger_packet()
    
#    print '*' * 80
#    print 'ingress -> pkt-in'
#    print '*' * 80
#    
#    ingress = GenerateIngress(300, proxy_client)
#    pkt_in = ReceivePktIn(proxy_client)
#    
#    pkt_in.start()
#    ingress.start()
#    util.verbose_sleep(20, 'Ingress -> pkt_in...')
#    ingress.stop()
#    pkt_in.stop()
#    
#    print 'sent pps:', ingress.get_sent_pps()
#    print 'recvd pps:', pkt_in.get_received_pps()
#
#    print '*' * 80
#    print 'flow-mod -> rules'
#    print '*' * 80
#
#    flow_mod = GenerateFlowMod(200, proxy_client)
#    check_rule = CheckRuleInstallationRate(proxy_client, flow_stat_interval=10, steady_state_start=30, steady_state_end=60)
#    
#    check_rule.start()
#    flow_mod.start()
#    util.verbose_sleep(60, 'flow-mod -> rules')
#    flow_mod.stop()
#    check_rule.stop()
#
#    print 'sent pps:', flow_mod.get_sent_pps()
#    print 'recvd pps:', check_rule.get_received_pps()

    print '*' * 80
    print 'pkt-out -> egress'
    print '*' * 80

    pkt_out = GeneratePktOut(200, proxy_client, packet_size=1500)
    egress = ReceiveEgress(proxy_client)

    egress.start()
    pkt_out.start()
    util.verbose_sleep(5, 'pkt-out -> egress')
    pkt_out.stop()
    egress.stop()
    
    print 'sent pps:', pkt_out.get_sent_pps()
    print 'recvd pps:', egress.get_received_pps()
def run(packet_size=1500):
    
    # Writer initial header to file.
    #result_file = './data/hp_sensitivity_%d_byte.csv' % packet_size # HP
    #result_file = './data/evs_verify_sensitivity_%d_byte.csv' % packet_size # EVS
    result_file = './data/monaco_sensitivity_%d_byte.csv' % packet_size # MONACO
    with open(result_file, 'w') as f:
        print >> f, 'ingress_pps,flow_mod_pps,pkt_out_pps,pkt_in_pps,rule_pps,egress_pps,expected_ingress,expected_flow_mod,expected_pkt_out'
    
    #input_list = [10, 100, 400, 700, 1000]  # HP
    #input_list = [10, 100, 1000]  # OVS
    
    #input_list = [10, 100, 400, 700, 1000]
    input_list = [10,30,100,320,330]
    
    for ingress_pps in input_list:
        for flow_mod_pps in input_list:
            for pkt_out_pps in input_list:

                # Ignore certain params. TODO: Debug.
#                if packet_size == 1500:
#                    if _param_hash(ingress_pps, flow_mod_pps, pkt_out_pps) <= \
#                        _param_hash(400, 1000, 10):
#                        continue


                for attempt in range(3):
                    try:
        
                        start_controller()
                        
                        while True:
                            try:
                                proxy_client = StateProxyClient(FLEXI_CONTROLLER_HOST)
                                proxy_client.hello()
                                break
                            except:
                                print 'Waiting for controller...'
                                time.sleep(2)
                        
                        proxy_client.reset()
                        print proxy_client.getall()
                        send_trigger_packet()
        
                        # Confirm trigger.
                        while not proxy_client.run('trigger_event_is_ready'):
                            print proxy_client.getall()
                            print 'Waiting for trigger...'
                            time.sleep(2)
        
                        # Set up the pkt generators    
                        ingress = GenerateIngress(ingress_pps, proxy_client, packet_size=packet_size)    
                        flow_mod = GenerateFlowMod(flow_mod_pps, proxy_client)    
                        pkt_out = GeneratePktOut(pkt_out_pps, proxy_client, packet_size=packet_size)
        
                        # Set up pkt receivers.
                        pkt_in = ReceivePktIn(proxy_client)
                        check_rule = CheckRuleInstallationRate(proxy_client)
                        egress = ReceiveEgress(proxy_client)
                        
                        print proxy_client.getall()
                        
                        # Start receiving and sending.
                        for obj in [pkt_in, check_rule, egress, ingress, flow_mod, pkt_out]:
                            obj.start()
        
                        # Wait.
                        prompt = '(ingress_pps, flow_mod_pps, pkt_out_pps) = '
                        prompt += str((ingress_pps, flow_mod_pps, pkt_out_pps))
                        util.verbose_sleep(MAX_RUNNING_TIME, prompt)
                            
                        # Stop sending and receiving.
                        for obj in [ingress, flow_mod, pkt_out, pkt_in, check_rule, egress]:
                            obj.stop()
                            
                        # Gather data.
                        data_list = [ingress.get_sent_pps(),
                                     flow_mod.get_sent_pps(),
                                     pkt_out.get_sent_pps(),
                                     pkt_in.get_received_pps(),
                                     check_rule.get_received_pps(),
                                     egress.get_received_pps(),
                                     ingress_pps,
                                     flow_mod_pps, 
                                     pkt_out_pps]
                        
                        # Write csv data.
                        data = ','.join(['%.4f' % pps for pps in data_list])  
                        with open(result_file, 'a') as f:
                            print >> f, data
                            
                        print '*' * 80
                        print data
                        print '*' * 80
                        
                        proxy_client.exit()                                                
                        util.verbose_sleep(5, 'Waiting for the next experiment...')
                        break
                
                    except:
                        
                        proxy_client.exit()
                        
                        if attempt == 2:
                            raise
                        else:
                            with open('./data/CRASH.log', 'a') as crash_f:
                                print >> crash_f, traceback.format_exc()