def __init__(self, local_uri, logger, use_sessmatch=False): GreenTransportBase.__init__(self) if local_uri is not None and not isinstance(local_uri, protocol.URI): raise TypeError('Not MSRP URI instance: %r' % (local_uri, )) # The following members define To-Path and From-Path headers as following: # * Outgoing request: # From-Path: local_uri # To-Path: local_path + remote_path + [remote_uri] # * Incoming request: # From-Path: remote_path + remote_uri # To-Path: remote_path + local_path + [local_uri] # XXX self.local_uri = local_uri if logger is None: logger = Logger() self.logger = logger self.local_path = [] self.remote_uri = None self.remote_path = [] self.use_sessmatch = use_sessmatch
# Copyright (C) 2008-2009 AG Projects. See LICENSE for details. # import sys from eventlib import proc from msrplib.connect import AcceptorDirect from msrplib.protocol import URI from msrplib.trafficlog import Logger from msrplib.session import GreenMSRPSession from twisted.internet import reactor # let eventlib know we want twisted-based hub local_uri = URI(session_id='server', use_tls=False) remote_uri = URI(session_id='client', use_tls=False) connector = AcceptorDirect(logger=Logger()) connector.prepare(local_uri) transport = connector.complete([remote_uri]) session = GreenMSRPSession(transport) session.send_message('hello', 'text/plain') print 'received: %s' % session.receive_chunk().data session.shutdown()
class TestBase(unittest.TestCase): PER_TEST_TIMEOUT = 30 client_relay = None client_logger = Logger(prefix='C ') server_relay = None server_logger = Logger(prefix='S ') debug = True use_tls = False server_credentials = None def get_client_uri(self): return URI(use_tls=self.use_tls) def get_server_uri(self): return URI(port=0, use_tls=self.use_tls, credentials=self.server_credentials) def get_connector(self): if self.client_relay is not None: return RelayConnection(self.client_relay, 'active', logger=self.client_logger) else: return DirectConnector(logger=self.client_logger) def get_acceptor(self): if self.server_relay is not None: return RelayConnection(self.server_relay, 'passive', logger=self.client_logger) else: return DirectAcceptor(logger=self.server_logger) def setup_two_endpoints(self): server_path = TimeoutEvent() client_path = TimeoutEvent() client = proc.spawn_link_exception(_connect_msrp, client_path, server_path, self.get_connector(), self.get_client_uri()) server = proc.spawn_link_exception(_connect_msrp, server_path, client_path, self.get_acceptor(), self.get_server_uri()) return client, server def setUp(self): print('\n%s.%s' % (self.__class__.__name__, self._testMethodName)) self.timer = api.exc_after( self.PER_TEST_TIMEOUT, api.TimeoutError('per test timeout expired')) def tearDown(self): self.timer.cancel() del self.timer def assertHeaderEqual(self, header, chunk1, chunk2): self.assertEqual(chunk1.headers[header].decoded, chunk2.headers[header].decoded) def assertSameData(self, chunk1, chunk2): try: self.assertHeaderEqual('Content-Type', chunk1, chunk2) self.assertEqual(chunk1.data, chunk2.data) self.assertEqual(chunk1.contflag, chunk2.contflag) except Exception: print('Error while comparing %r and %r' % (chunk1, chunk2)) raise def make_hello(self, msrptransport, success_report=None, failure_report=None): chunk = msrptransport.make_send_request(data='hello') chunk.add_header(ContentTypeHeader('text/plain')) # because MSRPTransport does not send the responses, the relay must not either if success_report is not None: chunk.add_header(SuccessReportHeader(success_report)) if failure_report is not None: chunk.add_header(FailureReportHeader(failure_report)) return chunk def _test_write_chunk(self, sender, receiver): chunk = self.make_hello(sender, failure_report='no') sender.write(chunk.encode()) chunk_received = receiver.read_chunk() self.assertSameData(chunk, chunk_received)
# Copyright (C) 2008-2009 AG Projects. See LICENSE for details. # from msrplib.connect import DirectConnector from msrplib.protocol import URI from msrplib.trafficlog import Logger from msrplib.session import GreenMSRPSession from twisted.internet import reactor del reactor # let eventlib know we want twisted-based hub logger = Logger() local_uri = URI(session_id='client', use_tls=False) remote_uri = URI(session_id='server', use_tls=False) connector = DirectConnector(logger=logger) connector.prepare(local_uri) transport = connector.complete([remote_uri]) session = GreenMSRPSession(transport) session.send_message('hi', 'text/plain') logger.info('received: %s', session.receive_chunk().data) session.shutdown()