Development Guide

Module Worker

Module workers are started when PETEP core starts and stopped when PETEP core stops.

Module Worker class

com.warxim.petep.module.ModuleWorker
/**
 * Base class for module instances (workers), which are used in running PETEP core.
 * <p>Module worker represents running (working) module.</p>
 * @param <M> Type of the module
 */
@PetepAPI
public abstract class ModuleWorker<M extends Module<?>> {
    /**
     * PETEP helper for current running PETEP core instance.
     */
    protected final PetepHelper helper;

    /**
     * Parent module.
     */
    protected final M module;

    /**
     * Constructs module worker.
     * @param module Parent module of the worker
     * @param helper Helper for accessing running instance of PETEP core
     */
    protected ModuleWorker(M module, PetepHelper helper) {
        this.helper = helper;
        this.module = module;
    }

    /**
     * Obtains parent module.
     * @return Parent module
     */
    public final M getModule() {
        return module;
    }

    /**
     * Obtains PetepHelper of current PETEP core instance.
     * @return Helper for working with PETEP core
     */
    public final PetepHelper getHelper() {
        return helper;
    }

    /**
     * Obtains code of the module (represented by worker)
     * @return Module code
     */
    public String getCode() {
        return module.getCode();
    }

    /**
     * Obtains name of the module (represented by worker)
     * @return Module name
     */
    public String getName() {
        return module.getName();
    }

    /**
     * Obtains description of the module (represented by worker)
     * @return Module description
     */
    public String getDescription() {
        return module.getDescription();
    }

    @Override
    public String toString() {
        return module.toString();
    }
}

Proxy class

Proxy listens for incomming connections, provides connection manager.

Methods prepare and start can return "false" and by that abort start of PETEP core.

Method stop should be used to free resources - stop threads, close connections and connection manager etc.

Start and stop methods should be used to start(stop) new threads inside proxy and they have to be nonblocking!

com.warxim.petep.proxy.worker.Proxy
/**
 * Proxy base class.
 */
@PetepAPI
public abstract class Proxy extends ModuleWorker<ProxyModule> {
    /**
     * Constructs proxy
     * @param module Parent module of the worker
     * @param helper Helper for accessing running instance of PETEP core
     */
    protected Proxy(ProxyModule module, PetepHelper helper) {
        super(module, helper);
    }

    /**
     * Prepares proxy to start.
     * @return  {@code true} if the proxy has been successfully prepared;<br>
     *          {@code false} if the proxy has failed preparation (this will abort start of PETEP core)
     */
    public abstract boolean prepare();

    /**
     * Starts proxy.
     * @return  {@code true} if the proxy has been successfully started;<br>
     *          {@code false} if the proxy has failed to start (this will abort start of PETEP core)
     */
    public abstract boolean start();

    /**
     * Stops proxy.
     */
    public abstract void stop();

    /**
     * Checks whether the proxy supports the specified pdu.
     * @param pdu PDU to be checked
     * @return {@code true} if the PDU is supported by this proxy
     */
    public abstract boolean supports(PDU pdu);

    /**
     * Obtains proxy connection manager.
     * @return Current connection manager of this proxy
     */
    public abstract ConnectionManager getConnectionManager();
}

Interceptor class

Method prepare can return "false" and by that abort start of PETEP core.

Method stop should be used to free resources.

Interceptors are wrapped inside threads automatically and you do not have to create any threads. Intercept method is called automatically for each PDU that goes through the interceptor.

com.warxim.petep.interceptor.worker.Interceptor
/**
 * Interceptor base class.
 * <p>Interceptors are made for intercepting PDUs during their processing in PETEP.</p>
 * <p>Interceptors can change data, modify metadata, drop PDUs and so on.</p>
 */
@Getter
@PetepAPI
public abstract class Interceptor extends ModuleWorker<InterceptorModule> {
    /**
     * ID of interceptor (interceptor index in interceptors list).
     */
    protected final int id;

    /**
     * Constructs interceptor.
     * @param id Identifier of interceptor (index of the interceptor)
     * @param module Parent module of the interceptor
     * @param helper Helper for accessing running instance of PETEP core
     */
    protected Interceptor(int id, InterceptorModule module, PetepHelper helper) {
        super(module, helper);
        this.id = id;
    }

    /**
     * Prepares instance for intercepting.
     * @return  {@code true} if the interceptor has been successfully prepared;<br>
     *          {@code false} if the interceptor has failed preparation (this will abort start of PETEP core)
     */
    public abstract boolean prepare();

    /**
     * Intercepts PDUs.
     * @param pdu PDU to be intercepted
     * @return  {@code true} if the PDU should be send to next interceptor;<br>
     *          {@code false} if the PDU should be dropped
     */
    public abstract boolean intercept(PDU pdu);

    /**
     * Stops intercepting.
     */
    public abstract void stop();
}