Esempio n. 1
0
        /// <summary>
        /// Adds the function handler to the service. All methods defined as MSB function via <see cref="MsbFunctionAttribute"/> will be added as <see cref="Function"/>s.
        /// </summary>
        /// <param name="functionHandler">Function handler that should be added.</param>
        public void AddFunctionHandler(AbstractFunctionHandler functionHandler)
        {
            if (this.registeredFunctionHandlerAndRelatedFunctions.ContainsKey(functionHandler))
            {
                Log.Info($"Nothing to do, FunctionHandler '{functionHandler.GetType().FullName}' already registered to Service '{this.Uuid}'");
            }
            else
            {
                List <Function> addedFunctions = new List <Function>();
                foreach (var functionHandlerMethod in functionHandler.GetType().GetRuntimeMethods())
                {
                    if (!functionHandlerMethod.IsPublic || functionHandlerMethod.DeclaringType == typeof(object))
                    {
                        continue;
                    }

                    Function function = new Function(functionHandler, functionHandlerMethod);
                    this.AddFunction(function);
                    addedFunctions.Add(function);
                }

                this.registeredFunctionHandlerAndRelatedFunctions.Add(functionHandler, addedFunctions);
                Log.Info($"FunctionHandler '{functionHandler.GetType().FullName}' registered to Service '{this.Uuid}'");
            }
        }
        private string GetFunctionIdFromHandler(AbstractFunctionHandler functionhandler, MethodInfo methodInfo)
        {
            var functionId = string.Empty;

            // Process path of IFunctionHandler
            var functionHandlerId = string.Empty;

            if (functionhandler.GetType().GetTypeInfo().GetCustomAttributes(typeof(MsbFunctionHandlerAttribute), true).FirstOrDefault() is MsbFunctionHandlerAttribute functionHandlerAttribute)
            {
                functionHandlerId = functionHandlerAttribute.Id;
                if (functionHandlerId.Equals(string.Empty))
                {
                    functionHandlerId = functionhandler.GetType().Name;
                    Log.Warn("Id of MsbFunctionHandlerAttribute is not set. Name of type is used as id.");
                }
            }
            else
            {
                functionHandlerId += functionhandler.GetType().Name;
            }

            functionId += functionHandlerId;
            if (!functionId.EndsWith("/") && !functionId.StartsWith("/"))
            {
                functionId = functionId + "/";
            }

            functionId += this.GetFunctionIdFromMethodInfo(methodInfo);
            return(functionId);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="Function"/> class.
 /// </summary>
 /// <param name="functionHandler">The <see cref="AbstractFunctionHandler"/> that contains the method.</param>
 /// <param name="methodInfo">The info about the method.</param>
 public Function(AbstractFunctionHandler functionHandler, MethodInfo methodInfo)
 {
     if (this.IsValidMsbMethodDefintion(methodInfo))
     {
         var id          = this.GetFunctionIdFromHandler(functionHandler, methodInfo);
         var name        = this.Name = this.GetFunctionNameFromMethodInfo(methodInfo);
         var description = this.GetFunctionDescriptionFromMethodInfo(methodInfo);
         this.Init(id, name, description, methodInfo, functionHandler);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Removes the function handler to the service. All MSB function that belong to this function handler will be removed.
        /// </summary>
        /// <param name="functionHandler">The function handler.</param>
        public void RemoveFunctionHandler(AbstractFunctionHandler functionHandler)
        {
            if (this.registeredFunctionHandlerAndRelatedFunctions.ContainsKey(functionHandler))
            {
                List <Function> addedFunctions = this.registeredFunctionHandlerAndRelatedFunctions[functionHandler];
                foreach (var function in addedFunctions)
                {
                    this.RemoveFunction(function);
                }

                this.registeredFunctionHandlerAndRelatedFunctions.Remove(functionHandler);
                Log.Info($"FunctionHandler '{functionHandler.GetType().FullName}' removed from Service '{this.Uuid}'");
            }
            else
            {
                Log.Info($"Nothing to do, FunctionHandler '{functionHandler.GetType().FullName}' not registered to Service '{this.Uuid}'");
            }
        }