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

org.bdware.doip.sbyterepo.ByteRepoHandler Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.sbyterepo;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.RocksDBUtil;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.endpoint.server.DoipRequestHandler;
import org.bdware.doip.endpoint.server.RepositoryHandler;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.irp.stateinfo.StateInfoBase;

import java.nio.charset.StandardCharsets;

public class ByteRepoHandler implements DoipRequestHandler, RepositoryHandler {
    private static final Logger LOGGER = LogManager.getLogger(ByteRepoHandler.class);
    AuditIrpClient irpClient;
    public String repoId;
    AutoSplitByteArrayStorage storage;

    public ByteRepoHandler(AuditIrpClient irpClient, String repoId) {
        this.irpClient = irpClient;
        this.repoId = repoId;
        RocksDBUtil doTable = RocksDBUtil.loadDB("./bytesDOTable", false);
        storage = new AutoSplitByteArrayStorage(doTable);
    }

    @Override
    public DoipMessage onRequest(ChannelHandlerContext ctx, DoipMessage msg) {
        DoipMessage result = handle(msg);
        return result;
    }

    private DoipMessage handle(DoipMessage msg) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        DoipMessage ret = builder.createResponse(DoipResponseCode.Invalid, msg).create();
        ret.body.encodedData = "Failed!!!".getBytes(StandardCharsets.UTF_8);
        return ret;
    }

    @Override
    public DoipMessage handleHello(DoipMessage doipMessage) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        try {
            builder.createResponse(DoipResponseCode.Success, doipMessage);
            long size = storage.size(doipMessage.header.parameters.id);
            builder.addAttributes("size", size + "");
            builder.setBody(("Hello, size:" + size).getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            builder.createResponse(DoipResponseCode.Invalid, doipMessage);
            builder.setBody(("exception " + e.getMessage()).getBytes(StandardCharsets.UTF_8));
            e.printStackTrace();
        }
        return builder.create();
    }


    @Override
    public DoipMessage handleListOps(DoipMessage doipMessage) {
        return onRequest(null, doipMessage);
    }

    @Override
    public DoipMessage handleCreate(DoipMessage doipMessage) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        try {
            byte[] data = doipMessage.body.encodedData;
            String doid = registerDOID();
            storage.updateDOBytes(doid, 0, data.length, data);
            DoipMessage ret = builder.createResponse(DoipResponseCode.Success, doipMessage).create();
            ret.header.parameters.id = doid;
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            DoipMessage ret = builder.createResponse(DoipResponseCode.Invalid, doipMessage).create();
            ret.body.encodedData = e.getMessage().getBytes(StandardCharsets.UTF_8);
            return ret;
        }
    }

    public String registerDOID() {
        //检查前缀是不是repo自身id,后缀是要获取的对应do
        StateInfoBase base = new StateInfoBase();
        base.handleValues = new JsonObject();
        base.handleValues.addProperty("repoId", repoId);
        base.handleValues.addProperty("createTime", System.currentTimeMillis());
        String ret = null;
        try {
            ret = irpClient.register(base);
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public DoipMessage handleUpdate(DoipMessage doipMessage) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        try {
            String doid = doipMessage.header.parameters.id;
            long offset = doipMessage.header.parameters.attributes.get("offset").getAsLong();
            if (offset < 0)
                throw new IllegalArgumentException("offset should >=0");
            int count = doipMessage.header.parameters.attributes.get("count").getAsInt();
            if (count <= 0)
                throw new IllegalArgumentException("count should >0");
            if (doipMessage.body.encodedData.length < count)
                count = doipMessage.body.encodedData.length;
            if (storage.has(doid)) {
                storage.updateDOBytes(doid, offset, count, doipMessage.body.encodedData);
            } else throw new IllegalArgumentException("no such do");
            builder.createResponse(DoipResponseCode.Success, doipMessage);
            builder.addAttributes("updatedSize", count);
            DoipMessage ret = builder.create();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            DoipMessage ret = builder.createResponse(DoipResponseCode.Invalid, doipMessage).create();
            ret.body.encodedData = e.getMessage().getBytes(StandardCharsets.UTF_8);
            return ret;
        }
    }

    @Override
    public DoipMessage handleDelete(DoipMessage doipMessage) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        try {
            String doid = doipMessage.header.parameters.id;
            irpClient.unRegister(doid);
            storage.delete(doid);
            builder.createResponse(DoipResponseCode.Success, doipMessage);
            builder.setBody("success".getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
            builder.createResponse(DoipResponseCode.Declined, doipMessage);
            builder.setBody(e.getMessage().getBytes(StandardCharsets.UTF_8));
        }
        return builder.create();
    }

    private JsonElement getPropWithDefault(JsonObject attr, String headers, JsonElement jsonObject) {
        try {
            return attr.get(headers);
        } catch (Exception e) {
            return jsonObject;
        }
    }

    @Override
    public DoipMessage handleRetrieve(DoipMessage doipMessage) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        try {
            long offset = getPropWithDefault(doipMessage.header.parameters.attributes,
                    "offset", new JsonPrimitive(0L)).getAsLong();
            if (offset < 0)
                throw new IllegalArgumentException("offset should >=0");
            int count = getPropWithDefault(doipMessage.header.parameters.attributes,
                    "count", new JsonPrimitive(storage.size(doipMessage.header.parameters.id))).getAsInt();
            if (count <= 0)
                throw new IllegalArgumentException("count should >0");
            if (count > 1024L * 1024L)
                throw new IllegalArgumentException("count should <=1024*1024");
            byte[] data = storage.readDOBytes(doipMessage.header.parameters.id, offset, count);
            builder.createResponse(DoipResponseCode.Success, doipMessage);
            builder.setBody(data);
            return builder.create();
        } catch (Exception e) {
            e.printStackTrace();
            builder.createResponse(DoipResponseCode.Declined, doipMessage);
            builder.setBody(e.getMessage().getBytes(StandardCharsets.UTF_8));
            return builder.create();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy