Esempio n. 1
0
        /// <summary>
        /// A new service publisher.
        /// </summary>
        /// <remarks>
        /// The new service publisher will automatically bind to the next available port
        /// (as defined by XFS4IoT.)
        /// </remarks>
        /// <param name="Logger">To use for all logging</param>
        /// <param name="CommandDispatcher">For dispatching incomming command messages</param>
        public ServicePublisher(ILogger Logger)
            : base(typeof(ServicePublisher), Logger)
        {
            Logger.IsNotNull($"Invalid parameter received in the {nameof(ServicePublisher)} constructor. {nameof(Logger)}");

            foreach (int port in XFSConstants.PortRanges)
            {
                try
                {
                    // From the spec, valid URI are like:
                    // wss://Terminal321.ATMNetwork.corporatenet:443/xfs4iot/v1.0
                    // wss://192.168.21.43:5848/xfs4iot/v1.0/CardReader1
                    // We're going to open a HTTP connection first, then upgrade to WSS, hence http://

                    // Service URI is configuration parameter
                    string serverAddressUri = ConfigurationManager.AppSettings.Get(Configurations.ServerAddressUri);
                    if (string.IsNullOrEmpty(serverAddressUri))
                    {
                        serverAddressUri = Configurations.Default.ServerAddressUri;
                    }
                    else
                    {
                        bool result = Regex.IsMatch(serverAddressUri, "^https?://[-_.!~*'()a-z0-9%]+$", RegexOptions.IgnoreCase);
                        result.IsTrue($"Invalid service URI is configured. URI must be with out port number. i.e. http(s)://Terminal321.ATMNetwork.corporatenet and no ");
                    }

                    Uri = new Uri($"{serverAddressUri}:{port}/xfs4iot/v1.0/");

                    string serverAddressWUri = Regex.Replace(serverAddressUri, "^http", "ws", RegexOptions.IgnoreCase);
                    WSUri = new Uri($"{serverAddressWUri}:{port}/xfs4iot/v1.0/");

                    EndpointDetails = new EndpointDetails(serverAddressUri, serverAddressWUri, port);

                    Logger.Log(Constants.Component, $"Attempting to bind to {Uri}");

                    EndPoint = new EndPoint(Uri,
                                            CommandDecoder: CommandDecoder,
                                            CommandDispatcher: this,
                                            Logger);

                    _Services.Add(this);
                    return;
                }
                catch (System.Net.HttpListenerException)
                {
                    continue;
                }
            }
            // If we excape from the loop then we've run out of things to try and
            // we've failed. Time to die.
            Contracts.Fail($"Can't find an XFS4IoT port to listen on");
        }
Esempio n. 2
0
        public TextTerminalServiceProvider(XFS4IoTServer.EndpointDetails EndpointDetails, ITextTerminalDevice Device, ILogger Logger)
            : base(typeof(TextTerminalServiceProvider), Logger)
        {
            EndpointDetails.IsNotNull($"The endpoint details are invalid. {nameof(EndpointDetails)}");
            Device.IsNotNull($"The device interface is an invalid. {nameof(Device)}");

            this.Device = Device;

            (Uri, WSUri) = EndpointDetails.ServiceUri(ServiceClass);

            Logger.Log(Constants.Framework, $"Listening on {Uri}");

            this.EndPoint = new EndPoint(Uri,
                                         CommandDecoder,
                                         this,
                                         Logger);
        }