/***************************************************************************** * Function : createSession * Description : This function creates a session object and opens the market * bar service. Returns false on failure of either. * Arguments : none * Returns : bool *****************************************************************************/ private bool createSession() { if (d_session != null) { d_session.Stop(); } System.Console.WriteLine("Connecting to " + d_sessionOptions.ServerHost + ":" + d_sessionOptions.ServerPort); d_session = new Session(d_sessionOptions, new EventHandler(processEvent)); if (!d_session.Start()) { System.Console.WriteLine("Failed to start session"); return(false); } System.Console.WriteLine("Connected successfully\n"); if (!d_session.OpenService("//blp/mktbar")) { System.Console.WriteLine("Failed to open service //blp/mktbar"); d_session.Stop(); return(false); } System.Console.WriteLine("Subscribing..."); d_session.Subscribe(d_subscriptions); return(true); }
/// <summary> /// Initialises the specified URI session and opens the service. /// </summary> /// <param name="uri">The URI.</param> /// <returns></returns> private bool Initialise(string uri, int requestCount) { sessionOptions = new BB.SessionOptions(); sessionOptions.ServerHost = "localhost"; sessionOptions.ServerPort = 8194; if (requestCount > sessionOptions.MaxPendingRequests) { sessionOptions.MaxPendingRequests = requestCount; } UpdateStatus("Starting the Bloomberg session."); session = new BB.Session(sessionOptions, new BB.EventHandler(processEvent)); if (session.Start()) { if (!session.OpenService(uri)) { throw new Exception("Bloomberg failed to open session " + uri); } return(true); } else { throw new Exception("An error occurred starting the Bloomberg session. Ensure Bloomberg is installed."); } }
/// <summary> /// Initializes the SecurityLookup instance by creating a session and authorizing it so that its /// ready to conduct searches. Once initialized multiple searches may be conducted with the same instance. /// </summary> public void Init() { try { SessionOptions sessionOptions = new SessionOptions(); sessionOptions.ServerHost = d_host; sessionOptions.ServerPort = d_port; sessionOptions.AuthenticationOptions = d_authOptions; Console.WriteLine("Connecting to {0}:{1}", d_host, d_port); session = new Session(sessionOptions); if (!session.Start()) { throw new Exception("Failed to start session"); } identity = null; if (d_authOptions != null) { Authorize(out identity, session); } if (!session.OpenService(INSTRUMENT_SERVICE)) { throw new Exception( string.Format("Failed to open: {0}", INSTRUMENT_SERVICE)); } } catch (Exception e) { Console.WriteLine(string.Format("Exception: {0}", e.Message)); Console.WriteLine(); } }
/***************************************************************************** * Function : IsBPipeAuthorized * Description : This function authorize the token. A valid Identiy will contain * the user/application credentials on authorization success. * Returns false on failure. * Arguments : token string from GenerateToken(), reference to Identity * Returns : bool *****************************************************************************/ private bool IsBPipeAuthorized(string token, out Identity identity) { bool isAuthorized = false; bool isRunning = true; identity = null; if (!d_session.OpenService(AUTH_SVC)) { System.Console.Error.WriteLine("Failed to open " + AUTH_SVC); return(isAuthorized = false); } Service authService = d_session.GetService(AUTH_SVC); Request authRequest = authService.CreateAuthorizationRequest(); authRequest.Set("token", token); identity = d_session.CreateIdentity(); EventQueue authEventQueue = new EventQueue(); d_session.SendAuthorizationRequest(authRequest, identity, authEventQueue, new CorrelationID(1)); while (isRunning) { Event eventObj = authEventQueue.NextEvent(); System.Console.WriteLine("processEvent"); if (eventObj.Type == Event.EventType.RESPONSE || eventObj.Type == Event.EventType.REQUEST_STATUS) { foreach (Message msg in eventObj) { if (msg.MessageType == AUTHORIZATION_SUCCESS) { System.Console.WriteLine("Authorization SUCCESS"); isAuthorized = true; isRunning = false; break; } else if (msg.MessageType == AUTHORIZATION_FAILURE) { System.Console.WriteLine("Authorization FAILED"); System.Console.WriteLine(msg); isRunning = false; } else { System.Console.WriteLine(msg); } } } } return(isAuthorized); }
public void Connect() { string serverHost = "api.bloomberg.com"; int serverPort = 0; SessionOptions sessionOptions = new SessionOptions(); sessionOptions.ServerHost = serverHost; sessionOptions.ServerPort = serverPort; Log("Connecting to " + serverHost + ":" + serverPort); session = new Session(sessionOptions); bool sessionStarted = session.Start(); if (!sessionStarted) { throw new Exception("Failed to start Bloomberg API session."); } if (!session.OpenService("//blp/refdata")) { throw new Exception("Failed to open //blp/refdata"); } this.service = session.GetService("//blp/refdata"); }
/// <summary> /// Sets config and other session data to create the session /// </summary> /// <returns></returns> private Session CreateBloombergSession() { // retrieves the appsettings and fills the config object config = new BloombergApiSessionConfig(); var sessionOptions = new SessionOptions { ServerHost = config.BbHost, ServerPort = config.BbPort }; var session = new Session(sessionOptions); if (!session.Start()) { //_Logging.Error("Unable to initiate Bloomberg session."); return null; } if (!session.OpenService(config.BbService)) { //_Logging.Error("Unable to open Bloomberg service."); return null; } return session; }
/// <summary> /// Authorize should be called before any requests are sent. Called by init. /// </summary> /// <param name="identity"></param> /// <param name="session"></param> private static void Authorize(out Identity identity, Session session) { identity = session.CreateIdentity(); if (!session.OpenService(AUTH_SERVICE)) { throw new Exception( string.Format("Failed to open auth service: {0}", AUTH_SERVICE)); } Service authService = session.GetService(AUTH_SERVICE); EventQueue tokenEventQueue = new EventQueue(); session.GenerateToken(new CorrelationID(tokenEventQueue), tokenEventQueue); string token = null; // Generate token responses will come on the dedicated queue. There would be no other // messages on that queue. Event eventObj = tokenEventQueue.NextEvent( Convert.ToInt32(WAIT_TIME.TotalMilliseconds)); if (eventObj.Type == Event.EventType.TOKEN_STATUS || eventObj.Type == Event.EventType.REQUEST_STATUS) { foreach (Message msg in eventObj) { System.Console.WriteLine(msg); if (msg.MessageType == TOKEN_SUCCESS) { token = msg.GetElementAsString(TOKEN_ELEMENT); } } } if (token == null) { throw new Exception("Failed to get token"); } Request authRequest = authService.CreateAuthorizationRequest(); authRequest.Set(TOKEN_ELEMENT, token); session.SendAuthorizationRequest(authRequest, identity, null); TimeSpan ts = WAIT_TIME; for (DateTime startTime = DateTime.UtcNow; ts.TotalMilliseconds > 0; ts = ts - (DateTime.UtcNow - startTime)) { eventObj = session.NextEvent(Convert.ToInt32(ts.TotalMilliseconds)); // Since no other requests were sent using the session queue, the response can // only be for the Authorization request if (eventObj.Type != Event.EventType.RESPONSE && eventObj.Type != Event.EventType.PARTIAL_RESPONSE && eventObj.Type != Event.EventType.REQUEST_STATUS) { continue; } foreach (Message msg in eventObj) { System.Console.WriteLine(msg); if (msg.MessageType != AUTHORIZATION_SUCCESS) { throw new Exception("Authorization Failed"); } } return; } throw new Exception("Authorization Failed"); }