001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.client.connection.strategy.connector.streaming;
023
024import com.google.gwt.user.client.rpc.AsyncCallback;
025import de.novanic.eventservice.client.connection.strategy.connector.ConnectionStrategyClientConnector;
026import de.novanic.eventservice.client.event.DomainEvent;
027import de.novanic.eventservice.client.event.listener.EventNotification;
028import de.novanic.eventservice.client.event.service.EventServiceAsync;
029
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * The {@link ConnectionStrategyClientConnector} listens for occurred events ({@link de.novanic.eventservice.client.event.Event})
035 * of the server side and has the task to encode / process the transferred events at the client side.
036 *
037 * The {@link de.novanic.eventservice.client.connection.strategy.connector.streaming.DefaultStreamingClientConnector} is an
038 * abstract implementation for streaming listen methods and needs an implementation to deserialize sent events.
039 *
040 * Streaming means that the connection is hold open for a specified time and when an event
041 * occurs, the answer / event is streamed directly to the client without closing and re-open the connection. The connection is
042 * closed and re-opened (by the client) when the configured max. waiting time is reached.
043 *
044 * @author sstrohschein
045 *         <br>Date: 18.03.2010
046 *         <br>Time: 00:07:46
047 */
048public abstract class DefaultStreamingClientConnector implements ConnectionStrategyClientConnector
049{
050        protected static final String CYCLE_TAG = "cycle";
051
052    private EventNotification myEventNotification;
053    private AsyncCallback<List<DomainEvent>> myCallback;
054    private boolean isInitialized;
055
056    /**
057     * Initializes the {@link de.novanic.eventservice.client.connection.strategy.connector.ConnectionStrategyClientConnector} with
058     * the {@link de.novanic.eventservice.client.event.service.EventServiceAsync}.
059     * @param anEventService the {@link de.novanic.eventservice.client.event.service.EventServiceAsync}
060     */
061    public void init(EventServiceAsync anEventService) {
062        isInitialized = true;
063    }
064
065    /**
066     * Deactivates the {@link de.novanic.eventservice.client.connection.strategy.connector.ConnectionStrategyClientConnector}.
067     */
068    public void deactivate() {}
069
070    /**
071     * Checks if the {@link de.novanic.eventservice.client.connection.strategy.connector.ConnectionStrategyClientConnector} is
072     * initialized.
073     * @return true when the {@link de.novanic.eventservice.client.connection.strategy.connector.ConnectionStrategyClientConnector} is
074     * initialized, otherwise false
075     */
076    public boolean isInitialized() {
077        return isInitialized;
078    }
079
080    /**
081     * The listen method implements the listen / connection strategy to receive occurred events. The occurred events
082     * will be passed to the {@link de.novanic.eventservice.client.event.listener.EventNotification} and to the callback.
083     *
084     * That abstract streaming implementation handles the notifications itself. The concrete implementation has to implement
085     * the abstract listen method ({@link DefaultStreamingClientConnector#listen()}) and can notify about events with a simple call to
086     * {@link de.novanic.eventservice.client.connection.strategy.connector.streaming.DefaultStreamingClientConnector#receiveEvent(String)}.
087     * @param anEventNotification {@link de.novanic.eventservice.client.event.listener.EventNotification} which will be notified about occurred / received events
088     * @param aCallback The callback will be notified about occurred / received events.
089     */
090    public void listen(EventNotification anEventNotification, AsyncCallback<List<DomainEvent>> aCallback) {
091        myEventNotification = anEventNotification;
092        myCallback = aCallback;
093        listen();
094    }
095
096    /**
097     * That method can be used by a concrete implementation to sent received events. It de-serializes the event
098     * and notifies the callback and the {@link de.novanic.eventservice.client.event.listener.EventNotification} about the occurred
099     * event, itself. The callback isn't notified about events when the cycle ({@link de.novanic.eventservice.client.connection.strategy.connector.streaming.DefaultStreamingClientConnector#CYCLE_TAG})
100     * is triggered, because the events were already processed to the EventNotification before.
101     * @param anEvent event or cycle tag ({@link de.novanic.eventservice.client.connection.strategy.connector.streaming.DefaultStreamingClientConnector#CYCLE_TAG})
102     */
103    public void receiveEvent(String anEvent) {
104        if(CYCLE_TAG.equals(anEvent)) {
105                myCallback.onSuccess(new ArrayList<DomainEvent>(0));
106        } else {
107            DomainEvent theDeserializedEvent = deserializeEvent(anEvent);
108            myEventNotification.onNotify(theDeserializedEvent);
109        }
110    }
111
112    /**
113     * That de-serialization method has to be implemented by the extending implementation to de-serialize occurred events.
114     * @param anEvent event to de-serialize
115     * @return de-serialized event
116     */
117    protected abstract DomainEvent deserializeEvent(String anEvent);
118
119    /**
120     * That method can be implemented to react on a listen start call. That method is executed by
121     * {@link de.novanic.eventservice.client.connection.strategy.connector.streaming.DefaultStreamingClientConnector#listen(de.novanic.eventservice.client.event.listener.EventNotification, com.google.gwt.user.client.rpc.AsyncCallback)}.
122     */
123    protected abstract void listen();
124}