Пример #1
0
    def send_broadcast(self, 
                message, 
                username, 
                queue = None, 
                origin = None):
        """ append message to user's out queue
            queue = user/_tok/en_as_path/new_message_token
        """
        if message is None or username is None:
            raise NotifStorageException("incomplete storage request")
        max_msgs = int(self.config.get('redis.max_msgs_per_user', '200'))
        file_ok = False
        doc_path = self._user_storage_path(username)
        if queue is None:
            queue = origin;

        if not os.path.exists(doc_path):
            os.makedirs(doc_path)
        try:
            while (not file_ok):
                doc_file = new_token()
                file_ok = not os.path.isfile(os.path.join(doc_path, doc_file))
            file_path = os.path.join(doc_path, doc_file)
            file = os.open(file_path, os.O_WRONLY | os.O_CREAT)
            os.write(file, message)
            os.close(file)
        except IOError, e:
            logger.error("Could not write message file %s" % str(e))
            raise NotifStorageException("Error storing message content")
Пример #2
0
    def create_client_queue(self, username):
        self.channel_info = {}
        self.channel_info['exchange_name'] = username
        self.channel_info['queue_name'] = new_token()

        # Create a unique client id to also be used as the name of the queue
        client_queue_name = new_token()

        logger.info("Creating queue %s for user %s",
                        self.channel_info.get('queue_name'),
                        self.channel_info.get('exchange_name'))

        # Create the user exchange (if it doesn't already exist) and a client
        # queue with the generated name.
        conn = self._get_blocking_amqp_conn()
        try:
            channel = conn.channel()

            logger.debug("Declaring exchange %s",
                        self.channel_info.get('exchange_name'))
            channel.exchange_declare(
                exchange=self.channel_info.get('exchange_name'),
                durable=True,
                type='fanout',
            )

            logger.debug("Declaring queue %s", client_queue_name)
            channel.queue_declare(queue=client_queue_name, durable=True)

            logger.debug("Binding queue %s to exchange %s",
                client_queue_name,
                self.channel_info.get('exchange_name'),
            )
            channel.queue_bind(
                exchange=self.channel_info.get('exchange_name'),
                queue=client_queue_name,
            )
        except Exception, e:
            logger.error("Error creating new client queue. %s" % e.message)
            raise NotifStorageException('Cannot create new client')
Пример #3
0
    def new_token(self, request):
        """ Create an return a new valid token 
        
        @method GET

        @params None

        Returns a string containing the new token.
        """
        self._init(self.app.config)
        try:
            token = new_token()
            return json_response(token)
        except Exception, e:
            logger.error("Error generating token %s" % str(e))
            raise HTTPInternalServerError
Пример #4
0
 def create_client_queue(self, username):
     logger.info("Creating incoming queue for user %s" % username)
     try:
         # Check to see if the user already has a token.
         # ut: user -> token
         user_token = self.redis.get('u2t:%s' % username)
         if user_token is None:
             user_token = new_token()
             self.redis.set('u2t:%s' % username, user_token)
             self.redis.set('t2u:%s' % user_token, username)
             info = {"created": int(time.time()),
                  "state": 'active',
                  "changedate": int(time.time())}
             self.redis.hmset('sin:%s:%s' % (username, user_token), info)
         return {'queue_id': user_token,
                 'port': self.config.get('notifserver.port', 80),
                 'host': self.config.get('notifserver.host')}
     except Exception, ex:
         logger.error("Could not create user queue %s" % str(ex))
Пример #5
0
    def create_client_queue(self, username):
        self.mutex.acquire()

        self._ensure_user_exists(username)

        #TODO: Probably unnecessary, but make sure the queue name isn't taken
        queue_name = new_token()

        logger.info("Creating queue %s for user %s", queue_name, username)

        self.userToQueues[username].append(queue_name)
        self.queues[queue_name] = Queue()

        self.mutex.release()

        return {
            'queue_id': queue_name,
            'host': self.broker_host,
            'port': self.broker_port,
        }
Пример #6
0
    def create_client_queue(self, username):
        # create the mapping record

        channel_info = {u'token': new_token(),
                        u'user_id': username,
                        u'type': 'queue',
                        u'created': int(time.time())
                        }

        logger.info("Creating incoming queue %s for user %s" % (
                    channel_info.get('token'),
                    username))
        try:
            self.db.user.insert(channel_info, safe=True)
            return {'queue_id': channel_info['token'],
                    'host': self.config.get('notifserver.host'),
                    'port': self.config.get('notifserver.port')}

        except OperationFailure, e:
            logger.error('Could not create mapping: %s' % str(e))
            return False