-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnswerSubmitter.java
230 lines (212 loc) · 6.87 KB
/
AnswerSubmitter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.net.url;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 提出器. 過去に提出された解答よりも良い解答が与えられれば提出する.
*
*
* @author Kazuaki
*
*/
public class AnswerSubmitter {
// 一番良かった解答の得点
private int score_min = Integer.MAX_VALUE;
// 一番良かった解答の石数
private int num_stones_max = 0;
// 一番良かった解答(toString 用)
private String data_latest;
// 提出先のURL
private URL url;
// トークン
private String token_str;
// タイマ
private Timer timer = new Timer();
private class SubmitTask extends TimerTask {
@Override
public void run() {
if (data_latest != null) {
System.err.println(data_latest.replaceAll("[^\r\n]+", "")
.replaceAll("\r", "r").replaceAll("\n", "n").length());
System.err.println(data_latest);
System.err.println("END");
submitCLI(data_latest, token_str);
}
timer.schedule(new SubmitTask(), 1200);
}
};
public AnswerSubmitter(URL url, String token_strs) {
this.url = url;
this.token_str = token_strs;
timer.schedule(new SubmitTask(), 1200);
}
/**
* 過去に提出された解答よりも良い解答が与えられれば提出する.
*
* @param score
* 得点
* @param num_stones
* 石数
* @param data
* 解答
* @param token_str
* TODO
* @return 結果の真偽
*/
public boolean submit(int score, int num_stones, String data) {
if (score < score_min && num_stones > num_stones_max) {
// 過去に提出された解答よりも良かった
// 提出
data_latest = data;
score_min = score;
num_stones_max = num_stones;
return true;
}
return false;
}
// 提出
private void submit(String data, String token_str) {
// 改行文字の置換
String data_crlf = data.replaceAll("\r\n", "\n").replaceAll("\n",
"\r\n");
// POSTのときにファイルで渡すための一時ファイル
File tmpFile = null;
try {
// creates temporary file
tmpFile = File.createTempFile("post", ".txt");
// prints absolute path
System.out.println("File path: " + tmpFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
// 一時ファイルに解答を書く
try (BufferedWriter output = new BufferedWriter(new FileWriter(tmpFile))) {
output.write(data_crlf);
} catch (IOException e) {
e.printStackTrace();
}
// 送信とか
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpPost httppost = new HttpPost(url.toURI());
FileBody answer = new FileBody(tmpFile);
StringBody token = new StringBody(token_str,
ContentType.MULTIPART_FORM_DATA);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("answer", answer).addPart("token", token).build();
httppost.setEntity(reqEntity);
System.out
.println("executing request " + httppost.getRequestLine());
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: "
+ resEntity.getContentLength());
// UTF-8以外からは引数の文字列を変更(クソース)
InputStreamReader isr = new InputStreamReader(
resEntity.getContent(), Charset.forName("UTF-8"));
BufferedReader in = new BufferedReader(isr);
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
EntityUtils.consume(resEntity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
tmpFile.delete();
}
}
private void submitCLI(String data, String token) {
// 改行文字の置換
String data_crlf = data.replaceAll("\r\n", "\n").replaceAll("\n",
"\r\n");
// POSTのときにファイルで渡すための一時ファイル
File tmpFile = null;
//POST結果のtext
//File log = null;
try {
// creates temporary file
tmpFile = File.createTempFile("post", ".txt");
//log = new File("Serverlog.txt");
// prints absolute path
System.out.println("File path: " + tmpFile.getAbsolutePath());
} catch (FileNotFoundException e){
e.printStackTrace();
/*try {
log.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}*/
} catch (IOException e) {
e.printStackTrace();
}
// 一時ファイルに解答を書く
try (BufferedWriter output = new BufferedWriter(new FileWriter(tmpFile))) {
output.write(data_crlf);
} catch (IOException e) {
e.printStackTrace();
}
// cURLしゅるぅぅう
String[] arg = new String[]{"cmd", "/c","curl",url.toString(),
"--form-string","token="+token,
"-F","answer=@"+tmpFile.getAbsolutePath()};
ProcessBuilder pb = new ProcessBuilder(arg);
pb.command().forEach(s -> System.out.println(s));
//logに吐き出す
//pb.redirectErrorStream(true);
//pb.redirectOutput(log);
//stdoutに吐き出す
pb.inheritIO();
try {
pb.start();
} catch (IOException /*| InterruptedException*/ e) {
e.printStackTrace();
}
/*try(BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(log)))){
System.err.println(in.readLine());
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}*/
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[Latest Submitted Answer]");
sb.append(String.format("score: %d\nstones: %d\ndata:\n%s\n",
score_min, num_stones_max, data_latest));
return sb.toString();
}
}