private void Publish(TopicList topicList, ProviderSession session)
        {
            List <MyStream> myStreams = new List <MyStream>();

            for (int i = 0; i < topicList.Size; ++i)
            {
                if (topicList.StatusAt(i) == TopicList.TopicStatus.CREATED)
                {
                    Message  message = topicList.MessageAt(i);
                    Topic    topic   = session.GetTopic(message);
                    MyStream stream  = (MyStream)topicList.CorrelationIdAt(i).Object;
                    stream.setTopic(topic);
                    myStreams.Add(stream);
                    Console.WriteLine("Topic created: " + stream.getId());
                }
            }

            Service pubService = session.GetService(d_service);

            if (pubService == null)
            {
                Console.Error.WriteLine("service unavailable");
                return;
            }

            // Now we will start publishing
            Event          eventObj       = pubService.CreatePublishEvent();
            EventFormatter eventFormatter = new EventFormatter(eventObj);

            for (int index = 0; index < myStreams.Count; index++)
            {
                MyStream stream = (MyStream)myStreams[index];

                eventFormatter.AppendRecapMessage(stream.getTopic(), null);
                eventFormatter.SetElement("numRows", 25);
                eventFormatter.SetElement("numCols", 80);
                eventFormatter.PushElement("rowUpdate");
                for (int i = 1; i <= 5; ++i)
                {
                    eventFormatter.AppendElement();
                    eventFormatter.SetElement("rowNum", i);
                    eventFormatter.PushElement("spanUpdate");
                    eventFormatter.AppendElement();
                    eventFormatter.SetElement("startCol", 1);
                    eventFormatter.SetElement("length", 10);
                    eventFormatter.SetElement("text", "INITIAL");
                    eventFormatter.SetElement("fgColor", "RED");
                    eventFormatter.PushElement("attr");
                    eventFormatter.AppendValue("UNDERLINE");
                    eventFormatter.AppendValue("BLINK");
                    eventFormatter.PopElement();
                    eventFormatter.PopElement();
                    eventFormatter.PopElement();
                    eventFormatter.PopElement();
                }
                eventFormatter.PopElement();
            }
            if (d_verbose > 0)
            {
                PrintMessage(eventObj);
            }
            session.Publish(eventObj);

            while (d_running)
            {
                eventObj       = pubService.CreatePublishEvent();
                eventFormatter = new EventFormatter(eventObj);

                for (int index = 0; index < myStreams.Count; index++)
                {
                    MyStream stream = (MyStream)myStreams[index];
                    eventFormatter.AppendMessage("RowUpdate", stream.getTopic());
                    eventFormatter.SetElement("rowNum", 1);
                    eventFormatter.PushElement("spanUpdate");
                    eventFormatter.AppendElement();
                    eventFormatter.SetElement("startCol", 1);
                    String text = System.DateTime.Now.ToString();
                    eventFormatter.SetElement("length", text.Length);
                    eventFormatter.SetElement("text", text);
                    eventFormatter.PopElement();
                    eventFormatter.AppendElement();
                    eventFormatter.SetElement("startCol", text.Length + 10);
                    text = System.DateTime.Now.ToString();
                    eventFormatter.SetElement("length", text.Length);
                    eventFormatter.SetElement("text", text);
                    eventFormatter.PopElement();
                    eventFormatter.PopElement();
                }

                if (d_verbose > 0)
                {
                    PrintMessage(eventObj);
                }
                session.Publish(eventObj);
                Thread.Sleep(10 * 1000);
            }
        }
コード例 #2
0
        public void Run(String[] args)
        {
            if (!ParseCommandLine(args))
            {
                return;
            }

            SessionOptions.ServerAddress[] servers = new SessionOptions.ServerAddress[d_hosts.Count];
            for (int i = 0; i < d_hosts.Count; ++i)
            {
                servers[i] = new SessionOptions.ServerAddress(d_hosts[i], d_port);
            }

            SessionOptions sessionOptions = new SessionOptions();

            sessionOptions.ServerAddresses            = servers;
            sessionOptions.AuthenticationOptions      = d_authOptions;
            sessionOptions.AutoRestartOnDisconnection = true;
            sessionOptions.NumStartAttempts           = servers.Length;

            System.Console.Write("Connecting to");
            foreach (SessionOptions.ServerAddress server in sessionOptions.ServerAddresses)
            {
                System.Console.Write(" " + server);
            }
            System.Console.WriteLine();

            ProviderSession session = new ProviderSession(sessionOptions, ProcessEvent);

            if (!session.Start())
            {
                System.Console.Error.WriteLine("Failed to start session");
                return;
            }

            Identity identity = null;

            if (d_authOptions != null)
            {
                if (!Authorize(out identity, session))
                {
                    return;
                }
            }

            if (d_groupId != null)
            {
                // NOTE: will perform explicit service registration here, instead of letting
                //       createTopics do it, as the latter approach doesn't allow for custom
                //       ServiceRegistrationOptions
                ServiceRegistrationOptions serviceRegistrationOptions = new ServiceRegistrationOptions();
                serviceRegistrationOptions.GroupId         = d_groupId;
                serviceRegistrationOptions.ServicePriority = d_priority;

                if (!session.RegisterService(d_service, identity, serviceRegistrationOptions))
                {
                    System.Console.Write("Failed to register " + d_service);
                    return;
                }
            }

            TopicList topicList = new TopicList();

            for (int i = 0; i < d_topics.Count; i++)
            {
                topicList.Add(
                    d_service + "/" + d_topics[i],
                    new CorrelationID(new MyStream(d_topics[i])));
            }

            session.CreateTopics(
                topicList,
                ResolveMode.AUTO_REGISTER_SERVICES,
                identity);
            // createTopics() is synchronous, topicList will be updated
            // with the results of topic creation (resolution will happen
            // under the covers)

            List <MyStream> myStreams = new List <MyStream>();

            for (int i = 0; i < topicList.Size; ++i)
            {
                MyStream stream = (MyStream)topicList.CorrelationIdAt(i).Object;
                if (topicList.StatusAt(i) == TopicList.TopicStatus.CREATED)
                {
                    Message msg = topicList.MessageAt(i);
                    stream.setTopic(session.GetTopic(msg));
                    myStreams.Add(stream);
                    System.Console.WriteLine("Topic created: " + topicList.TopicStringAt(i));
                }
                else
                {
                    System.Console.WriteLine("Stream '" + stream.getId()
                                             + "': topic not resolved, status = " + topicList.StatusAt(i));
                }
            }
            Service service = session.GetService(d_service);

            // Now we will start publishing
            Name eventName = Name.GetName("MarketDataEvents");
            Name high      = Name.GetName("HIGH");
            Name low       = Name.GetName("LOW");
            long tickCount = 1;

            for (int eventCount = 0; eventCount < d_maxEvents; ++eventCount)
            {
                Event          eventObj       = service.CreatePublishEvent();
                EventFormatter eventFormatter = new EventFormatter(eventObj);

                for (int index = 0; index < myStreams.Count; index++)
                {
                    Topic topic = myStreams[index].getTopic();
                    if (!topic.IsActive())
                    {
                        continue;
                    }
                    eventFormatter.AppendMessage(eventName, topic);
                    if (1 == tickCount)
                    {
                        eventFormatter.SetElement("OPEN", 1.0);
                    }
                    else if (2 == tickCount)
                    {
                        eventFormatter.SetElement("BEST_BID", 3.0);
                    }
                    eventFormatter.SetElement(high, tickCount * 1.0);
                    eventFormatter.SetElement(low, tickCount * 0.5);
                    ++tickCount;
                }

                foreach (Message msg in eventObj)
                {
                    System.Console.WriteLine(msg);
                }

                session.Publish(eventObj);
                Thread.Sleep(2 * 1000);
            }

            session.Stop();
        }