コード例 #1
0
 private C8oPromise <T> Then(C8oOnResponse <T> c8oOnResponse, bool ui)
 {
     if (nextPromise != null)
     {
         return(nextPromise.Then(c8oOnResponse, ui));
     }
     else
     {
         c8oResponse = new KeyValuePair <C8oOnResponse <T>, bool>(c8oOnResponse, ui);
         nextPromise = new C8oPromise <T>(c8o);
         if (lastFailure != null)
         {
             nextPromise.lastFailure    = lastFailure;
             nextPromise.lastParameters = lastParameters;
         }
         if (lastResponse != null)
         {
             c8o.RunBG(OnResponse);
         }
         return(nextPromise);
     }
 }
コード例 #2
0
        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;
                        }
                    }
                });
            }
        }