9
9
import java .nio .charset .Charset ;
10
10
import java .security .MessageDigest ;
11
11
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 ;
12
16
import java .util .concurrent .Future ;
13
- import java .util .concurrent .TimeUnit ;
14
17
15
18
import org .json .JSONException ;
16
19
import org .json .JSONObject ;
17
- import org .springframework .scheduling .annotation .Async ;
18
- import org .springframework .scheduling .annotation .AsyncResult ;
19
20
20
21
21
22
public class Utils {
22
23
24
+ private static WaitingTask task ;
25
+ private static ExecutorService executor ;
26
+ public static Future <String > jsonTextC ;
23
27
/**
24
28
* The url of the current api.<br>
25
29
* As of now it is {@value url}.
@@ -80,45 +84,56 @@ public static String readJson(Reader rd) throws IOException {
80
84
* @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>
81
85
* Example "vh_network.php"
82
86
* @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
83
89
*/
84
90
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() != ""*/ ){
90
91
91
- jsonText = jsonTextC .get (2000 , TimeUnit .MILLISECONDS );
92
+ executor = Executors .newFixedThreadPool (3 );
93
+ Future <JSONObject > cmsoon = executor .submit (new Callable <JSONObject >(){
92
94
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 {
94
103
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 ) {
100
107
101
- try {
102
- Thread .sleep (1000 );
103
- } catch (InterruptedException e1 ) {
108
+ try {
109
+ Thread .sleep (1000 );
110
+ } catch (InterruptedException e1 ) {
104
111
105
- }
106
- JSONRequest (format ,data ,php );
112
+ }
113
+ JSONRequest (format ,data ,php );
107
114
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 );
119
135
}
120
- json = new JSONObject (jsonText );
121
- return json ;
136
+ return null ;
122
137
}
123
138
124
139
//it'll just do the request without any checks
@@ -136,59 +151,71 @@ else if (jsonText.length() == 1) {
136
151
* @return The resulte Json as a Future<String>.
137
152
*/
138
153
//JDOC needs rewriting
139
- @ Async
140
154
public static Future <String > Request (String format , String data , String php )
141
155
{
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 ){
149
166
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 ());
152
169
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 ;
153
185
}
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 ;
167
188
}
168
189
169
190
public static String StringRequest (String format , String data , String php )
170
191
{
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 ;
185
200
186
- }
187
- } catch (Exception e ) {
201
+ } catch (Exception e ) {
188
202
189
- StringRequest (format ,data ,php );
203
+ try {
204
+ Thread .sleep (1000 );
205
+ } catch (InterruptedException e1 ) {
206
+
207
+ }
208
+ StringRequest (format ,data ,php );
190
209
191
- }
210
+ }
211
+ return null ;
212
+ }
213
+ });
214
+ try {
215
+ return cmsoon .get ();
216
+ } catch (Exception e ){
217
+ StringRequest (format ,data ,php );
218
+ }
192
219
return null ;
193
220
194
221
}
@@ -205,6 +232,12 @@ public static void useProxy(String proxyUrl, int proxyPort){
205
232
206
233
}
207
234
235
+ public static <T extends WaitingTask > void setWaitingTask (T wt ){
236
+
237
+ task = wt ;
238
+
239
+ }
240
+
208
241
/**
209
242
* Sets a proxy that requires auth for the system
210
243
* @param proxyUrl The proxy's IP/URL
0 commit comments