All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.client.SmartContractHandler Maven / Gradle / Ivy

The newest version!
package org.bdware.client;

import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.client.ws.Action;
import org.bdware.client.ws.Handler;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public abstract class SmartContractHandler implements Handler {
    private static final Logger LOGGER = LogManager.getLogger(SmartContractHandler.class);

    private final Map methodMap;

    public SmartContractHandler() {
        methodMap = new HashMap<>();
        for (Class clz = this.getClass();
             clz != null && clz != Object.class;
             clz = clz.getSuperclass()) {
            for (Method m : clz.getDeclaredMethods()) {
                String actionName = getActionName(clz, m);
                if (actionName != null && !methodMap.containsKey(actionName)) {
                    m.setAccessible(true);
                    LOGGER.debug(
                            String.format(
                                    "addAction: %s %s.%s",
                                    actionName, clz.getCanonicalName(), m.getName()));
                    methodMap.put(actionName, m);
                }
            }
        }
    }

    private String getActionName(Class clz, Method m) {
        if (m.getParameterCount() != 1) {
            return null;
        }
        if (m.getParameterTypes()[0] != JsonObject.class) {
            return null;
        }
        Action action = null;
        for (Method m2; clz != Object.class && action == null; clz = clz.getSuperclass()) {
            try {
                m2 = clz.getDeclaredMethod(m.getName(), JsonObject.class);
                action = m2.getAnnotation(Action.class);
            } catch (Exception e) {
                // e.printStackTrace();
            }
        }
        if (action != null) {
            if (action.name().length() > 0) return action.name();
            else return m.getName();
        }
        return null;
    }

    @Action
    public abstract void onListContractProcess(JsonObject obj);

    @Action
    public abstract void onOutputStream(JsonObject obj);

    @Action
    public abstract void onStartContract(JsonObject obj);

    @Action
    public abstract void onKillContractProcess(JsonObject obj);

    @Action
    public abstract void onHashResult(JsonObject obj);

    @Action
    public abstract void onKillAllContract(JsonObject obj);

    @Action
    public abstract void onExecuteResult(JsonObject obj);

    @Action
    public abstract void onUndefined(JsonObject obj);

    @Action
    public abstract void onSessionID(JsonObject obj);

    @Action
    public abstract void onLogin(JsonObject obj);

    @Action
    public abstract void onLoadNodeConfig(JsonObject obj);

    @Override
    public void handle(JsonObject obj) {
        try {
            LOGGER.info("receive:" + obj);
            if (!obj.has("action")) {
                LOGGER.warn("can't handle message without action: " + obj);
            }
            String methodName = obj.get("action").getAsString();
            Method method = methodMap.get(methodName);
            if (method != null) {
                method.invoke(this, obj);
            } else {
                LOGGER.warn("can't handle action: " + methodName + " -> " + obj);
            }
        } catch (Exception e) {
            onUndefined(obj);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy