<reponame>grobertson/PyOGP.lib.Client

"""
Contributors can be viewed at:
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/lib/base/trunk/CONTRIBUTORS.txt 

$LicenseInfo:firstyear=2008&license=apachev2$

Copyright 2009, Linden Research, Inc.

Licensed under the Apache License, Version 2.0.
You may obtain a copy of the License at:
    http://www.apache.org/licenses/LICENSE-2.0
or in 
    http://svn.secondlife.com/svn/linden/projects/2008/pyogp/lib/base/LICENSE.txt

$/LicenseInfo$
"""

# standard python libraries
from logging import getLogger
import signal
import sys

# related
try:
    from eventlet import api as eventlet
except ImportError:
    import eventlet

# pyogp
from pyogp.lib.base.datatypes import UUID
from pyogp.lib.client.exc import LoginError
from pyogp.lib.base.helpers import Wait

# initialize logging
logger = getLogger('pyogp.lib.client.agentmanager')

class AgentManager(object):
    """ a simple class to track multiple agents

    This class can perhaps begin to manage sessions in time.
    """

    def __init__(self, settings = None):
        """ initialize the agent manager """

        # allow the settings to be passed in
        # otherwise, grab the defaults
        if settings != None:
            self.settings = settings
        else:
            from pyogp.lib.client.settings import Settings
            self.settings = Settings()

        # store the agents in a dictionary keyed by a uuid
        # this could be agent_id, if we know it ahead of time
        # or, create a new one
        self.agents = {}

        # signal handler to capture erm signals
        self.signal_handler = signal.signal(signal.SIGINT, self.sigint_handler)

        if self.settings.LOG_VERBOSE: 
            logger.debug('Initializing agent manager for %s agents' % (len(self.agents)))

    def initialize(self, agents):
        """ accept a list of Agent() instances, and store them in the agents attribute """

        if type(agents) != list:
            logger.warning("The AgentManager requires a list of Agent instances to initialize. Stopping.")
            return False

        for agent in agents:

            self.store_agent(agent)

    def store_agent(self, agent):
        """ adds an agent to the store """

        if str(type(agent)) != '<class \'pyogp.lib.client.agent.Agent\'>':
            logger.warning("The AgentManager stores only Agent instances to initialize. Stopping.")
            return False

        # check if the agent is on the store
        if self._is_stored(agent) == False:

            if agent.agent_id == None:
                key = UUID().random()
            else:
                key = agent.agent_id

            self.agents[key] = agent

            logger.info("Stored agent %s with a key of %s" % (agent.name, key))

        else: 

            if agent.firstname != None and agent.lastname != None:
                uniq = ' named ' + agent.Name()

            logger.warning("The AgentManager is already storing an agent%s. Stopping." % (uniq))

    def _is_stored(self, agent):
        """ returns True if an agent is already stored, False if not """

        if agent.Name() in self._stored_names():
            return True
        elif agent.agent_id in self._stored_agent_ids():
            return True
        else:
            return False

    def _stored_names(self):
        """ returns the stored agent's names in a list """

        names = []

        for key in self.agents:
            names.append(self.agents[key].Name())

        return names

    def _stored_agent_ids(self):
        """ returns the stored agent's names in a list """

        ids = []

        for key in self.agents:
            if self.agents[key].agent_id != None:
                ids.append(self.agents[key].agent_id)

        return ids

    def login(self, key, loginuri, start_location=None):
        """ spawns a new agent via an eventlet coroutine """

        if self.settings.LOG_COROUTINE_SPAWNS: 
            logger.info("Spawning a coroutine for agent login for %s." % (self.agents[key].Name()))

        try:
            eventlet.spawn(self.agents[key].login, loginuri = loginuri, start_location = start_location)
        except LoginError, error:
            logger.error("Skipping agent with failed login: %s due to %s." % (self.agents[key].Name(), error))

    def has_agents_running(self):
        """ returns true if there is a client who's running value = True """

        for key in self.agents:
            if self.agents[key].running:
                return True

        return False

    def get_active_agents(self):
        """ returns a list of agents that are connected to a grid """

        active = []

        for key in self.agents:
            active.append(self.agents[key])

        return active

    def sigint_handler(self, sigint, frame):
        """ handles signals from the command line (and others) and logs out all agents """

        logger.info("Caught signal... %d. Stopping" % sigint)

        for agent in self.get_active_agents():
            agent.region.message_manager._is_running = False
            agent.running = False
            #agent.logout()

        #Wait(10)

        #if self.has_agents_running():
        #    logger.warning("These agents have not yet shut down. Killing the process hard.\n\t\t%s" % (self.get_active_agents()))
        #    sys.exit(1)




"""
Contributors can be viewed at:
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/lib/base/trunk/CONTRIBUTORS.txt 

$LicenseInfo:firstyear=2008&license=apachev2$

Copyright 2009, Linden Research, Inc.

Licensed under the Apache License, Version 2.0.
You may obtain a copy of the License at:
    http://www.apache.org/licenses/LICENSE-2.0
or in 
    http://svn.secondlife.com/svn/linden/projects/2008/pyogp/lib/base/LICENSE.txt

$/LicenseInfo$
"""

# standard python libraries
from logging import getLogger
import signal
import sys

# related
try:
    from eventlet import api as eventlet
except ImportError:
    import eventlet

# pyogp
from pyogp.lib.base.datatypes import UUID
from pyogp.lib.client.exc import LoginError
from pyogp.lib.base.helpers import Wait

# initialize logging
logger = getLogger('pyogp.lib.client.agentmanager')

class AgentManager(object):
    """ a simple class to track multiple agents

    This class can perhaps begin to manage sessions in time.
    """

    def __init__(self, settings = None):
        """ initialize the agent manager """

        # allow the settings to be passed in
        # otherwise, grab the defaults
        if settings != None:
            self.settings = settings
        else:
            from pyogp.lib.client.settings import Settings
            self.settings = Settings()

        # store the agents in a dictionary keyed by a uuid
        # this could be agent_id, if we know it ahead of time
        # or, create a new one
        self.agents = {}

        # signal handler to capture erm signals
        self.signal_handler = signal.signal(signal.SIGINT, self.sigint_handler)

        if self.settings.LOG_VERBOSE: 
            logger.debug('Initializing agent manager for %s agents' % (len(self.agents)))

    def initialize(self, agents):
        """ accept a list of Agent() instances, and store them in the agents attribute """

        if type(agents) != list:
            logger.warning("The AgentManager requires a list of Agent instances to initialize. Stopping.")
            return False

        for agent in agents:

            self.store_agent(agent)

    def store_agent(self, agent):
        """ adds an agent to the store """

        if str(type(agent)) != '<class \'pyogp.lib.client.agent.Agent\'>':
            logger.warning("The AgentManager stores only Agent instances to initialize. Stopping.")
            return False

        # check if the agent is on the store
        if self._is_stored(agent) == False:

            if agent.agent_id == None:
                key = UUID().random()
            else:
                key = agent.agent_id

            self.agents[key] = agent

            logger.info("Stored agent %s with a key of %s" % (agent.name, key))

        else: 

            if agent.firstname != None and agent.lastname != None:
                uniq = ' named ' + agent.Name()

            logger.warning("The AgentManager is already storing an agent%s. Stopping." % (uniq))

    def _is_stored(self, agent):
        """ returns True if an agent is already stored, False if not """

        if agent.Name() in self._stored_names():
            return True
        elif agent.agent_id in self._stored_agent_ids():
            return True
        else:
            return False

    def _stored_names(self):
        """ returns the stored agent's names in a list """

        names = []

        for key in self.agents:
            names.append(self.agents[key].Name())

        return names

    def _stored_agent_ids(self):
        """ returns the stored agent's names in a list """

        ids = []

        for key in self.agents:
            if self.agents[key].agent_id != None:
                ids.append(self.agents[key].agent_id)

        return ids

    def login(self, key, loginuri, start_location=None):
        """ spawns a new agent via an eventlet coroutine """

        if self.settings.LOG_COROUTINE_SPAWNS: 
            logger.info("Spawning a coroutine for agent login for %s." % (self.agents[key].Name()))

        try:
            eventlet.spawn(self.agents[key].login, loginuri = loginuri, start_location = start_location)
        except LoginError, error:
            logger.error("Skipping agent with failed login: %s due to %s." % (self.agents[key].Name(), error))

    def has_agents_running(self):
        """ returns true if there is a client who's running value = True """

        for key in self.agents:
            if self.agents[key].running:
                return True

        return False

    def get_active_agents(self):
        """ returns a list of agents that are connected to a grid """

        active = []

        for key in self.agents:
            active.append(self.agents[key])

        return active

    def sigint_handler(self, sigint, frame):
        """ handles signals from the command line (and others) and logs out all agents """

        logger.info("Caught signal... %d. Stopping" % sigint)

        for agent in self.get_active_agents():
            agent.region.message_manager._is_running = False
            agent.running = False
            #agent.logout()

        #Wait(10)

        #if self.has_agents_running():
        #    logger.warning("These agents have not yet shut down. Killing the process hard.\n\t\t%s" % (self.get_active_agents()))
        #    sys.exit(1)
Contributors can be viewed at: http://svn.secondlife.com/svn/linden/projects/2008/pyogp/lib/base/trunk/CONTRIBUTORS.txt   $LicenseInfo:firstyear=2008&license=apachev2$  Copyright 2009, Linden Research, Inc.  Licensed under the Apache License, Version 2.0. You may obtain a copy of the License at:     http://www.apache.org/licenses/LICENSE-2.0 or in      http://svn.secondlife.com/svn/linden/projects/2008/pyogp/lib/base/LICENSE.txt  $/LicenseInfo$ # standard python libraries # related # pyogp # initialize logging a simple class to track multiple agents      This class can perhaps begin to manage sessions in time. initialize the agent manager # allow the settings to be passed in # otherwise, grab the defaults # store the agents in a dictionary keyed by a uuid # this could be agent_id, if we know it ahead of time # or, create a new one # signal handler to capture erm signals accept a list of Agent() instances, and store them in the agents attribute adds an agent to the store # check if the agent is on the store returns True if an agent is already stored, False if not returns the stored agent's names in a list returns the stored agent's names in a list spawns a new agent via an eventlet coroutine returns true if there is a client who's running value = True returns a list of agents that are connected to a grid handles signals from the command line (and others) and logs out all agents #agent.logout() #Wait(10) #if self.has_agents_running(): #    logger.warning("These agents have not yet shut down. Killing the process hard.\n\t\t%s" % (self.get_active_agents())) #    sys.exit(1)