org.bdware.doip.cluster.callback.BDODelegateDoipMessageCallback Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
The newest version!
package org.bdware.doip.cluster.callback;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.cluster.entity.BDOEntity;
import org.bdware.doip.cluster.util.DoipClientCacheUtil;
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.client.DoipMessageCallback;
import java.net.URI;
public class BDODelegateDoipMessageCallback implements DoipMessageCallback {
static Logger LOGGER = LogManager.getLogger(BDODelegateDoipMessageCallback.class);
private final DoipMessageCallback originalCallback;
private final DoipClientCacheUtil connectionCache;
DoipMessage originalMessage;
AuditDoipClient previousClient;
public BDODelegateDoipMessageCallback(AuditDoipClient previousClient, DoipClientCacheUtil connectionCache, DoipMessageCallback doipMessageCallback, DoipMessage originalMessage) {
this.previousClient = previousClient;
this.originalCallback = doipMessageCallback;
this.originalMessage = originalMessage;
this.connectionCache = connectionCache;
}
@Override
public void onResult(DoipMessage msg) {
logDoipMsgInfo(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的映射关系
connectionCache.getClientByUrl(new BDOEntity(newAddress, version, connectionCache), previousClient.getAuditConfig(),
(client) -> {
previousClient = client;
client.sendMessage(originalMessage, this);
});
// AuditDoipClient client =
//
// DoipClientCacheUtil.getFastClientByUrl(previousClient, newAddress, version);
//
// client.sendRawMessage(originalMessage, this);
}
} catch (Exception e) {
e.printStackTrace();
originalCallback.onResult(msg);
}
} else {
originalCallback.onResult(msg);
}
}
public void logDoipMsgInfo(DoipMessage msg) {
try {
if (!DoipMessageFactory.isLocalTimeoutMessage(msg)) {
connectionCache.popReconnectRequest(previousClient);
} else {
// LOGGER.error("收到错误的节点回复 : " + "Something wrong happens in" + previousClient.getRepoUrl() + " msg:" + new Gson().toJson(msg.header.parameters));
connectionCache.pushReconnectRequest(previousClient);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}