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

master
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 mAccount The mAccount to be monitor
* @param mContext This app mContext * @param mContext This app mContext
*/ */
public AccountActivityWatcher(GeneralCoinAccount mAccount, Context mContext) { public AccountActivityWatcher(String serverUrl, 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()) + "/";
this.mAccount = mAccount; this.mAccount = mAccount;
this.mContext = mContext; this.mContext = mContext;
System.out.println("accountActivityWatcher " + serverUrl);
try { try {
IO.Options opts = new IO.Options();
System.out.println("accountActivityWatcher default path " + opts.path);
this.mSocket = IO.socket(serverUrl); this.mSocket = IO.socket(serverUrl);
this.mSocket.on(Socket.EVENT_CONNECT, onConnect); this.mSocket.on(Socket.EVENT_CONNECT, onConnect);
this.mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect); 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 account The account who signs the transaction
* @param context This app context * @param context This app context
*/ */
public BroadcastTransaction(String RawTx, GeneralCoinAccount account, Context context){ public BroadcastTransaction(String RawTx, GeneralCoinAccount account, String serverUrl, Context context){
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl); this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
this.mContext = context; this.mContext = context;
this.mRawTx = RawTx; this.mRawTx = RawTx;

View File

@ -20,55 +20,31 @@ import retrofit2.Response;
public abstract class GetEstimateFee { 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 * The funciton to get the rate for the transaction be included in the next 2 blocks
* @param coin The coin to get the rate * @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 { public static void getEstimateFee(final CryptoCoin coin, String serverUrl, final estimateFeeListener listener) {
String serverUrl = InsightApiConstants.sProtocol + "://"
+ InsightApiConstants.getAddress(coin) + "/";
InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl); InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl);
InsightApiService service = serviceGenerator.getService(InsightApiService.class); InsightApiService service = serviceGenerator.getService(InsightApiService.class);
Call<JsonObject> call = service.estimateFee(InsightApiConstants.getPath(coin)); Call<JsonObject> call = service.estimateFee(InsightApiConstants.getPath(coin));
final Object SYNC = new Object();
final JsonObject answer = new JsonObject(); final JsonObject answer = new JsonObject();
call.enqueue(new Callback<JsonObject>() { call.enqueue(new Callback<JsonObject>() {
@Override @Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
synchronized (SYNC) { listener.estimateFee((long) (answer.get("answer").getAsDouble()));
answer.addProperty("answer",
(long) (response.body().get("2").getAsDouble()* Math.pow(10, coin.getPrecision())));
SYNC.notifyAll();
}
} }
@Override @Override
public void onFailure(Call<JsonObject> call, Throwable t) { public void onFailure(Call<JsonObject> call, Throwable t) {
synchronized (SYNC) { listener.estimateFee(-1);
SYNC.notifyAll();
}
} }
}); });
synchronized (SYNC){ }
for(int i = 0; i < 6; i++) {
try { public static interface estimateFeeListener{
SYNC.wait(5000); public void estimateFee(long value);
} 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());
} }
} }

View File

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