Android Open Source - OHC-android Transaction_generator






From Project

Back to project page OHC-android.

License

The source code is released under:

MIT License

If you think the Android project OHC-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 io.openhc.ohc.skynet.transaction;
/* w  ww  .  j  ava 2 s  .co m*/
import org.json.JSONException;
import org.json.JSONObject;

import java.util.UUID;
import java.util.logging.Level;

import io.openhc.ohc.OHC;

public class Transaction_generator
{
  public static int DEFAULT_RETRY_COUNT = 5;

  public Transaction generate_transaction(JSONObject json)
  {
    return generate_transaction(json, DEFAULT_RETRY_COUNT);
  }

  public Transaction generate_transaction(JSONObject json, int tries)
  {
    UUID uuid = UUID.randomUUID();
    try
    {
      return new Transaction(json, uuid, tries);
    }
    catch(JSONException ex)
    {
      /* This should NEVER, absolutely __NEVER__ happen (except if you hand in a broken JSON object)
       * But that's why it's called an exception, isn't it?*/
      OHC.logger.log(Level.SEVERE, "Transaction generator dying x(");
    }
    return null;
  }

  public class Transaction
  {
    public final String UUID_KEY = "transaction_uuid";

    private JSONObject json_request;
    private JSONObject json_response;
    private UUID uuid;
    private int retry_counter;
    private int max_retry_num;

    protected Transaction(JSONObject json, UUID uuid, int max_retry) throws JSONException
    {
      this.json_request = json;
      this.uuid = uuid;
      this.max_retry_num = max_retry;
      try
      {
        json.put(UUID_KEY, this.get_uuid());
      }
      catch(JSONException ex)
      {
        OHC.logger.log(Level.SEVERE, "FATAL ERROR! FAILED TO SET TRANSACTION ID!");
        throw ex;
      }
    }

    public String get_uuid()
    {
      return this.uuid.toString();
    }

    public JSONObject get_json()
    {
      return this.json_request;
    }

    public int get_retry_counter()
    {
      return this.retry_counter;
    }

    public void inc_retry_counter()
    {
      this.retry_counter++;
    }

    public void set_response(JSONObject response)
    {
      this.json_response = response;
    }

    public JSONObject get_response()
    {
      return this.json_response;
    }

    public boolean is_valid_response(JSONObject json)
    {
      try
      {
        return this.get_uuid().equals(json.getString(UUID_KEY));
      }
      catch(Exception ex)
      {
        return false;
      }
    }

    public boolean do_retry()
    {
      return this.retry_counter < this.max_retry_num || this.max_retry_num == 0;
    }

    public void reset()
    {
      this.uuid = UUID.randomUUID();
      this.retry_counter = 0;
      this.json_response = null;
    }
  }
}




Java Source Code List

io.openhc.ohc.ApplicationTest.java
io.openhc.ohc.OHC.java
io.openhc.ohc.OHC_ui.java
io.openhc.ohc.basestation.Basestation.java
io.openhc.ohc.basestation.device.Device.java
io.openhc.ohc.basestation.device.Field.java
io.openhc.ohc.basestation.rpc.Base_rpc.java
io.openhc.ohc.logging.OHC_Logger.java
io.openhc.ohc.skynet.Broadcaster.java
io.openhc.ohc.skynet.Network.java
io.openhc.ohc.skynet.Receiver.java
io.openhc.ohc.skynet.Sender.java
io.openhc.ohc.skynet.Socket_timeout.java
io.openhc.ohc.skynet.transaction.Transaction_generator.java
io.openhc.ohc.ui.Field_adapter.java
io.openhc.ohc.ui.input.filter.Input_filter_float.java
io.openhc.ohc.ui.input.filter.Input_filter_int.java
io.openhc.ohc.ui.input.filter.Input_filter_string.java