Search in sources :

Example 1 with Operations

use of com.gianlu.pyxreborn.Operations in project PretendYoureXyzzyReborn by devgianlu.

the class ConsoleClient method mainMenu.

private void mainMenu() throws IOException {
    ListChoicePrompt.Builder builder = new ListChoicePrompt.Builder();
    builder.text("Request operation:").name("req");
    for (Operations op : Operations.values()) builder.newItem().name(op.toString()).text(op.name()).add();
    ChoiceAnswer answer = prompt.prompt(builder.build());
    Operations op = Operations.parse(answer.getName());
    JsonObject req = client.createRequest(op);
    Pair<String, String> additionalParams;
    do {
        additionalParams = askForRequestParam();
        if (additionalParams != null)
            req.addProperty(additionalParams.getKey(), additionalParams.getValue());
    } while (additionalParams != null);
    System.out.println("REQUEST: " + req);
    JsonObject resp;
    try {
        resp = client.sendMessageBlocking(req);
    } catch (InterruptedException | PyxException ex) {
        Logger.severe(ex);
        mainMenu();
        return;
    }
    System.out.println("RESPONSE: " + resp);
    mainMenu();
}
Also used : ListChoicePrompt(com.gianlu.consoleui.Choice.List.ListChoicePrompt) ChoiceAnswer(com.gianlu.consoleui.Choice.ChoiceAnswer) JsonObject(com.google.gson.JsonObject) PyxException(com.gianlu.pyxreborn.Exceptions.PyxException) Operations(com.gianlu.pyxreborn.Operations)

Example 2 with Operations

use of com.gianlu.pyxreborn.Operations in project PretendYoureXyzzyReborn by devgianlu.

the class Server method onMessage.

/**
 * Picks the right {@link BaseHandler} to perform the requested {@link Operations}.
 */
@Override
@Nullable
protected JsonObject onMessage(WebSocket conn, User user, JsonObject request, JsonObject response) throws GeneralException {
    Operations op = Operations.parse(request.get(Fields.OPERATION.toString()).getAsString());
    if (op == null)
        throw new GeneralException(ErrorCodes.UNKNOWN_OPERATION);
    BaseHandler handler;
    try {
        handler = Handlers.LIST.get(op).newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new GeneralException(ErrorCodes.SERVER_ERROR, ex);
    }
    return handler.handleRequest(this, request, response);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) BaseHandler(com.gianlu.pyxreborn.server.Handlers.BaseHandler) Operations(com.gianlu.pyxreborn.Operations) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Operations (com.gianlu.pyxreborn.Operations)2 ChoiceAnswer (com.gianlu.consoleui.Choice.ChoiceAnswer)1 ListChoicePrompt (com.gianlu.consoleui.Choice.List.ListChoicePrompt)1 GeneralException (com.gianlu.pyxreborn.Exceptions.GeneralException)1 PyxException (com.gianlu.pyxreborn.Exceptions.PyxException)1 BaseHandler (com.gianlu.pyxreborn.server.Handlers.BaseHandler)1 JsonObject (com.google.gson.JsonObject)1 Nullable (org.jetbrains.annotations.Nullable)1