Reorganizing the code a bit

This commit is contained in:
Nelson R. Perez 2016-11-26 19:02:16 -05:00
parent 8a5c7e51a4
commit 8c929e2dc0
6 changed files with 145 additions and 238 deletions

View file

@ -33,8 +33,6 @@ public class Address {
private ECKey key; private ECKey key;
private String prefix; private String prefix;
private String accountName = null;
private String accountId = null;
public Address(ECKey key) { public Address(ECKey key) {
this.key = key; this.key = key;
@ -56,110 +54,4 @@ public class Address {
byte[] pubKeyChecksummed = Bytes.concat(pubKey, Arrays.copyOfRange(checksum, 0, 4)); byte[] pubKeyChecksummed = Bytes.concat(pubKey, Arrays.copyOfRange(checksum, 0, 4));
return this.prefix + Base58.encode(pubKeyChecksummed); return this.prefix + Base58.encode(pubKeyChecksummed);
} }
public void getAccountDetail() {
try {
SSLContext context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountsByAddress(this.toString(), accountIdListener));
System.out.println("Before connecting");
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}
}
public String getAccountName() {
return accountName;
}
public String getAccountId() {
return accountId;
}
WitnessResponseListener accountIdListener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
if (response.result.getClass() == ArrayList.class) {
List l = (List) response.result;
if (l.size() > 0) {
if (l.get(0).getClass() == ArrayList.class) {
List sl = (List) l.get(0);
if (sl.size() > 0) {
accountId = (String) sl.get(0);
try {
// Create a custom SSL context.
SSLContext context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountNameById(accountId, accountListener));
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException ex) {
}
} else {
//TODO Error empty answer
}
} else {
//TODO Error bad type of answer
System.out.println("Got empty list!");
}
} else {
//TODO Error bad type of answer
System.out.println("Got empty list!");
}
} else {
//TODO Error in response
System.out.println("accountIdListener Got other: " + response.result.getClass());
}
}
@Override
public void onError(BaseResponse.Error error) {
System.out.println("onError. message: " + error.message);
}
};
WitnessResponseListener accountListener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
if (response.result.getClass() == ArrayList.class) {
List l = (List) response.result;
if (l.size() > 0) {
System.out.println("list class " + l.get(0).getClass());
if (l.get(0).getClass() == LinkedTreeMap.class) {
LinkedTreeMap ltm = (LinkedTreeMap) l.get(0);
accountName = (String) ltm.get("name");
} else {
//TODO Error bad type of answer
System.out.println("Got bad type!");
}
} else {
//TODO Error bad type of answer
System.out.println("Got empty list!");
}
} else {
//TODO Error in response
System.out.println("accountIdListener Got other: " + response.result.getClass());
}
}
@Override
public void onError(BaseResponse.Error error) {
System.out.println("onError. message: " + error.message);
}
};
} }

View file

@ -72,7 +72,9 @@ public class Main {
// test.testBip39Opertion(); // test.testBip39Opertion();
test.testAccountNamebyAddress(); // test.testAccountNamebyAddress();
test.testAccountNameById();
// test.testRelativeAccountHistory(); // test.testRelativeAccountHistory();
} }

View file

@ -55,15 +55,19 @@ public class Test {
System.out.println("Got account properties"); System.out.println("Got account properties");
System.out.println("id: " + accountProperties.id); System.out.println("id: " + accountProperties.id);
} else if (response.result.getClass() == ArrayList.class) { } else if (response.result.getClass() == ArrayList.class) {
List l = (List) response.result; List list = (List) response.result;
if (l.size() > 0) { if (list.size() > 0) {
if(list.get(0) instanceof AccountProperties){
if (l.get(0).getClass() == AssetAmount.class) { List<AccountProperties> accountPropertiesList = list;
AssetAmount assetAmount = (AssetAmount) l.get(0); for(AccountProperties accountProperties : accountPropertiesList){
System.out.println("Account id: "+accountProperties.id);
}
}else if (list.get(0) instanceof AssetAmount) {
AssetAmount assetAmount = (AssetAmount) list.get(0);
System.out.println("Got fee"); System.out.println("Got fee");
System.out.println("amount: " + assetAmount.getAmount() + ", asset id: " + assetAmount.getAsset().getObjectId()); System.out.println("amount: " + assetAmount.getAmount() + ", asset id: " + assetAmount.getAsset().getObjectId());
} else if (l.get(0).getClass() == ArrayList.class) { } else if (list.get(0).getClass() == ArrayList.class) {
List sl = (List) l.get(0); List sl = (List) list.get(0);
if (sl.size() > 0) { if (sl.size() > 0) {
String accountId = (String) sl.get(0); String accountId = (String) sl.get(0);
System.out.println("account id : " + accountId); System.out.println("account id : " + accountId);
@ -606,11 +610,8 @@ public class Test {
public void testAccountNamebyAddress() { public void testAccountNamebyAddress() {
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0); BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
Address address = new Address(brainKey.getPrivateKey()); Address address = new Address(brainKey.getPrivateKey());
address.getAccountName(); try {
/*try {
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
Address address = new Address(brainKey.getPrivateKey());
// Create a custom SSL context. // Create a custom SSL context.
SSLContext context = null; SSLContext context = null;
context = NaiveSSLContext.getInstance("TLS"); context = NaiveSSLContext.getInstance("TLS");
@ -620,8 +621,7 @@ public class Test {
factory.setSSLContext(context); factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL); WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountsByAddress(address.toString(), mListener)); mWebSocket.addListener(new GetAccountsByAddress(address, mListener));
System.out.println("Before connecting");
mWebSocket.connect(); mWebSocket.connect();
} catch (IOException e) { } catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage()); System.out.println("IOException. Msg: " + e.getMessage());
@ -629,7 +629,29 @@ public class Test {
System.out.println("WebSocketException. Msg: " + e.getMessage()); System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage()); System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}*/ }
}
public void testAccountNameById(){
try {
// Create a custom SSL context.
SSLContext context = null;
context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
// Set the custom SSL context.
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountNameById("1.2.138632", mListener));
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}
} }
public void testRelativeAccountHistory() { public void testRelativeAccountHistory() {

View file

@ -77,12 +77,15 @@ public class ApiCall implements JsonSerializable {
methodParams.add((String) this.params.get(i)); methodParams.add((String) this.params.get(i));
}else if(this.params.get(i) instanceof ArrayList){ }else if(this.params.get(i) instanceof ArrayList){
// Other times it might be an array // Other times it might be an array
System.out.println("es un arreglo");
JsonArray array = new JsonArray(); JsonArray array = new JsonArray();
ArrayList<JsonSerializable> listArgument = (ArrayList<JsonSerializable>) this.params.get(i); ArrayList<Serializable> listArgument = (ArrayList<Serializable>) this.params.get(i);
for(int l = 0; l < listArgument.size(); l++){ for(int l = 0; l < listArgument.size(); l++){
JsonSerializable s = listArgument.get(l); Serializable element = listArgument.get(l);
array.add(s.toJsonObject()); if(element instanceof JsonSerializable)
array.add(((JsonSerializable) element).toJsonObject());
else if(element instanceof String){
array.add((String) element);
}
} }
methodParams.add(array); methodParams.add(array);
} }

View file

@ -1,88 +1,88 @@
package com.luminiasoft.bitshares.ws; package com.luminiasoft.bitshares.ws;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.luminiasoft.bitshares.RPC; import com.luminiasoft.bitshares.RPC;
import com.luminiasoft.bitshares.interfaces.JsonSerializable; import com.luminiasoft.bitshares.interfaces.JsonSerializable;
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
import com.luminiasoft.bitshares.models.ApiCall; import com.luminiasoft.bitshares.models.AccountProperties;
import com.luminiasoft.bitshares.models.BaseResponse; import com.luminiasoft.bitshares.models.ApiCall;
import com.luminiasoft.bitshares.models.WitnessResponse; import com.luminiasoft.bitshares.models.BaseResponse;
import com.neovisionaries.ws.client.WebSocket; import com.luminiasoft.bitshares.models.WitnessResponse;
import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketFrame; import com.neovisionaries.ws.client.WebSocketException;
import java.io.Serializable; import com.neovisionaries.ws.client.WebSocketFrame;
import java.lang.reflect.Type; import java.io.Serializable;
import java.util.ArrayList; import java.lang.reflect.Type;
import java.util.List; import java.util.ArrayList;
import java.util.Map; import java.util.List;
import java.util.Map;
/**
* /**
* @author henry *
*/ * @author henry
public class GetAccountNameById extends WebSocketAdapter { */
public class GetAccountNameById extends WebSocketAdapter {
private String accountID;
private WitnessResponseListener mListener; private String accountID;
private WitnessResponseListener mListener;
public GetAccountNameById(String accountID, WitnessResponseListener listener) {
this.accountID = accountID; public GetAccountNameById(String accountID, WitnessResponseListener listener) {
this.mListener = listener; this.accountID = accountID;
} this.mListener = listener;
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { @Override
ArrayList<Serializable> accountParams = new ArrayList(); public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<JsonSerializable> paramAddress = new ArrayList(); ArrayList<Serializable> accountParams = new ArrayList();
paramAddress.add(new JsonSerializable() { ArrayList<Serializable> paramAddress = new ArrayList();
@Override paramAddress.add(accountID);
public String toJsonString() { accountParams.add(paramAddress);
return accountID; ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, RPC.VERSION, 1);
} websocket.sendText(getAccountByAddress.toJsonString());
}
@Override
public JsonElement toJsonObject() { @Override
return new JsonParser().parse(accountID); public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} System.out.println("<<< "+frame.getPayloadText());
}); String response = frame.getPayloadText();
accountParams.add(paramAddress); Gson gson = new Gson();
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, "2.0", 1);
websocket.sendText(getAccountByAddress.toJsonString()); Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<AccountProperties>>>() {}.getType();
} WitnessResponse<WitnessResponse<List<AccountProperties>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
@Override if (witnessResponse.error != null) {
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { this.mListener.onError(witnessResponse.error);
System.out.println(frame.toString()); } else {
String response = frame.getPayloadText(); this.mListener.onSuccess(witnessResponse);
Gson gson = new Gson(); }
websocket.disconnect();
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<Object>>>() { }
}.getType();
@Override
WitnessResponse<WitnessResponse<List<Object>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
if(frame.isTextFrame())
if (witnessResponse.error != null) { System.out.println(">>> "+frame.getPayloadText());
this.mListener.onError(witnessResponse.error); }
} else {
this.mListener.onSuccess(witnessResponse); @Override
} public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
System.out.println("onError");
websocket.disconnect(); mListener.onError(new BaseResponse.Error(cause.getMessage()));
} websocket.disconnect();
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception { @Override
mListener.onError(new BaseResponse.Error(cause.getMessage())); public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
websocket.disconnect(); System.out.println("handleCallbackError");
} StackTraceElement[] stack = cause.getStackTrace();
for(StackTraceElement element : stack) {
@Override System.out.println("> "+element.getClassName()+"."+element.getMethodName()+" : "+element.getLineNumber());
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { }
mListener.onError(new BaseResponse.Error(cause.getMessage())); mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect(); websocket.disconnect();
} }
} }

View file

@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.luminiasoft.bitshares.Address;
import com.luminiasoft.bitshares.RPC; import com.luminiasoft.bitshares.RPC;
import com.luminiasoft.bitshares.interfaces.JsonSerializable; import com.luminiasoft.bitshares.interfaces.JsonSerializable;
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
@ -27,51 +28,38 @@ import java.util.Map;
*/ */
public class GetAccountsByAddress extends WebSocketAdapter { public class GetAccountsByAddress extends WebSocketAdapter {
private String publicKey; private Address address;
private WitnessResponseListener mListener; private WitnessResponseListener mListener;
public GetAccountsByAddress(String publicKey, WitnessResponseListener listener) { public GetAccountsByAddress(Address address, WitnessResponseListener listener) {
this.publicKey = publicKey; this.address = address;
this.mListener = listener; this.mListener = listener;
} }
@Override @Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList(); ArrayList<Serializable> accountParams = new ArrayList();
ArrayList<JsonSerializable> paramAddress = new ArrayList(); ArrayList<Serializable> paramAddress = new ArrayList();
paramAddress.add(new JsonSerializable() { paramAddress.add(address.toString());
@Override
public String toJsonString() {
return publicKey;
}
@Override
public JsonElement toJsonObject() {
return new JsonParser().parse(publicKey);
}
});
accountParams.add(paramAddress); accountParams.add(paramAddress);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, "2.0", 1); ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, RPC.VERSION, 1);
websocket.sendText(getAccountByAddress.toJsonString()); websocket.sendText(getAccountByAddress.toJsonString());
} }
@Override @Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
System.out.println(frame.toString()); System.out.println("<<< "+frame.getPayloadText());
String response = frame.getPayloadText(); String response = frame.getPayloadText();
Gson gson = new Gson(); Gson gson = new Gson();
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<Object>>>() { Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<String>>>(){}.getType();
}.getType(); WitnessResponse<WitnessResponse<List<String>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
WitnessResponse<WitnessResponse<List<Object>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
if (witnessResponse.error != null) { if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error); this.mListener.onError(witnessResponse.error);
} else { } else {
this.mListener.onSuccess(witnessResponse); this.mListener.onSuccess(witnessResponse);
} }
websocket.disconnect(); websocket.disconnect();
} }