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.command.ListSymbolEnum;
021 import org.otfeed.command.ListSymbolsCommand.MatchStyleEnum;
022 import org.otfeed.event.ICompletionDelegate;
023 import org.otfeed.event.IDataDelegate;
024 import org.otfeed.event.InstrumentEnum;
025 import org.otfeed.event.OTSymbol;
026 import org.otfeed.protocol.CommandEnum;
027 import org.otfeed.protocol.ProtocolException;
028
029 import java.nio.ByteBuffer;
030
031 import java.util.Set;
032
033 /**
034 * Request to receive list of symbols.
035 * If exchange name is "@", and symbol name is non-blank, then
036 * symbol is looked up on all exchanges. If symbol name is "" (empty string)
037 * all symbols are returned.
038 */
039 public class ListSymbolsExRequest extends AbstractSessionRequest {
040
041 private final IDataDelegate<OTSymbol> dataDelegate;
042
043 private final String exchangeCode;
044 private final String symbolCodePattern;
045 private final Set<ListSymbolEnum> types;
046 private final MatchStyleEnum matchStyle;
047
048 public String getExchangeCode() {
049 return exchangeCode;
050 }
051
052 public String getSymbolCodePattern() {
053 return symbolCodePattern;
054 }
055
056 public Set<ListSymbolEnum> getTypes() {
057 return types;
058 }
059
060 public ListSymbolsExRequest(int requestId,
061 String exchangeCode,
062 String symbolCodePattern,
063 Set<ListSymbolEnum> types,
064 MatchStyleEnum matchStyle,
065 IDataDelegate<OTSymbol> dataDelegate,
066 ICompletionDelegate completionDelegate) {
067 super(CommandEnum.REQUEST_LIST_SYMBOLS_EX,
068 requestId, completionDelegate);
069
070 Check.notNull(exchangeCode, "exchangeCode");
071 Check.notNull(symbolCodePattern, "symbolCodePattern");
072 Check.notNull(matchStyle, "matchStyle");
073 Check.notNull(types, "types");
074 Check.notNull(dataDelegate, "dataDelegate");
075
076 this.exchangeCode = exchangeCode;
077 this.symbolCodePattern = symbolCodePattern;
078 this.types = types;
079 this.matchStyle = matchStyle;
080
081 this.dataDelegate = dataDelegate;
082 }
083
084 @Override
085 public void writeRequest(ByteBuffer out) {
086 super.writeRequest(out);
087
088 Util.writeString(out, exchangeCode, 15);
089 Util.writeString(out, symbolCodePattern, 15);
090 Util.writeListSymbolMask(out, types, matchStyle);
091 }
092
093 @Override
094 public JobStatus handleMessage(Header header, ByteBuffer in) {
095
096 if(header.getRequestId() != getRequestId()) {
097 throw new ProtocolException("wrong request id", in);
098 }
099
100 if(header.getCommand() != CommandEnum.REQUEST_LIST_SYMBOLS) {
101 throw new ProtocolException("wrong command: "
102 + header.getCommand(), in);
103 }
104
105 int symbolsNum = in.getShort();
106
107 if (symbolsNum > 0) {
108 for (int i = 0; i < symbolsNum; i++) {
109 String currencyID = Util.readString(in, 4);
110 String symbolCodePattern = Util.readString(in, 15);
111 InstrumentEnum symbolType = Util.readInstrumentEnum(in);
112 String company = Util.readString(in);
113
114 OTSymbol symbol = new OTSymbol(
115 symbolCodePattern,
116 company,
117 currencyID,
118 symbolType);
119
120 dataDelegate.onData(symbol);
121 }
122
123 return JobStatus.ACTIVE;
124 }
125
126 return JobStatus.FINISHED;
127 }
128 }