001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.samples;
019    
020    import java.util.Date;
021    
022    import org.otfeed.IConnection;
023    import org.otfeed.IRequest;
024    import org.otfeed.command.SnapshotCommand;
025    import org.otfeed.event.ICompletionDelegate;
026    import org.otfeed.event.IDataDelegate;
027    import org.otfeed.event.OTBBO;
028    import org.otfeed.event.OTError;
029    import org.otfeed.event.OTMMQuote;
030    import org.otfeed.event.OTQuote;
031    import org.otfeed.event.OTTrade;
032    
033    /**
034     * Sample application: requests a snapshot data.
035     */
036    public class Snapshot {
037    
038            public static void main(String ... args) throws Exception {
039    
040                    if(args.length != 2) {
041                            System.out.println("USAGE: Snapshot <exchangeName> <symbolName>");
042                            System.exit(-1);
043                    }
044    
045                    System.out.println("# generated by " + Snapshot.class + " on " + FormatUtil.DATE_FORMAT_AS_STRING.format(new Date()));
046                    System.out.println("#");
047                    System.out.println("# snapshot data for: " + args[0] + "/" + args[1]);
048                    System.out.println("#");
049                    
050                    IConnection connection = Connector.connect();
051    
052                    SnapshotCommand command = new SnapshotCommand();
053                    command.setExchangeCode(args[0]);
054                    command.setSymbolCode(args[1]);
055                    command.setQuoteDelegate(new IDataDelegate<OTQuote>() {
056                            public void onData(OTQuote data) {
057                                    System.out.println(data);
058                            }
059                    });
060                    command.setTradeDelegate(new IDataDelegate<OTTrade>() {
061                            public void onData(OTTrade data) {
062                                    System.out.println(data);
063                            }
064                    });
065                    command.setMmQuoteDelegate(new IDataDelegate<OTMMQuote>() {
066                            public void onData(OTMMQuote data) {
067                                    System.out.println(data);
068                            }
069                    });
070                    command.setBboDelegate(new IDataDelegate<OTBBO>() {
071                            public void onData(OTBBO data) {
072                                    System.out.println(data);
073                            }
074                    });
075    
076                    command.setCompletionDelegate(new ICompletionDelegate() {
077                            public void onDataEnd(OTError error) {
078                                    if(error != null) {
079                                            System.out.println("# Error: " + error);
080                                    }
081                            }
082                    });
083                    
084                    try {
085                            IRequest request = connection.prepareRequest(command);
086                            request.submit();
087    
088                            request.waitForCompletion();
089                    } finally {
090    
091                            connection.shutdown();
092                            connection.waitForCompletion();
093                    }
094            }
095    }