<s> import cPickle as pickle <EOL> import time <EOL> from traits . api import HasTraits , Instance , Dict , Str , Int , Property , WeakRef <EOL> from pyface . gui import GUI <EOL> import zmq <EOL> class ZmqProvider ( HasTraits ) : <EOL> application = WeakRef <EOL> host = Str ( '<STR_LIT:127.0.0.1>' ) <EOL> data_port = Int ( <NUM_LIT> ) <EOL> handshake_port = Int ( <NUM_LIT> ) <EOL> poll_period = Int ( <NUM_LIT> ) <EOL> poll_timeout = Int ( <NUM_LIT> ) <EOL> data_string = Property <EOL> handshake_string = Property <EOL> _zmq_context = Instance ( '<STR_LIT>' ) <EOL> _poller = Instance ( '<STR_LIT>' ) <EOL> _handshake_socket = Instance ( '<STR_LIT>' ) <EOL> _data_socket = Instance ( '<STR_LIT>' ) <EOL> _pid_mapping = Dict ( Int , WeakRef ) <EOL> def _get_handshake_string ( self ) : <EOL> return '<STR_LIT>' . format ( self . host , self . handshake_port ) <EOL> def _get_data_string ( self ) : <EOL> return '<STR_LIT>' . format ( self . host , self . data_port ) <EOL> def start ( self ) : <EOL> self . _zmq_context = zmq . Context ( ) <EOL> self . _poller = zmq . Poller ( ) <EOL> self . _handshake_socket = self . _zmq_context . socket ( zmq . REP ) <EOL> self . _handshake_socket . bind ( self . handshake_string ) <EOL> self . _data_socket = self . _zmq_context . socket ( zmq . SUB ) <EOL> self . _data_socket . setsockopt ( zmq . SUBSCRIBE , '<STR_LIT>' ) <EOL> self . _data_socket . connect ( self . data_string ) <EOL> self . _poller . register ( self . _handshake_socket , zmq . POLLIN ) <EOL> self . _poller . register ( self . _data_socket , zmq . POLLIN ) <EOL> GUI . invoke_after ( self . poll_period , self . _wait_for_data ) <EOL> def stop ( self ) : <EOL> self . _pid_mapping = { } <EOL> if self . _data_socket is not None : <EOL> self . _data_socket . close ( ) <EOL> if self . _handshake_socket is not None : <EOL> self . _handshake_socket . close ( ) <EOL> if self . _zmq_context is not None : <EOL> self . _zmq_context . term ( ) <EOL> def _add_view ( self , pid , profile , fields ) : <EOL> from pikos . live . utils import get_model_for_profile <EOL> model_class = get_model_for_profile ( profile ) <EOL> model = model_class ( pid = pid , profile = profile , fields = fields ) <EOL> self . _pid_mapping [ pid ] = model <EOL> self . application . active_window . central_pane . add_tab ( model ) <EOL> def _handle_data ( self ) : <EOL> records = { } <EOL> start = time . time ( ) <EOL> while ( time . time ( ) - start ) <= <NUM_LIT:0.1> : <EOL> socks = dict ( self . _poller . poll ( timeout = self . poll_timeout ) ) <EOL> if self . _data_socket not in socks or socks [ self . _data_socket ] != zmq . POLLIN : <EOL> break <EOL> record = pickle . loads ( self . _data_socket . recv ( ) ) <EOL> if not isinstance ( record , tuple ) or len ( record ) != <NUM_LIT:2> : <EOL> continue <EOL> pid , record_data = record <EOL> if pid not in self . _pid_mapping : <EOL> continue <EOL> records . setdefault ( pid , [ ] ) . append ( record_data ) <EOL> for pid , record_data in records . iteritems ( ) : <EOL> model = self . _pid_mapping [ pid ] <EOL> model . add_data ( record_data ) <EOL> if len ( records ) > <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return self . poll_period <EOL> def _handle_connection ( self ) : <EOL> handshake = pickle . loads ( self . _handshake_socket . recv ( ) ) <EOL> pid , profile , fields = handshake <EOL> self . _handshake_socket . send ( pickle . dumps ( True ) ) <EOL> self . _add_view ( pid , profile , fields ) <EOL> def _wait_for_data ( self ) : <EOL> next_poll = self . _handle_data ( ) <EOL> socks = dict ( self . _poller . poll ( timeout = self . poll_timeout ) ) <EOL> if self . _handshake_socket in socks and socks [ self . _handshake_socket ] == zmq . POLLIN : <EOL> self . _handle_connection ( ) <EOL> GUI . invoke_after ( next_poll , self . _wait_for_data ) </s>
