Example #1
0
    def _handle_request(self, conn, addr):
        while True:
            try:
                req = Flow.from_socket(conn)

                if req is None:
                    break

                print("SERVER: Received from {}:{} {}".format(
                    addr[0], addr[1], self.shorten(req.raw)))
                print("SERVER: handling on {}".format(
                    threading.current_thread().name))

                res_data = self.target(req)
                status = "OKAY"
                res = Flow(content=res_data,
                           path=req.path,
                           format=self.format,
                           Status=status)
            except Exception as e:
                status = "ERROR"
                res = Flow(content=traceback.format_exc() + "\n\n" + str(e),
                           path=Path.empty(),
                           format="text",
                           Status=status)

            res_bytes = res.to_bytes()

            print("SERVER: Replying to {}:{} with {}: {}".format(
                addr[0], addr[1], status, self.shorten(res_bytes)))
            conn.sendall(res_bytes)

        conn.close()
Example #2
0
    def disconnect(self):
        with SocketClient(self.vertex_host, self.vertex_port) as client:
            res = client.send_message(
                Path(self.vertex_host, "Vertex", "Disconnect"),
                {"name": self.name})
            raise NotImplemented()

        self.state = NodeState.DISCONNECTED
Example #3
0
def test_vertex_general():
    config = ConfigLoader("tests/config.json", "default").load_json()
    boot_config = ConfigLoader("tests/node_config.json", "default").load_json()

    val = 0

    def task1(task: Task):
        nonlocal val
        val = task.data
        return val["x"] + 1

    with VertexServer(config, Logic(config)) as server:

        boot_config.alter("vertex_port", server.port)

        with pytest.raises(ServerException):
            SocketClient("localhost", server.port).send_single(Path("TEST", "Vertex", "Lookup"), {"layer": "layer1"})

        # connect and set up a new node
        layer_router = Router("Layer 1", {"task1": task1})
        node_router = Router("NODE", {"layer1": layer_router})
        node = Node(boot_config, NodeServer(node_router))
        node.start()

        assert node.config == config

        # activate lease for layer-1
        server.logic.activate_lease(server.logic.get_worker("localhost").leases[0].uid)
        assert node.leases[0].state == LeaseState.ACTIVE
        assert node.leases[0].is_valid()

        # start a new connection from a caller
        res = SocketClient("localhost", server.port).send_single(Path("TEST", "Vertex", "Lookup"), {"layer": "layer1"})
        print(res.raw)

        target = res.content["name"], res.content["server_port"]

        res = SocketClient(target[0], target[1]).send_single(Path(target[0], "layer1", "task1"), {"x": 44})

        assert val == {"x": 44}
        assert res.content == 45

        node.close()
Example #4
0
def test_flow():
    p = Path("TEST", "layer", ["TASK", "SUB"])
    f = Flow(content="abc123", path=p, format="text", a=1, b=7, c="aaaa")

    s = str(p).encode() + b"""
text
a: 1
b: 7
c: aaaa

abc123"""

    assert f.to_bytes()[5:] == s
Example #5
0
def test_sockets():
    val = None

    def recv(req):
        nonlocal val
        task = req.path.action_path[0]
        if task == "NULL":
            return None

        val = req.content["val"]
        return val + 1 if task == "ADD" else val - 1

    server = SocketServer(recv)
    run_server(server)  # start server in another thread

    with SocketClient("localhost", server.port) as client:

        assert val is None

        res = client.send_message(Path("TEST", "layer", "ADD"), {
            "val": 42
        }).content

        assert val == 42
        assert res == 43

        res = client.send_message(Path("TEST", "layer", "SUB"), {
            "val": 22
        }).content

        assert val == 22
        assert res == 21

        # This test is to ensure that null values can be transferred nicely
        res = client.send_message(Path("TEST", "layer", "NULL"), None).content

        assert val is 22
        assert res is None
Example #6
0
def test_sockets_error():
    def server_error(req):
        print(req.content)
        raise Exception("There was a problem")

    server = SocketServer(server_error, format="json")
    run_server(server)  # start server in another thread

    client = SocketClient("localhost", server.port)
    client.connect()
    with pytest.raises(ServerException):
        client.send_message(Path("TEST", "layer", "test"), {"example": "data"},
                            "json")
    client.close()
Example #7
0
    def start(self):
        self.server.serve_async()

        with SocketClient(self.vertex_host, self.vertex_port) as client:
            msg = {
                "name": self.name,
                "server_port": self.server.port,
                "resources": self.resources
            }
            res = client.send_message(
                Path(self.vertex_host, "Vertex", "Connect"), msg)

            self.config = Config(res.content["config"])
            self.node_config = self.config.select("node")
            self.pending_leases = list(
                map(lambda l: Lease(**l), res.content["leases"]))

        self.state = NodeState.CONNECTED
        self.clean()
Example #8
0
def test_sockets_load():
    val = None

    def recv(data):
        nonlocal val
        val = data.content
        return bytes(reversed(data.content))

    data = os.urandom(256000)  # send ~256KB of random data
    reverse = bytes(reversed(data))

    server = SocketServer(recv, format="bytes")
    run_server(server)  # start server in another thread

    with SocketClient("localhost", server.port) as client:
        assert val is None

        res = client.send_message(Path("TEST", "layer", "test"), data,
                                  "bytes").content

        assert val == data
        assert res == reverse