<s> import json <EOL> import base64 <EOL> import uuid <EOL> import urllib <EOL> import urllib2 <EOL> import threading <EOL> import time <EOL> import weblab . experiment . experiment as Experiment <EOL> import weblab . experiment . level as ExperimentApiLevel <EOL> from voodoo . override import Override <EOL> ROUTER_URL = '<STR_LIT>' <EOL> ROUTER_WEB = '<STR_LIT>' <EOL> POLLING_URL = '<STR_LIT>' <EOL> POLLING_PERIOD = <NUM_LIT:15> <EOL> ROUTER_REALM = '<STR_LIT>' <EOL> class ControlAppExperiment ( Experiment . Experiment ) : <EOL> def __init__ ( self , coord_address , locator , cfg_manager , * args , ** kwargs ) : <EOL> super ( ControlAppExperiment , self ) . __init__ ( * args , ** kwargs ) <EOL> self . cfg_manager = cfg_manager <EOL> self . debug = self . cfg_manager . get_value ( '<STR_LIT>' , False ) <EOL> self . admin_user = self . cfg_manager . get_value ( '<STR_LIT>' ) <EOL> self . admin_pass = self . cfg_manager . get_value ( '<STR_LIT>' ) <EOL> self . router_url = self . cfg_manager . get_value ( '<STR_LIT>' , ROUTER_URL ) <EOL> self . router_web = self . cfg_manager . get_value ( '<STR_LIT>' , ROUTER_WEB ) <EOL> self . router_realm = self . cfg_manager . get_value ( '<STR_LIT>' , ROUTER_REALM ) <EOL> self . router_poll = self . cfg_manager . get_value ( '<STR_LIT>' , POLLING_URL ) <EOL> self . poll_period = self . cfg_manager . get_value ( '<STR_LIT>' , POLLING_PERIOD ) <EOL> if self . poll_period <= <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . timer = None <EOL> self . _username = '<STR_LIT>' <EOL> self . start_time = <NUM_LIT:0> <EOL> @ Override ( Experiment . Experiment ) <EOL> def do_get_api ( self ) : <EOL> return ExperimentApiLevel . level_2 <EOL> def _reset_password ( self ) : <EOL> auth = urllib2 . HTTPBasicAuthHandler ( ) <EOL> auth . add_password ( <EOL> realm = self . router_realm , <EOL> uri = self . router_url , <EOL> user = self . admin_user , <EOL> passwd = self . admin_pass , <EOL> ) <EOL> opener = urllib2 . build_opener ( auth ) <EOL> random_uuid = uuid . uuid4 ( ) <EOL> password = base64 . encodestring ( random_uuid . bytes ) [ : <NUM_LIT:8> ] <EOL> content = dict ( <EOL> edLogin = self . _username , <EOL> edPassword = password , <EOL> edConfirmPassword = password , <EOL> cbPageList = <NUM_LIT:0> , <EOL> cbDirList = <NUM_LIT:0> , <EOL> cbCBMode = <NUM_LIT:0> , <EOL> B1 = '<STR_LIT>' , <EOL> AST_EditUserId = <NUM_LIT:4> <EOL> ) <EOL> opener . open ( '<STR_LIT>' % ( self . router_url , urllib . urlencode ( content ) ) ) . read ( ) <EOL> return password <EOL> @ Override ( Experiment . Experiment ) <EOL> def do_start_experiment ( self , serialized_client_initial_data , serialized_server_initial_data ) : <EOL> raw_back_url = json . loads ( serialized_client_initial_data ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> back_url = urllib2 . quote ( raw_back_url , '<STR_LIT>' ) <EOL> self . start_time = time . time ( ) <EOL> server_data = json . loads ( serialized_server_initial_data ) <EOL> time_slot = float ( server_data [ '<STR_LIT>' ] ) <EOL> self . _username = server_data [ '<STR_LIT>' ] <EOL> self . timer = threading . Timer ( time_slot , function = self . _reset_password ) <EOL> self . timer . start ( ) <EOL> password = self . _reset_password ( ) <EOL> url = { '<STR_LIT:url>' : self . router_web % dict ( login = self . _username , password = password , back = back_url ) } <EOL> return json . dumps ( { "<STR_LIT>" : json . dumps ( url ) , '<STR_LIT>' : False } ) <EOL> @ Override ( Experiment . Experiment ) <EOL> def do_should_finish ( self ) : <EOL> if ( time . time ( ) - <NUM_LIT:30> ) < self . start_time : <EOL> if self . debug : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:10> <EOL> content = urllib2 . urlopen ( self . router_poll ) . read ( ) <EOL> user_alive = json . loads ( content ) . get ( '<STR_LIT:user>' , <NUM_LIT:0> ) <EOL> if self . debug : <EOL> print "<STR_LIT>" , user_alive != <NUM_LIT:0> <EOL> if user_alive == <NUM_LIT:0> : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return self . poll_period <EOL> @ Override ( Experiment . Experiment ) <EOL> def do_dispose ( self ) : <EOL> if self . timer is not None : <EOL> self . timer . cancel ( ) <EOL> self . _reset_password ( ) <EOL> return '<STR_LIT>' </s>
