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 package org.otfeed.protocol.request;
018
019 import org.otfeed.IConnection;
020 import org.otfeed.IRequest;
021 import org.otfeed.event.ICompletionDelegate;
022 import org.otfeed.event.OTError;
023
024 /**
025 * Base for synthetic requests: a snapshot request followed by
026 * a stream request.
027 */
028 public abstract class AbstractStreamWithSnapshotRequest extends RequestJob {
029
030 final IConnection connection;
031
032 private IRequest snapshotRequest;
033 private IRequest streamRequest;
034
035 abstract IRequest prepareSnapshotRequest(IConnection connection, ICompletionDelegate d);
036 abstract IRequest prepareStreamRequest(IConnection connection, ICompletionDelegate d);
037
038 /**
039 * Creates new object.
040 *
041 * @param connection connection reference.
042 * @param completionDelegate client's completion delegate.
043 */
044 AbstractStreamWithSnapshotRequest(
045 IConnection connection,
046 ICompletionDelegate completionDelegate) {
047 super(completionDelegate);
048
049 Check.notNull(connection, "connection");
050 this.connection = connection;
051 }
052
053 public final void prepareRequest() {
054
055 this.snapshotRequest = prepareSnapshotRequest(connection, new ICompletionDelegate() {
056 public void onDataEnd(OTError error) {
057 if(error != null) {
058 fireCompleted(error);
059 streamRequest.cancel();
060 }
061 }
062 });
063
064 this.streamRequest = prepareStreamRequest(connection, new ICompletionDelegate() {
065 public void onDataEnd(OTError error) {
066 fireCompleted(error);
067 snapshotRequest.cancel();
068 }
069 });
070 }
071
072 public void submit() {
073 snapshotRequest.submit();
074 streamRequest.submit();
075 }
076
077 public void cancel() {
078 snapshotRequest.cancel();
079 streamRequest.cancel();
080 }
081 }