示例#1
0
 def _keep_alive(self):
     """
     Method for creating and spawning a separate thread for sending keep
     alive messages to the RasManager
     """
     if not self._rasmgr_keep_alive_running:
         self._rasmgr_keep_alive_running = True
         if not self._keep_alive_thread:
             self._keep_alive_thread = StoppableTimeoutThread(
                 rasmgr_keep_alive, self.session.keepAliveTimeout / 2000,
                 self.stub, self.session.clientUUID)
             self._keep_alive_thread.daemon = True
             self._keep_alive_thread.start()
     else:
         raise Exception("RasMgrKeepAlive already running")
示例#2
0
class Connection(object):
    """
    Class to represent the connection from the python client to the rasdaman
    server
    """

    def __init__(self, hostname="0.0.0.0", port=7001, username="******",
                 password="******"):
        """
        Constructor for the connection class
        :param str hostname: the hostname of the rasdaman server (default:
        localhost)
        :param int port: the port on which rasdaman listens on (default: 7001)
        :param str username: username for the database (default: rasguest)
        :param str password: password for the database (default: rasguest)
        """
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password
        self.channel = insecure_channel(hostname, port)
        self.stub = rasmgr.beta_create_RasmgrClientService_stub(self.channel)
        self.session = None
        self._rasmgr_keep_alive_running = None
        self._keep_alive_thread = None

    def disconnect(self):
        """
        Method for disconnecting the connection to the RasManager. Stops
        sending keep alive messages to the RasManager, disconnects, and destroys
        the session.
        """
        self._stop_keep_alive()
        rasmgr_disconnect(self.stub, self.session.clientUUID,
                          self.session.clientId)
        self.session = None

    def connect(self):
        """
        Method for connecting to the RasManager. Sends a connection request
        and once connected starts sending keep alive messages to the
        RasManager
        """
        self.session = rasmgr_connect(self.stub, self.username, self.password)
        self._keep_alive()

    def _keep_alive(self):
        """
        Method for creating and spawning a separate thread for sending keep
        alive messages to the RasManager
        """
        if not self._rasmgr_keep_alive_running:
            self._rasmgr_keep_alive_running = True
            if not self._keep_alive_thread:
                self._keep_alive_thread = StoppableTimeoutThread(
                    rasmgr_keep_alive,
                    self.session.keepAliveTimeout / 2000,
                    self.stub, self.session.clientUUID)
                self._keep_alive_thread.daemon = True
                self._keep_alive_thread.start()
        else:
            raise Exception("RasMgrKeepAlive already running")

    def _stop_keep_alive(self):
        """
        Method for stopping the thread that is responsible for sending keep
        alive messages to the RasManager
        """
        if self._rasmgr_keep_alive_running is not None:
            self._rasmgr_keep_alive_running = None
            if self._keep_alive_thread is not None:
                self._keep_alive_thread.stop()
                self._keep_alive_thread.join()
                self._keep_alive_thread = None
            else:
                raise Exception("No thread named _keep_alive_thread to stop")
        else:
            raise Exception("rasmgr_keep_alive thread not running")

    def database(self, name):
        """
        Returns a database object initialized with this connection
        :param str name: name of the database collection to access
        :rtype: Database
        :return: a new database object
        """
        database = Database(self, name)
        return database
示例#3
0
class Database(object):
    """
    Class to represent a database stored inside a rasdaman server
    """

    def __init__(self, connection, name):
        """
        Constructor for the Database class
        :param Connection connection: the connection object for the rasdaman
        server
        :param str name: the name of the database
        """
        self.connection = connection
        self.name = name
        self.rasmgr_db = None
        self.rassrvr_db = None
        self.channel = None
        self.stub = None
        self._rassrvr_keep_alive_running = None
        self._keep_alive_thread = None

    def open(self):
        """
        Opens a connection to the RasServer on which the database is stored
        and starts sending the keep alive messages to the RasServer. Also, stops
        sending keep alive messages to the RasManager in case they are on the
        same machine.
        """
        self.rasmgr_db = rasmgr_open_db(self.connection.stub,
                                        self.connection.session.clientUUID,
                                        self.connection.session.clientId,
                                        self.name)
        if self.rasmgr_db.dbSessionId == self.connection.session.clientUUID:
            self.connection._stop_keep_alive()
        self.channel = insecure_channel(
            self.rasmgr_db.serverHostName, self.rasmgr_db.port)
        self.stub = rassrvr.beta_create_ClientRassrvrService_stub(self.channel)
        self.rassrvr_db = rassrvr_open_db(self.stub,
                                          self.connection.session.clientId,
                                          self.name)

        # Open another thread to keep connection with rasserver
        self._keep_alive()

    def close(self):
        """
        Closes the connection to RasServer and RasManager. Also, stops
        sending the keep alive messages to the RasServer.
        """

        # Trying to stop keep alive message thread to rasserver
        self._stop_keep_alive()

        # Trying to close connection to rasserver
        rassrvr_close_db(self.stub, self.connection.session.clientId)

        # Trying to close connection to rasmgr
        rasmgr_close_db(self.connection.stub,
                        self.connection.session.clientUUID,
                        self.connection.session.clientId,
                        self.rasmgr_db.dbSessionId)

    def create(self):
        """
        Method for creating a collection
        """
        raise NotImplementedError("Sorry, not implemented yet")

    def destroy(self):
        """
        Method for destroying a collection
        """
        raise NotImplementedError("Sorry, not implemented yet")

    def transaction(self, rw=False):
        """
        Returns a new transaction object for this database
        :param bool rw: Boolean value for write access
        :rtype: Transaction
        :return: a new transaction object
        """
        transaction = Transaction(self, rw=rw)
        return transaction

    @property
    def collections(self):
        """
        Returns all the collections for this database
        :rtype: list[Collection]
        """
        transaction = self.transaction()
        query = transaction.query("select r from RAS_COLLECTIONNAMES as r")
        result = query._execute_read()
        return result

    def _keep_alive(self):
        """
        Method for creating and spawning a separate thread for sending keep
        alive messages to the RasServer
        """
        if not self._rassrvr_keep_alive_running:
            self._rassrvr_keep_alive_running = True
            if not self._keep_alive_thread:
                self._keep_alive_thread = StoppableTimeoutThread(
                    rassrvr_keep_alive,
                    self.connection.session.keepAliveTimeout / 2000,
                    self.stub, self.connection.session.clientUUID,
                    self.rasmgr_db.dbSessionId)

                self._keep_alive_thread.daemon = True
                self._keep_alive_thread.start()
        else:
            raise Exception("RasSrvrKeepAlive already running")

    def _stop_keep_alive(self):
        """
        Method for stopping the thread that is responsible for sending keep
        alive messages to the RasServer
        """
        if self._rassrvr_keep_alive_running is not None:
            self._rassrvr_keep_alive_running = None
            if self._keep_alive_thread is not None:
                self._keep_alive_thread.stop()
                self._keep_alive_thread.join()
                self._keep_alive_thread = None
            else:
                raise Exception("No thread named _keep_alive_thread to stop")
        else:
            raise Exception("rassrvr_keep_alive thread not running")