package com.mongodb.internal.connection;
import com.mongodb.MongoInternalException;
import com.mongodb.ServerAddress;
import com.mongodb.async.SingleResultCallback;
abstract class ResponseCallback implements SingleResultCallback<ResponseBuffers> {
private volatile boolean closed;
private final ServerAddress serverAddress;
private final long requestId;
ResponseCallback(final long requestId, final ServerAddress serverAddress) {
this.serverAddress = serverAddress;
this.requestId = requestId;
}
protected ServerAddress getServerAddress() {
return serverAddress;
}
protected long getRequestId() {
return requestId;
}
@Override
public void onResult(final ResponseBuffers responseBuffers, final Throwable t) {
if (closed) {
throw new MongoInternalException("Callback should not be invoked more than once");
}
closed = true;
if (responseBuffers != null) {
callCallback(responseBuffers, t);
} else {
callCallback(null, t);
}
}
protected abstract void callCallback(ResponseBuffers responseBuffers, Throwable t);
}