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

Commit 8f162f1

Browse files
authored
Merge pull request #43 from C0mm4nd/master
Better async
2 parents 4339890 + 076a245 commit 8f162f1

File tree

5 files changed

+126
-120
lines changed

5 files changed

+126
-120
lines changed

pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@
1515
<artifactId>json</artifactId>
1616
<version>20160810</version>
1717
</dependency>
18-
<dependency>
19-
<groupId>org.springframework.boot</groupId>
20-
<artifactId>spring-boot-starter</artifactId>
21-
</dependency>
22-
<dependency>
23-
<groupId>org.springframework</groupId>
24-
<artifactId>spring-context</artifactId>
25-
<version>4.3.6.RELEASE</version>
26-
</dependency>
2718
</dependencies>
2819
<properties>
2920
<maven.compiler.source>1.8</maven.compiler.source>
@@ -47,10 +38,6 @@
4738
</descriptorRefs>
4839
</configuration>
4940
</plugin>
50-
<plugin>
51-
<groupId>org.springframework.boot</groupId>
52-
<artifactId>spring-boot-maven-plugin</artifactId>
53-
</plugin>
5441
</plugins>
5542
</build>
5643
</project>

src/main/java/me/checkium/vhackapi/Utils.java

Lines changed: 107 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
import java.nio.charset.Charset;
1010
import java.security.MessageDigest;
1111
import java.security.NoSuchAlgorithmException;
12+
import java.util.concurrent.Callable;
13+
import java.util.concurrent.ExecutionException;
14+
import java.util.concurrent.ExecutorService;
15+
import java.util.concurrent.Executors;
1216
import java.util.concurrent.Future;
13-
import java.util.concurrent.TimeUnit;
1417

1518
import org.json.JSONException;
1619
import org.json.JSONObject;
17-
import org.springframework.scheduling.annotation.Async;
18-
import org.springframework.scheduling.annotation.AsyncResult;
1920

2021

2122
public class Utils {
2223

24+
private static WaitingTask task;
25+
private static ExecutorService executor;
26+
public static Future<String> jsonTextC;
2327
/**
2428
* The url of the current api.<br>
2529
* As of now it is {@value url}.
@@ -80,45 +84,56 @@ public static String readJson(Reader rd) throws IOException {
8084
* @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>
8185
* Example "vh_network.php"
8286
* @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.
87+
* @throws ExecutionException
88+
* @throws InterruptedException
8389
*/
8490
public static JSONObject JSONRequest(String format, String data, String php){
85-
JSONObject json = null;
86-
Future<String> jsonTextC = Request(format, data, php);
87-
String jsonText = "";
88-
try{
89-
if(jsonTextC.isDone() /*|| jsonTextC.get() != ""*/){
9091

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

93-
} else {
95+
@Override
96+
public JSONObject call(){
97+
JSONObject json = null;
98+
jsonTextC = Request(format, data, php);
99+
String jsonText = "";
100+
if(task != null)
101+
executor.submit(task);
102+
try{
94103

95-
Thread.sleep(1000);
96-
jsonText = jsonTextC.get(2000, TimeUnit.MILLISECONDS);
97-
98-
}
99-
} catch(Exception e) {
104+
jsonText = jsonTextC.get();
105+
106+
} catch(Exception e) {
100107

101-
try {
102-
Thread.sleep(1000);
103-
} catch (InterruptedException e1) {
108+
try {
109+
Thread.sleep(1000);
110+
} catch (InterruptedException e1) {
104111

105-
}
106-
JSONRequest(format,data,php);
112+
}
113+
JSONRequest(format,data,php);
107114

108-
}
109-
if("".equals(jsonText))
110-
{
111-
throw new RuntimeException("Old API URL");
112-
}
113-
else if("8".equals(jsonText))
114-
{
115-
throw new RuntimeException("Wrong Password/User");
116-
}
117-
else if (jsonText.length() == 1) {
118-
return null;
115+
}
116+
if("".equals(jsonText))
117+
{
118+
throw new RuntimeException("Old API URL");
119+
}
120+
else if("8".equals(jsonText))
121+
{
122+
throw new RuntimeException("Wrong Password/User");
123+
}
124+
else if (jsonText.length() == 1) {
125+
return null;
126+
}
127+
json = new JSONObject(jsonText);
128+
return json;
129+
}
130+
});
131+
try {
132+
return cmsoon.get();
133+
} catch (Exception e){
134+
JSONRequest(format,data,php);
119135
}
120-
json = new JSONObject(jsonText);
121-
return json;
136+
return null;
122137
}
123138

124139
//it'll just do the request without any checks
@@ -136,59 +151,71 @@ else if (jsonText.length() == 1) {
136151
* @return The resulte Json as a Future<String>.
137152
*/
138153
//JDOC needs rewriting
139-
@Async
140154
public static Future<String> Request(String format, String data, String php)
141155
{
142-
143-
Future<String> jText;
144-
System.setProperty("http.agent", "Chrome");
145-
InputStream is;
146-
try {
147-
is = new URL(Utils.generateURL(format, data, php)).openStream();
148-
if(debug == true){
156+
157+
Future<String> result = executor.submit(new Callable<String>(){
158+
@Override
159+
public String call() {
160+
String jText;
161+
System.setProperty("http.agent", "Chrome");
162+
InputStream is;
163+
try {
164+
is = new URL(Utils.generateURL(format, data, php)).openStream();
165+
if(debug == true){
149166

150-
URL url = new URL(Utils.generateURL(format, data, php));
151-
System.out.println(url.toString());
167+
URL url = new URL(Utils.generateURL(format, data, php));
168+
System.out.println(url.toString());
152169

170+
}
171+
Thread.sleep(1000);
172+
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
173+
jText = Utils.readJson(rd);
174+
return jText;
175+
} catch (Exception e) {
176+
try {
177+
Thread.sleep(1000);
178+
} catch (InterruptedException e1) {
179+
e1.printStackTrace();
180+
}
181+
Request(format,data,php);
182+
}
183+
184+
return null;
153185
}
154-
Thread.sleep(1000);
155-
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
156-
jText = new AsyncResult<String>(Utils.readJson(rd));
157-
return jText;
158-
} catch (Exception e) {
159-
try {
160-
Thread.sleep(1000);
161-
} catch (InterruptedException e1) {
162-
e1.printStackTrace();
163-
}
164-
Request(format,data,php);
165-
}
166-
return null;
186+
});
187+
return result;
167188
}
168189

169190
public static String StringRequest(String format, String data, String php)
170191
{
171-
172-
Future<String> jsonTextC = Request(format, data, php);
173-
String jsonText = "";
174-
try{
175-
if(jsonTextC.isDone()){
176-
177-
jsonText = jsonTextC.get();
178-
return jsonText;
179-
180-
} else {
181-
182-
Thread.sleep(1000);
183-
jsonText = jsonTextC.get(1000, TimeUnit.MILLISECONDS);
184-
return jsonText;
192+
Future<String> cmsoon = executor.submit(new Callable<String>(){
193+
@Override
194+
public String call() {
195+
jsonTextC = Request(format, data, php);
196+
String jsonText = "";
197+
try{
198+
jsonText = jsonTextC.get();
199+
return jsonText;
185200

186-
}
187-
} catch(Exception e) {
201+
} catch(Exception e) {
188202

189-
StringRequest(format,data,php);
203+
try {
204+
Thread.sleep(1000);
205+
} catch (InterruptedException e1) {
206+
207+
}
208+
StringRequest(format,data,php);
190209

191-
}
210+
}
211+
return null;
212+
}
213+
});
214+
try {
215+
return cmsoon.get();
216+
} catch (Exception e){
217+
StringRequest(format,data,php);
218+
}
192219
return null;
193220

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

206233
}
207234

235+
public static <T extends WaitingTask> void setWaitingTask(T wt){
236+
237+
task = wt;
238+
239+
}
240+
208241
/**
209242
* Sets a proxy that requires auth for the system
210243
* @param proxyUrl The proxy's IP/URL
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package me.checkium.vhackapi;
2+
3+
public abstract class WaitingTask implements Runnable{
4+
5+
public abstract void run();
6+
7+
}

src/main/java/me/checkium/vhackapi/console/TransferResult.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class TransferResult {
99
protected int moneyamount;
1010
protected int repgained;
1111
protected int replost;
12-
protected String Ip;
12+
protected String ip;
1313

14-
public TransferResult(JSONObject result, String IP) throws JSONException {
15-
Ip = IP;
14+
public TransferResult(JSONObject result, String ip) throws JSONException {
15+
this.ip = ip;
1616
success = result.getString("result").contains("0");
1717
if (!success) return;
1818
try {
@@ -35,7 +35,7 @@ public boolean getSuccess() {
3535
}
3636

3737
public String getTarget() {
38-
return Ip;
38+
return ip;
3939
}
4040

4141
public int getMoneyAmount() {

src/main/java/me/checkium/vhackapi/upgrades/UpgradeManager.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.concurrent.TimeUnit;
55

66
import org.json.JSONArray;
7-
import org.json.JSONException;
87
import org.json.JSONObject;
98

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

2625
JSONObject json = new JSONObject();
27-
try {
28-
TimeUnit.MILLISECONDS.sleep(100);
29-
} catch (InterruptedException e) {
30-
e.printStackTrace();
31-
}
3226
json = Utils.JSONRequest("user::::pass::::uhash::::utype", username + "::::" + password + "::::" + userHash + "::::" + type.toString(), "vh_addUpdate.php");
3327
int result = json.getInt("result");
3428
if (result == 1) {
@@ -47,29 +41,14 @@ public UpgradeResult addUpdate(UpgradeType type) {
4741
public ArrayList<Task> getTasks() {
4842
ArrayList<Task> array = new ArrayList<Task>();
4943
JSONObject json = new JSONObject();
50-
try {
51-
TimeUnit.MILLISECONDS.sleep(100);
52-
53-
JSONArray arrayy;
54-
try {
55-
json = Utils.JSONRequest("user::::pass::::uhash", username + "::::" + password + "::::" + userHash, "vh_tasks.php");
56-
57-
58-
arrayy = json.getJSONArray("data");
59-
} catch (JSONException e) {
60-
return array;
61-
}
62-
63-
for (int i = 0; i < arrayy.length(); i++) {
64-
JSONObject object = arrayy.getJSONObject(i);
65-
Task task = new Task(object);
66-
array.add(task);
67-
}
68-
69-
70-
} catch (InterruptedException e) {
71-
e.printStackTrace();
72-
}
44+
JSONArray arrayy;
45+
json = Utils.JSONRequest("user::::pass::::uhash", username + "::::" + password + "::::" + userHash, "vh_tasks.php");
46+
arrayy = json.getJSONArray("data");
47+
for (int i = 0; i < arrayy.length(); i++) {
48+
JSONObject object = arrayy.getJSONObject(i);
49+
Task task = new Task(object);
50+
array.add(task);
51+
}
7352
return array;
7453
}
7554

0 commit comments

Comments
 (0)