001package org.intellimate.izou.sdk.util;
002
003import org.intellimate.izou.events.MultipleEventsException;
004import org.intellimate.izou.identification.Identifiable;
005import org.intellimate.izou.identification.IdentificationManager;
006import org.intellimate.izou.identification.IllegalIDException;
007import org.intellimate.izou.sdk.events.Event;
008
009import java.util.Collections;
010import java.util.List;
011import java.util.Optional;
012
013/**
014 * @author Leander Kurscheidt
015 * @version 1.0
016 */
017public interface FireEvent extends ContextProvider, Identifiable {
018
019    /**
020     * tries to fire an Event 5 times, returns true if succeed.
021     * <p>
022     * If there is currently another Event getting processed, it will wait for 100 milliseconds and try for 5 times.
023     * </p>
024     * @param type the type of the Event (See static Strings in IzouSDK Events)
025     * @param descriptor the single descriptor of the Event
026     * @return true if fired, false if unable
027     */
028    default boolean fire(String type, String descriptor) {
029        return fire(type, Collections.singletonList(descriptor), 5);
030    }
031
032    /**
033     * tries to fire an Event 5 times, returns true if succeed.
034     * <p>
035     * If there is currently another Event getting processed, it will wait for 100 milliseconds and try for 5 times.
036     * </p>
037     * @param type the type of the Event (See static Strings in IzouSDK Events)
038     * @param descriptors the Descriptors of the Event
039     * @return true if fired, false if unable
040     */
041    default boolean fire(String type, List<String> descriptors) {
042        return fire(type, descriptors, 5);
043    }
044
045    /**
046     * tries to fire an an Event specified times, returns true if succeed.
047     * <p>
048     * If there is currently another Event getting processed, it will wait for 100 milliseconds and try for retry-times.
049     * </p>
050     * @param type the type of the Event (See static Strings in IzouSDK Events)
051     * @param descriptors the Descriptors of the Event
052     * @param retry how many times it should try
053     * @return true if fired, false if unable
054     */
055    default boolean fire(String type, List<String> descriptors, int retry) {
056        Optional<Event> event = IdentificationManager.getInstance().getIdentification(this)
057                .flatMap(id -> Event.createEvent(type, id, descriptors));
058        if (!event.isPresent()) {
059            getContext().getLogger().error("unable to obtain ID");
060            return false;
061        } else {
062            return fire(event.get(), retry);
063        }
064    }
065
066    /**
067     * tries to fire an an Event specified times, returns true if succeed.
068     * <p>
069     * If there is currently another Event getting processed, it will wait for 100 milliseconds and try for retry-times.
070     * </p>
071     * @param event the event to fire
072     * @param retry how many times it should try
073     * @return true if fired, false if unable
074     */
075    default boolean fire(Event event, int retry) {
076        int counter = 0;
077        while (counter < retry) {
078            try {
079                getContext().getEvents().fireEvent(event);
080                return true;
081            } catch (MultipleEventsException e) {
082                try {
083                    Thread.sleep(100);
084                } catch (InterruptedException e1) {
085                    throw new RuntimeException(e);
086                }
087            } catch (IllegalIDException e) {
088                //maybe change in future SDK-Versions? currently not implemented in Izou
089                getContext().getLogger().error("Illegal ID!", e);
090            }
091        }
092        return false;
093    }
094
095    /**
096     * tries to fire an an Event 5 times, returns true if succeed.
097     * <p>
098     * If there is currently another Event getting processed, it will wait for 100 milliseconds and try for retry-times.
099     * </p>
100     * @param event the event to fire
101     * @return true if fired, false if unable
102     */
103    default boolean fire(Event event) {
104        return fire(event, 5);
105    }
106}