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    import org.otfeed.*;
020    import org.otfeed.event.*;
021    import org.otfeed.command.*;
022    import org.otfeed.support.*;
023     
024    /**
025     * Demo of the Driver API.
026     * 
027     * Runnable class that connects to the Opentick server, loggs in, and 
028     * requests the list of supported exchanges. 
029     */
030    public class ListExchangesSample {
031            
032        @SuppressWarnings("unchecked")
033            public static void main(String ... av) throws Exception {
034            if(av.length != 2) {
035                    throw new IllegalArgumentException("wrong number of arguments. "
036                                    + "Expected 2 parameters: <username> <password>");
037            }
038     
039            /// Create and configure connection factory
040            OTConnectionFactory factory = new OTConnectionFactory();
041            factory.getHosts().add(new OTHost("feed1.opentick.com:10015"));
042            factory.setUsername(av[0]);
043            factory.setPassword(av[1]);
044     
045            /// Open connection to the server. Use SimpleConnectionStateListener
046            /// to monitor connection progress.
047            IConnection connection = factory.connect(null, new SimpleConnectionStateListener());
048     
049            /// Create the command. Use CommonListener to receive the events. By default,
050            /// CommonListener just prints the objects to the screen.
051            ListExchangesCommand command = new ListExchangesCommand();
052            command.setDataDelegate(new CommonDelegate());
053            command.setCompletionDelegate(new CommonDelegate());
054     
055            try {
056                    /// Prepare the request (nothing is shipped to the server yet)
057                    IRequest request = connection.prepareRequest(command);
058     
059                    /// Now we have prepared request. Send it to the server.
060                    request.submit();
061     
062                    /// Wait till request finishes
063                    request.waitForCompletion();
064            } finally {
065     
066                    /// Disconnect from the server
067                    connection.shutdown();
068     
069                    /// Wait till connection is closed and all resources are freed
070                    connection.waitForCompletion();
071            }
072        }
073    }