private void RecordToFile(CdmRequest r, string s) { if (!RecordToFileOn) { return; } string filepath; if (r.NodeType == NodeType.Addr) { // Addr can be paged filepath = RecordToFileFolderAddr + AdaptorHelpers.GetFilenameForAddressRequest(r); } else { // Tx are never paged, we get all of it in one go from blockchain.info filepath = RecordToFileFolderTx + r.NodeId + @".txt"; } using (StreamWriter wr = new StreamWriter(filepath)) { wr.WriteLine(s); } }
/// <summary> /// Given a node id, node type, paging information (edges from and to), a payload method (used to do the actual work) and some /// callbacks, this method reads Resource files, then processes the results with the payload. /// </summary> /// <param name="r">Request for a graph fragment, details nodeid, node type and edge counts from and to (as counted by nodeid)</param> /// <param name="payload">This method will be executed for the request r against the Resources file</param> /// <param name="callbackOnSuccess">Called with string filled with nodeId</param> /// <param name="callbackOnFail">Called with string filled with nodeId</param> /// <returns></returns> private void ProcessRequestUsingFiles(CdmRequest r, Func <CdmRequest, JSONNode, bool> payload, Action <string> callbackOnSuccess, Action <string> callbackOnFail) { // on the fly loading of addr and tx data from files if (r.NodeType == NodeType.Tx) { if (_offlineTxs == null || _offlineTxs.Count == 0) { LoadOfflineTxDataFromResources(); } } else if (r.NodeType == NodeType.Addr) { if (_offlineAddresses == null || _offlineAddresses.Count == 0) { LoadOfflineAddressDataFromResources(); } } else { Msg.LogWarning("AdaptorBtcOfflineFiles.ProcessRequestUsingFiles received unknown Node Type"); callbackOnFail(r.NodeId); return; } // does tx or addr exist in the files we loaded? string s = null; string addressKey = null; if (r.NodeType == NodeType.Tx) { if (_offlineTxs.ContainsKey(r.NodeId)) { s = _offlineTxs[r.NodeId]; } else { Msg.Log("AdaptorBtcOfflineFiles.ProcessRequestUsingFiles: Tx " + r.NodeId + " does not exist in offline store"); callbackOnFail("Data not available in offline store."); return; } } else if (r.NodeType == NodeType.Addr) { // Addresses handled differently because they can be paged, and exist across multiple files addressKey = AdaptorHelpers.GetFilenameForAddressRequest(r); if (_offlineAddresses.ContainsKey(addressKey)) { s = _offlineAddresses[addressKey]; } else { Msg.Log("AdaptorBtcOfflineFiles.ProcessRequestUsingFiles: AddressKey " + addressKey + " does not exist in offline store"); callbackOnFail("Data not available in offline store."); return; } } //Debug.Log("AdaptorBtcOfflineFiles.ProcessRequestUsingFiles call returned: " + s); var N = JSON.Parse(s); if (N == null) { Debug.LogWarning("AdaptorBtcOfflineFiles.ProcessRequestUsingFiles got back null data, maybe asked for bad data?"); callbackOnFail(r.NodeId); } else { // For the request r, launch the payload against the JSON Node N that we got back from the file bool payloadProcessingWasOk = payload(r, N); if (payloadProcessingWasOk) { callbackOnSuccess(r.NodeId); } else { Debug.LogWarning("AdaptorBtcOfflineFiles.ProcessRequestUsingFiles got back data, but payload could not process it"); callbackOnFail(r.NodeId); } } }