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

org.bdware.doip.cluster.ClusterDoaClient Maven / Gradle / Ivy

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

import com.google.gson.JsonObject;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.cluster.client.DoipClusterClient;
import org.bdware.doip.codec.JsonDoipMessage;
import org.bdware.doip.codec.digitalObject.DigitalObject;
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.codec.metadata.SearchParameter;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.doip.endpoint.client.DoipMessageCallback;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.sc.bean.JoinInfo;

import javax.script.ScriptException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 一个Router中可能存了很多BDO的信息,ClusterDoaClient负责维护,BDO与ClusterDoaClient之间,一对一的映射关系,避免反复解析
public class ClusterDoaClient extends DoipClientImpl {
    private final String clientDoid;
    AuditIrpClient irsClient;
    // 维护具体的DOID和ClusterClient之间的映射关系
    Map bdoClients;
    Map repoIDToUrl;
    EndpointConfig routerConfig;

    public ClusterDoaClient(String clientDoid, EndpointConfig config) {
        irsClient = new AuditIrpClient(config);
        bdoClients = new ConcurrentHashMap<>();
        repoIDToUrl = new ConcurrentHashMap<>();
        routerConfig = config;
        this.clientDoid = clientDoid;
    }

    @Override
    public void hello(String id, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(id);
        doipClusterClient.hello(id, cb);
    }

    @Override
    public void listOperations(String id, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(id);
        doipClusterClient.listOperations(id, cb);
    }

    @Override
    public void retrieve(String id, String element, boolean includeElementData, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(id);
        doipClusterClient.retrieve(id, element, includeElementData, cb);
    }

    @Override
    public void create(String targetDOIPServiceID, DigitalObject digitalObject, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(targetDOIPServiceID);
        doipClusterClient.create(targetDOIPServiceID, digitalObject, cb);
    }

    @Override
    public void update(DigitalObject digitalObject, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(digitalObject.id);
        doipClusterClient.update(digitalObject, cb);
    }

    @Override
    public void delete(String id, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(id);
        doipClusterClient.delete(id, cb);
    }

    @Override
    public void search(String id, SearchParameter sp, DoipMessageCallback cb) {
        DoipClusterClient doipClusterClient = createOrGetClusterClient(id);
        doipClusterClient.search(id, sp, cb);
    }

    public synchronized DoipClusterClient createOrGetClusterClient(String bdoid) {
        try {
            if (!bdoClients.containsKey(bdoid)) {
                DoipClusterClient doipClusterClient = new DoipClusterClient(routerConfig);
                bdoClients.put(bdoid, doipClusterClient);
            }
            return bdoClients.get(bdoid);
        } catch (IrpClientException | ScriptException e) {
            e.printStackTrace();
        }

        return null;
    }

    public DoipClusterClient createOrGetClusterClientLocalTest(String bdoid) {
        try {
            if (!bdoClients.containsKey(bdoid)) {
                DoipClusterClient doipClusterClient = new DoipClusterClient(routerConfig) {
                    @Override
                    public void sendMessage(DoipMessage doipMessage, DoipMessageCallback cb) {

                        BasicOperations op = BasicOperations.getDoOp(doipMessage.header.parameters.operation);
                        JsonDoipMessage message = JsonDoipMessage.fromDoipMessage(doipMessage);
                        String[] BDRepoIDs = route(message);
                        // joinParams都来自于callback,joinParams就是所有节点callback到达之后,merge之后的结果
                        JoinInfo joinInfo = getJoinInfo(op);
                        sendMsgAndJoin(doipMessage, joinInfo, BDRepoIDs, cb);

                    }

                    @Override
                    public void sendMsgAndJoin(DoipMessage msg, JoinInfo joinInfo, String[] BDRepoIDs, DoipMessageCallback cb) {
                        int serversNum = BDRepoIDs.length;
                        int count = this.getJoinCount(joinInfo, serversNum, new JsonObject());
                        // 如果joinCountFunc执行算出的joinCount,比所有的servers还要多,就更新为servers的数量
                        if (serversNum < count) {
                            count = serversNum;
                        }
                        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
                        builder.createResponse(DoipResponseCode.Success, msg);
                        builder.setBody("success".getBytes());
                        cb.onResult(builder.create());
                    }

                };
                bdoClients.put(bdoid, doipClusterClient);
            }
            return bdoClients.get(bdoid);
        } catch (IrpClientException | ScriptException e) {
            e.printStackTrace();
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy