Change the insight serverUrl, to make it not a constant but selected by the app

This commit is contained in:
hvarona 2018-09-05 23:43:37 -04:00
parent 08be77e9fb
commit 8cfbb63f1c
5 changed files with 18 additions and 47 deletions

View file

@ -106,15 +106,10 @@ public class AccountActivityWatcher {
* @param mAccount The mAccount to be monitor
* @param mContext This app mContext
*/
public AccountActivityWatcher(GeneralCoinAccount mAccount, Context mContext) {
//String serverUrl = InsightApiConstants.protocol + "://" + InsightApiConstants.getAddress(mAccount.getCoin()) + ":" + InsightApiConstants.getPort(mAccount.getCoin()) + "/"+InsightApiConstants.getRawPath(mAccount.getCoin())+"/mSocket.io/";
String serverUrl = InsightApiConstants.sProtocolSocketIO + "://" + InsightApiConstants.getAddress(mAccount.getCryptoCoin()) + ":" + InsightApiConstants.getPort(mAccount.getCryptoCoin()) + "/";
public AccountActivityWatcher(String serverUrl, GeneralCoinAccount mAccount, Context mContext) {
this.mAccount = mAccount;
this.mContext = mContext;
System.out.println("accountActivityWatcher " + serverUrl);
try {
IO.Options opts = new IO.Options();
System.out.println("accountActivityWatcher default path " + opts.path);
this.mSocket = IO.socket(serverUrl);
this.mSocket.on(Socket.EVENT_CONNECT, onConnect);
this.mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);

View file

@ -37,8 +37,7 @@ public class BroadcastTransaction extends Thread implements Callback<Txi> {
* @param account The account who signs the transaction
* @param context This app context
*/
public BroadcastTransaction(String RawTx, GeneralCoinAccount account, Context context){
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
public BroadcastTransaction(String RawTx, GeneralCoinAccount account, String serverUrl, Context context){
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
this.mContext = context;
this.mRawTx = RawTx;

View file

@ -20,55 +20,31 @@ import retrofit2.Response;
public abstract class GetEstimateFee {
//TODO add a funciton to get the rate of a specific port
/**
* The funciton to get the rate for the transaction be included in the next 2 blocks
* @param coin The coin to get the rate
* @return The rate number (coin/kbytes)
* @throws IOException If the server answer null, or the rate couldn't be calculated
*/
public static long getEstimateFee(final CryptoCoin coin) throws IOException {
String serverUrl = InsightApiConstants.sProtocol + "://"
+ InsightApiConstants.getAddress(coin) + "/";
public static void getEstimateFee(final CryptoCoin coin, String serverUrl, final estimateFeeListener listener) {
InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl);
InsightApiService service = serviceGenerator.getService(InsightApiService.class);
Call<JsonObject> call = service.estimateFee(InsightApiConstants.getPath(coin));
final Object SYNC = new Object();
final JsonObject answer = new JsonObject();
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
synchronized (SYNC) {
answer.addProperty("answer",
(long) (response.body().get("2").getAsDouble()* Math.pow(10, coin.getPrecision())));
SYNC.notifyAll();
}
listener.estimateFee((long) (answer.get("answer").getAsDouble()));
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
synchronized (SYNC) {
SYNC.notifyAll();
}
listener.estimateFee(-1);
}
});
synchronized (SYNC){
for(int i = 0; i < 6; i++) {
try {
SYNC.wait(5000);
} catch (InterruptedException e) {
// this interruption never rises
}
if(answer.get("answer")!=null){
break;
}
}
}
if(answer.get("answer")==null){
throw new IOException("");
}
return (long) (answer.get("answer").getAsDouble());
public static interface estimateFeeListener{
public void estimateFee(long value);
}
}

View file

@ -48,8 +48,7 @@ public class GetTransactionByAddress extends Thread implements Callback<AddressT
* @param account The account to be query
* @param context This app context
*/
public GetTransactionByAddress(GeneralCoinAccount account, Context context) {
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
public GetTransactionByAddress(GeneralCoinAccount account, String serverUrl, Context context) {
this.mAccount = account;
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
this.mContext = context;

View file

@ -36,6 +36,8 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
* This app context, used to save on the DB
*/
private Context mContext;
private String mServerUrl;
/**
* If has to wait for another confirmation
*/
@ -47,8 +49,8 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
* @param account The account to be query
* @param context This app Context
*/
public GetTransactionData(String txid, GeneralCoinAccount account, Context context) {
this(txid, account, context, false);
public GetTransactionData(String txid, GeneralCoinAccount account,String serverUrl, Context context) {
this(txid, account, serverUrl, context, false);
}
/**
@ -58,8 +60,8 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
* @param context This app Context
* @param mustWait If there is less confirmation that needed
*/
public GetTransactionData(String txid, GeneralCoinAccount account, Context context, boolean mustWait) {
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
public GetTransactionData(String txid, GeneralCoinAccount account,String serverUrl, Context context, boolean mustWait) {
this.mServerUrl = serverUrl;
this.mAccount = account;
this.mTxId= txid;
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
@ -179,7 +181,7 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
if (transaction.getConfirm() < this.mAccount.getCryptoNet().getConfirmationsNeeded()) {
//If transaction weren't confirmed, add the transaction to watch for change on the confirmations
new GetTransactionData(this.mTxId, this.mAccount, this.mContext, true).start();
new GetTransactionData(this.mTxId, this.mAccount, this.mServerUrl, this.mContext, true).start();
}
}
}