private RenderConfiguration GetNewRenderConfiguration(Job jobToDo)
        {
            RenderConfiguration config = new RenderConfiguration();
            config.RenderJobID = Guid.NewGuid();
            config.JobID = this.configuration.JobID;
            config.JobToDo = jobToDo;
            config.RenderWidth = (int)this.renderSize.Width / 2;
            config.RenderHeight = (int)this.renderSize.Height / 2;

            config.UpdateInterval = 30;
            config.IgnoreEqualImages = true;

            return config;
        }
        private async void HandleProcessResource(Job job, ProcessResource resource)
        {
            List<Agent> foundAgents = this.agentSelectors[job].AvailableAgents.Where(x => x.IP.Equals(resource.ProcessAgent.IP)).ToList();

            if (foundAgents.Count >= 1)
            {
                RenderConfiguration config = this.GetNewRenderConfiguration(job);
                WorkingAgent worker = await this.agentSelectors[job].GetCompatibleAgentFromList(config, foundAgents);

                this.HandleFoundWorkingAgent(job, worker);
            }
            else
            {
                if (this.OnAgentWithProcessNotFound != null)
                {
                    this.OnAgentWithProcessNotFound(job);
                }
            }
        }
        private void HandleFoundWorkingAgent(Job job, WorkingAgent worker)
        {
            if (worker == null)
            {
                if (this.OnNoResourceCompatibleAgentFound != null)
                {
                    this.OnNoResourceCompatibleAgentFound(job);
                }
            }
            else
            {
                Debug.WriteLine("--> Found agent for job " + job.Resource.Name);

                if (this.currentlyTreatedJob != null && worker.Configuration.JobToDo.OrderingNumber == this.currentlyTreatedJob.OrderingNumber)
                {
                    worker.OnResultReceived += Worker_OnResultReceived;
                    worker.OnMessageReceived += Worker_OnMessageReceived;
                    worker.OnAgentGotUnreachable += Worker_OnAgentGotUnreachable;

                    this.workingAgents.Add(worker);
                }
                else
                {
                    worker.CancelRenderJob();
                }
            }
        }
        private void RunJob(Job job)
        {
            if (job.Resource is FileResource)
            {
                FileResource fr = (FileResource)job.Resource;

                if (!fr.Local)
                {
                    this.HandleFileResource(job, fr);
                }
                else
                {
                    this.HandleFileResource(job, new FileResource() {
                        Local = fr.Local,
                        Path = Path.Combine(
                            PersistenceManager.GetWriteablePath(), 
                            PersistenceManager.SavedCustomFilesDirectoryName, 
                            fr.Path) });
                }
            }
            else if (job.Resource is WebResource)
            {
                WebResource wr = (WebResource)job.Resource;

                this.HandleWebResource(wr);
            }
            else if (job.Resource is ProcessResource)
            {
                ProcessResource pr = (ProcessResource)job.Resource;

                this.HandleProcessResource(job, pr);
            }
        }
        private async void HandleFileResource(Job job, FileResource resource)
        {
            bool accessable = false;

            try
            {
                // Try to access file at the given path
                StorageFile file = await StorageFile.GetFileFromPathAsync(resource.Path);

                accessable = true;
            }
            catch (Exception ex)
            {
                // It is a file, but it could not be found or accessed
                if (ex is FileNotFoundException)
                {
                    if (this.OnResourceNotAvailable != null)
                    {
                        this.OnResourceNotAvailable(job);
                    }
                }
                else if (ex is UnauthorizedAccessException)
                {
                    EventsManager.Log(Job_EventType.Error, this.configuration, "Could not access " + resource.Path);
                }
                else
                {
                    // non html web resources are also file resources
                    if (await CompatibilityManager.IsAvailableWebResource(resource.Path))
                    {
                        accessable = true;
                    }
                }
            }

            if (accessable)
            {
                if (CompatibilityManager.IsCompatibleImage(resource))
                {
                    if (this.OnImageDisplayRequested != null)
                    {
                        await this.displayDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            BitmapImage img = new BitmapImage(new Uri(resource.Path));

                            this.OnImageDisplayRequested(img);
                        });
                    }
                }
                else if (CompatibilityManager.IsCompatibleVideo(resource))
                {
                    if (this.OnVideoDisplayRequested != null)
                    {
                        await this.displayDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            this.OnVideoDisplayRequested(new Uri(resource.Path));
                        });
                    }
                }
            }
            else
            {
                RenderConfiguration config = this.GetNewRenderConfiguration(job);

                WorkingAgent worker = await this.agentSelectors[job].GetCompatibleAgent(config);

                this.HandleFoundWorkingAgent(job, worker);
            }
        }
        public void Cancel()
        {
            this.Running = false;
            this.CancelJob(this.currentlyTreatedJob);

            this.currentlyTreatedJob = null;
        }
        private async void CancelJob(Job job)
        {
            List<WorkingAgent> jobTreatingAgents = this.workingAgents.Where(x => x.Configuration.JobToDo.OrderingNumber == job.OrderingNumber).ToList();

            foreach (WorkingAgent agent in jobTreatingAgents)
            {
                agent.CancelRenderJob();
            }
            
            this.workingAgents.RemoveAll(x => jobTreatingAgents.Contains(x));

            if (this.OnDisplayAbortRequested != null)
            {
                await this.displayDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    this.OnDisplayAbortRequested();
                });
            }
        }
 public AgentSelector(Job jobToDo, List<Agent> availableAgents)
 {
     this.JobToDo = jobToDo;
     this.AvailableAgents = availableAgents.ToList();
 }
        public void Start()
        {
            this.Running = true;

            if (jobForWindow != null)
            {
                Task.Factory.StartNew(async () =>
                {
                    List<Job> ordered = jobForWindow.Jobs.OrderBy(x => x.OrderingNumber).ToList();
                    
                    do
                    {
                        foreach (Job job in ordered)
                        {
                            this.currentlyTreatedJob = job;

                            await Task.Factory.StartNew(() =>
                            {
                                this.RunJob(job);
                            });

                            Debug.WriteLine("--> start delaying...");

                            await Task.Delay(job.Duration * 1000);

                            Debug.WriteLine("--> start canceling...");

                            this.CancelJob(job);
                        }
                    }
                    while (jobForWindow.Looping && this.Running);
                });
            }
        }
Example #10
0
        private void Manager_OnAgentNotReachable(Job job, Agent agent)
        {
            //throw new NotImplementedException();
            string errorText = string.Empty;

            errorText += "Error while handling job " + job.OrderingNumber + " (" + job.Resource.Name + "): ";
            errorText += "The agent " + agent.IP.ToString() + " is offline.";

            EventsManager.Log(Job_EventType.Error, this.Configuration, errorText);
        }
Example #11
0
        private void Manager_OnAgentWithProcessNotFound(Job job)
        {
            //throw new NotImplementedException();
            string errorText = string.Empty;

            errorText += "Error while handling job " + job.OrderingNumber + " (" + job.Resource.Name + "): ";
            errorText += "There is no agent running the process " + ((ProcessResource)job.Resource).ProcessID + ".";

            EventsManager.Log(Job_EventType.Error, this.Configuration, errorText);
        }
Example #12
0
        private void Manager_OnNoResourceCompatibleAgentFound(Job job)
        {
            //throw new NotImplementedException();
            string errorText = string.Empty;

            errorText += "Error while handling job " + job.OrderingNumber + " (" + job.Resource.Name + "): ";
            errorText += "No agent is able to handle the given resource.";

            EventsManager.Log(Job_EventType.Error, this.Configuration, errorText);
        }
Example #13
0
        private void Manager_OnResourceNotAvailable(Job job)
        {
            //throw new NotImplementedException();
            string errorText = string.Empty;

            errorText += "Error while handling job " + job.OrderingNumber + " (" + job.Resource.Name + "): ";
            errorText += "The resource is not available.";

            EventsManager.Log(Job_EventType.Error, this.Configuration, errorText);
        }
Example #14
0
        private void Manager_OnAgentAborted(Job job, Agent agent)
        {
            string errorText = string.Empty;

            errorText += "Error while handling job " + job.OrderingNumber + " (" + job.Resource.Name + "): ";
            errorText += "The agent aborted the execution of the given resource.";

            EventsManager.Log(Job_EventType.Error, this.Configuration, errorText);
        }