/// <summary>
 /// Initializes a new instance of the <see cref="TaskWrapper"/> class.
 /// </summary>
 /// <param name="dllFile">The path to the assembly file.</param>
 /// <param name="taskModule">The task instance.</param>
 /// <param name="configurationData">The configuration data.</param>
 /// <param name="logger">The proxy to the remote logger.</param>
 internal TaskWrapper(string dllFile, ITaskModule taskModule, TaskConfigurationData configurationData, ILogger logger)
 {
     this._dllFile = dllFile;
     this._task = taskModule;
     this._logger = logger;
     this._configurationData = configurationData;
     this._taskName = taskModule.GetType().FullName;
 }
Exemple #2
0
        public static void AddThread(this ITaskModule module, string taskName, Action <Guid> taskAction)
        {
            var thread = new Thread(() => taskAction(module.ThreadKey))
            {
                Name = taskName
            };

            module.ThreadList.Add(thread);
        }
Exemple #3
0
        /// <summary>
        /// LoadModule
        /// </summary>
        /// <param name="modulesNode"></param>
        private void LoadModules(XmlNode modulesNode)
        {
            TaskApplication taskApplication = TaskApplication.Instance();
            XmlAttribute    attribute;
            string          value;

            foreach (XmlNode node in modulesNode.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Comment)
                {
                    continue;
                }
                string name = node.Name;
                if (name != null)
                {
                    if (name != "clear")
                    {
                        if (name == "remove")
                        {
                            attribute = node.Attributes["name"];
                            value     = (attribute == null) ? null : attribute.Value;
                            if (!string.IsNullOrEmpty(value) && taskApplication.Modules.ContainsKey(value))
                            {
                                taskApplication.Modules.Remove(value);
                            }
                        }
                        if (name == "add")
                        {
                            attribute = node.Attributes["enabled"];
                            if ((attribute == null) || (attribute.Value != "false"))
                            {
                                XmlAttribute attName    = node.Attributes["name"];
                                XmlAttribute attType    = node.Attributes["type"];
                                string       strAttName = (attName == null) ? null : attName.Value;
                                string       strAttType = (attType == null) ? null : attType.Value;
                                if (!string.IsNullOrEmpty(strAttName) && !string.IsNullOrEmpty(strAttType))
                                {
                                    Type type = Type.GetType(strAttType);
                                    if (type != null)
                                    {
                                        ITaskModule module = Activator.CreateInstance(type) as ITaskModule;
                                        if (module != null)
                                        {
                                            module.Init(taskApplication, node);
                                            taskApplication.Modules[strAttName] = module;
                                        }
                                    }
                                }
                            }
                        }
                        continue;
                    }
                    taskApplication.Modules.Clear();
                }
            }
        }
Exemple #4
0
        public PIOService(ILogger Logger,
                          IPhraseModule PhraseModule, IPlanetModule PlanetModule, ICellModule CellModule,
                          IBuildingModule BuildingModule,
                          IWorkerModule WorkerModule,
                          IStackModule StackModule, IResourceTypeModule ResourceTypeModule,
                          IBuildingTypeModule BuildingTypeModule,
                          ITaskTypeModule TaskTypeModule,
                          IMaterialModule MaterialModule,
                          IIngredientModule IngredientModule, IProductModule ProductModule,
                          ITaskModule TaskModule,

                          ISchedulerModule SchedulerModule,

                          IResourceCheckerModule ResourceCheckerModule, ILocationCheckerModule LocationCheckerModule,
                          IIdlerModule IdlerModule, IProducerModule ProducerModule, IHarvesterModule HarvesterModule,
                          IMoverModule MoverModule, ITakerModule TakerModule, IStorerModule StorerModule,
                          IBuilderModule BuilderModule
                          ) : base(Logger)
        {
            LogEnter();

            this.phraseModule = PhraseModule;

            this.planetModule = PlanetModule;
            this.cellModule   = CellModule;

            this.buildingModule = BuildingModule;

            this.workerModule          = WorkerModule;
            this.stackModule           = StackModule;
            this.locationCheckerModule = LocationCheckerModule;
            this.resourceTypeModule    = ResourceTypeModule;
            this.taskTypeModule        = TaskTypeModule;

            this.buildingTypeModule = BuildingTypeModule;

            this.taskTypeModule   = TaskTypeModule;
            this.materialModule   = MaterialModule;
            this.ingredientModule = IngredientModule;
            this.productModule    = ProductModule;
            this.taskModule       = TaskModule;


            this.resourceCheckerModule = ResourceCheckerModule;
            this.idlerModule           = IdlerModule;
            this.producerModule        = ProducerModule;
            this.harvesterModule       = HarvesterModule;
            this.moverModule           = MoverModule;
            this.takerModule           = TakerModule;
            this.storerModule          = StorerModule;

            this.builderModule = BuilderModule;
        }
Exemple #5
0
        public SchedulerModule(ILogger Logger, ITaskModule TaskModule, IIdlerModule IdlerModule, IProducerModule ProducerModule, IHarvesterModule HarvesterModule, IMoverModule MoverModule, ITakerModule TakerModule, IStorerModule StorerModule, IBuilderModule FactoryBuilderModule) : base(Logger, ThreadPriority.Normal)
        {
            this.taskModule           = TaskModule; this.idlerModule = IdlerModule; this.producerModule = ProducerModule; this.harvesterModule = HarvesterModule;
            this.moverModule          = MoverModule; this.takerModule = TakerModule; this.storerModule = StorerModule;
            this.factoryBuilderModule = FactoryBuilderModule;


            idlerModule.TaskCreated     += TaskGeneratorModule_TaskCreated;
            producerModule.TaskCreated  += TaskGeneratorModule_TaskCreated;
            harvesterModule.TaskCreated += TaskGeneratorModule_TaskCreated;
            moverModule.TaskCreated     += TaskGeneratorModule_TaskCreated;
            takerModule.TaskCreated     += TaskGeneratorModule_TaskCreated;
            storerModule.TaskCreated    += TaskGeneratorModule_TaskCreated;


            factoryBuilderModule.TaskCreated += TaskGeneratorModule_TaskCreated;
        }
        /// <summary>
        ///     Tries the create task 模块.
        /// </summary>
        /// <param name="moduleConstructor">The 模块 constructor.</param>
        /// <param name="taskConfiguration">The task configuration.</param>
        /// <param name="taskModule">The task 模块.</param>
        private bool TryCreateTaskModule(ConstructorInfo moduleConstructor, object taskConfiguration,
                                         out ITaskModule taskModule)
        {
            IList <Expression> taskModuleParameters = new List <Expression>();
            var parameters = moduleConstructor.GetParameters();

            foreach (var item in parameters)
            {
                if (item.ParameterType == taskConfiguration.GetType())
                {
                    var taskConfigurationExpression        = Expression.Constant(taskConfiguration);
                    var taskConfigurationConvertExpression =
                        Expression.Convert(taskConfigurationExpression, taskConfiguration.GetType());
                    taskModuleParameters.Add(taskConfigurationConvertExpression);
                }
                else
                {
                    var parameter =
                        _taskContext.HttpContextAccessor.HttpContext.RequestServices.GetService(item.ParameterType);
                    if (parameter == null)
                    {
                        taskModule = null;
                        return(false);
                    }

                    var parameterExpression        = Expression.Constant(parameter);
                    var parameterConvertExpression = Expression.Convert(parameterExpression, item.ParameterType);
                    taskModuleParameters.Add(parameterConvertExpression);
                }
            }

            var newExpression    = Expression.New(moduleConstructor, taskModuleParameters.ToArray());
            var lambdaExpression = Expression.Lambda <Func <ITaskModule> >(newExpression);

            taskModule = lambdaExpression.Compile()();
            return(true);
        }
Exemple #7
0
 protected TaskGeneratorModule(ILogger Logger, ITaskModule TaskModule, IWorkerModule WorkerModule) : base(Logger)
 {
     this.taskModule = TaskModule; this.workerModule = WorkerModule;
 }
Exemple #8
0
 public StorerModule(ILogger Logger, ITaskModule TaskModule, IWorkerModule WorkerModule, IBuildingModule BuildingModule, IStackModule StackModule) : base(Logger, TaskModule, WorkerModule)
 {
     this.buildingModule = BuildingModule; this.stackModule = StackModule;
 }
Exemple #9
0
 public IdlerModule(ILogger Logger, ITaskModule TaskModule, IWorkerModule WorkerModule) : base(Logger, TaskModule, WorkerModule)
 {
     this.workerModule = WorkerModule;
 }
Exemple #10
0
 public HarvesterModule(ILogger Logger, ITaskModule TaskModule, IWorkerModule WorkerModule, IBuildingModule BuildingModule, IBuildingTypeModule BuildingTypeModule, IStackModule StackModule, IProductModule ProductModule) : base(Logger, TaskModule, WorkerModule)
 {
     this.buildingModule = BuildingModule; this.buildingTypeModule = BuildingTypeModule; this.stackModule = StackModule; this.productModule = ProductModule;
 }
Exemple #11
0
 public MoverModule(ILogger Logger, ITaskModule TaskModule, IWorkerModule WorkerModule, IBuildingModule BuildingModule) : base(Logger, TaskModule, WorkerModule)
 {
     this.buildingModule = BuildingModule;
 }
Exemple #12
0
 public ProducerModule(ILogger Logger, ITaskModule TaskModule, IWorkerModule WorkerModule, IBuildingModule BuildingModule, IBuildingTypeModule BuildingTypeModule, IStackModule StackModule, IIngredientModule IngredientModule, IProductModule ProductModule) : base(Logger, TaskModule, WorkerModule)
 {
     this.buildingModule = BuildingModule; this.buildingTypeModule = BuildingTypeModule; this.stackModule = StackModule; this.ingredientModule = IngredientModule; this.productModule = ProductModule;
 }
Exemple #13
0
        /// <summary>
        /// Inspects the assemblies in the current AppDomain and returns any valid tasks found.
        /// </summary>
        /// <param name="dllFile">The path to the task module assembly file.</param>
        /// <param name="xmlFile">The path to the task module configuration file.</param>
        /// <param name="logger">The remote logging component provided by the module supervisor.</param>
        /// <param name="baseDirectory">The base path.</param>
        /// <returns>Array containing any valid tasks found.</returns>
        public TaskWrapper[] LoadAndConfigure(string dllFile, string xmlFile, ILogger logger, string baseDirectory)
        {
            this._logger        = logger;
            this._baseDirectory = baseDirectory;

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.CurrentDomain_UnhandledException);
            AppDomain.CurrentDomain.AssemblyResolve    += new ResolveEventHandler(this.CurrentDomain_AssemblyResolve);

            List <TaskWrapper> result = new List <TaskWrapper>();

            Assembly dll = Assembly.LoadFrom(dllFile);

            IEnumerable <TaskConfigurationData> taskConfigurationData = xmlFile.ToTaskConfiguration();

            logger.Log(string.Format("Found {0} task definitions in configuration file.", taskConfigurationData.Count()));

            foreach (TaskConfigurationData task in taskConfigurationData)
            {
                if (string.IsNullOrWhiteSpace(task.TypeName))
                {
                    logger.Log("Invalid configuration: no type name specified.");
                    continue;
                }

                string targetTypeName = task.TypeName;

                if (null == task.ConfigurationData)
                {
                    logger.Log("Invalid configuration: no module configuration specified.");
                    continue;
                }

                int slaLimit = (int)(task.SLA ?? ConfigurationHelpers.DefaultSLA).TotalSeconds;

                int timeout = (int)(task.Timeout ?? ConfigurationHelpers.DefaultTimeout).TotalSeconds;

                int delayStart = (int)(task.DelayStart ?? ConfigurationHelpers.DefaultDelayStart).TotalSeconds;

                Type targetType = dll.GetType(targetTypeName, false);

                if (null != targetType)
                {
                    ITaskModule taskModuleInstance = null;

                    try
                    {
                        taskModuleInstance = (ITaskModule)Activator.CreateInstance(targetType);

                        XElement element = task.ConfigurationData.ToXml();

                        taskModuleInstance.Configure(element);
                    }
                    catch (Exception e)
                    {
                        logger.Log(string.Format("Unable to load task '{0}'", targetType.FullName), e);
                        throw;
                    }

                    logger.Log(string.Format("Task '{0}' successfully configured and initialized. ({1} schedules, {4}s start delay, {2}s SLA, {3}s timeout)", targetType.FullName, (task.Schedule ?? new ScheduleData[0]).Count(), slaLimit, timeout, delayStart));
                    result.Add(new TaskWrapper(dllFile, taskModuleInstance, task, logger));
                }
            }

            return(result.ToArray());
        }