示例#1
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);
        }
示例#2
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);
        }