Exemple #1
0
 def Delete(self, request: chat.GetRequest, context):
     try:
         query, username = self.find_query(request)
     except KeyError:
         return chat.Response(ok=False)
     self.table.delete_one(query)
     return chat.Response(ok=True)
Exemple #2
0
    def Publish(self, request: chat.PublishRequest, context):
        Pscrud.log('publish', request)
        if request.topic not in self.subscriptions:
            print('Server publish: topic {} not found'.format(request.topic))
            return chat.Response(ok=False)
        subscribers = self.subscriptions[request.topic]
        print('Server publish: subscribers={}'.format(subscribers))
        if not subscribers:
            print('Server publish: no subscribers for ' + request.topic)
            return chat.Response(ok=true)

        for subscriber in subscribers:
            print('Server publish: subscriber = {}'.format(subscriber))
            if subscriber in self.queues:
                publication = chat.Publication(topic=request.topic,
                                               data=request.data)
                publication.id = self.store(subscriber, publication.data)
                self.queues[subscriber].put(publication)
                print('Server put')
            else:
                print('Server publish: no queue for {} in {}'.format(
                    subscriber, self.queues.keys()))

        print('Server published')
        return chat.Response(ok=True)
Exemple #3
0
 def Authenticate(self, request, context):
     print('Server: {} {}'.format('authenticate', request.session))
     self.sessions[request.session] = request.session
     username = self.sessions[request.session]
     if not username:
         print('Server Authenticate: session {} not found'.format(
             request.session))
         return chat.Response(ok=False)
     self.subscriptions[username] = set()
     self.queues[request.session] = Queue()
     return chat.Response(ok=True)
Exemple #4
0
 def Deregister(self, request: chat.Request, context):
     username = self.sessions[request.session]
     if not username:
         print('Server Deregister: session {} not found'.format(
             request.session))
         return chat.Response(ok=False)
     print('Server: {} {}'.format('deregister', username))
     query = {Pscrud.key_username: username}
     self.table.delete_one(query)
     self.queues[username] = None
     return chat.Response(ok=True)
Exemple #5
0
 def Subscribe(self, request, context):
     Pscrud.log('subscribe', request)
     username = self.sessions[request.session]
     if not username:
         print('Server subscribe: session {} not found'.format(
             request.session))
         return chat.Response(ok=False)
     print('Server subscribe: username={}'.format(username))
     print('Server subscribe: subscriptions={}'.format(self.subscriptions))
     if request.topic not in self.subscriptions:
         print('subscriptions new topic {}'.format(request.topic))
         self.subscriptions[request.topic] = set()
     print('subscriptions add {} for {}'.format(request.topic, username))
     self.subscriptions[request.topic].add(username)
     print('subscriptions done: {}'.format(self.subscriptions))
     return chat.Response(ok=True)
Exemple #6
0
 def Unsubscribe(self, request, context):
     Pscrud.log('subscribe', request)
     username = self.sessions[request.session]
     if not username:
         print('Server Unsubscribe: session {} not found'.format(
             request.session))
         return chat.Response(ok=False)
     if request.topic not in self.subscriptions:
         print('Server unsubscribe: topic {} not found'.format(
             request.topic))
         return chat.Response(ok=False)
     if username not in self.subscriptions[request.topic]:
         print('Server unsubscribe: username {} not subscribed to topic'.
               format(username, request.topic))
         return chat.Response(ok=False)
     subscribers = self.subscriptions[
         request.topic] if request.topic in self.subscriptions else None
     # phew!
     subscribers.remove(username)
     return chat.Response(ok=True)
Exemple #7
0
 def Update(self, request: chat.PutRequest, context):
     try:
         query, username = self.find_query(request)
     except KeyError:
         return chat.PutResponse(ok=False)
     entry = {
         "$set": {
             Pscrud.key_username: username,
             Pscrud.key: request.key,
             Pscrud.key_data: request.data
         }
     }
     self.table.update_one(query, entry)
     return chat.Response(ok=True)
Exemple #8
0
 def Listen(self, request, context):
     Pscrud.log('listen', request)
     username = self.sessions[request.session]
     if not username:
         print('Server Listen: session {} not found'.format(
             request.session))
         return chat.Response(ok=False)
     if username in self.queues:
         while True:
             publication = self.queues[request.session].get(
             )  # blocking until the next .put for this queue
             print('Server listen: {} q.get {} for topic {}'.format(
                 username, publication.data, publication.topic))
             yield publication
     else:
         print('Server listen: no subscription queue for ' +
               request.session)