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 org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.cluster.client.DoipClusterClient;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.metadata.SearchParameter;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.doip.endpoint.client.DoipMessageCallback;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 一个Router中可能存了很多BDO的信息,ClusterDoaClient负责维护,BDO与ClusterDoaClient之间,一对一的映射关系,避免反复解析
@Deprecated
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) {
        if (!bdoClients.containsKey(bdoid)) {
            DoipClusterClient doipClusterClient = new DoipClusterClient(bdoid, routerConfig);
            bdoClients.put(bdoid, doipClusterClient);
        }
        return bdoClients.get(bdoid);
    }

//    public DoipClusterClient createOrGetClusterClientLocalTest(String bdoid) {
//        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 = this.doipOperationToJoinInfo.get(op.getName());
//                    sendMsgAndJoin(doipMessage, joinInfo, BDRepoIDs, cb);
//                }
//
//                public void sendMsgAndJoin(DoipMessage msg, JoinInfo joinInfo, String[] BDRepoIDs, DoipMessageCallback cb) {
//                    int serversNum = BDRepoIDs.length;
//                    int count = 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());
//                }
//
//                int getJoinCount(JoinInfo joinInfo, int serversNum, JsonObject joinParams) {
//                    try {
//                        if (joinInfo == null) return serversNum;
//                        if (joinInfo.joinCountFuncName != null) {
//                            JsonElement requester = routerConfig.publicKey == null ? JsonNull.INSTANCE : new JsonPrimitive(routerConfig.publicKey);
//                            return engineUtil.invokeFunction(joinInfo.joinCountFuncName, Integer.class, joinParams, requester);
//                        }
//                        if (joinInfo.joinCount != 0) return joinInfo.joinCount;
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    return serversNum;
//                }
//            };
//            bdoClients.put(bdoid, doipClusterClient);
//        }
//        return bdoClients.get(bdoid);
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy