def on_connect(self, req: HTTPRequest, res: HTTPResponse):
     data = req.body_json()
     c = self._clients.connect(data)
     id = str(uuid.uuid4())
     self._connected[id] = c
     c.status = Client.STATUS_CONNECTED
     res.header("x-session-id", id)
     self._clients.save(False)
     c.save()
     res.ok(errors.OK, "OK", None)
    def on_result(self, req: HTTPRequest, res: HTTPResponse):
        c = self.get_client(req, res)
        if not c: return

        js = req.body_json()

        ret = c.result(js)
        if ret:
            res.ok(errors.OK, "OK", None)
        else:
            res.not_found(errors.ID_NOT_FOUND,
                          "L'id de la réponse n'existe pas")
    def _on_command(self, cmd, req: HTTPRequest, res: HTTPResponse):
        data = req.body_json()
        id = data["target"]
        sync = data["sync"]
        if not id in self._clients._clients:
            return res.not_found(errors.ID_NOT_FOUND,
                                 "Id " + id + " not found", None)

        c = self._clients._clients[id]
        c.send(cmd)
        if sync:
            r = c.find_response(cmd.id)
            while not r:
                time.sleep(0.01)
                r = c.find_response(cmd.id)
            res.ok(errors.OK, "OK", r)
        else:
            res.ok(errors.OK, "OK", CommandReturn(errors.OK, "").json())
    def on_put_file(self, req: HTTPRequest, res: HTTPResponse):
        c = self.get_client(req, res)
        id = str(uuid.uuid4())
        path = "download/" + id

        with open(path, "wb") as f:
            f.write(req.data)
            self._clients.add_file(id, req.header("x-filename"), c.id)
            return res.ok(errors.OK, "OK", id)
        return res.unauthorized(errors.ERROR_HTTP, "", "Unknown error")
    def on_get_result(self, req: HTTPRequest, res: HTTPResponse):
        if not self.is_authorized(req, res): return
        cid = req.params["clientid"]
        cmdid = req.params["cmdid"]
        if not self._clients.has(cid):
            return res.not_found(errors.ID_NOT_FOUND, "Client not found", None)

        client = self._clients[cid]

        if not client.has_command(cmdid):
            return res.not_found(errors.ID_NOT_FOUND, "Command not found",
                                 None)

        r = client.find_response(cmdid)
        while not r:
            time.sleep(0.01)
            client.find_response(cmdid)

        res.ok(errors.OK, "OK", r)
 def admin_on_server_command(self, req: HTTPRequest, res: HTTPResponse):
     if not self.is_authorized(req, res): return
     data = req.body_json()
     cmd = Command.from_text(data["cmd"])
     out = cmd.start(self).json()
     res.ok(errors.OK, "OK", out)
 def on_wait(self, req: HTTPRequest, res: HTTPResponse):
     c = self.get_client(req, res)
     if not c: return
     cmd = c.wait_fo_command()
     c.status = Client.STATUS_WAITING
     res.ok(errors.OK, "OK", cmd.json())