All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.savl.ripple.client.transactions.ManagedTxn Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.savl.ripple.client.transactions;

import com.savl.ripple.client.pubsub.CallbackContext;
import com.savl.ripple.client.pubsub.Publisher;
import com.savl.ripple.client.pubsub.Publisher.Callback;
import com.savl.ripple.client.requests.Request;
import com.savl.ripple.client.responses.Response;
import com.savl.ripple.core.coretypes.Amount;
import com.savl.ripple.core.coretypes.hash.Hash256;
import com.savl.ripple.core.coretypes.uint.UInt32;
import com.savl.ripple.core.types.known.tx.Transaction;
import com.savl.ripple.core.types.known.tx.result.TransactionResult;
import com.savl.ripple.core.types.known.tx.signed.SignedTransaction;

import java.util.ArrayList;
import java.util.TreeSet;

public class ManagedTxn extends SignedTransaction {
    public static interface events extends Callback {}
    public static interface OnSubmitSuccess extends events {}
    public static interface OnSubmitFailure extends events {}
    public static interface OnSubmitError extends events {}
    public static interface OnTransactionValidated extends events {}

    public TransactionResult result;

    public ManagedTxn onValidated(final Callback handler) {
        on(OnTransactionValidated.class, new OnTransactionValidated() {
            @Override
            public void called(TransactionResult args) {
                result = args;
                handler.called(ManagedTxn.this);
            }
        });
        return this;
    }

    public ManagedTxn onError(final Callback cb) {
        on(OnSubmitFailure.class, new OnSubmitFailure() {
            @Override
            public void called(Response args) {
                cb.called(ManagedTxn.this);
            }
        });
        on(OnSubmitError.class, new OnSubmitError() {
            @Override
            public void called(Response args) {
                cb.called(ManagedTxn.this);
            }
        });
        return this;
    }

    public  boolean removeListener(Class key, Callback cb) {
        return publisher.removeListener(key, cb);
    }

    public  int emit(Class key, Object args) {
        return publisher.emit(key, args);
    }

    public  void once(Class key, CallbackContext executor, T cb) {
        publisher.once(key, executor, cb);
    }

    public  void once(Class key, T cb) {
        publisher.once(key, cb);
    }

    public  void on(Class key, CallbackContext executor, T cb) {
        publisher.on(key, executor, cb);
    }

    public  void on(Class key, T cb) {
        publisher.on(key, cb);
    }

    public Publisher publisher() {
        return publisher;
    }

    private boolean isSequencePlug;
    public boolean isSequencePlug() {
        return isSequencePlug;
    }
    public void setSequencePlug(boolean isNoop) {
        this.isSequencePlug = isNoop;
        setDescription("SequencePlug");
    }

    private String description;
    public String description() {
        if (description == null) {
            return txn.transactionType().toString();
        }
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }


    public ManagedTxn(Transaction txn) {
        this.txn = txn;
    }
    private final Publisher publisher = new Publisher();
//    private final MyTransaction publisher = new MyTransaction();
    private boolean finalized = false;

    public boolean responseWasToLastSubmission(Response res) {
        Request req = lastSubmission().request;
        return res.request == req;
    }


    public boolean finalizedOrResponseIsToPriorSubmission(Response res) {
        return isFinalized() || !responseWasToLastSubmission(res);
    }

    public ArrayList submissions = new ArrayList();

    public Submission lastSubmission() {
        if (submissions.isEmpty()) {
            return null;
        } else {
            return submissions.get(submissions.size() - 1);
        }
    }
    private TreeSet submittedIDs = new TreeSet();

    public boolean isFinalized() {
        return finalized;
    }

    public void setFinalized() {
        finalized = true;
    }

    public void trackSubmitRequest(Request submitRequest, long ledger_index) {
        Submission submission = new Submission(submitRequest,
                                               sequence(),
                                               hash,
                                               ledger_index,
                                               txn.get(Amount.Fee),
                                               txn.get(UInt32.LastLedgerSequence));
        submissions.add(submission);
        trackSubmittedID();
    }

    public void trackSubmittedID() {
        submittedIDs.add(hash);
    }

    boolean wasSubmittedWith(Hash256 hash) {
        return submittedIDs.contains(hash);
    }

    public UInt32 sequence() {
        return txn.sequence();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy