//A worker for Read mode private static async Task doWork(ICollection <string> listOfIds, IDictionary <string, string> creds, string obj, ICryptoTransform cryptoTrans, TextWriter writer) { SynchronizedIds psid = new SynchronizedIds(); int currentId; Guid guid = Guid.NewGuid(); Trace.TraceInformation($"A worker {guid} has started."); HttpResponseMessage resp = null; do { currentId = psid.GetCurrentID(); if (currentId < listOfIds.Count && !(listOfIds.ToList())[currentId].Equals(string.Empty)) { try { resp = await ReadFromSalesForce(new Uri(creds["serverUrl"] + "/sobjects/" + obj + "/" + (listOfIds.ToList())[currentId] + "/Body") , creds, HttpMethod.Get, null); } catch (Exception ex) { Trace.TraceError("An exception occured while working in read mode.\n" + ex.Message); } if (resp != null && resp.Content != null && resp.StatusCode == HttpStatusCode.OK) { using (MemoryStream ms = resp.Content.ReadAsStreamAsync().Result as MemoryStream) { Trace.TraceInformation( $"Input #{currentId} with ID:{(listOfIds.ToList())[currentId]} has resulted in {ms.Length} bytes read by a thread #{guid}"); using (MemoryStream stream = new MemoryStream()) { using (CryptoStream cstream = new CryptoStream(stream, cryptoTrans, CryptoStreamMode.Write)) { cstream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length)); cstream.FlushFinalBlock(); byte[] encrypted = stream.ToArray(); writer.WriteLine(listOfIds.ToList()[currentId] + "," + Convert.ToBase64String(encrypted)); } } } } else { Trace.TraceError($"{(listOfIds.ToList())[currentId]} failed to read. Sending to the queue again"); listOfIds.Append((listOfIds.ToList())[currentId]); } continue; } break; } while (true); Trace.TraceInformation($"A worker {guid} has finished the work."); }
//A worker for Compare mode private static async Task doWork(MinSizeQueue <KeyValuePair <string, string> > queue, IDictionary <string, string> creds, string obj, ICryptoTransform cryptoTrans, TextWriter writer) { SynchronizedIds psid = new SynchronizedIds(); int currentId; Guid guid = Guid.NewGuid(); Trace.TraceInformation($"A worker {guid} has started."); while (true) { currentId = psid.GetCurrentID(); KeyValuePair <string, string> att; HttpResponseMessage response = null; if (minSizeQueue.TryDequeue(out att)) { byte[] valueBytes = null; try { valueBytes = Convert.FromBase64String(att.Value); } catch { Trace.TraceError($"Error decoding Base64 value for {att.Key}"); continue; } if (valueBytes.Length > 0) { using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(att.Value))) { using (CryptoStream cstream = new CryptoStream(stream, cryptoTrans, CryptoStreamMode.Read)) { byte[] decrypted = new byte[valueBytes.Length]; try { int bytesRead = cstream.Read(decrypted, 0, valueBytes.Length); if (bytesRead > 0) { string decryptedValue = Convert.ToBase64String(decrypted); try { response = await ReadFromSalesForce(new Uri(creds["serverUrl"] + "/sobjects/" + obj + "/" + att.Key + "/Body"), creds, HttpMethod.Get, null); } catch (Exception ex) { Trace.TraceError("An error occured while working in Compare mode.\n" + ex.Message); } if (response != null && response.Content != null && response.StatusCode == HttpStatusCode.OK) { using (MemoryStream ms = new MemoryStream()) { response.Content.ReadAsStreamAsync().Result.CopyTo(ms); byte[] res = ms.ToArray(); Array.Resize <byte>(ref decrypted, res.Length); if (res.SequenceEqual(decrypted)) { Trace.TraceInformation($"#{currentId} - {att.Key} is Equal from {guid}."); writer.WriteLine(att.Key + ",EQ"); } else { Trace.TraceInformation($"#{currentId} - {att.Key} is OK from {guid}."); } } } else { Trace.TraceWarning($"#{currentId} - {att.Key} failed to read from {guid}."); writer.WriteLine(att.Key + ",SF_ERROR"); } } } catch (Exception ex) { Trace.TraceError($"{ex.Message}\noccured while trying to compare {att.Key} from {guid}"); } } } } } else { minSizeQueue.Close(); break; } } Trace.TraceInformation($"A worker {guid} has finished the work."); }