Esempio n. 1
0
 def locate(host, port=None):
     """Simple class to easily contact an Manager
     """
     p = Proxy(uri=getManagerURI(host, port or MANAGER_DEFAULT_PORT))
     if not p.ping():
         raise ManagerNotFoundException("Couldn't find manager running on %s:%d" % (host, port))
     return p
Esempio n. 2
0
 def locate(host, port=None):
     """Simple class to easily contact an Manager
     """
     p = Proxy(uri=getManagerURI(host, port or MANAGER_DEFAULT_PORT))
     if not p.ping():
         raise ManagerNotFoundException(
             "Couldn't find manager running on %s:%d" % (host, port))
     return p
Esempio n. 3
0
    def locate():
        """Use to locate running instance of Chimera.

        When started, Manager creates a UDP broadcast server. This
        method tries to locate the Manager using a broadcast message
        and waiting for an answer.
        """
        
        sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sk.setblocking(0)

        try:
            try:
                sk.sendto(MANAGER_BEACON_CHALLENGE, ("", MANAGER_BEACON_PORT))
            
                ins, outs, excs = select.select([sk], [], [sk], 1)
            
                if sk in ins:
                    data, _ = sk.recvfrom(1024)
                    if data.strip() != MANAGER_BEACON_ERROR:
                        host, port = data.split(":")
                        return Proxy(uri=getManagerURI(host, int(port)))

                raise ManagerNotFoundException("Couldn't locate any suitable Manager.")

            except socket.error, e:
                raise ManagerNotFoundException("Error trying to locate a suitable Manager.")

        finally:
            sk.close()
Esempio n. 4
0
    def publish (self, topic, *args, **kwargs):

        if topic not in self.handlers:
            return True

        excluded = []

        for handler in self.handlers[topic]:

            # FIXME: reuse connections? if not, TIME_WAIT sockets start to slow down things
            proxy = Proxy (uri=handler["proxy"])

            try:
                dispatcher = getattr(proxy, handler["method"])
                #proxy._setOneway ([handler["method"]]) should be faster but results say no!
                dispatcher (*args, **kwargs)
            except AttributeError, e:
                tb_size = len(traceback.extract_tb(sys.exc_info()[2]))
                if tb_size == 1:
                    log.debug("Invalid proxy method ('%s %s') for '%s' handler." % \
                              (handler["proxy"], handler["method"], topic))
                else:
                    log.debug ("Handler (%s) raised an exception. Removing from subscribers list." % proxy)
                    log.exception(e)

                excluded.append(handler)
                continue
            except Pyro.errors.ProtocolError, e:
                log.debug ("Unreachable handler (%s). Removing from subscribers list." % proxy)
                excluded.append(handler)
                continue
Esempio n. 5
0
    def getProxyForObj(self, obj):

        # return Proxy(uri=Pyro4.URI(self.hostname,
        #                                    obj.GUID(),
        #                                    prtcol=self.protocol,
        #                                    port=self.port))

        #URI = Pyro4.URI("PYRO:ciccio@"+MANAGER_LOCATION+":"+str(self.port))
        URI = Pyro4.URI("PYRO:ciccio@" + MANAGER_LOCATION + ":7666")

        return Proxy(uri=URI)
Esempio n. 6
0
    def getProxy (self, location, name='0', host=None, port=None, lazy=False):
        """
        Get a proxy for the object pointed by location. The given location can contain index
        instead of names, e.g. '/Object/0' to get objects when you don't know their names.

        location can also be a class. getProxy will return an instance
        named 'name' at the given host/port (or on the current
        manager, if None given).

        host and port parameters determines which Manager we will
        lookup for location/instance. If None, look at this
        Manager. host/port is only used when location is a
        class, otherwise, host and port are determined by location
        itself.

        lazy parameter determines if Manager will try to locate the
        selected Manager at host/port and ask them for a valid
        object/instance. If False, Manager just return an proxy for
        the selected parameters but can't guarantee that the returned
        Proxy have an active object bounded.

        For objects managed by this own Manager, lazy is always False.

        @param location: Object location or class.
        @type location: Location or class

        @param name: Instance name.
        @type name: str

        @param host: Manager's hostname.
        @type host: str

        @param port: Manager's port.
        @type port: int

        @param lazy: Manager's laziness (check for already bound objects on host/port Manager)
        @type lazy: bool

        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises ObjectNotFoundException: When te request object or the Manager was not found.
        @raises InvalidLocationException: When the requested location s invalid.

        @return: Proxy for selected object.
        @rtype: Proxy
        """

        if not location:
            raise ObjectNotFoundException ("Couldn't find an object at the"
                                           " given location %s" % location)

        if not isinstance(location, StringType) and not isinstance(location, Location):

            if issubclass(location, ChimeraObject):
                location = Location(cls=location.__name__, name=name, host=host or self.getHostname(), port=port or self.getPort())
            else:
                raise NotValidChimeraObjectException ("Can't get a proxy from non ChimeraObject's descendent object (%s)." % location)

        else:
            location = Location(location, host=host or self.getHostname(), port=port or self.getPort())

        # who manages this location?
        if self._belongsToMe(location):

            ret = self.resources.get(location)
            
            if not ret:
                raise ObjectNotFoundException ("Couldn't found an object at the"
                                               " given location %s" % location)
            p = Proxy (uri=ret.uri)
            if lazy:
                return p
            else:
                p.ping()
                return p
        else:

            if lazy:
                return Proxy(location)
            else:
                # contact other manager
                try:
                    other = Proxy(location=MANAGER_LOCATION,
                                  host=location.host or host,
                                  port=location.port or port)
                except Pyro.errors.URIError, e:
                    raise InvalidLocationException("Invalid remote location given. '%s' (%s)." % (location, str(e)))

                if not other.ping():
                    raise ObjectNotFoundException ("Can't contact %s manager at %s." % (location, other.URI.address))

                proxy = other.getProxy(location)

                if not proxy:
                    raise ObjectNotFoundException ("Couldn't find an object at the"
                                                   " given location %s" % location)
                else:
                    return proxy
Esempio n. 7
0
 def getProxyForObj(self, obj):
     return Proxy(uri=Pyro.core.PyroURI(
         self.hostname, obj.GUID(), prtcol=self.protocol, port=self.port))
Esempio n. 8
0
    def getProxy(self, location, name='0', host=None, port=None, lazy=False):
        """
        Get a proxy for the object pointed by location. The given location can contain index
        instead of names, e.g. '/Object/0' to get objects when you don't know their names.

        location can also be a class. getProxy will return an instance
        named 'name' at the given host/port (or on the current
        manager, if None given).

        host and port parameters determines which Manager we will
        lookup for location/instance. If None, look at this
        Manager. host/port is only used when location is a
        class, otherwise, host and port are determined by location
        itself.

        lazy parameter determines if Manager will try to locate the
        selected Manager at host/port and ask them for a valid
        object/instance. If False, Manager just return an proxy for
        the selected parameters but can't guarantee that the returned
        Proxy have an active object bounded.

        For objects managed by this own Manager, lazy is always False.

        @param location: Object location or class.
        @type location: Location or class

        @param name: Instance name.
        @type name: str

        @param host: Manager's hostname.
        @type host: str

        @param port: Manager's port.
        @type port: int

        @param lazy: Manager's laziness (check for already bound objects on host/port Manager)
        @type lazy: bool

        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises ObjectNotFoundException: When te request object or the Manager was not found.
        @raises InvalidLocationException: When the requested location s invalid.

        @return: Proxy for selected object.
        @rtype: Proxy
        """

        if not location:
            raise ObjectNotFoundException("Couldn't find an object at the"
                                          " given location %s" % location)

        if not isinstance(location, StringType) and not isinstance(
                location, Location):

            if issubclass(location, ChimeraObject):
                location = Location(cls=location.__name__,
                                    name=name,
                                    host=host or self.getHostname(),
                                    port=port or self.getPort())
            else:
                raise NotValidChimeraObjectException(
                    "Can't get a proxy from non ChimeraObject's descendent object (%s)."
                    % location)

        else:
            location = Location(location,
                                host=host or self.getHostname(),
                                port=port or self.getPort())

        # who manages this location?
        if self._belongsToMe(location):

            ret = self.resources.get(location)

            if not ret:
                raise ObjectNotFoundException("Couldn't found an object at the"
                                              " given location %s" % location)
            p = Proxy(uri=ret.uri)
            if lazy:
                return p
            else:
                p.ping()
                return p
        else:

            if lazy:
                return Proxy(location)
            else:
                # contact other manager
                try:
                    other = Proxy(location=MANAGER_LOCATION,
                                  host=location.host or host,
                                  port=location.port or port)
                except Pyro.errors.URIError, e:
                    raise InvalidLocationException(
                        "Invalid remote location given. '%s' (%s)." %
                        (location, str(e)))

                if not other.ping():
                    raise ObjectNotFoundException(
                        "Can't contact %s manager at %s." %
                        (location, other.URI.address))

                proxy = other.getProxy(location)

                if not proxy:
                    raise ObjectNotFoundException(
                        "Couldn't find an object at the"
                        " given location %s" % location)
                else:
                    return proxy
Esempio n. 9
0
            log.exception("Error configuring %s." % location)
            raise ChimeraObjectException("Error configuring %s. (%s)" %
                                         (location, e))

        # connect
        obj.__setlocation__(location)
        next = len(self.resources.getByClass(location.cls))
        uri = self.adapter.connect(obj,
                                   index=str(
                                       Location(cls=location.cls, name=next)))
        self.resources.add(location, obj, uri)

        if start:
            self.start(location)

        return Proxy(uri=uri)

    def remove(self, location):
        """
        Remove the object pointed by 'location' from the system
        stopping it before if needed.

        @param location: The object to remove.
        @type location: Location,str

        @raises ObjectNotFoundException: When te request object or the Manager was not found.

        @return: retuns True if sucessfull. False otherwise.
        @rtype: bool
        """
Esempio n. 10
0
minimo = chimera.Minimo("min", host='localhost')
minimo.doFoo ("ra dec")

# this will use index as no name was passed
minimo = chimera.Minimo(host='localhost')
minimo.doFoo ("ra dec")

#
# 3. 2. ChimeraProxy objects.
# this is the low level proxy used by the system. They will try to contact
# the object directly, bypassing manager. So, if the object was not
# created by the manager, this will raise an exception (the syntax is the same as above)
#

# proxy without manager interface (fastest, don't ask manager about it)
minimo = Proxy ("/Minimo/min")
minimo.doFoo ("ra dec")

minimo = Proxy ("/Minimo/0")
minimo.doFoo ("ra dec")

minimo = Proxy ("/Minimo/min", host = 'localhost')
minimo.doFoo ("ra dec")

minimo = Proxy ("/Minimo/0", host = 'localhost')
minimo.doFoo ("ra dec")

# OK, enough proxies, they are REALLY important, its the ONLY way to get an object,
# so you really need to know how to get them.

manager.shutdown()
Esempio n. 11
0
class Manager(RemoteObject):
    """
    This is the main class of Chimera.

    Use this class to get Proxies, add objects to the system, and so on.

    This class handles objects life-cycle as described in ILifecycle.

    @group Add/Remove: add*, remove
    @group Start/Stop: start, stop
    @group Proxy: getProxy
    @group Shutdown: wait, shutdown

    """
    def __init__(self, host=None, port=None, local=False):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        if not local:
            try:
                ManagerLocator.locate()
                raise ChimeraException("Chimera is already running"
                                       " on this system. Use chimera-admin"
                                       " to manage it.")
            except ManagerNotFoundException:
                # ok, we are alone.
                pass

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # finder beacon
        if not local:
            self.beacon = ManagerBeacon(self)
            self.beaconThread = threading.Thread(target=self.beacon.run)
            self.beaconThread.setDaemon(True)
            self.beaconThread.start()
        else:
            self.beacon = None

        # register ourself
        self.resources.add(MANAGER_LOCATION, self,
                           getManagerURI(self.getHostname(), self.getPort()))

        # signals
        signal.signal(signal.SIGTERM, self._sighandler)
        signal.signal(signal.SIGINT, self._sighandler)
        atexit.register(self._sighandler)

    # private
    def __repr__(self):
        if hasattr(self, 'adapter') and self.adapter:
            return "<Manager for %s:%d at %s>" % (
                self.adapter.hostname, self.adapter.port, hex(id(self)))
        else:
            return "<Manager at %s>" % hex(id(self))

    def _sighandler(self, sig=None, frame=None):
        self.shutdown()

    # adapter host/port
    def getHostname(self):
        if self.adapter:
            return self.adapter.hostname
        else:
            return None

    def getPort(self):
        if self.adapter:
            return self.adapter.port
        else:
            return None

    # reflection (console)
    def getResources(self):
        """
        Returns a list with the Location of all the available resources
        """
        return self.resources.keys()

    def getResourcesByClass(self, cls):
        resources = self.getResources()
        toRet = []
        for r in resources:
            if r.cls == cls:
                toRet.append(r)
        return toRet

    # helpers

    def getDaemon(self):
        return self.adapter

    def getProxy(self, location, name='0', host=None, port=None, lazy=False):
        """
        Get a proxy for the object pointed by location. The given location can contain index
        instead of names, e.g. '/Object/0' to get objects when you don't know their names.

        location can also be a class. getProxy will return an instance
        named 'name' at the given host/port (or on the current
        manager, if None given).

        host and port parameters determines which Manager we will
        lookup for location/instance. If None, look at this
        Manager. host/port is only used when location is a
        class, otherwise, host and port are determined by location
        itself.

        lazy parameter determines if Manager will try to locate the
        selected Manager at host/port and ask them for a valid
        object/instance. If False, Manager just return an proxy for
        the selected parameters but can't guarantee that the returned
        Proxy have an active object bounded.

        For objects managed by this own Manager, lazy is always False.

        @param location: Object location or class.
        @type location: Location or class

        @param name: Instance name.
        @type name: str

        @param host: Manager's hostname.
        @type host: str

        @param port: Manager's port.
        @type port: int

        @param lazy: Manager's laziness (check for already bound objects on host/port Manager)
        @type lazy: bool

        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises ObjectNotFoundException: When te request object or the Manager was not found.
        @raises InvalidLocationException: When the requested location s invalid.

        @return: Proxy for selected object.
        @rtype: Proxy
        """

        if not location:
            raise ObjectNotFoundException("Couldn't find an object at the"
                                          " given location %s" % location)

        if not isinstance(location, StringType) and not isinstance(
                location, Location):

            if issubclass(location, ChimeraObject):
                location = Location(cls=location.__name__,
                                    name=name,
                                    host=host or self.getHostname(),
                                    port=port or self.getPort())
            else:
                raise NotValidChimeraObjectException(
                    "Can't get a proxy from non ChimeraObject's descendent object (%s)."
                    % location)

        else:
            location = Location(location,
                                host=host or self.getHostname(),
                                port=port or self.getPort())

        # who manages this location?
        if self._belongsToMe(location):

            ret = self.resources.get(location)
            if not ret:
                raise ObjectNotFoundException("Couldn't found an object at the"
                                              " given location %s" % location)

            return Proxy(uri=ret.uri)

        else:

            if lazy:
                return Proxy(location)
            else:

                # contact other manager
                other = Proxy(location=MANAGER_LOCATION,
                              host=location.host or host,
                              port=location.port or port)

                if not other.ping():
                    raise ObjectNotFoundException(
                        "Can't contact %s manager at %s." %
                        (location, other.URI.address))

                proxy = other.getProxy(location)

                if not proxy:
                    raise ObjectNotFoundException(
                        "Couldn't found an object at the"
                        " given location %s" % location)
                else:
                    return proxy

    def _belongsToMe(self, location):

        meHost = self.getHostname()
        meName = socket.gethostbyname(meHost)
        mePort = self.getPort()

        return (location.host == None or location.host in (meHost, meName)) and \
               (location.port == None or location.port == self.getPort())

    # shutdown management

    def shutdown(self):
        """
        Ask the system to shutdown. Closing all sockets and stopping
        all threads.

        @return: Nothing
        @rtype: None
        """

        # die, but only if we are alive ;)
        if not self.died.isSet():

            log.info("Shuting down manager.")

            # stop objects
            # damm 2.4, on 2.5 try/except/finally works
            try:
                try:

                    elderly_first = sorted(
                        list(self.resources.values()),
                        cmp=lambda x, y: cmp(x.created, y.created),
                        reverse=True)

                    for resource in elderly_first:

                        # except Manager
                        if resource.location == MANAGER_LOCATION: continue

                        # stop object
                        self.stop(resource.location)

                except ChimeraException:
                    pass
            finally:
                # kill our adapter
                self.adapter.shutdown(disconnect=True)
                if self.beacon:
                    self.beacon.shutdown()
                    self.beaconThread.join()

                # die!
                self.died.set()
                log.info("Manager finished.")

    def wait(self):
        """
        Ask the system to wait until anyone calls L{shutdown}.

        If nobody calls L{shutdown}, you can stop the system using
        Ctrl+C.

        @return: Nothing
        @rtype: None
        """

        try:
            while not self.died.isSet():
                time.sleep(1)
        except IOError:
            # On Windows, Ctrl+C on a sleep call raise IOError 'cause
            # of the interrupted syscall
            pass

    # objects lifecycle

    def addLocation(self, location, path=[], start=True):
        """
        Add the class pointed by 'location' to the system configuring it using 'config'.

        Manager will look for the class in 'path' plus sys.path.

        @param path: The class search path.
        @type path: list

        @param start: start the object after initialization.
        @type start: bool

        @raises ChimeraObjectException: Internal error on managed (user) object.
        @raises ClassLoaderException: Class not found.
        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises InvalidLocationException: When the requested location s invalid.              

        @return: retuns a proxy for the object if sucessuful, False otherwise.
        @rtype: Proxy or bool
        """

        if type(location) != Location:
            location = Location(location)

        # get the class
        cls = None

        cls = self.classLoader.loadClass(location.cls, path)

        return self.addClass(cls, location.name, location.config, start)

    def addClass(self, cls, name, config={}, start=True):
        """
        Add the class 'cls' to the system configuring it using 'config'.

        @param cls: The class to add to the system.
        @type cls: ChimeraObject

        @param name: The name of the new class instance.
        @type name: str

        @param config: The configuration dictionary for the object.
        @type config: dict

        @param start: start the object after initialization.
        @type start: bool

        @raises ChimeraObjectException: Internal error on managed (user) object.
        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises InvalidLocationException: When the requested location s invalid.              

        @return: retuns a proxy for the object if sucessuful, False otherwise.
        @rtype: Proxy or bool
        """

        location = Location(cls=cls.__name__, name=name, config=config)

        # names must not start with a digit
        if location.name[0] in "0123456789":
            raise InvalidLocationException(
                "Invalid instance name: %s (must start with a letter)" %
                location)

        if location in self.resources:
            raise InvalidLocationException(
                "Location %s is already in the system. Only one allowed (Tip. change the name!)."
                % location)

        # check if it's a valid ChimeraObject
        if not issubclass(cls, ChimeraObject):
            raise NotValidChimeraObjectException(
                "Cannot add the class %s. It doesn't descend from ChimeraObject."
                % cls.__name__)

        # run object __init__ and configure using location configuration
        # it runs on the same thread, so be a good boy
        # and don't block manager's thread
        try:
            obj = cls()
        except Exception:
            log.exception("Error in %s __init__." % location)
            raise ChimeraObjectException("Error in %s __init__." % location)

        try:
            for k, v in location.config.items():
                obj[k] = v
        except (OptionConversionException, KeyError), e:
            log.exception("Error configuring %s." % location)
            raise ChimeraObjectException("Error configuring %s. (%s)" %
                                         (location, e))

        # connect
        obj.__setlocation__(location)
        next = len(self.resources.getByClass(location.cls))
        uri = self.adapter.connect(obj,
                                   index=str(
                                       Location(cls=location.cls, name=next)))
        self.resources.add(location, obj, uri)

        if start:
            self.start(location)

        return Proxy(uri=uri)
Esempio n. 12
0
minimo = chimera.Minimo("min", host='localhost')
minimo.doFoo("ra dec")

# this will use index as no name was passed
minimo = chimera.Minimo(host='localhost')
minimo.doFoo("ra dec")

#
# 3. 2. ChimeraProxy objects.
# this is the low level proxy used by the system. They will try to contact
# the object directly, bypassing manager. So, if the object was not
# created by the manager, this will raise an exception (the syntax is the same as above)
#

# proxy without manager interface (fastest, don't ask manager about it)
minimo = Proxy("/Minimo/min")
minimo.doFoo("ra dec")

minimo = Proxy("/Minimo/0")
minimo.doFoo("ra dec")

minimo = Proxy("/Minimo/min", host='localhost')
minimo.doFoo("ra dec")

minimo = Proxy("/Minimo/0", host='localhost')
minimo.doFoo("ra dec")

# OK, enough proxies, they are REALLY important, its the ONLY way to get an object,
# so you really need to know how to get them.

manager.shutdown()
Esempio n. 13
0
    def addClass(self, cls, name, config={}, start=True):
        """
        Add the class 'cls' to the system configuring it using 'config'.

        @param cls: The class to add to the system.
        @type cls: ChimeraObject

        @param name: The name of the new class instance.
        @type name: str

        @param config: The configuration dictionary for the object.
        @type config: dict

        @param start: start the object after initialization.
        @type start: bool

        @raises ChimeraObjectException: Internal error on managed (user) object.
        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises InvalidLocationException: When the requested location s invalid.              

        @return: retuns a proxy for the object if sucessuful, False otherwise.
        @rtype: Proxy or bool
        """

        location = Location(cls=cls.__name__, name=name, config=config)

        # names must not start with a digit
        if location.name[0] in "0123456789":
            raise InvalidLocationException(
                "Invalid instance name: %s (must start with a letter)" %
                location)

        if location in self.resources:
            raise InvalidLocationException(
                "Location %s is already in the system. Only one allowed (Tip: change the name!)."
                % location)

        # check if it's a valid ChimeraObject
        if not issubclass(cls, ChimeraObject):
            raise NotValidChimeraObjectException(
                "Cannot add the class %s. It doesn't descend from ChimeraObject."
                % cls.__name__)

        # run object __init__ and configure using location configuration
        # it runs on the same thread, so be a good boy
        # and don't block manager's thread
        try:
            obj = cls()
        except Exception:
            log.exception("Error in %s __init__." % location)
            raise ChimeraObjectException("Error in %s __init__." % location)

        try:
            for k, v in list(location.config.items()):
                obj[k] = v
        except (OptionConversionException, KeyError) as e:
            log.exception("Error configuring %s." % location)
            raise ChimeraObjectException("Error configuring %s. (%s)" %
                                         (location, e))

        # connect
        obj.__setlocation__(location)
        next = len(self.resources.getByClass(location.cls))
        uri = self.adapter.connect(obj,
                                   index=str(
                                       Location(cls=location.cls, name=next)))
        self.resources.add(location, obj, uri)

        if start:
            self.start(location)

        return Proxy(uri=uri)