Exemplo n.º 1
0
        //*** C8o calls ***//
        public void Call(string requestable, IDictionary <string, object> parameters = null, C8oResponseListener c8oResponseListener = null, C8oExceptionListener c8oExceptionListener = null)
        {
            try
            {
                if (requestable == null)
                {
                    throw new System.ArgumentNullException(C8oExceptionMessage.InvalidArgumentNullParameter("requestable"));
                }

                // Checks parameters validity
                if (parameters == null)
                {
                    parameters = new Dictionary <string, object>();
                }
                else
                {
                    // Clone parameters in order to modify them
                    parameters = new Dictionary <string, object>(parameters);
                }

                // Use the requestable string to add parameters corresponding to the c8o project, sequence, connector and transaction (<project>.<sequence> or <project>.<connector>.<transaction>)
                var matches = RE_REQUESTABLE.Match(requestable);
                if (!matches.Success)
                {
                    // The requestable is not correct so the default transaction of the default connector will be called
                    throw new System.ArgumentException(C8oExceptionMessage.InvalidRequestable(requestable));
                }

                // If the project name is specified
                if (matches.Groups[1].Value != "")
                {
                    parameters[ENGINE_PARAMETER_PROJECT] = matches.Groups[1].Value;
                }
                // If the C8o call use a sequence
                if (matches.Groups[2].Value != "")
                {
                    parameters[ENGINE_PARAMETER_SEQUENCE] = matches.Groups[2].Value;
                }
                else
                {
                    parameters[ENGINE_PARAMETER_CONNECTOR]   = matches.Groups[3].Value;
                    parameters[ENGINE_PARAMETER_TRANSACTION] = matches.Groups[4].Value;
                }

                Call(parameters, c8oResponseListener, c8oExceptionListener);
            }
            catch (Exception e)
            {
                HandleCallException(c8oExceptionListener, parameters, e);
            }
        }
Exemplo n.º 2
0
        private static IDictionary <string, object> ToParameters(object[] parameters)
        {
            if (parameters.Length % 2 != 0)
            {
                throw new System.ArgumentException(C8oExceptionMessage.InvalidParameterValue("parameters", "needs pairs of values"));
            }

            var newParameters = new Dictionary <string, object>();

            for (var i = 0; i < parameters.Length; i += 2)
            {
                newParameters["" + parameters[i]] = parameters[i + 1];
            }

            return(newParameters);
        }
Exemplo n.º 3
0
        //*** Constructors ***//

        /// <summary>
        /// This is the base object representing a Convertigo Server end point. This object should be instanciated
        /// when the apps starts and be accessible from any class of the app. Although this is not common, you may have
        /// several C8o objects instantiated in your app.
        /// </summary>
        /// <param name="endpoint">The Convertigo endpoint, syntax : &lt;protocol&gt;://&lt;server&gt;:&lt;port&gt;/&lt;Convertigo web app path&gt;/projects/&lt;project name&gt;<para/>
        /// Example : http://computerName:18080/convertigo/projects/MyProject
        /// </param>
        /// <param name="c8oSettings">Initialization options.<para/>
        /// Example : new C8oSettings().setLogRemote(false).setDefaultDatabaseName("sample")
        /// </param>
        public C8o(string endpoint, C8oSettings c8oSettings = null)
        {
            // Checks the URL validity
            if (!C8oUtils.IsValidUrl(endpoint))
            {
                throw new System.ArgumentException(C8oExceptionMessage.InvalidArgumentInvalidURL(endpoint));
            }

            // Checks the endpoint validty
            var matches = RE_ENDPOINT.Match(endpoint);

            if (!matches.Success)
            {
                throw new System.ArgumentException(C8oExceptionMessage.InvalidArgumentInvalidEndpoint(endpoint));
            }

            this.endpoint = endpoint;

            endpointConvertigo = matches.Groups[1].Value;
            endpointIsSecure   = matches.Groups[2].Value != null;
            endpointHost       = matches.Groups[3].Value;
            endpointPort       = matches.Groups[4].Value;
            endpointProject    = matches.Groups[5].Value;

            if (c8oSettings != null)
            {
                Copy(c8oSettings);
            }

            if (uiDispatcher == null)
            {
                uiDispatcher = defaultUiDispatcher;
            }

            try
            {
                if (C8oHttpInterfaceUsed != null)
                {
                    httpInterface = C8oHttpInterfaceUsed.GetTypeInfo().DeclaredConstructors.ElementAt(1).Invoke(new object[] { this }) as C8oHttpInterface;
                }
                else
                {
                    httpInterface = new C8oHttpInterface(this);
                }
            }
            catch (Exception e)
            {
                throw new C8oException(C8oExceptionMessage.InitHttpInterface(), e);
            }

            c8oLogger = new C8oLogger(this);

            c8oLogger.LogMethodCall("C8o constructor");

            try
            {
                if (C8oFullSyncUsed != null)
                {
                    c8oFullSync = C8oFullSyncUsed.GetTypeInfo().DeclaredConstructors.ElementAt(0).Invoke(new object[0]) as C8oFullSync;
                    c8oFullSync.Init(this);
                }
                else
                {
                    c8oFullSync = null; // new C8oFullSyncHttp(FullSyncServerUrl, FullSyncUsername, FullSyncPassword);
                }
            }
            catch (Exception e)
            {
                throw new C8oException(C8oExceptionMessage.FullSyncInterfaceInstance(), e);
            }
        }
Exemplo n.º 4
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;
                        }
                    }
                });
            }
        }