private bool Authorize(out Identity identity, ProviderSession session)
        {
            identity = null;
            EventQueue tokenEventQueue = new EventQueue();

            session.GenerateToken(new CorrelationID(tokenEventQueue), tokenEventQueue);
            String    token = null;
            const int timeoutMilliSeonds = 10000;
            Event     eventObj           = tokenEventQueue.NextEvent(timeoutMilliSeonds);

            if (eventObj.Type == Event.EventType.TOKEN_STATUS)
            {
                foreach (Message msg in eventObj)
                {
                    System.Console.WriteLine(msg.ToString());
                    if (msg.MessageType == TOKEN_SUCCESS)
                    {
                        token = msg.GetElementAsString("token");
                    }
                }
            }
            if (token == null)
            {
                System.Console.WriteLine("Failed to get token");
                return(false);
            }

            const string authServiceName = "//blp/apiauth";

            if (session.OpenService(authServiceName))
            {
                Service authService = session.GetService(authServiceName);
                Request authRequest = authService.CreateAuthorizationRequest();
                authRequest.Set("token", token);

                EventQueue authEventQueue = new EventQueue();
                identity = session.CreateIdentity();
                session.SendAuthorizationRequest(authRequest, identity, authEventQueue, new CorrelationID(identity));

                eventObj = authEventQueue.NextEvent();
                if (eventObj.Type == Event.EventType.RESPONSE ||
                    eventObj.Type == Event.EventType.PARTIAL_RESPONSE ||
                    eventObj.Type == Event.EventType.REQUEST_STATUS)
                {
                    foreach (Message msg in eventObj)
                    {
                        System.Console.WriteLine(msg.ToString());
                        if (msg.MessageType == AUTHORIZATION_SUCCESS)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        private void ServerRun(ProviderSession providerSession)
        {
            Console.WriteLine("Server is starting------");
            if (!providerSession.Start())
            {
                Console.Error.WriteLine("Failed to start server session");
                return;
            }

            Identity identity = null;

            if (d_authOptions != null)
            {
                bool isAuthorized = false;
                identity = providerSession.CreateIdentity();
                if (providerSession.OpenService("//blp/apiauth"))
                {
                    Service authService = providerSession.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, providerSession, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    Console.Error.WriteLine("No authorization");
                    return;
                }
            }

            if (!providerSession.RegisterService(d_service, identity))
            {
                Console.Error.WriteLine("Failed to register " + d_service);
                return;
            }
        }
        public void Run(String[] args)         //throws Exception
        {
            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;

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

            ProviderSession session = new ProviderSession(
                sessionOptions,
                ProcessEvent);

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

            Identity identity = null;

            if (d_authOptions != null)
            {
                bool isAuthorized = false;
                identity = session.CreateIdentity();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, session, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    System.Console.Error.WriteLine("No authorization");
                    return;
                }
            }

            TopicList topicList = new TopicList();

            topicList.Add(
                d_service + "/1245/4/5",
                new CorrelationID(new MyStream("1245/4/5")));
            topicList.Add(
                d_service + "/330/1/1",
                new CorrelationID(new MyStream("330/1/1")));

            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)

            Publish(topicList, session);
        }
Ejemplo n.º 4
0
        public void Run(String[] args)
        {
            if (!ParseCommandLine(args))
            {
                return;
            }

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

            SessionOptions sessionOptions = new SessionOptions();

            sessionOptions.ServerAddresses            = servers;
            sessionOptions.AuthenticationOptions      = d_authOptions;
            sessionOptions.AutoRestartOnDisconnection = true;
            sessionOptions.NumStartAttempts           = d_serverHosts.Count;

            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())
            {
                Console.Error.WriteLine("Failed to start session");
                return;
            }

            Identity identity = null;

            if (d_authOptions != null)
            {
                bool isAuthorized = false;
                identity = session.CreateIdentity();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, session, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    System.Console.Error.WriteLine("No authorization");
                    return;
                }
            }

            TopicList topicList = new TopicList();

            topicList.Add(
                d_serviceName + "/" + d_topic,
                new CorrelationID(new MyStream(d_topic)));

            session.CreateTopics(
                topicList,
                ResolveMode.AUTO_REGISTER_SERVICES,
                identity);

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

            for (int i = 0; i < topicList.Size; ++i)
            {
                if (topicList.StatusAt(i) == TopicList.TopicStatus.CREATED)
                {
                    Topic    topic  = session.GetTopic(topicList.MessageAt(i));
                    MyStream stream = (MyStream)topicList.CorrelationIdAt(i).Object;
                    stream.SetTopic(topic);
                    myStreams.Add(stream);
                }
            }

            PublishEvents(session, myStreams);

            session.Stop();
        }
        public void Run(string[] args)         //throws Exception
        {
            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;

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

            ProviderSession session = new ProviderSession(
                sessionOptions,
                processEvent);

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

            Identity identity = null;

            if (d_authOptions != null)
            {
                bool isAuthorized = false;
                identity = session.CreateIdentity();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, session, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    System.Console.Error.WriteLine("No authorization");
                    return;
                }
            }

            ServiceRegistrationOptions serviceRegistrationOptions = new ServiceRegistrationOptions();

            serviceRegistrationOptions.GroupId         = d_groupId;
            serviceRegistrationOptions.ServicePriority = d_priority;

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

            //Publishing events for the active topics of the designated service.
            PublishEvents(session);

            session.Stop();
        }
        public void Run(String[] args)
        {
            if (!ParseCommandLine(args))
            {
                return;
            }

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

            SessionOptions sessionOptions = new SessionOptions();

            sessionOptions.ServerAddresses            = servers;
            sessionOptions.AuthenticationOptions      = d_authOptions;
            sessionOptions.AutoRestartOnDisconnection = true;
            sessionOptions.NumStartAttempts           = d_serverHosts.Count;

            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())
            {
                Console.Error.WriteLine("Failed to start session");
                return;
            }

            Identity identity = null;

            if (d_authOptions != null)
            {
                bool isAuthorized = false;
                identity = session.CreateIdentity();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, session, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    System.Console.Error.WriteLine("No authorization");
                    return;
                }
            }

            TopicList topicList = new TopicList();

            topicList.Add(
                d_serviceName + "/ticker/929903DF6 Corp",
                new CorrelationID(new MyStream("AUDEUR Curncy")));
            topicList.Add(
                d_serviceName + "/ticker/EC070336 Corp",
                new CorrelationID(new MyStream("EC070336 Corp")));
            topicList.Add(
                d_serviceName + "/ticker/6832348A9 Corp",
                new CorrelationID(new MyStream("6832348A9 Corp")));

            session.CreateTopics(
                topicList,
                ResolveMode.AUTO_REGISTER_SERVICES,
                identity);

            Service service = session.GetService(d_serviceName);

            if (service == null)
            {
                System.Console.Error.WriteLine("Open service failed: " + d_serviceName);
                return;
            }

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

            for (int i = 0; i < topicList.Size; ++i)
            {
                if (topicList.StatusAt(i) == TopicList.TopicStatus.CREATED)
                {
                    Topic    topic  = session.GetTopic(topicList.MessageAt(i));
                    MyStream stream = (MyStream)topicList.CorrelationIdAt(i).Object;
                    stream.SetTopic(topic);
                    myStreams.Add(stream);
                }
            }

            int iteration = 0;

            while (iteration++ < d_maxEvents)
            {
                if (!d_running)
                {
                    break;
                }
                Event          eventObj       = service.CreatePublishEvent();
                EventFormatter eventFormatter = new EventFormatter(eventObj);

                foreach (MyStream stream in myStreams)
                {
                    eventFormatter.AppendMessage("MarketData", stream.GetTopic());
                    eventFormatter.SetElement(BID, stream.GetBid());
                    eventFormatter.SetElement(ASK, stream.GetAsk());
                    eventFormatter.SetElement(BID_SIZE, 1200);
                    eventFormatter.SetElement(ASK_SIZE, 1400);
                }

                System.Console.WriteLine(System.DateTime.Now.ToString() + " -");

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

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

            session.Stop();
        }
Ejemplo n.º 7
0
        public void Run(string[] args)         //throws Exception
        {
            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;

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

            ProviderSession session = new ProviderSession(
                sessionOptions,
                processEvent);

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

            Identity identity = null;

            if (d_authOptions.Length != 0)
            {
                Object tokenResponseMonitor = new Object();
                lock (tokenResponseMonitor)
                {
                    session.GenerateToken(new CorrelationID(tokenResponseMonitor));
                    long waitTime             = 10 * 1000;
                    long tokenResponseTimeout = System.DateTime.Now.Ticks / 10000 + waitTime;
                    while (d_tokenGenerationResponse == null && waitTime > 0)
                    {
                        Monitor.Wait(tokenResponseMonitor, (int)waitTime);
                        waitTime = tokenResponseTimeout - System.DateTime.Now.Ticks / 10000;
                    }
                    if (d_tokenGenerationResponse == null)
                    {
                        System.Console.Error.WriteLine("Timeout waiting for token");
                        System.Environment.Exit(1);
                    }
                    else if (d_tokenGenerationResponse == false || d_token == null)
                    {
                        System.Console.Error.WriteLine("Token generation failed");
                        System.Environment.Exit(1);
                    }
                }

                Object authorizationResponseMonitor = new Object();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    Request authRequest = authService.CreateAuthorizationRequest();
                    authRequest.Set("token", d_token);
                    identity = session.CreateIdentity();
                    d_authorizationResponseCorrelationId =
                        new CorrelationID(authorizationResponseMonitor);
                    lock (authorizationResponseMonitor)
                    {
                        session.SendAuthorizationRequest(
                            authRequest,
                            identity,
                            d_authorizationResponseCorrelationId);
                        long waitTime = 60 * 1000;
                        long authorizationResponseTimeout = System.DateTime.Now.Ticks / 10000 + waitTime;
                        while (d_authorizationResponse == null && waitTime > 0)
                        {
                            Monitor.Wait(authorizationResponseMonitor, 1000);
                            waitTime = authorizationResponseTimeout - System.DateTime.Now.Ticks / 10000;
                        }
                        if (d_authorizationResponse == null)
                        {
                            System.Console.Error.WriteLine("Timeout waiting for authorization");
                            System.Environment.Exit(1);
                        }
                        else if (d_authorizationResponse == false)
                        {
                            System.Console.Error.WriteLine("Authorization failed");
                            System.Environment.Exit(1);
                        }
                    }
                }
            }

            //Registering for subscription start and stop messages.
            if (!session.RegisterService(d_service, d_identity))
            {
                Console.WriteLine("Failed to register " + d_service);
                return;
            }
            Console.WriteLine("Service registered " + d_service);


            //Publishing events for the active topics of the designated service.
            PublishEvents(session);

            session.Stop();
        }
        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           = d_numRetry;

            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)
            {
                bool isAuthorized = false;
                identity = session.CreateIdentity();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, session, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    System.Console.Error.WriteLine("No authorization");
                    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 + "/ticker/" + 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);

            if (service == null)
            {
                System.Console.Error.WriteLine("Service registration failed: " + d_service);
                return;
            }

            // 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)
            {
                if (!d_running)
                {
                    break;
                }
                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())
                    {
                        System.Console.WriteLine("[WARNING] Publishing on an inactive topic.");
                    }
                    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();
        }
Ejemplo n.º 9
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;

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

            ProviderSession session = new ProviderSession(sessionOptions, ProcessEvent);

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

            Identity identity = null;

            if (d_authOptions != null)
            {
                bool isAuthorized = false;
                identity = session.CreateIdentity();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    if (Authorize(authService, identity, session, new CorrelationID()))
                    {
                        isAuthorized = true;
                    }
                }
                if (!isAuthorized)
                {
                    System.Console.Error.WriteLine("No authorization");
                    return;
                }
            }

            ServiceRegistrationOptions serviceRegistrationOptions
                = new ServiceRegistrationOptions();

            serviceRegistrationOptions.GroupId         = d_groupId;
            serviceRegistrationOptions.ServicePriority = d_priority;


            if (d_useSsc)
            {
                Console.WriteLine(
                    String.Format(
                        "Activating sub service code range [{0}, {1}] "
                        + "@ priority: {2}",
                        d_sscBegin,
                        d_sscEnd,
                        d_sscPriority));
                try {
                    serviceRegistrationOptions.AddActiveSubServiceCodeRange(
                        d_sscBegin,
                        d_sscEnd,
                        d_sscPriority);
                } catch (Exception e) {
                    Console.WriteLine(
                        "FAILED to add active sub service codes. Exception " + e);
                }
            }

            bool wantAsyncRegisterService = true;

            if (wantAsyncRegisterService)
            {
                Object        registerServiceResponseMonitor = new Object();
                CorrelationID registerCID = new CorrelationID(registerServiceResponseMonitor);
                lock (registerServiceResponseMonitor)
                {
                    if (d_verbose > 0)
                    {
                        Console.WriteLine("start registerServiceAsync, cid = " + registerCID);
                    }
                    session.RegisterServiceAsync(
                        d_service,
                        identity,
                        registerCID,
                        serviceRegistrationOptions);
                    for (int i = 0; d_registerServiceResponse == null && i < 10; ++i)
                    {
                        Monitor.Wait(registerServiceResponseMonitor, 1000);
                    }
                }
            }
            else
            {
                bool result = session.RegisterService(
                    d_service,
                    identity,
                    serviceRegistrationOptions);
                d_registerServiceResponse = result;
            }

            Service service = session.GetService(d_service);

            if (service != null && d_registerServiceResponse == true)
            {
                Console.WriteLine("Service registered: " + d_service);
            }
            else
            {
                Console.Error.WriteLine("Service registration failed: " + d_service);
                return;
            }

            // Dump schema for the service
            if (d_verbose > 1)
            {
                Console.WriteLine("Schema for service:" + d_service);
                for (int i = 0; i < service.NumEventDefinitions; ++i)
                {
                    SchemaElementDefinition eventDefinition = service.GetEventDefinition(i);
                    Console.WriteLine(eventDefinition);
                }
            }

            // Now we will start publishing
            int  eventCount = 0;
            long tickCount  = 1;

            while (d_running)
            {
                Event eventObj;
                lock (d_topicSet)
                {
                    if (d_topicSet.Count == 0)
                    {
                        Monitor.Wait(d_topicSet, 100);
                    }

                    if (d_topicSet.Count == 0)
                    {
                        continue;
                    }

                    eventObj = service.CreatePublishEvent();
                    EventFormatter eventFormatter = new EventFormatter(eventObj);

                    bool publishNull = false;
                    if (d_clearInterval > 0 && eventCount == d_clearInterval)
                    {
                        eventCount  = 0;
                        publishNull = true;
                    }

                    foreach (Topic topic in d_topicSet.Keys)
                    {
                        if (!topic.IsActive())
                        {
                            System.Console.WriteLine("[WARNING] Publishing on an inactive topic.");
                        }
                        eventFormatter.AppendMessage("MarketDataEvents", topic);
                        if (publishNull)
                        {
                            eventFormatter.SetElementNull("HIGH");
                            eventFormatter.SetElementNull("LOW");
                        }
                        else
                        {
                            ++eventCount;
                            if (1 == tickCount)
                            {
                                eventFormatter.SetElement("BEST_ASK", 100.0);
                            }
                            else if (2 == tickCount)
                            {
                                eventFormatter.SetElement("BEST_BID", 99.0);
                            }
                            eventFormatter.SetElement("HIGH", 100 + tickCount * 0.01);
                            eventFormatter.SetElement("LOW", 100 - tickCount * 0.005);
                            ++tickCount;
                        }
                    }
                }

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

                session.Publish(eventObj);
                Thread.Sleep(10 * 1000);
                if (tickCount % 3 == 0)
                {
                    Deactivate(session);
                    Thread.Sleep(10 * 1000);
                    Activate(session);
                }
            }

            session.Stop();
        }
Ejemplo n.º 10
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.Length != 0)
            {
                Object tokenResponseMonitor = new Object();
                lock (tokenResponseMonitor)
                {
                    session.GenerateToken(new CorrelationID(tokenResponseMonitor));
                    long waitTime             = 10 * 1000;
                    long tokenResponseTimeout = System.DateTime.Now.Ticks / 10000 + waitTime;
                    while (d_tokenGenerationResponse == null && waitTime > 0)
                    {
                        Monitor.Wait(tokenResponseMonitor, (int)waitTime);
                        waitTime = tokenResponseTimeout - System.DateTime.Now.Ticks / 10000;
                    }
                    if (d_tokenGenerationResponse == null)
                    {
                        System.Console.Error.WriteLine("Timeout waiting for token");
                        System.Environment.Exit(1);
                    }
                    else if (d_tokenGenerationResponse == false || d_token == null)
                    {
                        System.Console.Error.WriteLine("Token generation failed");
                        System.Environment.Exit(1);
                    }
                }

                Object authorizationResponseMonitor = new Object();
                if (session.OpenService("//blp/apiauth"))
                {
                    Service authService = session.GetService("//blp/apiauth");
                    Request authRequest = authService.CreateAuthorizationRequest();
                    authRequest.Set("token", d_token);
                    identity = session.CreateIdentity();
                    d_authorizationResponseCorrelationId =
                        new CorrelationID(authorizationResponseMonitor);
                    lock (authorizationResponseMonitor)
                    {
                        session.SendAuthorizationRequest(
                            authRequest,
                            identity,
                            d_authorizationResponseCorrelationId);
                        long waitTime = 60 * 1000;
                        long authorizationResponseTimeout = System.DateTime.Now.Ticks / 10000 + waitTime;
                        while (d_authorizationResponse == null && waitTime > 0)
                        {
                            Monitor.Wait(authorizationResponseMonitor, 1000);
                            waitTime = authorizationResponseTimeout - System.DateTime.Now.Ticks / 10000;
                        }
                        if (d_authorizationResponse == null)
                        {
                            System.Console.Error.WriteLine("Timeout waiting for authorization");
                            System.Environment.Exit(1);
                        }
                        else if (d_authorizationResponse == false)
                        {
                            System.Console.Error.WriteLine("Authorization failed");
                            System.Environment.Exit(1);
                        }
                    }
                }
            }

            ServiceRegistrationOptions serviceRegistrationOptions = new ServiceRegistrationOptions();

            serviceRegistrationOptions.GroupId         = d_groupId;
            serviceRegistrationOptions.ServicePriority = d_priority;

            bool wantAsyncRegisterService = true;

            if (wantAsyncRegisterService)
            {
                Object        registerServiceResponseMonitor = new Object();
                CorrelationID registerCID = new CorrelationID(registerServiceResponseMonitor);
                lock (registerServiceResponseMonitor)
                {
                    if (d_verbose > 0)
                    {
                        System.Console.WriteLine("start registerServiceAsync, cid = " + registerCID);
                    }
                    session.RegisterServiceAsync(d_service, identity, registerCID, serviceRegistrationOptions);
                    for (int i = 0; d_registerServiceResponse == null && i < 10; ++i)
                    {
                        Monitor.Wait(registerServiceResponseMonitor, 1000);
                    }
                }
            }
            else
            {
                bool result = session.RegisterService(d_service, identity, serviceRegistrationOptions);
                d_registerServiceResponse = result;
            }

            Service service = session.GetService(d_service);

            if (service != null && d_registerServiceResponse == true)
            {
                System.Console.WriteLine("Service registered: " + d_service);
            }
            else
            {
                System.Console.Error.WriteLine("Service registration failed: " + d_service);
                System.Environment.Exit(1);
            }

            // Dump schema for the service
            if (d_verbose > 1)
            {
                System.Console.WriteLine("Schema for service:" + d_service);
                for (int i = 0; i < service.NumEventDefinitions; ++i)
                {
                    SchemaElementDefinition eventDefinition = service.GetEventDefinition(i);
                    System.Console.WriteLine(eventDefinition);
                }
            }

            // Now we will start publishing

            long tickCount = 1;

            while (g_running)
            {
                Event eventObj;
                lock (d_topicSet)
                {
                    if (d_topicSet.Count == 0)
                    {
                        Monitor.Wait(d_topicSet, 100);
                    }

                    if (d_topicSet.Count == 0)
                    {
                        continue;
                    }

                    eventObj = service.CreatePublishEvent();
                    EventFormatter eventFormatter = new EventFormatter(eventObj);

                    foreach (Topic topic in d_topicSet.Keys)
                    {
                        if (!topic.IsActive())
                        {
                            continue;
                        }
                        eventFormatter.AppendMessage("MarketDataEvents", topic);
                        if (1 == tickCount)
                        {
                            eventFormatter.SetElement("BEST_ASK", 100.0);
                        }
                        else if (2 == tickCount)
                        {
                            eventFormatter.SetElement("BEST_BID", 99.0);
                        }
                        eventFormatter.SetElement("HIGH", 100 + tickCount * 0.01);
                        eventFormatter.SetElement("LOW", 100 - tickCount * 0.005);
                        ++tickCount;
                    }
                }

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

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

            session.Stop();
        }