Esempio n. 1
0
        /// <summary>
        /// Tries to connect to the server and returns a value indicating whether the demanded object exists
        /// </summary>
        /// <returns>a value indicating whether the remote object exists</returns>
        private bool CheckConnection()
        {
            lock (client.Sync)
            {
                if (!connected)
                {
                    if (client.ValidateConnection())
                    {
                        var result = client.CheckRemoteObjectAvailability(remoteObjectName);
                        if (result.Available)
                        {
                            connected = true;
                            if (initial)
                            {
                                client.SubscribeEvent(remoteObjectName,
                                                      ParallellResources
                                                      .ParallelMethod_PackageProcessed,
                                                      packageFinishedHandler);
                                initial = false;
                            }
                        }
                        else
                        {
                            LogEnvironment.LogDebugEvent(result.Message, LogSeverity.Warning);
                        }
                    }
                }

                return(connected);
            }
        }
        /// <summary>
        /// Completes the satrtup and puts this proxy into an operational state
        /// </summary>
        private void StartupComplete()
        {
            client.SubscribeEvent(remoteObjectName, "PackageProcessed",
                                  new PackageFinishedEventHandler((s, e) =>
            {
                PackageSender ntask;

                lock (processingPackages)
                {
                    var retVal =
                        (from t in processingPackages where t.Package.Id == e.Package.Id select t)
                        .FirstOrDefault();
                    if (retVal != null)
                    {
                        processingPackages.Remove(retVal);
                    }

                    ntask = retVal;
                }

                PackageTrigger trigger;
                lock (unCommittedPackages)
                {
                    trigger = (from t in unCommittedPackages
                               where t.Args.Package.Id == e.Package.Id
                               select t).FirstOrDefault();
                }


                if (ntask != null || trigger != null)
                {
                    if ((e.Tasks.All(Success) && Success(e.Package)) || !e.Tasks.Any(RequireReboot))
                    {
                        bool reTriggered = trigger != null;
                        if (!reTriggered)
                        {
                            lock (unCommittedPackages)
                            {
                                if (unCommittedPackages.All(t => t.Args.Package.Id != e.Package.Id))
                                {
                                    unCommittedPackages.Add(new PackageTrigger
                                    {
                                        Args        = e,
                                        LastTrigger = DateTime.MinValue
                                    });
                                }
                            }
                        }

                        if (reTriggered)
                        {
                            LogEnvironment.LogDebugEvent(string.Format("The calling system did not commit the job {0} yet", e.Package.Id), LogSeverity.Warning);
                        }
                    }
                    else
                    {
                        lock (processingPackages)
                        {
                            processingPackages.Add(ntask);
                        }

                        RebootService();
                    }
                }
            }));
            void OpCallback(object o, EventArgs e)
            {
                if (client.Operational)
                {
                    lock (processingPackages)
                    {
                        processingPackages.ForEach(t => t.Sent = false);
                    }
                }
                else
                {
                    timeOfCommunicationLoss = DateTime.Now;
                }

                ((IBidirectionalClient)o).OperationalChanged -= OpCallback;
            };

            client.OperationalChanged += OpCallback;
            InitializeService();
            BackgroundRunner.AddPeriodicTask(ReTriggerPendingTasks, 20000);
        }
 /// <summary>
 /// Enables a child object to add a specific event-subscription
 /// </summary>
 /// <param name="eventName">the name of the target-event</param>
 /// <param name="target">the delegate to add to the subscription</param>
 protected override void AddEventSubscription(string eventName, Delegate target)
 {
     consumer.SubscribeEvent(objectName, eventName, target);
 }