<s> import os <EOL> import time <EOL> import logging <EOL> from sts . replay_event import WaitTime <EOL> from sts . syncproto . base import SyncTime <EOL> from sts . util . convenience import timestamp_string <EOL> import sts . dataplane_traces . trace_generator as tg <EOL> replay_config_template = '''<STR_LIT>''' <EOL> interactive_replay_config_template = '''<STR_LIT>''' <EOL> openflow_replay_config_template = '''<STR_LIT>''' <EOL> mcs_config_template = '''<STR_LIT>''' <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> class InputLogger ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . last_time = SyncTime . now ( ) <EOL> self . _disallow_timeouts = False <EOL> self . _events_after_close = [ ] <EOL> self . output = None <EOL> self . output_path = "<STR_LIT>" <EOL> def open ( self , results_dir = None , output_filename = "<STR_LIT>" ) : <EOL> if results_dir is not None : <EOL> self . output_path = results_dir + "<STR_LIT:/>" + output_filename <EOL> self . replay_cfg_path = results_dir + "<STR_LIT>" <EOL> self . mcs_cfg_path = results_dir + "<STR_LIT>" <EOL> self . interactive_replay_cfg_path = results_dir + "<STR_LIT>" <EOL> self . openflow_replay_cfg_path = results_dir + "<STR_LIT>" <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . output = open ( self . output_path , '<STR_LIT:w>' ) <EOL> def disallow_timeouts ( self ) : <EOL> self . _disallow_timeouts = True <EOL> def allow_timeouts ( self ) : <EOL> self . _disallow_timeouts = False <EOL> def _serialize_event ( self , event , output ) : <EOL> if self . _disallow_timeouts and hasattr ( event , "<STR_LIT>" ) : <EOL> event . timeout_disallowed = True <EOL> self . last_time = event . time <EOL> json_hash = event . to_json ( ) <EOL> log . debug ( "<STR_LIT>" % event ) <EOL> output . write ( json_hash + '<STR_LIT:\n>' ) <EOL> def log_input_event ( self , event ) : <EOL> '''<STR_LIT>''' <EOL> if not self . output : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not self . output . closed : <EOL> self . _serialize_event ( event , self . output ) <EOL> else : <EOL> self . _events_after_close . append ( event ) <EOL> def dump_buffered_events ( self , events ) : <EOL> '''<STR_LIT>''' <EOL> with open ( self . output_path + "<STR_LIT>" , '<STR_LIT:w>' ) as output : <EOL> for event in events + self . _events_after_close : <EOL> self . _serialize_event ( event , output ) <EOL> def close ( self , control_flow , simulation_cfg , skip_mcs_cfg = False ) : <EOL> self . log_input_event ( WaitTime ( <NUM_LIT:1.0> , time = self . last_time ) ) <EOL> self . output . close ( ) <EOL> path_templates = [ ( self . replay_cfg_path , replay_config_template ) , <EOL> ( self . interactive_replay_cfg_path , interactive_replay_config_template ) , <EOL> ( self . openflow_replay_cfg_path , openflow_replay_config_template ) ] <EOL> if not skip_mcs_cfg : <EOL> path_templates . append ( ( self . mcs_cfg_path , mcs_config_template ) ) <EOL> wait_on_deterministic_values = False <EOL> if hasattr ( control_flow . sync_callback , "<STR_LIT>" ) : <EOL> wait_on_deterministic_values = control_flow . sync_callback . record_deterministic_values <EOL> delay_flow_mods = False <EOL> if hasattr ( control_flow , "<STR_LIT>" ) : <EOL> delay_flow_mods = control_flow . delay_flow_mods <EOL> bug_signature = "<STR_LIT>" <EOL> if hasattr ( control_flow , "<STR_LIT>" ) : <EOL> bug_signature = control_flow . bug_signature <EOL> for path , template in path_templates : <EOL> with open ( path , '<STR_LIT:w>' ) as cfg_out : <EOL> config_string = template % ( str ( simulation_cfg ) , <EOL> self . output_path , <EOL> str ( wait_on_deterministic_values ) , <EOL> str ( delay_flow_mods ) , <EOL> "<STR_LIT>" % str ( control_flow . invariant_check_name ) , <EOL> '<STR_LIT>' % str ( bug_signature ) ) <EOL> cfg_out . write ( config_string ) </s>
