Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Better async #43

Merged
merged 2 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand All @@ -47,10 +38,6 @@
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
181 changes: 107 additions & 74 deletions src/main/java/me/checkium/vhackapi/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;


public class Utils {

private static WaitingTask task;
private static ExecutorService executor;
public static Future<String> jsonTextC;
/**
* The url of the current api.<br>
* As of now it is {@value url}.
Expand Down Expand Up @@ -80,45 +84,56 @@ public static String readJson(Reader rd) throws IOException {
* @param php This is the api endpoint that the request will be sent to. In the case of the vHackAPI it are php documents.<br>
* Example "vh_network.php"
* @return The resulte Json as a JSONObject. Errors are thrown if user/password is wrong and (possibly) if the api url changed. null is returned if there are other errors.
* @throws ExecutionException
* @throws InterruptedException
*/
public static JSONObject JSONRequest(String format, String data, String php){
JSONObject json = null;
Future<String> jsonTextC = Request(format, data, php);
String jsonText = "";
try{
if(jsonTextC.isDone() /*|| jsonTextC.get() != ""*/){

jsonText = jsonTextC.get(2000, TimeUnit.MILLISECONDS);
executor = Executors.newFixedThreadPool(3);
Future<JSONObject> cmsoon = executor.submit(new Callable<JSONObject>(){

} else {
@Override
public JSONObject call(){
JSONObject json = null;
jsonTextC = Request(format, data, php);
String jsonText = "";
if(task != null)
executor.submit(task);
try{

Thread.sleep(1000);
jsonText = jsonTextC.get(2000, TimeUnit.MILLISECONDS);

}
} catch(Exception e) {
jsonText = jsonTextC.get();

} catch(Exception e) {

try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {

}
JSONRequest(format,data,php);
}
JSONRequest(format,data,php);

}
if("".equals(jsonText))
{
throw new RuntimeException("Old API URL");
}
else if("8".equals(jsonText))
{
throw new RuntimeException("Wrong Password/User");
}
else if (jsonText.length() == 1) {
return null;
}
if("".equals(jsonText))
{
throw new RuntimeException("Old API URL");
}
else if("8".equals(jsonText))
{
throw new RuntimeException("Wrong Password/User");
}
else if (jsonText.length() == 1) {
return null;
}
json = new JSONObject(jsonText);
return json;
}
});
try {
return cmsoon.get();
} catch (Exception e){
JSONRequest(format,data,php);
}
json = new JSONObject(jsonText);
return json;
return null;
}

//it'll just do the request without any checks
Expand All @@ -136,59 +151,71 @@ else if (jsonText.length() == 1) {
* @return The resulte Json as a Future<String>.
*/
//JDOC needs rewriting
@Async
public static Future<String> Request(String format, String data, String php)
{

Future<String> jText;
System.setProperty("http.agent", "Chrome");
InputStream is;
try {
is = new URL(Utils.generateURL(format, data, php)).openStream();
if(debug == true){

Future<String> result = executor.submit(new Callable<String>(){
@Override
public String call() {
String jText;
System.setProperty("http.agent", "Chrome");
InputStream is;
try {
is = new URL(Utils.generateURL(format, data, php)).openStream();
if(debug == true){

URL url = new URL(Utils.generateURL(format, data, php));
System.out.println(url.toString());
URL url = new URL(Utils.generateURL(format, data, php));
System.out.println(url.toString());

}
Thread.sleep(1000);
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
jText = Utils.readJson(rd);
return jText;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Request(format,data,php);
}

return null;
}
Thread.sleep(1000);
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
jText = new AsyncResult<String>(Utils.readJson(rd));
return jText;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Request(format,data,php);
}
return null;
});
return result;
}

public static String StringRequest(String format, String data, String php)
{

Future<String> jsonTextC = Request(format, data, php);
String jsonText = "";
try{
if(jsonTextC.isDone()){

jsonText = jsonTextC.get();
return jsonText;

} else {

Thread.sleep(1000);
jsonText = jsonTextC.get(1000, TimeUnit.MILLISECONDS);
return jsonText;
Future<String> cmsoon = executor.submit(new Callable<String>(){
@Override
public String call() {
jsonTextC = Request(format, data, php);
String jsonText = "";
try{
jsonText = jsonTextC.get();
return jsonText;

}
} catch(Exception e) {
} catch(Exception e) {

StringRequest(format,data,php);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {

}
StringRequest(format,data,php);

}
}
return null;
}
});
try {
return cmsoon.get();
} catch (Exception e){
StringRequest(format,data,php);
}
return null;

}
Expand All @@ -205,6 +232,12 @@ public static void useProxy(String proxyUrl, int proxyPort){

}

public static <T extends WaitingTask> void setWaitingTask(T wt){

task = wt;

}

/**
* Sets a proxy that requires auth for the system
* @param proxyUrl The proxy's IP/URL
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/checkium/vhackapi/WaitingTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.checkium.vhackapi;

public abstract class WaitingTask implements Runnable{

public abstract void run();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class TransferResult {
protected int moneyamount;
protected int repgained;
protected int replost;
protected String Ip;
protected String ip;

public TransferResult(JSONObject result, String IP) throws JSONException {
Ip = IP;
public TransferResult(JSONObject result, String ip) throws JSONException {
this.ip = ip;
success = result.getString("result").contains("0");
if (!success) return;
try {
Expand All @@ -35,7 +35,7 @@ public boolean getSuccess() {
}

public String getTarget() {
return Ip;
return ip;
}

public int getMoneyAmount() {
Expand Down
37 changes: 8 additions & 29 deletions src/main/java/me/checkium/vhackapi/upgrades/UpgradeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.concurrent.TimeUnit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import me.checkium.vhackapi.Utils;
Expand All @@ -24,11 +23,6 @@ public UpgradeManager(String user, String pass, String uhash) {
public UpgradeResult addUpdate(UpgradeType type) {

JSONObject json = new JSONObject();
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
json = Utils.JSONRequest("user::::pass::::uhash::::utype", username + "::::" + password + "::::" + userHash + "::::" + type.toString(), "vh_addUpdate.php");
int result = json.getInt("result");
if (result == 1) {
Expand All @@ -47,29 +41,14 @@ public UpgradeResult addUpdate(UpgradeType type) {
public ArrayList<Task> getTasks() {
ArrayList<Task> array = new ArrayList<Task>();
JSONObject json = new JSONObject();
try {
TimeUnit.MILLISECONDS.sleep(100);

JSONArray arrayy;
try {
json = Utils.JSONRequest("user::::pass::::uhash", username + "::::" + password + "::::" + userHash, "vh_tasks.php");


arrayy = json.getJSONArray("data");
} catch (JSONException e) {
return array;
}

for (int i = 0; i < arrayy.length(); i++) {
JSONObject object = arrayy.getJSONObject(i);
Task task = new Task(object);
array.add(task);
}


} catch (InterruptedException e) {
e.printStackTrace();
}
JSONArray arrayy;
json = Utils.JSONRequest("user::::pass::::uhash", username + "::::" + password + "::::" + userHash, "vh_tasks.php");
arrayy = json.getJSONArray("data");
for (int i = 0; i < arrayy.length(); i++) {
JSONObject object = arrayy.getJSONObject(i);
Task task = new Task(object);
array.add(task);
}
return array;
}

Expand Down