Files
Hippolyzer/pyogp/lib/base/agentdomain.py

133 lines
4.3 KiB
Python
Raw Normal View History

2008-06-27 15:10:24 +00:00
from agent import Agent
from interfaces import ISerialization
2008-06-27 15:10:24 +00:00
from caps import SeedCapability
USE_REDIRECT=True
2008-06-27 15:10:24 +00:00
import urllib2
from zope.interface import implements
import grokcore.component as grok
from indra.base import llsd
2008-06-27 15:10:24 +00:00
from interfaces import IPlaceAvatar, IAgentDomain
from network import IRESTClient, HTTPError
from zope.component import queryUtility, adapts, getUtility
from agent import Agent
from avatar import Avatar
from caps import SeedCapability
2008-06-27 15:10:24 +00:00
2008-06-27 15:10:24 +00:00
# URL Opener for the agent domain login
#
2008-06-27 15:10:24 +00:00
if USE_REDIRECT:
# REMOVE THIS WHEN THE REDIRECT IS NOT NEEDED ANYMORE FOR LINDEN LAB!
class RedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
#ignore the redirect, grabbing the seed cap url from the headers
# TODO: add logging and error handling
return headers['location']
2008-06-27 15:10:24 +00:00
# post to auth.cgi, ignoring the built in redirect
AgentDomainLoginOpener = urllib2.build_opener(RedirectHandler())
2008-06-27 15:10:24 +00:00
class AgentDomain(object):
"""an agent domain endpoint"""
implements(IAgentDomain)
2008-06-27 15:10:24 +00:00
def __init__(self,uri):
"""initialize the agent domain endpoint"""
self.uri = uri
def login(self, credentials):
"""login to the agent domain and return an agent object"""
serializer = ISerialization(credentials) # convert to string via adapter
2008-06-27 15:10:24 +00:00
payload = serializer.serialize()
content_type = serializer.content_type
headers = {'Content-Type': content_type}
2008-06-27 15:10:24 +00:00
# now create the request. We assume for now that self.uri is the login uri
# TODO: make this pluggable so we can use other transports like eventlet in the future
# TODO: add logging and error handling
if USE_REDIRECT:
request = urllib2.Request(self.uri,payload,headers)
try:
res = AgentDomainLoginOpener.open(request)
except urllib2.HTTPError,e:
print e.read()
raise
if type(res)!=type(""):
seed_cap_url_data = res.read() # it might be an addinfourl object
seed_cap_url = llsd.parse(seed_cap_url_data)['agent_seed_capability']
else:
# this only happens in the Linden Lab Agent Domain with their redirect
seed_cap_url = res
else:
restclient = getUtility(IRESTClient)
try:
response = restclient.POST(self.uri, payload, headers=headers)
except HTTPError, error:
print "error", error.code, error.msg
print error.fp.read()
raise
seed_cap_url_data = response.body
seed_cap_url = llsd.parse(seed_cap_url_data)['agent_seed_capability']
2008-06-27 15:10:24 +00:00
self.seed_cap = SeedCapability('seed_cap', seed_cap_url)
return Agent(self)
class PlaceAvatar(grok.Adapter):
"""handles placing an avatar for an agent object"""
grok.implements(IPlaceAvatar)
grok.context(IAgentDomain)
def __init__(self, context):
"""initialize this adapter"""
self.context = context
# let's retrieve the cap we need
self.seed_cap = self.context.seed_cap # ISeedCapability
self.place_avatar_cap = self.seed_cap.get(['place_avatar'])['place_avatar']
def __call__(self, region):
"""initiate the placing process"""
region_uri = region.uri
payload = {'region_url' : region_uri }
result = self.place_avatar_cap.POST(payload)
region.details = result
avatar = Avatar(region)
return avatar
from interfaces import IEventQueueGet
class EventQueueGet(grok.Adapter):
"""an event queue get capability"""
grok.implements(IEventQueueGet)
grok.context(IAgentDomain)
def __init__(self, context):
"""initialize this adapter"""
self.context = context
# let's retrieve the cap we need
self.seed_cap = self.context.seed_cap # ISeedCapability
self.cap = self.seed_cap.get(['event_queue'])['event_queue']
def __call__(self, data = {}):
"""initiate the event queue get request"""
result = self.cap.POST(data)
return result