From 8c929e2dc09380238ac5db01b7ffc280f74464bc Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Sat, 26 Nov 2016 19:02:16 -0500 Subject: [PATCH] Reorganizing the code a bit --- .../com/luminiasoft/bitshares/Address.java | 108 ----------- .../java/com/luminiasoft/bitshares/Main.java | 4 +- .../java/com/luminiasoft/bitshares/Test.java | 52 ++++-- .../luminiasoft/bitshares/models/ApiCall.java | 11 +- .../bitshares/ws/GetAccountNameById.java | 176 +++++++++--------- .../bitshares/ws/GetAccountsByAddress.java | 32 +--- 6 files changed, 145 insertions(+), 238 deletions(-) diff --git a/src/main/java/com/luminiasoft/bitshares/Address.java b/src/main/java/com/luminiasoft/bitshares/Address.java index f355399..f2e8703 100644 --- a/src/main/java/com/luminiasoft/bitshares/Address.java +++ b/src/main/java/com/luminiasoft/bitshares/Address.java @@ -33,8 +33,6 @@ public class Address { private ECKey key; private String prefix; - private String accountName = null; - private String accountId = null; public Address(ECKey key) { this.key = key; @@ -56,110 +54,4 @@ public class Address { byte[] pubKeyChecksummed = Bytes.concat(pubKey, Arrays.copyOfRange(checksum, 0, 4)); 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); - } - }; } diff --git a/src/main/java/com/luminiasoft/bitshares/Main.java b/src/main/java/com/luminiasoft/bitshares/Main.java index 07171a7..4099f23 100644 --- a/src/main/java/com/luminiasoft/bitshares/Main.java +++ b/src/main/java/com/luminiasoft/bitshares/Main.java @@ -72,7 +72,9 @@ public class Main { // test.testBip39Opertion(); - test.testAccountNamebyAddress(); +// test.testAccountNamebyAddress(); + + test.testAccountNameById(); // test.testRelativeAccountHistory(); } diff --git a/src/main/java/com/luminiasoft/bitshares/Test.java b/src/main/java/com/luminiasoft/bitshares/Test.java index 9c18cfa..bef0a8d 100644 --- a/src/main/java/com/luminiasoft/bitshares/Test.java +++ b/src/main/java/com/luminiasoft/bitshares/Test.java @@ -55,15 +55,19 @@ public class Test { System.out.println("Got account properties"); System.out.println("id: " + accountProperties.id); } else if (response.result.getClass() == ArrayList.class) { - List l = (List) response.result; - if (l.size() > 0) { - - if (l.get(0).getClass() == AssetAmount.class) { - AssetAmount assetAmount = (AssetAmount) l.get(0); + List list = (List) response.result; + if (list.size() > 0) { + if(list.get(0) instanceof AccountProperties){ + List accountPropertiesList = list; + 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("amount: " + assetAmount.getAmount() + ", asset id: " + assetAmount.getAsset().getObjectId()); - } else if (l.get(0).getClass() == ArrayList.class) { - List sl = (List) l.get(0); + } else if (list.get(0).getClass() == ArrayList.class) { + List sl = (List) list.get(0); if (sl.size() > 0) { String accountId = (String) sl.get(0); System.out.println("account id : " + accountId); @@ -606,11 +610,8 @@ public class Test { public void testAccountNamebyAddress() { BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0); - Address address = new Address(brainKey.getPrivateKey()); - address.getAccountName(); - /*try { - BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0); - Address address = new Address(brainKey.getPrivateKey()); + Address address = new Address(brainKey.getPrivateKey()); + try { // Create a custom SSL context. SSLContext context = null; context = NaiveSSLContext.getInstance("TLS"); @@ -620,8 +621,7 @@ public class Test { factory.setSSLContext(context); WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL); - mWebSocket.addListener(new GetAccountsByAddress(address.toString(), mListener)); - System.out.println("Before connecting"); + mWebSocket.addListener(new GetAccountsByAddress(address, mListener)); mWebSocket.connect(); } catch (IOException e) { System.out.println("IOException. Msg: " + e.getMessage()); @@ -629,7 +629,29 @@ public class Test { System.out.println("WebSocketException. Msg: " + e.getMessage()); } catch (NoSuchAlgorithmException e) { 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() { diff --git a/src/main/java/com/luminiasoft/bitshares/models/ApiCall.java b/src/main/java/com/luminiasoft/bitshares/models/ApiCall.java index b19c35e..91b1426 100644 --- a/src/main/java/com/luminiasoft/bitshares/models/ApiCall.java +++ b/src/main/java/com/luminiasoft/bitshares/models/ApiCall.java @@ -77,12 +77,15 @@ public class ApiCall implements JsonSerializable { methodParams.add((String) this.params.get(i)); }else if(this.params.get(i) instanceof ArrayList){ // Other times it might be an array - System.out.println("es un arreglo"); JsonArray array = new JsonArray(); - ArrayList listArgument = (ArrayList) this.params.get(i); + ArrayList listArgument = (ArrayList) this.params.get(i); for(int l = 0; l < listArgument.size(); l++){ - JsonSerializable s = listArgument.get(l); - array.add(s.toJsonObject()); + Serializable element = listArgument.get(l); + if(element instanceof JsonSerializable) + array.add(((JsonSerializable) element).toJsonObject()); + else if(element instanceof String){ + array.add((String) element); + } } methodParams.add(array); } diff --git a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java index 558a7ec..7a4db25 100644 --- a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java +++ b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java @@ -1,88 +1,88 @@ -package com.luminiasoft.bitshares.ws; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; -import com.google.gson.reflect.TypeToken; -import com.luminiasoft.bitshares.RPC; -import com.luminiasoft.bitshares.interfaces.JsonSerializable; -import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; -import com.luminiasoft.bitshares.models.ApiCall; -import com.luminiasoft.bitshares.models.BaseResponse; -import com.luminiasoft.bitshares.models.WitnessResponse; -import com.neovisionaries.ws.client.WebSocket; -import com.neovisionaries.ws.client.WebSocketAdapter; -import com.neovisionaries.ws.client.WebSocketException; -import com.neovisionaries.ws.client.WebSocketFrame; -import java.io.Serializable; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * - * @author henry - */ -public class GetAccountNameById extends WebSocketAdapter { - - private String accountID; - private WitnessResponseListener mListener; - - public GetAccountNameById(String accountID, WitnessResponseListener listener) { - this.accountID = accountID; - this.mListener = listener; - } - - @Override - public void onConnected(WebSocket websocket, Map> headers) throws Exception { - ArrayList accountParams = new ArrayList(); - ArrayList paramAddress = new ArrayList(); - paramAddress.add(new JsonSerializable() { - @Override - public String toJsonString() { - return accountID; - } - - @Override - public JsonElement toJsonObject() { - return new JsonParser().parse(accountID); - } - }); - accountParams.add(paramAddress); - ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, "2.0", 1); - websocket.sendText(getAccountByAddress.toJsonString()); - } - - @Override - public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - System.out.println(frame.toString()); - String response = frame.getPayloadText(); - Gson gson = new Gson(); - - Type GetAccountByAddressResponse = new TypeToken>>() { - }.getType(); - - WitnessResponse>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); - - if (witnessResponse.error != null) { - this.mListener.onError(witnessResponse.error); - } else { - this.mListener.onSuccess(witnessResponse); - } - - websocket.disconnect(); - } - - @Override - public void onError(WebSocket websocket, WebSocketException cause) throws Exception { - mListener.onError(new BaseResponse.Error(cause.getMessage())); - websocket.disconnect(); - } - - @Override - public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { - mListener.onError(new BaseResponse.Error(cause.getMessage())); - websocket.disconnect(); - } -} +package com.luminiasoft.bitshares.ws; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; +import com.luminiasoft.bitshares.RPC; +import com.luminiasoft.bitshares.interfaces.JsonSerializable; +import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; +import com.luminiasoft.bitshares.models.AccountProperties; +import com.luminiasoft.bitshares.models.ApiCall; +import com.luminiasoft.bitshares.models.BaseResponse; +import com.luminiasoft.bitshares.models.WitnessResponse; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketAdapter; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFrame; +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * + * @author henry + */ +public class GetAccountNameById extends WebSocketAdapter { + + private String accountID; + private WitnessResponseListener mListener; + + public GetAccountNameById(String accountID, WitnessResponseListener listener) { + this.accountID = accountID; + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList accountParams = new ArrayList(); + ArrayList paramAddress = new ArrayList(); + paramAddress.add(accountID); + accountParams.add(paramAddress); + ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, RPC.VERSION, 1); + websocket.sendText(getAccountByAddress.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + System.out.println("<<< "+frame.getPayloadText()); + String response = frame.getPayloadText(); + Gson gson = new Gson(); + + Type GetAccountByAddressResponse = new TypeToken>>() {}.getType(); + WitnessResponse>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); + + if (witnessResponse.error != null) { + this.mListener.onError(witnessResponse.error); + } else { + this.mListener.onSuccess(witnessResponse); + } + websocket.disconnect(); + } + + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + if(frame.isTextFrame()) + System.out.println(">>> "+frame.getPayloadText()); + } + + @Override + public void onError(WebSocket websocket, WebSocketException cause) throws Exception { + System.out.println("onError"); + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } + + @Override + public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { + System.out.println("handleCallbackError"); + StackTraceElement[] stack = cause.getStackTrace(); + for(StackTraceElement element : stack) { + System.out.println("> "+element.getClassName()+"."+element.getMethodName()+" : "+element.getLineNumber()); + } + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } +} diff --git a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java index 6b87d0a..3ffadf3 100644 --- a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java +++ b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; +import com.luminiasoft.bitshares.Address; import com.luminiasoft.bitshares.RPC; import com.luminiasoft.bitshares.interfaces.JsonSerializable; import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; @@ -27,51 +28,38 @@ import java.util.Map; */ public class GetAccountsByAddress extends WebSocketAdapter { - private String publicKey; + private Address address; private WitnessResponseListener mListener; - public GetAccountsByAddress(String publicKey, WitnessResponseListener listener) { - this.publicKey = publicKey; + public GetAccountsByAddress(Address address, WitnessResponseListener listener) { + this.address = address; this.mListener = listener; } @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList accountParams = new ArrayList(); - ArrayList paramAddress = new ArrayList(); - paramAddress.add(new JsonSerializable() { - @Override - public String toJsonString() { - return publicKey; - } - - @Override - public JsonElement toJsonObject() { - return new JsonParser().parse(publicKey); - } - }); + ArrayList paramAddress = new ArrayList(); + paramAddress.add(address.toString()); 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()); } @Override public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - System.out.println(frame.toString()); + System.out.println("<<< "+frame.getPayloadText()); String response = frame.getPayloadText(); Gson gson = new Gson(); - Type GetAccountByAddressResponse = new TypeToken>>() { - }.getType(); - - WitnessResponse>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); + Type GetAccountByAddressResponse = new TypeToken>>(){}.getType(); + WitnessResponse>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); if (witnessResponse.error != null) { this.mListener.onError(witnessResponse.error); } else { this.mListener.onSuccess(witnessResponse); } - websocket.disconnect(); }