org.bdware.doip.event.verified.VerifiedSubscriber 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.event.verified;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.doip.RocksDBUtil;
import org.bdware.doip.audit.AuditDoaClient;
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.event.Subscriber;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;
public abstract class VerifiedSubscriber implements Subscriber, Commiter {
private static final Gson GSON = new Gson();
private Map topicIdToQueue;
// key 是topicId:pub, value是[hash1,hash2,...]
// key 是hash, value是DoipMessageContent
private final RocksDBUtil storageManager;
;
AuditDoaClient client;
private String subscriberId;
public VerifiedSubscriber(RocksDBUtil storageManager, AuditDoaClient client, String subscriberId) {
this.storageManager = storageManager;
this.client = client;
this.subscriberId = subscriberId;
}
synchronized private void savePublishedEvents(String topicId, int order, String hash, String content) {
VerifiedEventQueue queue = getOrCreateQueue(topicId);
queue.appendEvent(order, hash, content);
}
private VerifiedEventQueue getOrCreateQueue(String topicId) {
if (!topicIdToQueue.containsKey(topicId)) {
topicIdToQueue.put(topicId, new VerifiedEventQueue());
}
return topicIdToQueue.get(topicId);
}
private JsonElement getAsJson(String key, JsonElement defaultValue) {
String content = storageManager.get(key);
try {
if (null != content && !content.isEmpty()) {
return JsonParser.parseString(content);
}
} catch (Exception e) {
e.printStackTrace();
}
return defaultValue;
}
@Override
public DoipMessage receiveData(String topicId, String publishId, DoipMessage request) {
throw new IllegalStateException("unsupported data message, missing hash value");
}
@Override
public DoipMessage receiveHash(String topicId, String subscriberId, DoipMessage data) {
throw new IllegalStateException("unsupported message type");
}
@Override
public DoipMessage receiveDataAndHash(String topicId, String publisherId, DoipMessage request) {
try {
int order = request.header.parameters.attributes.get("order").getAsInt();
String hash = request.header.parameters.attributes.get("hash").getAsString();
String content = request.body.getDataAsJsonString();
savePublishedEvents(topicId, order, hash, content);
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.Success, request).setBody(request.body.getEncodedData());
return builder.create();
} catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.UnKnownError, request);
builder.setBody(bo.toByteArray());
return builder.create();
}
}
@Override
public void receiveMerkelConfiguration(String topicId, JsonObject configuration, DoipMessage request) {
//unsupported message type
return;
}
@Override
public synchronized void commit(String topicId, String publishId, DoipMessage commitedMessage) {
onReceiveData(topicId, publishId, commitedMessage);
}
public String getId() {
return subscriberId;
}
}