Esempio n. 1
0
 async def helper(self, sid, request):
   try:
     result = None
     logger.info("REQUEST\nfunc_name: {}\nrequest: {}\n".format(
       name, request
     ))
     product = await func(self, sid, request)
     logger.info("REQUEST\nfunc_name: {}\nproduct: {}\nrequest: {}\n".format(
       name, product, request
     ))
     if not is_error(product):
       try:
         validate(instance=product, schema=bres.schema[name])
         result = dumps(product, cls=DictEncoder)
       except ValidationError:
         logger.info("VALIDATION ERROR\nfunc_name: {}\nReturn response: {}\nReturn schema: {}\n".format(
           name, product, name
         ))
         result = make_error(Errors.SERVER_ERR)
     else:
       logger.info("INPUT ERROR\nfunc_name: {}\nReturn response: {}".format(
         name, product
       ))
       result = product
   except Exception as e: # catch generic exceptions
     logger.info("GENERIC ERROR\nfunc_name: {}\nrequest: {}\nexception: {}\n".format(
       name, request, e
     ))
     result = make_error(Errors.SERVER_ERR)
   
   logger.info("RESULT\n{}\n".format(result))
   return result
Esempio n. 2
0
    async def on_join(self, sid, request):
        """
        :event: :ref:`dreq_join-label`
        :return: *joined_user* (:ref:`dres_joined_user-label`)
        :emit: *set_cursor* (:ref:`dres_set_cursor-label`)
        :errors: BAD_REQUEST, BAD_RESPONSE, BAD_DISCUSSION_ID 
        """
        user_id = request["user_id"]

        session = await self.get_session(sid)
        discussion_id = session["discussion_id"]

        result = gm.discussion_manager.join(
          discussion_id=discussion_id, 
          user_id=user_id
        )

        # result is successful, joined means we are in room
        if not is_error(result):
          # resave discussion ID, as this replaces user session
          await self.save_session(sid, {
            "joined": True,
            "user_id": user_id,
            "discussion_id": discussion_id
          })
          self.enter_room(sid, discussion_id)
          # need to enter before can emit

        return result
Esempio n. 3
0
 async def on_join_board(self, sid, request):
   board_id = request["board_id"]
   result = gm.board_manager.join_board(
     board_id=board_id,
   )
   if not is_error(result):
     await self.save_session(sid, {
       "board_id": board_id, 
     })
     self.enter_room(sid, board_id)
   return result
Esempio n. 4
0
async def on_create(sid, request):
    """
    :return: :ref:`bres_created-label`
    :errors: BAD_RESPONSE 
    """
    product = gm.board_manager.create()

    if not is_error(product):
      try:
        validate(instance=product, schema=bres.created)
        serialized = dumps(product, cls=DictEncoder)
      except ValidationError:
        serialized = make_error(Errors.BAD_RESPONSE)
    else:
      serialized = make_error(product)
    return serialized
Esempio n. 5
0
    async def on_leave_disc(self, sid, request):
      board_id = request["board_id"]
      discussion_id = request["discussion_id"]
      result = gm.discussion_manager.leave_disc(
        board_id=board_id,
        discussion_id=discussion_id,
        user_id=request["user_id"],
      )

      if not is_error(result):
        # may still be in same board
        #await self.save_session(sid, {
        #  "discussion_id": None, 
        #})
        # use session to emit properly to room
        self.leave_room(sid, get_room(board_id, discussion_id))

      return result
Esempio n. 6
0
        async def helper(self, sid, request):
          try:
            result = None
            logger.info("REQUEST (sid: {})\nfunc_name: {}\nrequest: {}\n".format(
              sid, name, request
            ))
            product = await func(self, sid, request)
            logger.info("func_name: {}\nproduct: {}\nrequest: {}\n".format(
              name, product, request
            ))
            if not is_error(product):
              try:
                validate(instance=product, schema=dres.schema[name])
                result = dumps(product, cls=DictEncoder)
                if emit: # send result to others in room
                  session = await self.get_session(sid)
                  logger.info("Emitting (sid: {}) with {}".format(sid, session))
                  if "board_id" in session and "discussion_id" in session:
                    board_id = session["board_id"]
                    discussion_id = session["discussion_id"]
                    logger.info("Emitting to {}/{}.".format(board_id, discussion_id))
                    await self.emit(
                      name, 
                      result, 
                      room=get_room(board_id, discussion_id), 
                      skip_sid=sid
                    )
              except ValidationError:
                logger.info("Return response: {}\nReturn schema: {}".format(
                  product, name
                ))
                result = make_error(Errors.SERVER_ERR)
            else:
              logger.info("INPUT ERROR\nfunc_name: {}\nReturn response: {}".format(
                name, product
              ))
              result = product
          except Exception as e: # catch generic exceptions
            logger.info("func_name: {}\nrequest: {}\nexception: {}\n".format(
              name, request, e
            ))
            result = make_error(Errors.SERVER_ERR)

          return result
Esempio n. 7
0
    async def on_join_disc(self, sid, request):
      board_id = request["board_id"]
      discussion_id = request["discussion_id"]
      user_id = request["user_id"]
      result = gm.discussion_manager.join_disc(
        board_id=board_id,
        discussion_id=discussion_id,
        user_id=user_id,
      )

      if not is_error(result):
        await self.save_session(sid, {
          "board_id": board_id,
          "discussion_id": discussion_id, 
          "user_id": user_id,
        })
        self.enter_room(sid, get_room(board_id, discussion_id))

      return result
Esempio n. 8
0
        async def helper(self, sid, request):
          try:
            result = None
            product = await func(self, sid, request)
            logger.info("func_name: {}\nproduct: {}\nrequest: {}\n".format(
              name, product, request
            ))

            if is_error(product):
              result = make_error(product, info={
                "func_name": name, "result": result, "request": request
              })
            else:
              ret_res, emits_res = product

              bad_response = False

              if ret is not None:
                try:
                  validate(instance=ret_res, schema=dres.schema[ret])
                  result = ret_res
                except ValidationError:
                  logger.info("Return response: {}\nReturn schema: {}".format(ret_res, ret))
                  bad_response = True

              if emits is not None:
                assert(emits_res is not None)
                for r, e in zip(emits_res, emits):
                  try:
                    validate(instance=r, schema=dres.schema[e])
                  except ValidationError:
                    logger.info("Emit response: {}\nEmit schema: {}".format(r, e))
                    bad_response = True

              if bad_response: # we cannot send off emits
                result = make_error(Errors.BAD_RESPONSE)
              else: # we can send off emits

                shared = {}
                if emits is not None:
                  assert(emits_res is not None)
                  for r, e in zip(emits_res, emits):
                    shared[e] = r

                if result is None:
                  result = {}
                # set emitted data to return
                result["shared"] = shared

                result = dumps(result, cls=DictEncoder) # default returns

                # every function except maybe leave should have a discussion id
                session = await self.get_session(sid)
                # send to everyone else
                if "discussion_id" in session:
                  discussion_id = session["discussion_id"]
                  emit_shared = dumps(shared, cls=DictEncoder)
                  await self.emit(name, emit_shared, room=discussion_id, skip_sid=sid)

            return result

          except Exception as e:
            logger.info("func_name: {}\nrequest: {}\nexception: {}\n".format(
              name, request, e
            ))