<s> from __future__ import print_function <EOL> from collections import defaultdict <EOL> from threading import Thread , Lock <EOL> from Queue import Queue <EOL> import os <EOL> import time <EOL> import random <EOL> import traceback <EOL> from BroControl import config <EOL> from BroControl import version <EOL> from BroControl . broctl import BroCtl <EOL> from BroControl import ser as json <EOL> from BroControl import web <EOL> STOP_RUNNING = object ( ) <EOL> class TermUI : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def info ( self , msg ) : <EOL> print ( msg ) <EOL> warn = info <EOL> def error ( self , msg ) : <EOL> print ( "<STR_LIT>" , msg ) <EOL> class Logs : <EOL> def __init__ ( self ) : <EOL> self . store = defaultdict ( list ) <EOL> def append ( self , id , stream , txt ) : <EOL> self . store [ id ] . append ( ( stream , txt ) ) <EOL> def get ( self , id , since = <NUM_LIT:0> ) : <EOL> msgs = self . store . get ( id ) or [ ] <EOL> return msgs [ since : ] <EOL> class Common : <EOL> def dump ( self , * args ) : <EOL> return json . dumps ( * args ) <EOL> def load ( self , msg ) : <EOL> return json . loads ( msg ) <EOL> class BroCtrldWorker ( Thread , Common ) : <EOL> def __init__ ( self , command_queue ) : <EOL> self . q = Queue ( ) <EOL> Thread . __init__ ( self ) <EOL> self . daemon = True <EOL> self . command_queue = command_queue <EOL> self . _id = None <EOL> def send ( self , id , cmd , * args ) : <EOL> self . q . put ( ( id , cmd , args ) ) <EOL> def run ( self ) : <EOL> self . broctl = BroCtl ( ui = TermUI ( ) ) <EOL> self . broctl . ui = self <EOL> self . broctl . controller . ui = self <EOL> self . broctl . executor . ui = self <EOL> while True : <EOL> if self . iteration ( ) : <EOL> return <EOL> def noop ( self , * args , ** kwargs ) : <EOL> return True <EOL> def iteration ( self ) : <EOL> id , cmd , args = self . q . get ( ) <EOL> self . _id = id <EOL> if cmd is STOP_RUNNING : <EOL> return True <EOL> func = getattr ( self . broctl , cmd , self . noop ) <EOL> def respond ( r ) : <EOL> self . call ( "<STR_LIT:result>" , id , r ) <EOL> if not hasattr ( func , '<STR_LIT>' ) : <EOL> return respond ( "<STR_LIT>" ) <EOL> try : <EOL> res = func ( * args ) <EOL> except Exception as e : <EOL> res = traceback . format_exc ( ) <EOL> respond ( res ) <EOL> def call ( self , func , * args ) : <EOL> self . command_queue . put ( ( None , func , args ) ) <EOL> def info ( self , msg ) : <EOL> return self . call ( "<STR_LIT:info>" , self . _id , msg ) <EOL> def error ( self , msg ) : <EOL> return self . call ( "<STR_LIT>" , self . _id , msg ) <EOL> warn = error <EOL> class BroCtld ( Thread , Common ) : <EOL> def __init__ ( self , command_queue , logs ) : <EOL> Thread . __init__ ( self ) <EOL> self . daemon = True <EOL> self . logs = logs <EOL> self . command_queue = command_queue <EOL> self . worker = BroCtrldWorker ( self . command_queue ) <EOL> self . results = { } <EOL> self . running = True <EOL> self . id_gen = iter ( range ( <NUM_LIT> ) ) . next <EOL> self . init ( ) <EOL> def init ( self ) : <EOL> self . worker . start ( ) <EOL> def recv ( self ) : <EOL> msg = self . command_queue . get ( ) <EOL> print ( "<STR_LIT>" , msg ) <EOL> return msg <EOL> def run ( self ) : <EOL> return self . _run ( ) <EOL> def handle_result ( self , id , result ) : <EOL> print ( "<STR_LIT>" % ( id , result ) ) <EOL> self . results [ id ] = result <EOL> return "<STR_LIT>" <EOL> def handle_out ( self , id , txt ) : <EOL> print ( "<STR_LIT>" % ( '<STR_LIT>' , id , txt ) ) <EOL> self . logs . append ( id , '<STR_LIT>' , txt ) <EOL> return "<STR_LIT>" <EOL> def handle_info ( self , id , txt ) : <EOL> print ( "<STR_LIT>" % ( '<STR_LIT:info>' , id , txt ) ) <EOL> self . logs . append ( id , '<STR_LIT:info>' , txt ) <EOL> return "<STR_LIT>" <EOL> def handle_err ( self , id , txt ) : <EOL> print ( "<STR_LIT>" % ( '<STR_LIT>' , id , txt ) ) <EOL> self . logs . append ( id , '<STR_LIT>' , txt ) <EOL> return "<STR_LIT>" <EOL> def handle_getresult ( self , id ) : <EOL> result = self . results . get ( id ) <EOL> if result : <EOL> del self . results [ id ] <EOL> print ( "<STR_LIT>" % ( result , id ) ) <EOL> return result <EOL> def handle_getlog ( self , id , since ) : <EOL> result = self . logs . get ( id , since ) <EOL> print ( "<STR_LIT>" % ( result , id ) ) <EOL> return result <EOL> def _run ( self ) : <EOL> while self . running : <EOL> ( rq , cmd , args ) = self . recv ( ) <EOL> func = getattr ( self , '<STR_LIT>' + cmd , None ) <EOL> if func : <EOL> res = func ( * args ) <EOL> if rq : <EOL> rq . put ( res ) <EOL> else : <EOL> t_id = self . send_to_worker ( cmd , * args ) <EOL> rq . put ( t_id ) <EOL> def send_to_worker ( self , cmd , * args ) : <EOL> t_id = self . id_gen ( ) <EOL> self . worker . send ( t_id , cmd , * args ) <EOL> print ( "<STR_LIT>" % ( t_id , cmd , args ) ) <EOL> return t_id <EOL> class Client ( Common ) : <EOL> def __init__ ( self , command_queue ) : <EOL> self . command_queue = command_queue <EOL> def call ( self , func , * args ) : <EOL> rq = Queue ( ) <EOL> self . command_queue . put ( ( rq , func , args ) ) <EOL> return rq . get ( ) <EOL> def sync_call ( self , func , * args ) : <EOL> id = self . call ( func , * args ) <EOL> print ( "<STR_LIT>" , id ) <EOL> while True : <EOL> res = self . getresult ( id ) <EOL> if res : <EOL> return res <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> def result ( self , id , result ) : <EOL> return self . call ( "<STR_LIT:result>" , id , result ) <EOL> def getresult ( self , id ) : <EOL> return self . call ( "<STR_LIT>" , id ) <EOL> def getlog ( self , id , since = <NUM_LIT:0> ) : <EOL> return self . call ( "<STR_LIT>" , id , since ) <EOL> def main ( basedir = '<STR_LIT>' ) : <EOL> logs = Logs ( ) <EOL> command_queue = Queue ( ) <EOL> d = BroCtld ( command_queue , logs ) <EOL> d . start ( ) <EOL> c = Client ( command_queue ) <EOL> ww = Thread ( target = web . run_app , args = [ c ] ) <EOL> ww . start ( ) <EOL> print ( "<STR_LIT>" , c . sync_call ( '<STR_LIT:status>' ) ) <EOL> for x in range ( <NUM_LIT:20> ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
