Пример #1
0
        /// <summary>
        /// Updates the service data.
        /// </summary>
        /// <param name="systemId">The system identifier.</param>
        /// <param name="serviceData">The service data.</param>
        public void UpdateServiceData(string systemId, ServiceInfoData serviceData)
        {
            ServiceInfoData clonedService = serviceData.Clone();
            int             serviceIndex  = Array.IndexOf(SupportedServices, serviceData.serviceId);

            if (serviceIndex >= 0)
            {
                lock (_serviceDataLock)
                {
                    ServiceInfoData existingService;
                    if (_serviceData[serviceIndex].TryGetValue(systemId, out existingService))
                    {
                        if (existingService.Equals(serviceData))
                        {
                            clonedService = null;
                        }
                    }

                    if (clonedService != null)
                    {
                        _serviceData[serviceIndex][systemId] = clonedService;
                    }
                }

                string notificationUrl;
                bool   subscribed;
                lock (_serviceSubscriptionLock)
                {
                    subscribed      = _serviceSubscriptions[serviceIndex] == true;
                    notificationUrl = _serviceNotificationUrl;
                }

                if (subscribed)
                {
                    bool isOnline = _identificationService.IsSystemOnline(systemId);
                    DataPackageTests.T2GServiceInterface.Notification.serviceList serviceList = new DataPackageTests.T2GServiceInterface.Notification.serviceList();
                    serviceList.Capacity = 1;

                    if (isOnline || clonedService.isAvailable == false)
                    {
                        serviceList.Add(clonedService);
                    }
                    else
                    {
                        serviceList.Add(clonedService.Clone());
                        serviceList[0].isAvailable = false;
                    }

                    EndpointAddress address = new EndpointAddress(notificationUrl);
                    using (NotificationClient client = new NotificationClient("NotificationClient", address))
                    {
                        try
                        {
                            client.Open();
                            client.onServiceNotification(systemId, isOnline, serviceIndex + 1, serviceList);
                        }
                        finally
                        {
                            if (client.State == CommunicationState.Faulted)
                            {
                                client.Abort();
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public subscribeToServiceNotificationsOutput subscribeToServiceNotifications(subscribeToServiceNotificationsInput request)
        {
            if (!_identificationService.IsSessionValid(request.Body.sessionId))
            {
                throw FaultExceptionFactory.CreateInvalidSessionIdentifierFault();
            }

            string notificationUrl = _identificationService.GetNotificationUrl(request.Body.sessionId);

            if (string.IsNullOrEmpty(notificationUrl))
            {
                throw FaultExceptionFactory.CreateNoNotificationUrlFault();
            }

            if (request.Body.systemIdList.Count != 0)
            {
                throw FaultExceptionFactory.CreateOnlySubscriptionToAllSystemIsSupportedFault();
            }

            int serviceIndex = Array.IndexOf(SupportedServices, (ushort)request.Body.serviceId);

            if (serviceIndex < 0)
            {
                throw FaultExceptionFactory.CreateInvalidServiceIdentifierFault();
            }

            lock (_serviceSubscriptionLock)
            {
                _serviceSubscriptions[serviceIndex] = true;
                _serviceNotificationUrl             = notificationUrl;
            }

            Dictionary <string, ServiceInfoData> serviceData;

            lock (_serviceDataLock)
            {
                serviceData = new Dictionary <string, ServiceInfoData>(_serviceData[serviceIndex]);
            }

            // Notify the subscribers
            if (serviceData.Count > 0)
            {
                DataPackageTests.T2GServiceInterface.Notification.serviceList serviceList = new DataPackageTests.T2GServiceInterface.Notification.serviceList();
                serviceList.Capacity = 1;
                EndpointAddress address = new EndpointAddress(notificationUrl);
                using (NotificationClient client = new NotificationClient("NotificationClient", address))
                {
                    try
                    {
                        client.Open();
                        foreach (KeyValuePair <string, ServiceInfoData> notification in serviceData)
                        {
                            if (serviceList.Count == 0)
                            {
                                serviceList.Add(notification.Value);
                            }
                            else
                            {
                                serviceList[0] = notification.Value;
                            }
                            client.onServiceNotification(notification.Key, _identificationService.IsSystemOnline(notification.Key), serviceIndex + 1, serviceList);
                        }
                    }
                    finally
                    {
                        if (client.State == CommunicationState.Faulted)
                        {
                            client.Abort();
                        }
                    }
                }
            }

            subscribeToServiceNotificationsOutputBody body   = new subscribeToServiceNotificationsOutputBody(serviceIndex + 1);
            subscribeToServiceNotificationsOutput     result = new subscribeToServiceNotificationsOutput(body);

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Called when online status on a train changed.
        /// </summary>
        /// <param name="systemId">The system identifier.</param>
        /// <param name="isOnline">true if system became online, false if it became offline.</param>
        private void OnlineStatusChangedLogic(string systemId, bool isOnline)
        {
            ServiceInfoData[] data = (ServiceInfoData[])Array.CreateInstance(typeof(ServiceInfoData), SupportedServices.Length);

            lock (_serviceDataLock)
            {
                for (int i = 0; i < SupportedServices.Length; ++i)
                {
                    ServiceInfoData serviceInfo;
                    if (_serviceData[i].TryGetValue(systemId, out serviceInfo))
                    {
                        data[i]             = serviceInfo.Clone();
                        data[i].isAvailable = data[i].isAvailable && isOnline;
                    }
                }
            }

            bool[] subscribed;
            string notificationUrl;

            lock (_serviceSubscriptionLock)
            {
                notificationUrl = _serviceNotificationUrl;
                subscribed      = (bool[])_serviceSubscriptions.Clone();
            }

            if (!string.IsNullOrEmpty(notificationUrl))
            {
                using (NotificationClient client = new NotificationClient("NotificationClient", notificationUrl))
                {
                    try
                    {
                        DataPackageTests.T2GServiceInterface.Notification.serviceList serviceList = new DataPackageTests.T2GServiceInterface.Notification.serviceList();
                        serviceList.Capacity = 1;

                        for (int i = 0; i < subscribed.Length; ++i)
                        {
                            if (subscribed[i] && data[i] != null)
                            {
                                if (client.State == CommunicationState.Closed)
                                {
                                    client.Open();
                                }

                                serviceList.Add(data[i]);
                                client.onServiceNotification(systemId, isOnline, i + 1, serviceList);
                                serviceList.Clear();
                            }
                        }

                        if (client.State == CommunicationState.Opened)
                        {
                            client.Close();
                        }
                    }
                    finally
                    {
                        if (client.State == CommunicationState.Faulted)
                        {
                            client.Abort();
                        }
                    }
                }
            }
        }