public String OnStreamResponse(Stream streamResponse, IDictionary <String, Object> parameters, Boolean localCacheEnabled) { String responseString = null; if (localCacheEnabled) { responseString = C8oTranslator.StreamToString(streamResponse); this.OnStringResponse(responseString, parameters); } else { this.OnXmlResponse(C8oTranslator.StreamToXml(streamResponse), parameters); } return(responseString); }
internal C8oLogger(C8o c8o) { this.c8o = c8o; remoteLogUrl = c8o.EndpointConvertigo + "/admin/services/logs.Add"; remoteLogs = new Queue <JObject>(); alreadyRemoteLogging = new bool[] { false }; remoteLogLevel = C8oLogLevel.TRACE; var currentTime = DateTime.Now; startTimeRemoteLog = currentTime; uidRemoteLogs = C8oTranslator.DoubleToHexString(C8oUtils.GetUnixEpochTime(currentTime)); env = new JObject() { { "uid", uidRemoteLogs }, { "uuid", c8o.DeviceUUID }, { "project", c8o.EndpointProject } }.ToString(); }
public void OnStringResponse(String stringResponse, IDictionary <String, Object> parameters) { this.OnXmlResponse(C8oTranslator.StringToXml(stringResponse), parameters); }
private async Task DownloadChunk(C8o c8o, Stream createdFileStream, string filepath, string fsConnector, string uuid, int i, JObject task, C8oFileTransferStatus transferStatus) { Stream chunkFS = null; var chunkpath = filepath + ".chunk"; try { var fsurl = c8o.EndpointConvertigo + "/fullsync/" + fsConnector + "/" + uuid + "_" + i; var fsurlatt = fsurl + "/chunk"; var digest = "no digest"; int retry = 5; while (retry > 0) { try { Debug("Getting the document at: " + fsurl); var responseString = C8oTranslator.StreamToString(c8o.httpInterface.HandleGetRequest(fsurl).Result.GetResponseStream()); Debug("The document content: " + responseString); var json = C8oTranslator.StringToJson(responseString); digest = json["_attachments"]["chunk"]["digest"].ToString(); retry = 0; } catch (Exception e) { if (retry-- > 0) { Debug("Failed to get the chunk descriptor, retry. Cause: " + e.Message); } else { throw new Exception("Failed to get the chunk descriptor at " + fsurl, e); } } } chunkFS = fileManager.CreateFile(chunkpath); retry = 5; while (retry > 0) { var md5 = "no md5"; try { Debug("Getting the attachment at: " + fsurlatt); chunkFS.Position = 0; AppendChunk(chunkFS, fsurlatt, c8o); chunkFS.Position = 0; md5 = "md5-" + c8o.GetMD5(chunkFS); } catch (Exception e) { Debug("Download Chunk failed, retry it. Cause: " + e.Message); } Debug("Comparing digests: " + digest + " / " + md5); if (digest.Equals(md5)) { chunkFS.Position = 0; chunkFS.CopyTo(createdFileStream, 4096); Debug("Chunk '" + uuid + "_" + i + "' assembled."); retry = 0; await c8oTask.CallJson("fs://.post", C8o.FS_POLICY, C8o.FS_POLICY_MERGE, "_id", task["_id"].Value <string>(), "download", transferStatus.Current = i + 1, "position", createdFileStream.Position ).Async(); Notify(transferStatus); } else if (retry-- > 0) { Debug("The digest doesn't match, retry downloading."); } else { throw new Exception("Invalid digest: " + digest + " / " + md5); } } } catch (Exception e2) { Debug("Failed to DownloadChunk, retry soon."); throw e2; } finally { if (chunkFS != null) { chunkFS.Dispose(); } fileManager.DeleteFile(chunkpath); } }
/// <summary> /// Log the c8o call JSON response in TRACE level. /// </summary> /// <param name="response"></param> /// <param name="url"></param> /// <param name="parameters"></param> internal void LogC8oCallJSONResponse(JObject response, string url, IDictionary <string, object> parameters) { LogC8oCallResponse(C8oTranslator.JsonToString(response), "JSON", url, parameters); }
/// <summary> /// Log the c8o call XML response in TRACE level. /// </summary> /// <param name="response"></param> /// <param name="url"></param> /// <param name="parameters"></param> internal void LogC8oCallXMLResponse(XDocument response, string url, IDictionary <string, object> parameters) { LogC8oCallResponse(C8oTranslator.XmlToString(response), "XML", url, parameters); }
private void LogRemote() { bool canLog = false; lock (alreadyRemoteLogging) { // If there is no another thread already logging AND there is at least one log canLog = !alreadyRemoteLogging[0] && remoteLogs.Count > 0; if (canLog) { alreadyRemoteLogging[0] = true; } } if (canLog) { c8o.RunBG(async() => { try { // Take logs in the queue and add it to a json array int count = 0; int listSize = remoteLogs.Count; var logsArray = new JArray(); while (count < listSize && count < REMOTE_LOG_LIMIT) { logsArray.Add(remoteLogs.Dequeue()); count++; } // Initializes request paramters var parameters = new Dictionary <string, object>() { { JSON_KEY_LOGS, JsonConvert.SerializeObject(logsArray) }, { JSON_KEY_ENV, env }, { C8o.ENGINE_PARAMETER_DEVICE_UUID, c8o.DeviceUUID } }; JObject jsonResponse; try { var webResponse = await c8o.httpInterface.HandleRequest(remoteLogUrl, parameters); var streamResponse = webResponse.GetResponseStream(); jsonResponse = C8oTranslator.StreamToJson(streamResponse); } catch (Exception e) { c8o.LogRemote = false; if (c8o.LogOnFail != null) { c8o.LogOnFail(new C8oException(C8oExceptionMessage.RemoteLogFail(), e), null); } return; } if (jsonResponse != null) { var logLevelResponse = jsonResponse.GetValue(C8oLogger.JSON_KEY_REMOTE_LOG_LEVEL); if (logLevelResponse != null) { string logLevelResponseStr = logLevelResponse.Value <string>(); var c8oLogLevel = C8oLogLevel.GetC8oLogLevel(logLevelResponseStr); if (c8oLogLevel != null) { remoteLogLevel = c8oLogLevel; } LogRemote(); } } } finally { lock (alreadyRemoteLogging) { alreadyRemoteLogging[0] = false; } } }); } }