protocols / FetchDataProtocol

# FetchDataProtocol

`abstract class FetchDataProtocol<T : NamedByHash, W : Any> : ProtocolLogic<Result<T>>`

An abstract protocol for fetching typed data from a remote peer.

Given a set of hashes (IDs), either loads them from local disk or asks the remote peer to provide them.

A malicious response in which the data provided by the remote peer does not hash to the requested hash results in DownloadedVsRequestedDataMismatch being thrown. If the remote peer doesnt have an entry, it results in a HashNotFound exception being thrown.

By default this class does not insert data into any local database, if you want to do that after missing items were fetched then override maybeWriteToDisk. You <emph>must</emph> override load and queryTopic. If the wire type is not the same as the ultimate type, you must also override convert.

### Parameters

`T` - The ultimate type of the data being fetched.
`W` - The wire type of the data being fetched, for when it isnt the same as the ultimate type.

### Types

Result | `data class Result<T : NamedByHash>`

### Exceptions

BadAnswer | `open class BadAnswer : Exception`
DownloadedVsRequestedDataMismatch | `class DownloadedVsRequestedDataMismatch : BadAnswer`
HashNotFound | `class HashNotFound : BadAnswer`

### Constructors

<init> | `FetchDataProtocol(requests: Set<SecureHash>, otherSide: SingleMessageRecipient)` An abstract protocol for fetching typed data from a remote peer.

### Properties

otherSide | `val otherSide: SingleMessageRecipient`
queryTopic | `abstract val queryTopic: String`
requests | `val requests: Set<SecureHash>`

### Inherited Properties

logger | `val logger: <ERROR CLASS>` This is where you should log things to.
progressTracker | `open val progressTracker: ProgressTracker?` Override this to provide a ProgressTracker. If one is provided and stepped, the framework will do something helpful with the progress reports. If this protocol is invoked as a sub-protocol of another, then the tracker will be made a child of the current step in the parent. If its null, this protocol doesnt track progress.
psm | `lateinit var psm: ProtocolStateMachine<*>` Reference to the Fiber instance that is the top level controller for the entire flow.
serviceHub | `val serviceHub: ServiceHub` Provides access to big, heavy classes that may be reconstructed from time to time, e.g. across restarts

### Functions

call | `open fun call(): Result<T>` This is where you fill out your business logic.
convert | `open fun convert(wire: W): T`
load | `abstract fun load(txid: SecureHash): T?`
maybeWriteToDisk | `open fun maybeWriteToDisk(downloaded: List<T>): Unit`

### Inherited Functions

receive | `fun <T : Any> receive(topic: String, sessionIDForReceive: Long): UntrustworthyData<T>` `fun <T : Any> receive(topic: String, sessionIDForReceive: Long, clazz: Class<T>): UntrustworthyData<T>`
send | `fun send(topic: String, destination: MessageRecipients, sessionID: Long, obj: Any): Unit`
sendAndReceive | `fun <T : Any> sendAndReceive(topic: String, destination: MessageRecipients, sessionIDForSend: Long, sessionIDForReceive: Long, obj: Any): UntrustworthyData<T>`
subProtocol | `fun <R> subProtocol(subLogic: ProtocolLogic<R>): R` Invokes the given subprotocol by simply passing through this ProtocolLogics reference to the ProtocolStateMachine and then calling the call method.

### Inheritors

FetchAttachmentsProtocol | `class FetchAttachmentsProtocol : FetchDataProtocol<Attachment, ByteArray>` Given a set of hashes either loads from from local storage or requests them from the other peer. Downloaded attachments are saved to local storage automatically.
FetchTransactionsProtocol | `class FetchTransactionsProtocol : FetchDataProtocol<SignedTransaction, SignedTransaction>` Given a set of tx hashes (IDs), either loads them from local disk or asks the remote peer to provide them.