| 1 |
# standard python libs |
|---|
| 2 |
from logging import getLogger, CRITICAL, ERROR, WARNING, INFO, DEBUG |
|---|
| 3 |
import uuid |
|---|
| 4 |
|
|---|
| 5 |
#related |
|---|
| 6 |
from eventlet import api |
|---|
| 7 |
|
|---|
| 8 |
# pyogp |
|---|
| 9 |
from pyogp.lib.base.datamanager import DataManager |
|---|
| 10 |
# pyogp messaging |
|---|
| 11 |
from pyogp.lib.base.message.message_handler import MessageHandler |
|---|
| 12 |
from pyogp.lib.base.message.packets import * |
|---|
| 13 |
from pyogp.lib.base.utilities.helpers import Helpers |
|---|
| 14 |
from pyogp.lib.base.exc import NotImplemented |
|---|
| 15 |
from pyogp.lib.base.objects import Object |
|---|
| 16 |
from pyogp.lib.base.datatypes import * |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
# initialize logging |
|---|
| 20 |
logger = getLogger('pyogp.lib.base.assets') |
|---|
| 21 |
log = logger.log |
|---|
| 22 |
|
|---|
| 23 |
class AssetManager(DataManager): |
|---|
| 24 |
""" |
|---|
| 25 |
The AssetManager class handles the assets of an Agent() instance |
|---|
| 26 |
|
|---|
| 27 |
Sample implementations: |
|---|
| 28 |
Tests: |
|---|
| 29 |
""" |
|---|
| 30 |
|
|---|
| 31 |
def __init__(self, settings = None, agent = None): |
|---|
| 32 |
super(AssetManager, self).__init__(settings, agent) |
|---|
| 33 |
self.requests = [] # queue of requests? |
|---|
| 34 |
self.assets = {} # indexed by assetID? |
|---|
| 35 |
|
|---|
| 36 |
def enable_callbacks(self): |
|---|
| 37 |
self.agent.region.message_handler.register('TransferInfo').subscribe(self.onTransferInfo) |
|---|
| 38 |
self.agent.region.message_handler.register('TransferPacket').subscribe(self.onTransferPacket) |
|---|
| 39 |
|
|---|
| 40 |
def request_asset(self, assetID, assetType, isPriority): |
|---|
| 41 |
self.requests.append(Asset(assetID, assetType, isPriority)) |
|---|
| 42 |
packet = TransferRequestPacket() |
|---|
| 43 |
#TransferInfo |
|---|
| 44 |
transferID = UUID() #associate the assetID with the transferID |
|---|
| 45 |
transferID.random() |
|---|
| 46 |
packet.TransferInfo['TransferID'] = transferID |
|---|
| 47 |
packet.TransferInfo['ChannelType'] = 2 |
|---|
| 48 |
packet.TransferInfo['SourceType'] = 2 |
|---|
| 49 |
if isPriority: |
|---|
| 50 |
packet.TransferInfo['Priority'] = 1.0 |
|---|
| 51 |
else: |
|---|
| 52 |
packet.TransferInfo['Priority'] = 0.0 |
|---|
| 53 |
packet.TransferInfo['Params'] = assetID.get_bytes() \ |
|---|
| 54 |
+ Helpers().int_to_bytes(5) #FIXME <-assetID & type endianness of type may not be correct |
|---|
| 55 |
self.agent.region.enqueue_message(packet()) |
|---|
| 56 |
return transferID |
|---|
| 57 |
|
|---|
| 58 |
def onTransferInfo(self, packet): |
|---|
| 59 |
log(INFO, "TransferInfo received %s" % packet) |
|---|
| 60 |
#raise NotImplemented("onTranferInfo") |
|---|
| 61 |
|
|---|
| 62 |
def onTransferPacket(self, packet): |
|---|
| 63 |
# fill in data for Asset in the requests queue and pop it off and story in assets dict |
|---|
| 64 |
log(INFO, "TransferPacket received %s" % packet) |
|---|
| 65 |
#raise NotImplemented("onTransferPacket") |
|---|
| 66 |
|
|---|
| 67 |
def get_asset(self, xxxID): |
|---|
| 68 |
raise NotImplemented("get_asset") |
|---|
| 69 |
|
|---|
| 70 |
def pending_downloads(self): |
|---|
| 71 |
if len(self.requests) > 0: |
|---|
| 72 |
return True |
|---|
| 73 |
return False |
|---|
| 74 |
|
|---|
| 75 |
class Asset(object): |
|---|
| 76 |
def __init__(self, assetID, assetType, isPriority): |
|---|
| 77 |
self.assetID = assetID |
|---|
| 78 |
self.assetType = assetID |
|---|
| 79 |
self.isPriority = isPriority |
|---|
| 80 |
self.data = None |
|---|
| 81 |
""" |
|---|
| 82 |
Contributors can be viewed at: |
|---|
| 83 |
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/CONTRIBUTORS.txt |
|---|
| 84 |
|
|---|
| 85 |
$LicenseInfo:firstyear=2008&license=apachev2$ |
|---|
| 86 |
|
|---|
| 87 |
Copyright 2009, Linden Research, Inc. |
|---|
| 88 |
|
|---|
| 89 |
Licensed under the Apache License, Version 2.0 (the "License"). |
|---|
| 90 |
You may obtain a copy of the License at: |
|---|
| 91 |
http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 92 |
or in |
|---|
| 93 |
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/LICENSE.txt |
|---|
| 94 |
|
|---|
| 95 |
$/LicenseInfo$ |
|---|
| 96 |
""" |
|---|
| 97 |
|
|---|