001 package org.otfeed.j2ee.ra;
002
003 import java.util.Date;
004 import java.util.EnumSet;
005 import java.util.HashSet;
006 import java.util.Set;
007
008 import org.otfeed.command.AggregationSpan;
009 import org.otfeed.command.ListSymbolEnum;
010 import org.otfeed.command.MonthAndYear;
011 import org.otfeed.command.PriceRange;
012 import org.otfeed.command.VolumeStyleEnum;
013 import org.otfeed.command.ListSymbolsCommand.MatchStyleEnum;
014 import org.otfeed.event.OTDividend;
015 import org.otfeed.event.OTEquityInit;
016 import org.otfeed.event.OTExchange;
017 import org.otfeed.event.OTOHLC;
018 import org.otfeed.event.OTOptionInit;
019 import org.otfeed.event.OTSplit;
020 import org.otfeed.event.OTSymbol;
021 import org.otfeed.event.OTTodaysOHL;
022 import org.otfeed.protocol.DataEnum;
023 import org.otfeed.support.alt.IOutboundConnection;
024 import org.otfeed.support.alt.OutboundConnectionAdapter;
025
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 public class OtfeedConnection implements IOutboundConnection {
030
031 private static final Logger LOG = LoggerFactory.getLogger(OtfeedConnection.class);
032
033 private OtfeedManagedConnection managedConnection;
034 private IOutboundConnection engine;
035 private final Set<IDataReader<?>> readers = new HashSet<IDataReader<?>>();
036
037 public OtfeedConnection(OtfeedManagedConnection c) {
038 managedConnection = c;
039 engine = new OutboundConnectionAdapter(managedConnection.getEngine());
040 }
041
042 void associateConnection(OtfeedManagedConnection mc) {
043 managedConnection.removeHandle(this);
044 managedConnection = mc;
045 managedConnection.addHandle(this);
046 engine = new OutboundConnectionAdapter(managedConnection.getEngine());
047 }
048
049 void invalidate() {
050 LOG.trace("invalidate");
051 synchronized(readers) {
052 for(IDataReader<?> r : readers) {
053 r.cancel();
054 }
055 readers.clear();
056 }
057 }
058
059 private <T> IDataReader<T> register(IDataReader<T> reader) {
060 synchronized(readers) {
061 readers.add(reader);
062 }
063 return reader;
064 }
065
066 public void close() {
067 LOG.trace("close");
068 invalidate();
069 managedConnection.fireConnectionClosed(this);
070 managedConnection.removeHandle(this);
071 }
072
073 public IDataReader<OTDividend> requestDivident(String exchangeCode,
074 String symbolCode, Date startDate, Date endDate) {
075 return register(engine.requestDivident(exchangeCode, symbolCode, startDate, endDate));
076 }
077
078 public IDataReader<OTEquityInit> requestEquityInit(String exchangeCode,
079 String symbolCode) {
080 return register(engine.requestEquityInit(exchangeCode, symbolCode));
081 }
082
083 public IDataReader<Object> requestHistBook(String exchangeCode,
084 String symbolCode, Date startDate, Date endDate,
085 EnumSet<DataEnum> dataTypes) {
086 return register(engine.requestHistBook(exchangeCode, symbolCode, startDate, endDate, dataTypes));
087 }
088
089 public IDataReader<OTOHLC> requestHistData(String exchangeCode,
090 String symbolCode, Date startDate, Date endDate,
091 AggregationSpan aggregationSpan) {
092 return register(engine.requestHistData(exchangeCode, symbolCode, startDate, endDate, aggregationSpan));
093 }
094
095 public IDataReader<Object> requestHistTicks(String exchangeCode,
096 String symbolCode, Date startDate, Date endDate,
097 VolumeStyleEnum volumeStyle, EnumSet<DataEnum> dataTypes) {
098 return register(engine.requestHistTicks(exchangeCode, symbolCode, startDate, endDate, volumeStyle, dataTypes));
099 }
100
101 public IDataReader<OTExchange> requestListExchanges() {
102 return register(engine.requestListExchanges());
103 }
104
105 public IDataReader<OTSymbol> requestListSymbols(String exchangeCode,
106 String symbolCodePattern, Set<ListSymbolEnum> types,
107 MatchStyleEnum matchStyle) {
108 return register(engine.requestListSymbols(exchangeCode, symbolCodePattern, types, matchStyle));
109 }
110
111 public IDataReader<Object> requestOptionChainSnapshot(String exchangeCode,
112 String symbolCode, MonthAndYear expiration, PriceRange strikeRange,
113 VolumeStyleEnum volumeStyle, EnumSet<DataEnum> dataTypes) {
114 return register(engine.requestOptionChainSnapshot(exchangeCode, symbolCode, expiration, strikeRange, volumeStyle, dataTypes));
115 }
116
117 public IDataReader<OTOptionInit> requestOptionInit(String exchangeCode,
118 String symbolCode, MonthAndYear expiration, PriceRange strikeRange) {
119 return register(engine.requestOptionInit(exchangeCode, symbolCode, expiration, strikeRange));
120 }
121
122 public IDataReader<Object> requestSnapshot(String exchangeCode,
123 String symbolCode, VolumeStyleEnum volumeStyle,
124 EnumSet<DataEnum> dataTypes) {
125 return register(engine.requestSnapshot(exchangeCode, symbolCode, volumeStyle, dataTypes));
126 }
127
128 public IDataReader<OTSplit> requestSplit(String exchangeCode,
129 String symbolCode, Date startDate, Date endDate) {
130 return register(engine.requestSplit(exchangeCode, symbolCode, startDate, endDate));
131 }
132
133 public IDataReader<OTTodaysOHL> requestTodaysOHL(String exchangeCode,
134 String symbolCode) {
135 return register(engine.requestTodaysOHL(exchangeCode, symbolCode));
136 }
137 }