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.protocol.request;
019
020 import org.otfeed.event.ICompletionDelegate;
021 import org.otfeed.event.IDataDelegate;
022 import org.otfeed.event.OTExchange;
023 import org.otfeed.protocol.CommandEnum;
024
025 import java.nio.ByteBuffer;
026
027 /**
028 * Request to get list of exchanges.
029 */
030 public class ListExchangesRequest extends AbstractSessionRequest {
031
032 private final IDataDelegate<OTExchange> dataDelegate;
033
034 public ListExchangesRequest(int requestId,
035 IDataDelegate<OTExchange> dataDelegate,
036 ICompletionDelegate completionDelegate) {
037
038 super(CommandEnum.REQUEST_LIST_EXCHANGES,
039 requestId, completionDelegate);
040
041 Check.notNull(dataDelegate, "dataDelegate");
042
043 this.dataDelegate = dataDelegate;
044
045 }
046
047 @Override
048 public JobStatus handleMessage(Header header, ByteBuffer in) {
049 super.handleMessage(header, in);
050
051 String subscriptionURL = Util.readString(in);
052 int exchangesNum = in.getShort();
053
054 for (int i = 0; i < exchangesNum; i++) {
055
056 String code = Util.readString(in, 15);
057 boolean isAvailable = Util.readBoolean(in);
058 String title = Util.readString(in);
059 String desc = Util.readString(in);
060
061 OTExchange exchange = new OTExchange(
062 code,
063 title,
064 desc,
065 isAvailable,
066 subscriptionURL);
067
068 dataDelegate.onData(exchange);
069 }
070
071 return JobStatus.FINISHED;
072 }
073 }