Esempio n. 1
0
        public void DeleteSystem(string systemId)
        {
            bool systemDeleted;

            lock (_systemInfoLock)
            {
                systemDeleted = _systems.Remove(systemId);
            }

            // Notify about changes
            if (systemDeleted)
            {
                List <string> urlsToNotify;

                lock (_sessionLock)
                {
                    urlsToNotify = _sessions.Values.Where(s => s.NotificationEnabled && !string.IsNullOrEmpty(s.NotificationUrl)).Select(s => s.NotificationUrl).Distinct().ToList();
                }

                if (urlsToNotify.Count > 0)
                {
                    foreach (string url in urlsToNotify)
                    {
                        EndpointAddress address = new EndpointAddress(url);
                        using (NotificationClient client = new NotificationClient("NotificationClient", address))
                        {
                            try
                            {
                                client.Open();
                                client.onSystemDeletedNotification(systemId);
                            }
                            finally
                            {
                                if (client.State == CommunicationState.Faulted)
                                {
                                    client.Abort();
                                }
                            }
                        }
                    }
                }

                SystemDeletedHandler handlers = SystemDeleted;
                if (handlers != null)
                {
                    handlers(systemId);
                }
            }
        }
Esempio n. 2
0
        public void UpdateMessageData(MessageBase messageData, int messageIndex)
        {
            MessageBase clonedData = messageData.Clone();

            clonedData.timestamp = DateTime.UtcNow;
            lock (_messageDataLock)
            {
                _messagesData[messageIndex][messageData.systemId] = clonedData;
            }

            string notificationUrl;
            bool   subscribed;

            lock (_messageSubscriptionLock)
            {
                subscribed      = _messageSubscriptions[messageIndex];
                notificationUrl = _messageNotificationUrl;
            }

            if (subscribed)
            {
                EndpointAddress address = new EndpointAddress(notificationUrl);
                using (NotificationClient client = new NotificationClient("NotificationClient", address))
                {
                    try
                    {
                        client.Open();
                        client.onMessageNotification(clonedData.systemId, clonedData.messageId, clonedData.fieldList, clonedData.timestamp, clonedData.inhibited);
                    }
                    finally
                    {
                        if (client.State == CommunicationState.Faulted)
                        {
                            client.Abort();
                        }
                    }
                }
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public subscribeToMessageNotificationsOutput subscribeToMessageNotifications(subscribeToMessageNotificationsInput 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();
            }

            if (request.Body.messageSubscriptionList.Count != 1)
            {
                throw FaultExceptionFactory.CreateInvalidSubscriptionCountFault();
            }
            else if (request.Body.messageSubscriptionList[0].notificationMode != notificationModeEnum.onChanges)
            {
                throw FaultExceptionFactory.CreateOnlyOnChangeNotificationSupportedFault();
            }


            int messageIndex = Array.IndexOf(SupportedMessages, request.Body.messageSubscriptionList[0].messageId);

            if (messageIndex < 0)
            {
                throw FaultExceptionFactory.CreateInvalidMessageIdentifierFault();
            }

            int subscriptionId;

            lock (_messageSubscriptionLock)
            {
                _messageSubscriptions[messageIndex] = true;
                _messageNotificationUrl             = notificationUrl;
                subscriptionId = messageIndex + 1;
            }

            Dictionary <string, MessageBase> messageData;

            lock (_messageDataLock)
            {
                messageData = new Dictionary <string, MessageBase>(_messagesData[messageIndex]);
            }

            // Notify the subscribers
            if (messageData.Count > 0)
            {
                EndpointAddress address = new EndpointAddress(notificationUrl);
                using (NotificationClient client = new NotificationClient("NotificationClient", address))
                {
                    try
                    {
                        client.Open();
                        foreach (MessageBase notification in messageData.Values)
                        {
                            client.onMessageNotification(notification.systemId, notification.messageId, notification.fieldList, notification.timestamp, notification.inhibited);
                        }
                    }
                    finally
                    {
                        if (client.State == CommunicationState.Faulted)
                        {
                            client.Abort();
                        }
                    }
                }
            }

            subscribeToMessageNotificationsOutput result = new subscribeToMessageNotificationsOutput();

            result.Body = new DataPackageTests.T2GServiceInterface.VehicleInfo.subscribeToMessageNotificationsOutputBody(subscriptionId);

            return(result);
        }
Esempio n. 5
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();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 6
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();
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Updates the information on a system.
        /// </summary>
        /// <param name="systemId">The system identifier.</param>
        /// <param name="vehicleId">The vehicle identifier.</param>
        /// <param name="isOnline">The online status of the system.</param>
        /// <param name="status">The status.</param>
        /// <param name="missionId">The mission identifier.</param>
        /// <param name="communicationLink">The communication link.</param>
        public void UpdateSystem(string systemId, int vehicleId, bool isOnline, uint status, string missionId, CommLinkEnum communicationLink, string IPAddress)
        {
            SystemInfoStruct newSystemInfo = new SystemInfoStruct();

            newSystemInfo.communicationLink = communicationLink;
            newSystemInfo.isOnline          = isOnline;
            newSystemInfo.missionId         = missionId;
            newSystemInfo.status            = status;
            newSystemInfo.systemId          = systemId;
            newSystemInfo.IPAddress         = IPAddress;
            newSystemInfo.vehiclePhysicalId = Convert.ToUInt16(vehicleId);

            bool onlineStatusChanged = false;

            lock (_systemInfoLock)
            {
                SystemInfoStruct existingSystemInfo;
                if (_systems.TryGetValue(systemId, out existingSystemInfo))
                {
                    if (!(existingSystemInfo.vehiclePhysicalId != vehicleId ||
                          existingSystemInfo.isOnline != isOnline ||
                          existingSystemInfo.status != status ||
                          existingSystemInfo.missionId != missionId ||
                          existingSystemInfo.communicationLink != communicationLink ||
                          existingSystemInfo.IPAddress != IPAddress))
                    {
                        newSystemInfo = null;
                    }
                    else if (existingSystemInfo.isOnline != isOnline)
                    {
                        onlineStatusChanged = true;
                    }
                }
                else
                {
                    onlineStatusChanged = true;
                }

                if (newSystemInfo != null)
                {
                    _systems[systemId] = newSystemInfo;
                }
            }

            // Notify about changes
            if (newSystemInfo != null)
            {
                List <string> urlsToNotify;

                lock (_sessionLock)
                {
                    urlsToNotify = _sessions.Values.Where(s => s.NotificationEnabled && !string.IsNullOrEmpty(s.NotificationUrl)).Select(s => s.NotificationUrl).Distinct().ToList();
                }

                if (urlsToNotify.Count > 0)
                {
                    DataPackageTests.T2GServiceInterface.Notification.systemInfoStruct systemInfoNotification = newSystemInfo.ConvertToNotification();
                    foreach (string url in urlsToNotify)
                    {
                        EndpointAddress address = new EndpointAddress(url);
                        using (NotificationClient client = new NotificationClient("NotificationClient", address))
                        {
                            try
                            {
                                client.Open();
                                client.onSystemChangedNotification(systemInfoNotification);
                            }
                            finally
                            {
                                if (client.State == CommunicationState.Faulted)
                                {
                                    client.Abort();
                                }
                            }
                        }
                    }
                }

                if (onlineStatusChanged)
                {
                    OnlineStatusChangedHandler handlers = OnlineStatusChanged;
                    if (handlers != null)
                    {
                        handlers(newSystemInfo.systemId, newSystemInfo.isOnline);
                    }
                }
            }
        }