DEV Community

himansa
himansa

Posted on

DevLog 2 :Finishing Network Layer of the GUI framework.

this is a second DevLog of DevLogs of building a GUI Framework for java.

Today I started Implementing the framework after seeing Prototype seems to be success. and finished the Network Layer of it.

it is a abstraction around Rest and Web sockets that enable framework to jump between both communication protocols in the fly. simply, when building on top of it, we have one Logical Connection that can be either rest or web socket or both. and connection and disconnect events are also built around that logical connection, meaning they fire only if a new user connects or user closes last remaining Connection to server.

and the Network Layer Built to be swappable, so the framework implementation that build on top of this can be used without changing over any type of Network Layer. Currently I created Jetty Based Network Implementation and Mock Implementation for later testing needs.

for a Example, current Api of Network Layer looks like this.

NetworkServer server = new JettyNetworkServer(8080);

server.onConnect(client -> {
    System.out.println("Connected: " + client.id());
    System.out.println("Type: " + client.currentType());
});

server.onDisconnect(client -> {
    System.out.println("Disconnected: " + client.id());
});

server.onRequest((client, request) -> {
    if ("ping".equals(request.getCode())) {
        return DataResponse.ok()
                .command("message", "pong");
    }

    if ("echo".equals(request.getCode())) {
        DataResponse response = DataResponse.ok();

        if (request.getData() != null) {
            request.getData().forEach(response::command);
        }

        return response;
    }

    return DataResponse.error(404, "unknown code");
});

server.start();
Enter fullscreen mode Exit fullscreen mode

this mostly won't be exposed from Framework Api. but since this is isolated and independent from the framework, this can be used separately for any other project that need to keep one logical user connection no matter client changes their communication protocol in mid session.

Top comments (0)