private void CreateSession(List <SupportedProtocol> protocols)
        {
            MessageHeader header = new MessageHeader();

            header.Protocol      = (int)Protocols.Core;
            header.MessageType   = 1;
            header.MessageId     = EtpHelper.NextMessageId;
            header.MessageFlags  = 0;
            header.CorrelationId = 0;



            List <ISupportedProtocol> requestedProtocols = new List <ISupportedProtocol>();

            requestedProtocols.AddRange(protocols);

            //var requestSession = new RequestSession()
            //{
            //    ApplicationName = m_ApplicationName,
            //    ApplicationVersion = m_ApplicationVersion,
            //    RequestedProtocols = requestedProtocols.Cast<SupportedProtocol>().ToList(),
            //    SupportedObjects = new List<string>()
            //};

            string message = $"\nRequest: [Protocol {header.Protocol} MessageType {header.MessageType}]";

            Message?.Invoke(message, 0, TraceLevel.Info);
            m_client.Handler <ICoreClient>().RequestSession(m_ApplicationName, m_ApplicationVersion, requestedProtocols);
        }
Exemplo n.º 2
0
        public async Task Connect(string url, string username, string password, List <SupportedProtocol> protocols, CancellationToken token)
        {
            try
            {
                var auth = Authorization.Basic(username, password);
                m_client = EtpFactory.CreateClient(WebSocketType.WebSocket4Net, url, "ShellSquare ETP Client", "1.4.1.1", SUBPROTOCOL, auth);
                var protocol1Info = protocols.Where(x => x.Protocol == 1).FirstOrDefault();
                if (protocol1Info.Role.Equals("producer"))
                {
                    m_client.Register <IChannelStreamingConsumer, ChannelStreamingConsumerHandler>();
                    m_client.Handler <IChannelStreamingConsumer>().OnChannelMetadata   += HandleChannelMetadata;
                    m_client.Handler <IChannelStreamingConsumer>().OnProtocolException += HandleProtocolException;
                    m_client.Handler <IChannelStreamingConsumer>().OnChannelData       += HandleChannelData;
                }
                else
                {
                    m_client.Register <IChannelStreamingProducer, ChannelStreamingProducerHandler>();
                }

                m_client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>();
                m_client.Register <IStoreCustomer, StoreCustomerHandler>();
                m_client.Handler <ICoreClient>().OnOpenSession += HandleOpenSession;
                await CreateSession(protocols);

                await m_client.OpenAsync();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    throw new Exception($"{ex.Message} {ex.InnerException.Message}");
                }
                throw;
            }
        }
Exemplo n.º 3
0
        public async Task Describe(List <string> uris)
        {
            lock (m_ChannelStreamingInfo)
            {
                m_ChannelStreamingInfo.Clear();

                m_LogCurveEml.Clear();
                m_LogCurveEml.AddRange(uris);
            }
            MessageHeader header = new MessageHeader();

            header.Protocol      = (int)Protocols.ChannelStreaming;
            header.MessageType   = 1;
            header.MessageId     = EtpHelper.NextMessageId;
            header.MessageFlags  = 3;
            header.CorrelationId = 10;

            var channelDescribe = new ChannelDescribe()
            {
                Uris = uris
            };

            string message = $"\nRequest: [Protocol {header.Protocol} MessageType {header.MessageType}]";

            Message?.Invoke(message, 0, TraceLevel.Info);

            m_client.Handler <IChannelStreamingConsumer>().ChannelDescribe(uris);
        }
 private async Task SendChannelDataActual(List <ChannelStreamingInfo> lstChannels, IEtpClient m_client)
 {
     var handler = m_client.Handler <IChannelStreamingProducer>();
     // index = index + 1;
     await Task.Run(async() =>
     {
         var receivedTime     = DateTime.UtcNow;
         var timediff         = receivedTime - m_Time;
         MessageHeader header = new MessageHeader();
         header.Protocol      = (int)Protocols.ChannelStreaming;
         header.MessageType   = 3;
         header.MessageId     = EtpHelper.NextMessageId;
         header.MessageFlags  = 0;
         header.CorrelationId = 0;
         var recordData       = Activator.CreateInstance <ChannelData>();
         recordData.Data      = new List <DataItem>();
         Random random        = new Random();
         foreach (var item in lstChannels)
         {
             TimeSpan t  = (receivedTime - m_Epoch);
             index       = (long)t.TotalMilliseconds;
             DataItem d  = new DataItem();
             d.ChannelId = item.ChannelId;
             d.Indexes   = new List <long>();
             d.Indexes.Add(index);
             d.Value           = new DataValue();
             d.Value.Item      = Math.Round(random.NextDouble() * 1000, 2);//random.Next();
             d.ValueAttributes = new List <DataAttribute>();
             recordData.Data.Add(d);
         }
         var a          = m_client.Handler <IChannelStreamingProducer>().ChannelData(header, recordData.Data);
         string message = $"\nRequest: [Protocol {header.Protocol} MessageType {header.MessageType}]";
         Message?.Invoke(message + "\nChannel Data processed " + (receivedTime - m_Epoch).ToString(), 0, TraceLevel.Info);
     });
 }
        public async Task Connect(string url, string username, string password, CancellationToken token)
        {
            try
            {
                m_Time = DateTime.UtcNow;

                var protocols = new List <SupportedProtocol>();
                SupportedProtocol p;
                p = EtpHelper.ToSupportedProtocol(Protocols.ChannelStreaming, "consumer");
                protocols.Add(p);

                p = EtpHelper.ToSupportedProtocol(Protocols.Discovery, "store");
                protocols.Add(p);


                var auth = Authorization.Basic(username, password);
                m_client = EtpFactory.CreateClient(WebSocketType.WebSocket4Net, url, m_ApplicationName, m_ApplicationVersion, SUBPROTOCOL, auth);

                m_client.Register <IChannelStreamingProducer, ChannelStreamingProducerHandler>();

                m_client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>();
                m_client.Register <IStoreCustomer, StoreCustomerHandler>();
                m_client.Handler <ICoreClient>().OnOpenSession += HandleOpenSession;
                CreateSession(protocols);
                await m_client.OpenAsync();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    throw new Exception($"{ex.Message} {ex.InnerException.Message}");
                }
                throw;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the resources and asserts.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="errorCode">The error code.</param>
        /// <param name="exist">if set to <c>true</c> resources exist; otherwise, <c>false</c>.</param>
        /// <returns>A collection of resources.</returns>
        protected async Task <List <ProtocolEventArgs <GetResourcesResponse, string> > > GetResourcesAndAssert(EtpUri uri, EtpErrorCodes?errorCode = null, bool exist = true)
        {
            var handler = _client.Handler <IDiscoveryCustomer>();

            // Register event handler for GetResourcesResponse
            var onGetResourcesResponse = HandleMultiPartAsync <GetResourcesResponse, string>(
                x => handler.OnGetResourcesResponse += x);

            // Register exception hanlder
            var onProtocolException = HandleAsync <IProtocolException>(x => handler.OnProtocolException += x);

            // Send GetResources message for specified URI
            var messageId = handler.GetResources(uri);
            List <ProtocolEventArgs <GetResourcesResponse, string> > args = null;

            var tokenSource = new CancellationTokenSource();

            var taskList = new List <Task>()
            {
                WaitFor(onGetResourcesResponse, tokenSource.Token),
                WaitFor(onProtocolException, tokenSource.Token)
            };

            // Start each event
            taskList.ForEach(task => task.Start());

            // Wait for a task to finish
            await Task.WhenAny(taskList);

            // Cancel the rest of the task
            tokenSource.Cancel();

            // Wait for the rest to be finished
            await Task.WhenAll(taskList);

            if (onGetResourcesResponse.Status == TaskStatus.RanToCompletion)
            {
                // Wait for GetResourcesResponse
                args = await onGetResourcesResponse;
                Assert.IsNotNull(args);
                Assert.AreEqual(exist, args.Any());

                var folder = ResourceTypes.Folder.ToString();

                // Check Resource URIs
                foreach (var arg in args)
                {
                    VerifyCorrelationId(arg, messageId);
                    Assert.IsNotNull(arg?.Message?.Resource?.Uri);

                    var resourceUri = new EtpUri(arg.Message.Resource.Uri);

                    if (uri == EtpUri.RootUri)
                    {
                        Assert.IsTrue(uri.IsBaseUri);
                    }
                    else if (!folder.EqualsIgnoreCase(arg.Message.Resource.ResourceType))
                    {
                        Assert.AreEqual(uri.Family, resourceUri.Family);
                        Assert.AreEqual(uri.Version, resourceUri.Version);
                    }
                }
            }
            if (onProtocolException.Status == TaskStatus.RanToCompletion)
            {
                var exceptionArgs = onProtocolException.Result;

                // Assert exception details
                Assert.IsNotNull(errorCode);
                Assert.IsNotNull(exceptionArgs?.Message);
                Assert.AreEqual((int)errorCode, exceptionArgs.Message.ErrorCode);
            }

            return(args);
        }