<s> import gevent . monkey <EOL> gevent . monkey . patch_all ( ) <EOL> import json , gevent . queue , gevent <EOL> class SetHandler ( ) : <EOL> def __init__ ( self , finger ) : <EOL> self . finger = finger <EOL> self . dict = { } <EOL> self . queue = { } <EOL> gevent . spawn ( self . check_updates ) <EOL> def check_updates ( self ) : <EOL> known = len ( self . finger . known ) <EOL> while True : <EOL> if len ( self . finger . known ) != known : <EOL> known = len ( self . finger . known ) <EOL> for k in self . dict : <EOL> self . update ( k , self . dict [ k ] ) <EOL> gevent . sleep ( <NUM_LIT:0.1> ) <EOL> def get ( self , key ) : <EOL> for x in range ( <NUM_LIT:3> ) : <EOL> try : <EOL> self . queue [ key ] = gevent . queue . Queue ( <NUM_LIT:0> ) <EOL> self . finger . send ( hash ( key ) % int ( <NUM_LIT:32> * '<STR_LIT:F>' , <NUM_LIT:16> ) , '<STR_LIT>' + json . dumps ( key ) ) <EOL> with gevent . Timeout ( <NUM_LIT:0.5> ) : <EOL> result = self . queue [ key ] . get ( ) <EOL> break <EOL> except gevent . Timeout : <EOL> result = None <EOL> continue <EOL> del self . queue [ key ] <EOL> return result <EOL> def set ( self , key , value ) : <EOL> self . finger . send ( hash ( key ) % int ( <NUM_LIT:32> * '<STR_LIT:F>' , <NUM_LIT:16> ) , '<STR_LIT>' + json . dumps ( [ key , value ] ) ) <EOL> def update ( self , key , value ) : <EOL> self . finger . send ( hash ( key ) % int ( <NUM_LIT:32> * '<STR_LIT:F>' , <NUM_LIT:16> ) , '<STR_LIT>' + json . dumps ( [ key , value ] ) ) <EOL> def add ( self , key , value ) : <EOL> self . finger . send ( hash ( key ) % int ( <NUM_LIT:32> * '<STR_LIT:F>' , <NUM_LIT:16> ) , '<STR_LIT>' + json . dumps ( [ key , value ] ) ) <EOL> def handle_msg ( self , proto , msg ) : <EOL> if msg [ <NUM_LIT:0> : <NUM_LIT:3> ] not in [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return <EOL> if msg [ <NUM_LIT:0> : <NUM_LIT:3> ] in [ '<STR_LIT:GET>' ] : <EOL> _ , key = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> key = json . loads ( key ) <EOL> if key in self . dict : <EOL> item = self . dict [ key ] <EOL> proto . send ( '<STR_LIT>' + json . dumps ( [ key , item ] ) ) <EOL> else : <EOL> proto . send ( '<STR_LIT>' + json . dumps ( [ key , None ] ) ) <EOL> if msg [ <NUM_LIT:0> : <NUM_LIT:3> ] in [ '<STR_LIT>' ] : <EOL> _ , item = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> key , value = json . loads ( item ) <EOL> self . dict [ key ] = value <EOL> if msg [ <NUM_LIT:0> : <NUM_LIT:3> ] in [ '<STR_LIT>' ] : <EOL> _ , item = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> key , value = json . loads ( item ) <EOL> if key not in self . dict : <EOL> self . dict [ key ] = [ ] <EOL> self . dict [ key ] . append ( value ) <EOL> if msg [ <NUM_LIT:0> : <NUM_LIT:3> ] in [ '<STR_LIT>' ] : <EOL> _ , item = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> key , value = json . loads ( item ) <EOL> if key in self . queue : <EOL> self . queue [ key ] . put ( value ) <EOL> if msg [ <NUM_LIT:0> : <NUM_LIT:3> ] in [ '<STR_LIT>' ] : <EOL> _ , item = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> key , value = json . loads ( item ) <EOL> if key not in self . dict or self . dict [ key ] is not value : <EOL> self . dict [ key ] = value <EOL> import unittest <EOL> class TestStore ( unittest . TestCase ) : <EOL> def testConnection ( self ) : <EOL> gevent . monkey . patch_all ( ) <EOL> from network import NetworkListener <EOL> net = NetworkListener ( None , port = <NUM_LIT> ) <EOL> netlist = [ net ] <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> new = NetworkListener ( net . node . addr , port = <NUM_LIT> + i ) <EOL> netlist . append ( new ) <EOL> while len ( net . finger . known ) < <NUM_LIT:2> : <EOL> gevent . sleep ( ) <EOL> net . set_handler . set ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> net . set_handler . set ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> net . set_handler . add ( '<STR_LIT>' , '<STR_LIT:bar>' ) <EOL> with gevent . Timeout ( <NUM_LIT:3> ) : <EOL> while sum ( map ( lambda x : len ( x . set_handler . dict ) , netlist ) ) < <NUM_LIT:2> : <EOL> gevent . sleep ( ) <EOL> self . assertEqual ( net . set_handler . get ( '<STR_LIT>' ) , '<STR_LIT:bar>' ) <EOL> self . assertEqual ( net . set_handler . get ( '<STR_LIT>' ) , [ '<STR_LIT>' , '<STR_LIT:bar>' ] ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
