<s> import logging <EOL> import time <EOL> import os <EOL> import socket <EOL> from pox . core import core , UpEvent <EOL> from pox . lib . graph . nom import Switch , Host , Link <EOL> from pox . lib . graph . util import NOMEncoder <EOL> from sts . util . io_master import IOMaster <EOL> from sts . syncproto . base import SyncTime , SyncMessage , SyncProtocolSpeaker , SyncIODelegate <EOL> from pox . lib . util import parse_openflow_uri <EOL> from pox . lib . recoco import Task , Select <EOL> from logging import Logger <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> def launch ( interpose_on_logging = True , blocking = False ) : <EOL> interpose_on_logging = str ( interpose_on_logging ) . lower ( ) == "<STR_LIT:true>" <EOL> blocking = str ( blocking ) . lower ( ) == "<STR_LIT:true>" <EOL> if "<STR_LIT>" in os . environ : <EOL> sts_sync = os . environ [ "<STR_LIT>" ] <EOL> log . info ( "<STR_LIT>" % sts_sync ) <EOL> io_master = POXIOMaster ( ) <EOL> io_master . start ( core . scheduler ) <EOL> sync_master = POXSyncMaster ( io_master , <EOL> interpose_on_logging = interpose_on_logging , <EOL> blocking = blocking ) <EOL> sync_master . start ( sts_sync ) <EOL> else : <EOL> log . info ( "<STR_LIT>" ) <EOL> class POXIOMaster ( IOMaster , Task ) : <EOL> """<STR_LIT>""" <EOL> _select_timeout = <NUM_LIT:5> <EOL> def __init__ ( self ) : <EOL> IOMaster . __init__ ( self ) <EOL> Task . __init__ ( self ) <EOL> def run ( self ) : <EOL> while True : <EOL> read_sockets , write_sockets , exception_sockets = self . grab_workers_rwe ( ) <EOL> rlist , wlist , elist = yield Select ( read_sockets , write_sockets , exception_sockets , self . _select_timeout ) <EOL> self . handle_workers_rwe ( rlist , wlist , elist ) <EOL> class POXSyncMaster ( object ) : <EOL> def __init__ ( self , io_master , interpose_on_logging = True , blocking = True ) : <EOL> self . _in_get_time = False <EOL> self . io_master = io_master <EOL> self . interpose_on_logging = interpose_on_logging <EOL> self . blocking = blocking <EOL> self . core_up = False <EOL> core . addListener ( UpEvent , self . handle_UpEvent ) <EOL> def handle_UpEvent ( self , _ ) : <EOL> self . core_up = True <EOL> def start ( self , sync_uri ) : <EOL> self . connection = POXSyncConnection ( self . io_master , sync_uri ) <EOL> self . connection . listen ( ) <EOL> self . connection . wait_for_connect ( ) <EOL> self . patch_functions ( ) <EOL> def patch_functions ( self ) : <EOL> if hasattr ( time , "<STR_LIT>" ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> time . _orig_time = time . time <EOL> time . time = self . get_time <EOL> if self . interpose_on_logging : <EOL> Logger . _orig_log = Logger . _log <EOL> def new_log ( log_self , level , msg , * args , ** kwargs ) : <EOL> Logger . _orig_log ( log_self , level , msg , * args , ** kwargs ) <EOL> if self . blocking and self . core_up : <EOL> print "<STR_LIT>" <EOL> self . state_change ( msg , * args ) <EOL> Logger . _log = new_log <EOL> def get_time ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _in_get_time : <EOL> return time . _orig_time ( ) <EOL> try : <EOL> self . _in_get_time = True <EOL> time_array = self . connection . request ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> sync_time = SyncTime ( * time_array ) <EOL> return sync_time . as_float ( ) <EOL> finally : <EOL> self . _in_get_time = False <EOL> def state_change ( self , msg , * args ) : <EOL> '''<STR_LIT>''' <EOL> args = [ str ( s ) for s in args ] <EOL> if self . blocking and self . core_up : <EOL> self . connection . sync_notification ( "<STR_LIT>" , msg , args ) <EOL> print "<STR_LIT>" <EOL> else : <EOL> self . connection . async_notification ( "<STR_LIT>" , msg , args ) <EOL> class POXSyncConnection ( object ) : <EOL> def __init__ ( self , io_master , sync_uri ) : <EOL> ( self . mode , self . host , self . port ) = parse_openflow_uri ( sync_uri ) <EOL> self . io_master = io_master <EOL> self . speaker = None <EOL> def listen ( self ) : <EOL> if self . mode != "<STR_LIT>" : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> listen_socket = socket . socket ( ) <EOL> listen_socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> host = self . host if self . host else "<STR_LIT>" <EOL> listen_socket . bind ( ( host , self . port ) ) <EOL> listen_socket . listen ( <NUM_LIT:1> ) <EOL> self . listen_socket = listen_socket <EOL> def wait_for_connect ( self ) : <EOL> log . info ( "<STR_LIT>" % ( self . host , self . port ) ) <EOL> ( socket , _ ) = self . listen_socket . accept ( ) <EOL> log . info ( "<STR_LIT>" ) <EOL> self . speaker = POXSyncProtocolSpeaker ( SyncIODelegate ( self . io_master , socket ) ) <EOL> def request ( self , messageClass , name ) : <EOL> if self . speaker : <EOL> return self . speaker . sync_request ( messageClass = messageClass , name = name ) <EOL> else : <EOL> log . warn ( "<STR_LIT>" ) <EOL> def async_notification ( self , messageClass , fingerPrint , value ) : <EOL> if self . speaker : <EOL> self . speaker . async_notification ( messageClass , fingerPrint , value ) <EOL> else : <EOL> log . warn ( "<STR_LIT>" ) <EOL> def sync_notification ( self , messageClass , fingerPrint , value ) : <EOL> if self . speaker : <EOL> self . speaker . sync_notification ( messageClass , fingerPrint , value ) <EOL> else : <EOL> log . warn ( "<STR_LIT>" ) <EOL> class POXSyncProtocolSpeaker ( SyncProtocolSpeaker ) : <EOL> def __init__ ( self , io_delegate = None ) : <EOL> self . snapshotter = POXNomSnapshotter ( ) <EOL> handlers = { <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) : self . _get_nom_snapshot , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) : self . _link_discovery <EOL> } <EOL> SyncProtocolSpeaker . __init__ ( self , handlers , io_delegate ) <EOL> def _get_nom_snapshot ( self , message ) : <EOL> snapshot = self . snapshotter . get_snapshot ( ) <EOL> response = SyncMessage ( type = "<STR_LIT>" , messageClass = "<STR_LIT>" , time = SyncTime . now ( ) , xid = message . xid , value = snapshot ) <EOL> self . send ( response ) <EOL> def _link_discovery ( self , message ) : <EOL> link = message . value <EOL> core . openflow_discovery . install_link ( link [ <NUM_LIT:0> ] , link [ <NUM_LIT:1> ] , link [ <NUM_LIT:2> ] , link [ <NUM_LIT:3> ] ) <EOL> class POXNomSnapshotter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . encoder = NOMEncoder ( ) <EOL> def get_snapshot ( self ) : <EOL> nom = { "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] } <EOL> for s in core . topology . getEntitiesOfType ( Switch ) : <EOL> nom [ "<STR_LIT>" ] . append ( self . encoder . encode ( s ) ) <EOL> for h in core . topology . getEntitiesOfType ( Host ) : <EOL> nom [ "<STR_LIT>" ] . append ( self . encoder . encode ( h ) ) <EOL> for l in core . topology . getEntitiesOfType ( Link ) : <EOL> nom [ "<STR_LIT>" ] . append ( self . encoder . encode ( l ) ) <EOL> return nom </s>
