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

org.bdware.doip.cluster.client.DelegateDoipMessageCallback Maven / Gradle / Ivy

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.doip.endpoint.client.DoipMessageCallback;

import java.net.URI;

public class DelegateDoipMessageCallback implements DoipMessageCallback {
    static Logger LOGGER = LogManager.getLogger(DelegateDoipMessageCallback.class);
    private final DoipMessageCallback originalCallback;
    DoipMessage originalMessage;
    AuditDoipClient previousClient;
    DoipClusterClient doipClusterClient;

    public DelegateDoipMessageCallback(AuditDoipClient previousClient, DoipClusterClient doipClusterClient, DoipMessageCallback doipMessageCallback, DoipMessage originalMessage) {
        this.previousClient = previousClient;
        this.originalCallback = doipMessageCallback;
        this.originalMessage = originalMessage;
        this.doipClusterClient = doipClusterClient;
    }

    @Override
    public void onResult(DoipMessage msg) {
        if (msg.header.parameters.response == DoipResponseCode.DelegateRequired) {

            try {
                if (msg.header.parameters.attributes.has("port")) {
                    int port = msg.header.parameters.attributes.get("port").getAsInt();
                    URI uri = new URI(previousClient.getRepoUrl());
                    String host = uri.getHost();
                    if (msg.header.parameters.attributes.has("host"))
                        host = msg.header.parameters.attributes.get("host").getAsString();
                    String newAddress = String.format("%s://%s:%d", uri.getScheme(), host, port);
                    String version = "2.1";
                    if (msg.header.parameters.attributes.has("version"))
                        version = msg.header.parameters.attributes.get("version").getAsString();
                    LOGGER.info("recv delegate:"+newAddress +" previousRepo:"+previousClient.getRepoUrl());
                    // BDRepo doipMsgCallback -> BDOClient,维护repoToBDO的映射关系
                    AuditDoipClient client = doipClusterClient.getFastClientByUrl(previousClient.getRepoUrl(),newAddress, version);
                    previousClient = client;
                    client.sendRawMessage(originalMessage, this);
                }
            } catch (Exception e) {
                e.printStackTrace();
                originalCallback.onResult(msg);
            }
        } else originalCallback.onResult(msg);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy