def resolutionServiceRegistration(providerSession, providerIdentity, serviceName): """ This helper demonstrates how to register a service. This helper assumes the following: * Specified `providerSession` is already started * Specified `providerIdentity` is already authorized if auth is needed or default-constructed if authorization is not required. Args: providerSession (ProviderSession): The session to register services. providerIdentity (Identity): Identity used to verify permissions to provide the service being registered. serviceName (str): Name of the service. Returns: bool: True if service registration succeeds; False otherwise. """ serviceOptions = blpapi.ServiceRegistrationOptions() dummyPriority = 123 serviceOptions.setServicePriority(dummyPriority) serviceOptions.setPartsToRegister( blpapi.ServiceRegistrationOptions.PART_PUBLISHER_RESOLUTION) if not providerSession.registerService(serviceName, providerIdentity, serviceOptions): print("Failed to register {}".format(serviceName)) return False return True
def main(): options = parseCmdLine() # Fill SessionOptions sessionOptions = blpapi.SessionOptions() for idx, host in enumerate(options.hosts): sessionOptions.setServerAddress(host, options.port, idx) sessionOptions.setAuthenticationOptions(options.auth) sessionOptions.setAutoRestartOnDisconnection(True) # NOTE: If running without a backup server, make many attempts to # connect/reconnect to give that host a chance to come back up (the # larger the number, the longer it will take for SessionStartupFailure # to come on startup, or SessionTerminated due to inability to fail # over). We don't have to do that in a redundant configuration - it's # expected at least one server is up and reachable at any given time, # so only try to connect to each server once. sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000) print "Connecting to port %d on %s" % (options.port, " ".join( options.hosts)) PUBLISH_MESSAGE_TYPE = blpapi.Name(options.messageType) myEventHandler = MyEventHandler(options.service, PUBLISH_MESSAGE_TYPE, options.fields, options.eids) # Create a Session session = blpapi.ProviderSession(sessionOptions, myEventHandler.processEvent) # Start a Session if not session.start(): print "Failed to start session." return providerIdentity = session.createIdentity() if options.auth: isAuthorized = False authServiceName = "//blp/apiauth" if session.openService(authServiceName): authService = session.getService(authServiceName) isAuthorized = authorize(authService, providerIdentity, session, blpapi.CorrelationId("auth")) if not isAuthorized: print "No authorization" return serviceOptions = blpapi.ServiceRegistrationOptions() if options.groupId is not None: serviceOptions.setGroupId(options.groupId) serviceOptions.setServicePriority(options.priority) if not session.registerService(options.service, providerIdentity, serviceOptions): print "Failed to register '%s'" % options.service return service = session.getService(options.service) elementDef = service.getEventDefinition(PUBLISH_MESSAGE_TYPE) try: while g_running: event = service.createPublishEvent() with g_condition: while g_availableTopicCount == 0: # Set timeout to 1 - give a chance for CTRL-C g_condition.wait(1) if not g_running: return eventFormatter = blpapi.EventFormatter(event) for topicName, stream in g_streams.iteritems(): if not stream.isAvailable(): continue stream.next() eventFormatter.appendMessage(PUBLISH_MESSAGE_TYPE, stream.topic) stream.fillData(eventFormatter, elementDef) for msg in event: print msg session.publish(event) time.sleep(10) finally: # Stop the session session.stop()
def main(): options = parseCmdLine() # Fill SessionOptions sessionOptions = blpapi.SessionOptions() for idx, host in enumerate(options.hosts): sessionOptions.setServerAddress(host, options.port, idx) sessionOptions.setAuthenticationOptions(options.auth) sessionOptions.setAutoRestartOnDisconnection(True) # NOTE: If running without a backup server, make many attempts to # connect/reconnect to give that host a chance to come back up (the # larger the number, the longer it will take for SessionStartupFailure # to come on startup, or SessionTerminated due to inability to fail # over). We don't have to do that in a redundant configuration - it's # expected at least one server is up and reachable at any given time, # so only try to connect to each server once. sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000) print("Connecting to port %d on %s" % (options.port, " ".join(options.hosts))) PUBLISH_MESSAGE_TYPE = blpapi.Name(options.messageType) myEventHandler = MyEventHandler(options.service, PUBLISH_MESSAGE_TYPE, options.fields, options.eids, options.rssc) # Create a Session session = blpapi.ProviderSession(sessionOptions, myEventHandler.processEvent) # Start a Session if not session.start(): print("Failed to start session.") return providerIdentity = session.createIdentity() if options.auth: isAuthorized = False authServiceName = "//blp/apiauth" if session.openService(authServiceName): authService = session.getService(authServiceName) isAuthorized = authorize(authService, providerIdentity, session, blpapi.CorrelationId("auth")) if not isAuthorized: print("No authorization") return serviceOptions = blpapi.ServiceRegistrationOptions() if options.groupId is not None: serviceOptions.setGroupId(options.groupId) serviceOptions.setServicePriority(options.priority) if options.ssc: sscBegin, sscEnd, sscPriority = map(int, options.ssc.split(",")) print(("Adding active sub service code range [%s, %s] @ %s" % (sscBegin, sscEnd, sscPriority))) try: serviceOptions.addActiveSubServiceCodeRange( sscBegin, sscEnd, sscPriority) except blpapi.Exception as e: print(("FAILED to add active sub service codes." " Exception %s" % e.description())) if not session.registerService(options.service, providerIdentity, serviceOptions): print("Failed to register '%s'" % options.service) return service = session.getService(options.service) elementDef = service.getEventDefinition(PUBLISH_MESSAGE_TYPE) eventCount = 0 try: numPublished = 0 while g_running: event = service.createPublishEvent() with g_condition: while g_availableTopicCount == 0: # Set timeout to 1 - give a chance for CTRL-C g_condition.wait(1) if not g_running: return publishNull = False if (options.clearInterval > 0 and eventCount == options.clearInterval): eventCount = 0 publishNull = True eventFormatter = blpapi.EventFormatter(event) for topicName, stream in g_streams.items(): if not stream.isAvailable(): continue eventFormatter.appendMessage(PUBLISH_MESSAGE_TYPE, stream.topic) if publishNull: stream.fillDataNull(eventFormatter, elementDef) else: eventCount += 1 stream.next() stream.fillData(eventFormatter, elementDef) for msg in event: print(msg) session.publish(event) time.sleep(1) numPublished += 1 if numPublished % 10 == 0: deactivate(options, session) time.sleep(30) activate(options, session) finally: # Stop the session session.stop()
def main(): options = parseCmdLine() # Fill SessionOptions sessionOptions = blpapi.SessionOptions() for idx, host in enumerate(options.hosts): sessionOptions.setServerAddress(host, options.port, idx) sessionOptions.setAuthenticationOptions(options.auth) sessionOptions.setAutoRestartOnDisconnection(True) sessionOptions.setNumStartAttempts(len(options.hosts)) print("Connecting to port %d on %s" % (options.port, " ".join(options.hosts))) myEventHandler = MyEventHandler(options.service) # Create a Session session = blpapi.ProviderSession(sessionOptions, myEventHandler.processEvent) # Start a Session if not session.start(): print("Failed to start session.") return providerIdentity = session.createIdentity() if options.auth: isAuthorized = False authServiceName = "//blp/apiauth" if session.openService(authServiceName): authService = session.getService(authServiceName) isAuthorized = authorize(authService, providerIdentity, session, blpapi.CorrelationId("auth")) if not isAuthorized: print("No authorization") return serviceOptions = blpapi.ServiceRegistrationOptions() if options.groupId is not None: serviceOptions.setGroupId(options.groupId) serviceOptions.setServicePriority(options.priority) if not session.registerService(options.service, providerIdentity, serviceOptions): print("Failed to register '%s'" % options.service) return service = session.getService(options.service) try: # Now we will start publishing value = 1 while g_running: event = service.createPublishEvent() with g_condition: while g_availableTopicCount == 0: # Set timeout to 1 - give a chance for CTRL-C g_condition.wait(1) if not g_running: return eventFormatter = blpapi.EventFormatter(event) for topicName, stream in g_streams.items(): if not stream.isAvailable(): continue value += 1 if not stream.isInitialPaintSent: eventFormatter.appendRecapMessage(stream.topic) eventFormatter.setElement("numRows", 25) eventFormatter.setElement("numCols", 80) eventFormatter.pushElement("rowUpdate") for i in range(1, 6): 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.popElement() eventFormatter.popElement() eventFormatter.popElement() eventFormatter.popElement() stream.isInitialPaintSent = True eventFormatter.appendMessage("RowUpdate", stream.topic) eventFormatter.setElement("rowNum", 1) eventFormatter.pushElement("spanUpdate") eventFormatter.appendElement() START_COL = blpapi.Name("startCol") eventFormatter.setElement(START_COL, 1) strValue = str(value) eventFormatter.setElement("length", len(strValue)) eventFormatter.setElement("text", strValue) eventFormatter.popElement() eventFormatter.popElement() for msg in event: print(msg) session.publish(event) time.sleep(10) finally: # Stop the session session.stop()
def main(): options = parseCmdLine() # Fill SessionOptions sessionOptions = blpapi.SessionOptions() for idx, host in enumerate(options.hosts): sessionOptions.setServerAddress(host, options.port, idx) sessionOptions.setAuthenticationOptions(options.auth) sessionOptions.setAutoRestartOnDisconnection(True) # NOTE: If running without a backup server, make many attempts to # connect/reconnect to give that host a chance to come back up (the # larger the number, the longer it will take for SessionStartupFailure # to come on startup, or SessionTerminated due to inability to fail # over). We don't have to do that in a redundant configuration - it's # expected at least one server is up and reachable at any given time, # so only try to connect to each server once. sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000) myEventHandler = MyEventHandler() # Create a Session session = blpapi.ProviderSession(sessionOptions, myEventHandler.processEvent) # Start a Session if not session.start(): print("Failed to start session.") return providerIdentity = session.createIdentity() if options.auth: isAuthorized = False authServiceName = "//blp/apiauth" if session.openService(authServiceName): authService = session.getService(authServiceName) isAuthorized = authorize(authService, providerIdentity, session, blpapi.CorrelationId("auth")) if not isAuthorized: print("No authorization") return if options.groupId is not None: # NOTE: will perform explicit service registration here, instead # of letting createTopics do it, as the latter approach doesn't # allow for custom ServiceRegistrationOptions. serviceOptions = blpapi.ServiceRegistrationOptions() serviceOptions.setGroupId(options.groupId) if not session.registerService(options.service, identity, serviceOptions): print("Failed to register %s" % options.service) return topicList = blpapi.TopicList() topicList.add(options.service + "/ticker/" + options.topic, blpapi.CorrelationId(MyStream(options.topic))) # Create topics session.createTopics(topicList, blpapi.ProviderSession.AUTO_REGISTER_SERVICES, providerIdentity) # createTopics() is synchronous, topicList will be updated # with the results of topic creation (resolution will happen # under the covers) streams = [] for i in range(topicList.size()): stream = topicList.correlationIdAt(i).value() status = topicList.statusAt(i) topicString = topicList.topicStringAt(i) if (status == blpapi.TopicList.CREATED): print("Start publishing on topic: %s" % topicString) stream.topic = session.getTopic(topicList.messageAt(i)) streams.append(stream) else: print("Stream '%s': topic not created, status = %d" % (stream.id, status)) service = session.getService(options.service) PUBLISH_MESSAGE_TYPE = blpapi.Name(options.messageType) try: # Now we will start publishing tickCount = 1 while streams and g_running: event = service.createPublishEvent() eventFormatter = blpapi.EventFormatter(event) for stream in streams: topic = stream.topic if not topic.isActive(): print("[WARN] Publishing on an inactive topic.") eventFormatter.appendMessage(PUBLISH_MESSAGE_TYPE, topic) for i, f in enumerate(options.fields): eventFormatter.setElement(f, tickCount + i + 1.0) tickCount += 1 for msg in event: print(msg) session.publish(event) time.sleep(10) finally: # Stop the session session.stop()