Android Open Source - playnomics-android Event Worker






From Project

Back to project page playnomics-android.

License

The source code is released under:

Apache License

If you think the Android project playnomics-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.playnomics.android.client;
//from   w w  w .ja v  a 2  s. com
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

import com.playnomics.android.events.PlaynomicsEvent;
import com.playnomics.android.util.IConfig;
import com.playnomics.android.util.Logger;
import com.playnomics.android.util.Logger.LogLevel;

public class EventWorker implements IEventWorker {

  private IHttpConnectionFactory connectionFactory;
  private IEventQueue eventQueue;
  private HandlerThread handerThread;
  private Handler handler;
  private AtomicBoolean running;
  private AtomicBoolean startComplete;
  private IConfig config;
  private Logger logger;
  private HttpURLConnection connection;

  private static final int MSG_START_QUE = 1;
  private static final int MSG_EMPTY_WAIT_QUE = 2;
  private static final int MSG_NEW_EVENT = 3;

  public EventWorker(IConfig config, IHttpConnectionFactory factory,
      Logger logger) {
    this.running = new AtomicBoolean(false);
    this.connectionFactory = factory;
    this.config = config;
    this.logger = logger;
    this.eventQueue = new EventQueue(config, connectionFactory);
    this.startComplete = new AtomicBoolean(false);
  }

  public boolean isRunning(){
    return running.get();
  }

  public void start() {
    if (running.getAndSet(true)) {
      // return if we were already running
      return;
    }

    handerThread = createHandlerThread();
    handerThread.start();
  }

  public void stop() {
    if (!running.getAndSet(false)) {
      return;
    }

    startComplete.set(false);
    handerThread.quit();
    handerThread.interrupt();
  }


  private HandlerThread createHandlerThread() {
    return new HandlerThread("EventWorker") {
      @Override
      protected void onLooperPrepared() {
        super.onLooperPrepared();
        handler = new Handler(getLooper()) {
          public void handleMessage(Message message) {
            // Handle the message on the thread associated to the given looper.
            dataHandler(message.what);
          }
        };
        handler.sendEmptyMessage(MSG_START_QUE);
        startComplete.set(true);
      }
    };
  }

  private void dataHandler(int msg) {
    if (!running.get())
      return;

    String url = dequeueEventUrl();
    if (url==null) {
      if (running.get())
        handler.sendEmptyMessageDelayed(MSG_EMPTY_WAIT_QUE, config.getQueEmptyTimeoutInMilliseconds());
      return;
    }
    if (!running.get()) {
      enqueueEventUrl(url);
      return;
    }
    handler.sendEmptyMessage(MSG_NEW_EVENT);

    int responseCode = dispatchData(url);
    if (responseCode==HttpURLConnection.HTTP_OK) {
      logger.log(LogLevel.DEBUG, "Event URL Request succeeded %s", url);
    } else {
      // HTTP error retry sending again later.
      enqueueEventUrl(url);
      if (responseCode==0 && running.get()) {  // Connection failure error
        try {
          // Wait and try later
          logger.log(LogLevel.WARNING,
              "Could not connext to the event API. Shutting down the queue for 2 minutes ...", url);
          Thread.sleep(config.getQueHttpErrorTimeoutInSeconds());
        } catch (InterruptedException e) {
        }
      }
    }
  }

  private int dispatchData(String url) {
    try {
      connection = connectionFactory.startConnectionForUrl(url);
      if (connection!=null)
        return connection.getResponseCode();
    } catch (IOException ex) {
      logger.log(LogLevel.WARNING, ex,
          "Could not connext to the event API.", url);
    } catch (NullPointerException ex) {
      logger.log(LogLevel.WARNING, ex,
          "Could not connext to the event API.", url);
    } finally {
      if (connection != null) {
        connection.disconnect();
        connection = null;
      }
    }
    return 0;
  }

  private String dequeueEventUrl() {
    if (!eventQueue.isEmpty()) {
      try {
        return eventQueue.dequeueEventUrl();
      } catch(java.util.NoSuchElementException ex) {
      }
    }
    return null;
  }

  private boolean isReadyAndQueEmpty() {
    return (startComplete.get() && eventQueue.isEmpty());
  }

  public void enqueueEvent(PlaynomicsEvent event)
      throws UnsupportedEncodingException {
    boolean isReadyAndQueEmpty = isReadyAndQueEmpty();

    eventQueue.enqueueEvent(event);

    if (isReadyAndQueEmpty)
      handler.sendEmptyMessage(MSG_NEW_EVENT); // Ping the handler
  }

  public void enqueueEventUrl(String url) {
    boolean isReadyAndQueEmpty = isReadyAndQueEmpty();

    eventQueue.enqueueEventUrl(url);

    if (isReadyAndQueEmpty)
      handler.sendEmptyMessage(MSG_NEW_EVENT); // Ping the handler
  }

  public Set<String> getAllUnprocessedEvents() {
    HashSet<String> unprocessedEvents = new HashSet<String>();
    while(!eventQueue.isEmpty()){
      unprocessedEvents.add(eventQueue.dequeueEventUrl());
    }
    return unprocessedEvents;
  }
}




Java Source Code List

com.playnomics.PlaynomicsMoreTestActivity.java
com.playnomics.PlaynomicsTestAppActivity.java
com.playnomics.RichDataFrameDelegate.java
com.playnomics.android.client.AssetClient.java
com.playnomics.android.client.EventQueue.java
com.playnomics.android.client.EventWorker.java
com.playnomics.android.client.HttpConnectionFactory.java
com.playnomics.android.client.IEventQueue.java
com.playnomics.android.client.IEventWorker.java
com.playnomics.android.client.IHttpConnectionFactory.java
com.playnomics.android.client.PlacementDataClient.java
com.playnomics.android.client.SegmentationClient.java
com.playnomics.android.events.AppPageEvent.java
com.playnomics.android.events.AppPauseEvent.java
com.playnomics.android.events.AppResumeEvent.java
com.playnomics.android.events.AppRunningEvent.java
com.playnomics.android.events.AppStartEvent.java
com.playnomics.android.events.CustomEvent.java
com.playnomics.android.events.ExplicitEvent.java
com.playnomics.android.events.ImplicitEvent.java
com.playnomics.android.events.PlaynomicsEvent.java
com.playnomics.android.events.TransactionEvent.java
com.playnomics.android.events.UserInfoEvent.java
com.playnomics.android.messaging.CloseButton.java
com.playnomics.android.messaging.HtmlAdFactory.java
com.playnomics.android.messaging.HtmlAd.java
com.playnomics.android.messaging.HtmlCloseButton.java
com.playnomics.android.messaging.MessagingManager.java
com.playnomics.android.messaging.NativeCloseButton.java
com.playnomics.android.messaging.Placement.java
com.playnomics.android.messaging.Position.java
com.playnomics.android.messaging.Target.java
com.playnomics.android.messaging.ui.IPlayViewFactory.java
com.playnomics.android.messaging.ui.PlayDialog.java
com.playnomics.android.messaging.ui.PlayViewFactory.java
com.playnomics.android.messaging.ui.PlayWebView.java
com.playnomics.android.messaging.ui.RenderTaskFactory.java
com.playnomics.android.push.GcmBroadcastReceiver.java
com.playnomics.android.push.GcmIntentService.java
com.playnomics.android.push.GcmManager.java
com.playnomics.android.sdk.IGoogleCloudMessageConfig.java
com.playnomics.android.sdk.IPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementRawDelegate.java
com.playnomics.android.sdk.IPlaynomicsSegmentationDelegate.java
com.playnomics.android.sdk.IPushConfig.java
com.playnomics.android.sdk.IPushNotificationDelegate.java
com.playnomics.android.sdk.Playnomics.java
com.playnomics.android.segments.UserSegmentIds.java
com.playnomics.android.session.ActivityObserver.java
com.playnomics.android.session.GameSessionInfo.java
com.playnomics.android.session.HeartBeatHandler.java
com.playnomics.android.session.HeartBeatProducer.java
com.playnomics.android.session.IActivityObserver.java
com.playnomics.android.session.ICallbackProcessor.java
com.playnomics.android.session.IHeartBeatProducer.java
com.playnomics.android.session.SessionStateMachine.java
com.playnomics.android.session.Session.java
com.playnomics.android.session.TouchEventHandler.java
com.playnomics.android.session.WindowCallbackProxy.java
com.playnomics.android.util.AndroidLogger.java
com.playnomics.android.util.AsyncTaskRunner.java
com.playnomics.android.util.CacheFile.java
com.playnomics.android.util.Config.java
com.playnomics.android.util.ContextWrapper.java
com.playnomics.android.util.EventTime.java
com.playnomics.android.util.IAsyncCall.java
com.playnomics.android.util.IConfig.java
com.playnomics.android.util.IRandomGenerator.java
com.playnomics.android.util.LargeGeneratedId.java
com.playnomics.android.util.LogWriter.java
com.playnomics.android.util.Logger.java
com.playnomics.android.util.Util.java