def dataReceived(self, data): self._bufer = self._bufer + data #[uint32_t playerid][uint16_t msg_channel][uint16_t cmd][string amf3_data] LOG.info('len data: %s' % (len(data))) while(len(self._bufer) >= 12): (playerid, msg_channel, cmd, amf3_data_len) = struct.unpack("!IHHI", self._bufer[0:12]) LOG.info("playerid: %s, msg_channel: %s, cmd: %s, amf3_data_len:\ %s" % (playerid, msg_channel, pattern.to_hex(cmd), amf3_data_len,)) amf3_data_fmt = '!%ss' % (amf3_data_len) if (len(self._bufer[12:]) >= amf3_data_len): rqstid = uuid.uuid1() self._rqstid[rqstid] = {'rqstid':rqstid, 'msg_channel':msg_channel, 'playerid':playerid, 'cmd':cmd} (amf3_data,) = struct.unpack(amf3_data_fmt, self._bufer[12:12+amf3_data_len]) LOG.info("amf3_data: %s" % repr(amf3_data)) self._bufer = self._bufer[12+amf3_data_len:] decoder = Decoder(amf3=True) data = decoder.decode(amf3_data) LOG.info("data: %s" % (data)) try: callback = self._callback[cmd] callback(self, playerid, rqstid, data) except KeyError: LOG.ERROR("CMD error, The CMD: %s can not be handled" % (pattern.to_hex(cmd))) break
def setup_channel_set(channel_set): """Configures an amfast.remoting.channel.ChannelSet object.""" # Setup database schema = persistent.Schema() schema.createSchema() schema.createMappers() # Setup DB based connection_manager and subscription_manager channel_set.connection_manager = SaConnectionManager( persistent.engine, persistent.metadata) channel_set.connection_manager.createTables() channel_set.subscription_manager = SaSubscriptionManager( persistent.engine, persistent.metadata) channel_set.subscription_manager.createTables() # Send log messages to STDOUT handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.DEBUG) amfast.logger.addHandler(handler) # Map class aliases # These same aliases must be # registered in the client # with the registClassAlias function, # or the RemoteClass metadata tag. class_mapper = ClassDefMapper() class_mapper.mapClass(SaClassDef(models.User, 'models.User')) class_mapper.mapClass(SaClassDef(models.Email, 'models.Email')) class_mapper.mapClass(SaClassDef(models.PhoneNumber, 'models.PhoneNumber')) # Expose class_mapper to our controller sa_obj = controller.SAObject() sa_obj.class_def_mapper = class_mapper # Set Channel options # We're going to use the same # Encoder and Decoder for all channels encoder = Encoder(use_collections=True, use_proxies=True, class_def_mapper=class_mapper, use_legacy_xml=True) decoder = Decoder(class_def_mapper=class_mapper) for channel in channel_set: channel.endpoint.encoder = encoder channel.endpoint.decoder = decoder # Map service targets to controller methods service = Service('ExampleService') service.mapTarget(SaCallableTarget(sa_obj.load, 'load')) service.mapTarget(SaCallableTarget(sa_obj.loadAttr, 'loadAttr')) service.mapTarget(SaCallableTarget(sa_obj.loadAll, 'loadAll')) service.mapTarget(SaCallableTarget(sa_obj.saveList, 'saveList')) service.mapTarget(SaCallableTarget(sa_obj.save, 'save')) service.mapTarget(SaCallableTarget(sa_obj.remove, 'remove')) service.mapTarget(SaCallableTarget(sa_obj.removeList, 'removeList')) service.mapTarget( SaCallableTarget(sa_obj.insertDefaultData, 'insertDefaultData')) channel_set.service_mapper.mapService(service)
def createserver(config, jobqueue, db): channel_set = TornadoChannelSet() polling_channel = TornadoChannel('amf') channel_set.mapChannel(polling_channel) class_mapper = ClassDefMapper() encoder = Encoder(amf3=True, use_collections=True, use_proxies=True, use_references=True, class_def_mapper=class_mapper) decoder = Decoder(amf3=True, class_def_mapper=class_mapper) for channel in channel_set: channel.endpoint.encoder = encoder channel.endpoint.decoder = decoder srcpath = os.path.join(os.environ["CUREGAME_ROOT"], "../flex/lib/") if not os.path.exists(srcpath): srcpath = None for klass in EXPOSE_SERVICES: as3rpc.bind_amfast_service(klass, channel_set, class_mapper, args=(config, jobqueue, db), srcpath=srcpath) bin_path = os.path.join(os.environ["CUREGAME_ROOT"], "../flex/Dashboard/bin") static_path = os.path.join(os.environ["CUREGAME_ROOT"], "media") template_path = os.path.join(os.environ["CUREGAME_ROOT"], "media/html") return tornado.web.Application([ (r"/dashboard/amf/", polling_channel.request_handler), (r"/(.*)", tornado.web.StaticFileHandler, {"path": bin_path}), ], static_path=static_path, template_path=template_path)
def __init__(self, encoder=None, decoder=None): if encoder is None: from amfast.encoder import Encoder encoder = Encoder() self.encoder = encoder if decoder is None: from amfast.decoder import Decoder decoder = Decoder() self.decoder = decoder
def test_get_element_custom_mimetype_amf(self): doc_id = self.insert_docs(self.test_db, self.test_collection, self.insert_data) item_id = str(doc_id[0]) test_get_element_url = self.base_url + (self.get_item_path % (self.test_db, self.test_collection, item_id)) response_obj = GET(test_get_element_url, async = False, accept = [self.test_amf_mimetype] , resp = True, credentials = [self.test_user, self.test_password,""]) return_value, http_status = self.get_http_response(response_obj) self.assertEquals(type(return_value).__name__, "str") decoder = Decoder(amf3=True) doc_obj = decoder.decode(return_value)[settings.DOC_CONTAINER] self.assertTrue(u'_id' in doc_obj) del doc_obj[u'_id'] self.assertTrue(check_dictionaries_match(doc_obj, self.test_doc))
def setup_channel_set(channel_set): """Configures an amfast.remoting.channel.ChannelSet object.""" # Send log messages to STDOUT handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.DEBUG) amfast.logger.addHandler(handler) # These classes are for interacting with the Red5 echo test client. class_mapper = ClassDefMapper() class_mapper.mapClass( DynamicClassDef(RemoteClass, 'org.red5.server.webapp.echo.RemoteClass', amf3=False)) class_mapper.mapClass( ExternClassDef(ExternClass, 'org.red5.server.webapp.echo.ExternalizableClass')) # Set Channel options # We're going to use the same # Encoder and Decoder for all channels encoder = Encoder(use_collections=True, use_proxies=True, class_def_mapper=class_mapper, use_legacy_xml=True) decoder = Decoder(class_def_mapper=class_mapper) for channel in channel_set: channel.endpoint.encoder = encoder channel.endpoint.decoder = decoder # Map service targets to controller methods channel_set.service_mapper.default_service.mapTarget( CallableTarget(echo, 'echo')) service = Service('Red5Echo') service.mapTarget(CallableTarget(echo, 'echo')) channel_set.service_mapper.mapService(service)
import struct import json import sys import os import shutil from amfast.decoder import Decoder from action import Download, GetSlides, Prepare, Slice, Render, Concat from file import File decoder = Decoder(amf3=True) class Recording(object): def __init__(self, index='index.json'): self.path, _ = os.path.split(index) with open(index, 'r') as f: index = json.loads(f.read()) self.duration = index['duration'] self.chunks = index['chunks'] self.streams = index['streams'] self.snapshot = index['snapshots'][0]['name'] self.actions = [] self.files = [] self.events = [] self.counter = 0 self.docs = {} tmp = os.path.join(self.path, 'tmp') if not os.path.exists(tmp): os.makedirs(tmp)
def decoderAmf3Data(amf3_data): decoder = Decoder(amf3=True) data = decoder.decode(amf3_data) return data
def createserver(config, rootdir, db): channel_set = TornadoChannelSet() polling_channel = TornadoChannel('amf') channel_set.mapChannel(polling_channel) class_mapper = ClassDefMapper() encoder = Encoder(amf3=True, use_collections=True, use_proxies=True, use_references=True, class_def_mapper=class_mapper) decoder = Decoder(amf3=True, class_def_mapper=class_mapper) for channel in channel_set: channel.endpoint.encoder = encoder channel.endpoint.decoder = decoder srcpath = os.path.join(rootdir, "../flex/lib/") if not os.path.exists(srcpath): srcpath = None for klass in EXPOSE_SERVICES: as3rpc.bind_amfast_service(klass, channel_set, class_mapper, args=(config, db), srcpath=srcpath) swf_path = os.path.join(rootdir, "media/swf") static_path = os.path.join(rootdir, "media") template_path = os.path.join(rootdir, "media/html") pcms_md5 = hashlib.md5( open(os.path.join(swf_path, "pcms.swf"), "rb").read()).hexdigest() return tornado.web.Application( [ (r"/remotepy/rpc/", remotepy.RemotePyHandler), (r"/pcms/amf/", polling_channel.request_handler), (r"/pcms/excel/", GeneratePatientExcelHandler, { "db": db }), (r"/admin/login/", AdminLoginHandler), (r"/admin/logout/", AdminLogoutHandler), (r"/admin/list/", AdminListStationsHandler, { "db": db }), (r"/admin/reset/sync/", AdminResetClientSyncHandler, { "db": db }), (r"/admin/status/", AdminStatusCheckHandler, { "db": db }), (r"/", FlashAppHandler, { "config": config, "title": _("VRS Analytics"), "flash_id": "pcms", "flash_version": "10.1.0", "flash_movie": "pcms.swf", "movie_md5": pcms_md5 }), (r"/(.*)", tornado.web.StaticFileHandler, { "path": swf_path }), ], login_url="/admin/login/", cookie_secret="529397d22d7bd16a1f4616726f3312fe4d4b48c1", static_path=static_path, template_path=template_path)
def setUp(self): self.class_mapper = class_def.ClassDefMapper() self.decoder = Decoder()
class Amf0DecoderTestCase(unittest.TestCase): class Spam(object): def __init__(self): self.spam = 'eggs' def setUp(self): self.class_mapper = class_def.ClassDefMapper() self.decoder = Decoder() def testFalse(self): self.assertEquals(False, decode.decode(DecoderContext('\x01\x00'))) def testStringInput(self): self.assertEquals(False, decode.decode('\x01\x00')) def testDecoderObj(self): self.assertEquals(False, decode.decode('\x01\x00')) def testTrue(self): self.assertEquals(True, self.decoder.decode('\x01\x01')) def testNumber(self): tests = { 0: '\x00\x00\x00\x00\x00\x00\x00\x00\x00', 0.2: '\x00\x3f\xc9\x99\x99\x99\x99\x99\x9a', 1: '\x00\x3f\xf0\x00\x00\x00\x00\x00\x00', 42: '\x00\x40\x45\x00\x00\x00\x00\x00\x00', -123: '\x00\xc0\x5e\xc0\x00\x00\x00\x00\x00', 1.23456789: '\x00\x3f\xf3\xc0\xca\x42\x83\xde\x1b' } for number, encoding in tests.iteritems(): self.assertEquals(number, decode.decode(DecoderContext(encoding))) def testString(self): tests = {'': '\x02\x00\x00', 'hello': '\x02\x00\x05hello'} for string, encoding in tests.iteritems(): self.assertEquals(string, decode.decode(DecoderContext(encoding))) def testLongString(self): decoded = 's' * 65537 encoded = '\x0C\x00\x01\x00\x01' + decoded self.assertEquals(decoded, decode.decode(DecoderContext(encoded))) def testNull(self): self.assertEquals(None, decode.decode(DecoderContext('\x05'))) def testUndefined(self): self.assertEquals(None, decode.decode(DecoderContext('\x06'))) def testAnonObj(self): encoded = '\x03' #header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator result = decode.decode(DecoderContext(encoded)) self.assertEquals('eggs', result['spam']) result = decode.decode(DecoderContext(StringIO(encoded))) self.assertEquals('eggs', result['spam']) def testMixedArray(self): encoded = '\x08\x00\x00\x00\x00' # mixed array header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator result = decode.decode(DecoderContext(encoded)) self.assertEquals('eggs', result['spam']) result = decode.decode(DecoderContext(StringIO(encoded))) self.assertEquals('eggs', result['spam']) def testArray(self): decoded = [0, 1, 1.23456789] encoded = '\x0A\x00\x00\x00\x03' # 3 element array header encoded += '\x00\x00\x00\x00\x00\x00\x00\x00\x00' # element 1 encoded += '\x00\x3f\xf0\x00\x00\x00\x00\x00\x00' #element 2 encoded += '\x00\x3f\xf3\xc0\xca\x42\x83\xde\x1b' #element 3 result = decode.decode(DecoderContext(encoded)) for i, obj in enumerate(decoded): self.assertEquals(obj, result[i]) result = decode.decode(DecoderContext(StringIO(encoded))) for i, obj in enumerate(decoded): self.assertEquals(obj, result[i]) def testDate(self): import datetime encoded = '\x0BBp+6!\x15\x80\x00\x00\x00' result = decode.decode(DecoderContext(encoded)) self.assertEquals(2005, result.year) self.assertEquals(3, result.month) self.assertEquals(18, result.day) self.assertEquals(1, result.hour) self.assertEquals(58, result.minute) self.assertEquals(31, result.second) def testXml(self): import xml.dom.minidom encoded = '\x0F' # XML header encoded += '\x00\x00\x00\x55' # String header encoded += '<?xml version="1.0" ?><test>\n <test_me>tester</test_me>\n </test>' # encoded XML result = decode.decode(DecoderContext(encoded)) self.assertEquals(xml.dom.minidom.Document, result.__class__) def testDynamicObj(self): self.class_mapper.mapClass( class_def.DynamicClassDef(self.Spam, 'alias.spam', ())) encoded = '\x10\x00\x0Aalias.spam' encoded += '\x00\x04spam\x02\x00\x04eggs\x00\x00\x09' # dynamic attrs result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper)) self.class_mapper.unmapClass(self.Spam) self.assertEquals('eggs', result.spam) def testStaticObj(self): self.class_mapper.mapClass( class_def=class_def.ClassDef(self.Spam, 'alias.spam', ('spam'))) encoded = '\x10\x00\x0Aalias.spam' encoded += '\x00\x04spam\x02\x00\x04eggs\x00\x00\x09' # dynamic attrs result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper)) self.class_mapper.unmapClass(self.Spam) self.assertEquals('eggs', result.spam) def testReferences(self): encoded = '\x0A\x00\x00\x00\x04' # 3 element array header encoded += '\x03\x00\x04spam\x02\x00\x04eggs\x00\x00\t' # obj 1 encoded += '\x07\x00\x01' # ref to obj 1 encoded += '\x07\x00\x01' # ref to obj 1 encoded += '\x07\x00\x00' # circular ref result = decode.decode(DecoderContext(encoded)) self.assertEquals(4, len(result)) self.assertEquals(result, result.pop(-1)) for obj in result: self.assertEquals('eggs', obj['spam']) def testUnkownByteRaisesException(self): self.assertRaises(decode.DecodeError, decode.decode, DecoderContext('\xFF'))
class Amf0DecoderTestCase(unittest.TestCase): class Spam(object): def __init__(self): self.spam = 'eggs' def setUp(self): self.class_mapper = class_def.ClassDefMapper() self.decoder = Decoder() def testFalse(self): self.assertEquals(False, decode.decode(DecoderContext('\x01\x00'))) def testStringInput(self): self.assertEquals(False, decode.decode('\x01\x00')) def testDecoderObj(self): self.assertEquals(False, decode.decode('\x01\x00')) def testTrue(self): self.assertEquals(True, self.decoder.decode('\x01\x01')) def testNumber(self): tests = { 0: '\x00\x00\x00\x00\x00\x00\x00\x00\x00', 0.2: '\x00\x3f\xc9\x99\x99\x99\x99\x99\x9a', 1: '\x00\x3f\xf0\x00\x00\x00\x00\x00\x00', 42: '\x00\x40\x45\x00\x00\x00\x00\x00\x00', -123: '\x00\xc0\x5e\xc0\x00\x00\x00\x00\x00', 1.23456789: '\x00\x3f\xf3\xc0\xca\x42\x83\xde\x1b' } for number, encoding in tests.iteritems(): self.assertEquals(number, decode.decode(DecoderContext(encoding))) def testString(self): tests = { '': '\x02\x00\x00', 'hello': '\x02\x00\x05hello' } for string, encoding in tests.iteritems(): self.assertEquals(string, decode.decode(DecoderContext(encoding))) def testLongString(self): decoded = 's' * 65537 encoded = '\x0C\x00\x01\x00\x01' + decoded self.assertEquals(decoded, decode.decode(DecoderContext(encoded))) def testNull(self): self.assertEquals(None, decode.decode(DecoderContext('\x05'))) def testUndefined(self): self.assertEquals(None, decode.decode(DecoderContext('\x06'))) def testAnonObj(self): encoded = '\x03' #header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator result = decode.decode(DecoderContext(encoded)) self.assertEquals('eggs', result['spam']) result = decode.decode(DecoderContext(StringIO(encoded))) self.assertEquals('eggs', result['spam']) def testMixedArray(self): encoded = '\x08\x00\x00\x00\x00' # mixed array header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator result = decode.decode(DecoderContext(encoded)) self.assertEquals('eggs', result['spam']) result = decode.decode(DecoderContext(StringIO(encoded))) self.assertEquals('eggs', result['spam']) def testArray(self): decoded = [0, 1, 1.23456789] encoded = '\x0A\x00\x00\x00\x03' # 3 element array header encoded += '\x00\x00\x00\x00\x00\x00\x00\x00\x00' # element 1 encoded += '\x00\x3f\xf0\x00\x00\x00\x00\x00\x00' #element 2 encoded += '\x00\x3f\xf3\xc0\xca\x42\x83\xde\x1b' #element 3 result = decode.decode(DecoderContext(encoded)) for i, obj in enumerate(decoded): self.assertEquals(obj, result[i]) result = decode.decode(DecoderContext(StringIO(encoded))) for i, obj in enumerate(decoded): self.assertEquals(obj, result[i]) def testDate(self): import datetime encoded = '\x0BBp+6!\x15\x80\x00\x00\x00' result = decode.decode(DecoderContext(encoded)) self.assertEquals(2005, result.year) self.assertEquals(3, result.month) self.assertEquals(18, result.day) self.assertEquals(1, result.hour) self.assertEquals(58, result.minute) self.assertEquals(31, result.second) def testXml(self): import xml.dom.minidom encoded = '\x0F' # XML header encoded += '\x00\x00\x00\x55' # String header encoded += '<?xml version="1.0" ?><test>\n <test_me>tester</test_me>\n </test>' # encoded XML result = decode.decode(DecoderContext(encoded)) self.assertEquals(xml.dom.minidom.Document, result.__class__) def testDynamicObj(self): self.class_mapper.mapClass(class_def.DynamicClassDef(self.Spam, 'alias.spam', ())) encoded = '\x10\x00\x0Aalias.spam' encoded += '\x00\x04spam\x02\x00\x04eggs\x00\x00\x09' # dynamic attrs result = decode.decode(DecoderContext(encoded, class_def_mapper=self.class_mapper)) self.class_mapper.unmapClass(self.Spam) self.assertEquals('eggs', result.spam) def testStaticObj(self): self.class_mapper.mapClass(class_def = class_def.ClassDef(self.Spam, 'alias.spam', ('spam'))) encoded = '\x10\x00\x0Aalias.spam' encoded += '\x00\x04spam\x02\x00\x04eggs\x00\x00\x09' # dynamic attrs result = decode.decode(DecoderContext(encoded, class_def_mapper=self.class_mapper)) self.class_mapper.unmapClass(self.Spam) self.assertEquals('eggs', result.spam) def testReferences(self): encoded = '\x0A\x00\x00\x00\x04' # 3 element array header encoded += '\x03\x00\x04spam\x02\x00\x04eggs\x00\x00\t' # obj 1 encoded += '\x07\x00\x01' # ref to obj 1 encoded += '\x07\x00\x01' # ref to obj 1 encoded += '\x07\x00\x00' # circular ref result = decode.decode(DecoderContext(encoded)) self.assertEquals(4, len(result)) self.assertEquals(result, result.pop(-1)) for obj in result: self.assertEquals('eggs', obj['spam']) def testUnkownByteRaisesException(self): self.assertRaises(decode.DecodeError, decode.decode, DecoderContext('\xFF'))
def testDecodeAmf3(self): return self._testDecoder(Decoder(amf3=True), Encoder(amf3=True), 'sample_2.log')
def testDecodeAmf0(self): return self._testDecoder(Decoder(), Encoder(), 'sample.log')