class A2p2SampClient(): # TODO watch hub disconnection def __init__(self): self.sampClient = SAMPIntegratedClient( "A2P2 samp relay" ) # TODO get title from main program class instead of HardCoded value def __del__(self): self.disconnect() def connect(self): self.sampClient.connect() # an error is thrown here if no hub is present # TODO get samp client name and display it in the UI # Instantiate the receiver self.r = Receiver(self.sampClient) # Listen for any instructions to load a table self.sampClient.bind_receive_call("ob.load.data", self.r.receive_call) self.sampClient.bind_receive_notification("ob.load.data", self.r.receive_notification) def disconnect(self): self.sampClient.disconnect() def is_connected(self): # Workarround the 'non' reliable is_connected attribute # this helps to reconnect after hub connection lost try: return self.sampClient.is_connected and ( self.sampClient.ping() or self.sampClient.is_connected) except: # consider connection refused exception as not connected state return False def get_status(self): if self.is_connected(): return "connected [%s]" % (self.sampClient.get_public_id()) else: return "not connected" def get_public_id(self): return self.sampClient.get_public_id() def has_message(self): return self.is_connected() and self.r.received def clear_message(self): return self.r.clear() def get_ob_url(self): url = self.r.params['url'] if url.startswith("file:///"): return url[7:] elif url.startswith("file:/"): # work arround bugged file urls return url[5:] return url
class TestWebProfile(BaseTestStandardProfile): def setup_method(self, method): self.dialog = AlwaysApproveWebProfileDialog() t = threading.Thread(target=self.dialog.poll) t.start() self.tmpdir = tempfile.mkdtemp() lockfile = os.path.join(self.tmpdir, '.samp') self.hub = SAMPHubServer(web_profile_dialog=self.dialog, lockfile=lockfile, web_port=0, pool_size=1) self.hub.start() self.client1 = SAMPIntegratedClient() self.client1.connect(hub=self.hub, pool_size=1) self.client1_id = self.client1.get_public_id() self.client1_key = self.client1.get_private_key() self.client2 = SAMPIntegratedWebClient() self.client2.connect(web_port=self.hub._web_port, pool_size=2) self.client2_id = self.client2.get_public_id() self.client2_key = self.client2.get_private_key() def teardown_method(self, method): if self.client1.is_connected: self.client1.disconnect() if self.client2.is_connected: self.client2.disconnect() self.hub.stop() self.dialog.stop() # The full communication tests are run since TestWebProfile inherits # test_main from TestStandardProfile def test_web_profile(self): # Check some additional queries to the server with get_readable_fileobj('http://localhost:{0}/crossdomain.xml'.format(self.hub._web_port)) as f: assert f.read() == CROSS_DOMAIN with get_readable_fileobj('http://localhost:{0}/clientaccesspolicy.xml'.format(self.hub._web_port)) as f: assert f.read() == CLIENT_ACCESS_POLICY # Check headers req = Request('http://localhost:{0}/crossdomain.xml'.format(self.hub._web_port)) req.add_header('Origin', 'test_web_profile') resp = urlopen(req) assert resp.getheader('Access-Control-Allow-Origin') == 'test_web_profile' assert resp.getheader('Access-Control-Allow-Headers') == 'Content-Type' assert resp.getheader('Access-Control-Allow-Credentials') == 'true'