1+ /* Copyright 2017. Bloomberg Finance L.P.
2+ *
3+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4+ * of this software and associated documentation files (the "Software"), to
5+ * deal in the Software without restriction, including without limitation the
6+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+ * sell copies of the Software, and to permit persons to whom the Software is
8+ * furnished to do so, subject to the following conditions: The above
9+ * copyright notice and this permission notice shall be included in all copies
10+ * or substantial portions of the Software.
11+ *
12+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18+ * IN THE SOFTWARE.
19+ */
20+
21+ using Name = Bloomberglp . Blpapi . Name ;
22+ using SessionOptions = Bloomberglp . Blpapi . SessionOptions ;
23+ using Session = Bloomberglp . Blpapi . Session ;
24+ using Service = Bloomberglp . Blpapi . Service ;
25+ using Request = Bloomberglp . Blpapi . Request ;
26+ using Element = Bloomberglp . Blpapi . Element ;
27+ using CorrelationID = Bloomberglp . Blpapi . CorrelationID ;
28+ using Event = Bloomberglp . Blpapi . Event ;
29+ using Message = Bloomberglp . Blpapi . Message ;
30+ using EventHandler = Bloomberglp . Blpapi . EventHandler ;
31+ using System ;
32+
33+ namespace com . bloomberg . emsx . samples
34+ {
35+ public class GetTradeDesks
36+ {
37+
38+ private static readonly Name SESSION_STARTED = new Name ( "SessionStarted" ) ;
39+ private static readonly Name SESSION_STARTUP_FAILURE = new Name ( "SessionStartupFailure" ) ;
40+ private static readonly Name SERVICE_OPENED = new Name ( "ServiceOpened" ) ;
41+ private static readonly Name SERVICE_OPEN_FAILURE = new Name ( "ServiceOpenFailure" ) ;
42+
43+ private static readonly Name ERROR_INFO = new Name ( "ErrorInfo" ) ;
44+ private static readonly Name GET_TRADE_DESKS = new Name ( "GetTradeDesks" ) ;
45+
46+ private string d_service ;
47+ private string d_host ;
48+ private int d_port ;
49+
50+ private static bool quit = false ;
51+
52+ private CorrelationID requestID ;
53+
54+ public static void Main ( String [ ] args )
55+ {
56+ System . Console . WriteLine ( "Bloomberg - EMSX API Example - GetTradeDesks\n " ) ;
57+
58+ GetTradeDesks example = new GetTradeDesks ( ) ;
59+ example . run ( args ) ;
60+
61+ while ( ! quit ) { } ;
62+
63+ System . Console . WriteLine ( "Press ENTER to terminate..." ) ;
64+ System . Console . ReadKey ( ) ;
65+
66+ }
67+
68+ public GetTradeDesks ( )
69+ {
70+
71+ // Define the service required, in this case the beta service,
72+ // and the values to be used by the SessionOptions object
73+ // to identify IP/port of the back-end process.
74+
75+ // This is an AIM only function and thus there are no valid //blp/emapisvc_beta access.
76+ d_service = "//blp/emapisvc" ;
77+ d_host = "localhost" ;
78+ d_port = 8194 ;
79+
80+ }
81+
82+ private void run ( String [ ] args )
83+ {
84+
85+ SessionOptions d_sessionOptions = new SessionOptions ( ) ;
86+ d_sessionOptions . ServerHost = d_host ;
87+ d_sessionOptions . ServerPort = d_port ;
88+
89+ Session session = new Session ( d_sessionOptions , new EventHandler ( processEvent ) ) ;
90+
91+ session . StartAsync ( ) ;
92+
93+ }
94+
95+ public void processEvent ( Event evt , Session session )
96+ {
97+ try
98+ {
99+ switch ( evt . Type )
100+ {
101+ case Event . EventType . SESSION_STATUS :
102+ processSessionEvent ( evt , session ) ;
103+ break ;
104+ case Event . EventType . SERVICE_STATUS :
105+ processServiceEvent ( evt , session ) ;
106+ break ;
107+ case Event . EventType . RESPONSE :
108+ processResponseEvent ( evt , session ) ;
109+ break ;
110+ default :
111+ processMiscEvents ( evt , session ) ;
112+ break ;
113+ }
114+ }
115+ catch ( Exception e )
116+ {
117+ System . Console . Error . WriteLine ( e ) ;
118+ }
119+ }
120+
121+ private void processSessionEvent ( Event evt , Session session )
122+ {
123+ System . Console . WriteLine ( "\n Processing " + evt . Type ) ;
124+
125+ foreach ( Message msg in evt )
126+ {
127+ if ( msg . MessageType . Equals ( SESSION_STARTED ) )
128+ {
129+ System . Console . WriteLine ( "Session started..." ) ;
130+ session . OpenServiceAsync ( d_service ) ;
131+ }
132+ else if ( msg . MessageType . Equals ( SESSION_STARTUP_FAILURE ) )
133+ {
134+ System . Console . Error . WriteLine ( "Error: Session startup failed" ) ;
135+ }
136+ }
137+ }
138+
139+ private void processServiceEvent ( Event evt , Session session )
140+ {
141+
142+ System . Console . WriteLine ( "\n Processing " + evt . Type ) ;
143+
144+ foreach ( Message msg in evt )
145+ {
146+ if ( msg . MessageType . Equals ( SERVICE_OPENED ) )
147+ {
148+ System . Console . WriteLine ( "Service opened..." ) ;
149+
150+ Service service = session . GetService ( d_service ) ;
151+
152+ Request request = service . CreateRequest ( "GetTradeDesks" ) ;
153+
154+ //request.set("EMSX_REQUEST_SEQ", 1);
155+
156+ System . Console . WriteLine ( "Request: " + request . ToString ( ) ) ;
157+
158+ requestID = new CorrelationID ( ) ;
159+
160+ // Submit the request
161+ try
162+ {
163+ session . SendRequest ( request , requestID ) ;
164+ }
165+ catch ( Exception ex )
166+ {
167+ System . Console . Error . WriteLine ( "Failed to send the request: " + ex . Message ) ;
168+ }
169+
170+ }
171+ else if ( msg . MessageType . Equals ( SERVICE_OPEN_FAILURE ) )
172+ {
173+ System . Console . Error . WriteLine ( "Error: Service failed to open" ) ;
174+ }
175+ }
176+ }
177+
178+ private void processResponseEvent ( Event evt , Session session )
179+ {
180+ System . Console . WriteLine ( "Received Event: " + evt . Type ) ;
181+
182+ foreach ( Message msg in evt )
183+ {
184+
185+ System . Console . WriteLine ( "MESSAGE: " + msg . ToString ( ) ) ;
186+ System . Console . WriteLine ( "CORRELATION ID: " + msg . CorrelationID ) ;
187+
188+ if ( evt . Type == Event . EventType . RESPONSE && msg . CorrelationID == requestID )
189+ {
190+ System . Console . WriteLine ( "Message Type: " + msg . MessageType ) ;
191+
192+ if ( msg . MessageType . Equals ( ERROR_INFO ) )
193+ {
194+ int errorCode = msg . GetElementAsInt32 ( "ERROR_CODE" ) ;
195+ String errorMessage = msg . GetElementAsString ( "ERROR_MESSAGE" ) ;
196+ System . Console . WriteLine ( "ERROR CODE: " + errorCode + "\t ERROR MESSAGE: " + errorMessage ) ;
197+ }
198+ else if ( msg . MessageType . Equals ( GET_TRADE_DESKS ) )
199+ {
200+ Element tradeDesks = msg . GetElement ( "EMSX_TRADE_DESK" ) ;
201+
202+ int numValues = tradeDesks . NumValues ;
203+
204+ for ( int i = 0 ; i < numValues ; i ++ )
205+ {
206+
207+ String tradeDesk = tradeDesks . GetValueAsString ( i ) ;
208+
209+ System . Console . WriteLine ( "TRADEDESKS: " + tradeDesk ) ;
210+ }
211+ }
212+
213+ quit = true ;
214+ session . Stop ( ) ;
215+ }
216+ }
217+ }
218+
219+ private void processMiscEvents ( Event evt , Session session )
220+ {
221+ System . Console . WriteLine ( "Processing " + evt . Type ) ;
222+
223+ foreach ( Message msg in evt )
224+ {
225+ System . Console . WriteLine ( "MESSAGE: " + msg ) ;
226+ }
227+ }
228+ }
229+ }
0 commit comments