Repository navigation
Expand file tree
/
Copy pathSubProcess.cpp
More file actions
209 lines (196 loc) · 7.54 KB
/
Copy pathSubProcess.cpp
File metadata and controls
209 lines (196 loc) · 7.54 KB
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
#include "stdafx.h"
#include "SubProcess.h"
#ifndef USE_THREAD
SubProcess::SubProcess(MainFrame *parent,
int engine_type,
std::vector<wxString>& ini_line) : wxProcess(parent) {
wxLogDebug(wxT("SubProcess start."));
m_parent = parent;
Redirect();
m_katago_status = KATAGO_STARTING;
m_engine_type = engine_type;
m_send_msg.Clear();
m_stack_msg.Clear();
m_ini_line = ini_line;
m_ini_line_size = m_ini_line.size();
}
SubProcess::~SubProcess() {
wxLogDebug(wxT("SubProcess end."));
}
bool SubProcess::HasInput() {
bool hasInput = false;
try {
if ( IsInputAvailable() ) {
wxTextInputStream tis(*GetInputStream());
wxString msg;
msg << tis.ReadLine();
if (!msg.IsEmpty()) {
if (m_katago_status == KATAGO_STARTING) {
if (StartUpStdIn(msg)) {
auto event = new wxCommandEvent(wxEVT_RECIEVE_KATAGO);
event->SetClientData((void*)new auto(msg));
wxQueueEvent(m_parent->GetEventHandler(), event);
}
} else if (m_katago_status == KATAGO_IDLE && msg == "= symmetry 0") {
m_stack_msg = msg + " ";
m_stack_row = 12;
m_row_getting = false;
m_col_num = 0;
m_katago_status = KATA_RAW_NN_RECIEVING;
} else if (m_katago_status == KATA_RAW_NN_RECIEVING) {
if (KataRawNnRecieve(msg)) {
m_katago_status = KATAGO_IDLE;
auto event = new wxCommandEvent(wxEVT_RECIEVE_KATAGO);
event->SetClientData((void*)new auto(m_stack_msg));
wxQueueEvent(m_parent->GetEventHandler(), event);
}
} else {
if (msg.find(R"("id":"dummy")") == std::string::npos) {
auto event = new wxCommandEvent(wxEVT_RECIEVE_KATAGO);
event->SetClientData((void*)new auto(msg));
wxQueueEvent(m_parent->GetEventHandler(), event);
}
}
}
hasInput = true;
}
if ( IsErrorAvailable() ) {
wxTextInputStream tis(*GetErrorStream());
wxString msg;
msg << tis.ReadLine();
if (!msg.IsEmpty()) {
int offset = 0;
if (msg.Matches("2???-??-??*")) {
offset = 26;
}
if (m_katago_status == KATAGO_STARTING) {
StartUpStdErr(msg, offset);
auto event = new wxCommandEvent(wxEVT_RECIEVE_KATAGO);
event->SetClientData((void*)new auto(msg));
wxQueueEvent(m_parent->GetEventHandler(), event);
}
}
hasInput = true;
}
} catch (const std::exception& e) {
wxLogError(_("Exception at SubProcess::HasInput: %s %s\n"), typeid(e).name(), e.what());
wxString mess;
mess.Printf(_("Exception at SubProcess::HasInput: %s %s\n"), typeid(e).name(), e.what());
auto event = new wxCommandEvent(wxEVT_RECIEVE_KATAGO);
event->SetClientData((void*)new auto(mess));
wxQueueEvent(m_parent->GetEventHandler(), event);
}
return hasInput;
}
void SubProcess::OnTerminate(int pid, int status) {
while ( HasInput() )
;
wxLogStatus(m_parent, "Process %u terminated with exit code %d.", pid, status);
auto event = new wxCommandEvent(wxEVT_TERMINATED_KATAGO);
wxQueueEvent(m_parent->GetEventHandler(), event);
}
bool SubProcess::StartUpStdIn(wxString& msg) {
if (m_send_msg.find(R"("id":"dummy")") != std::string::npos) {
if (msg.find(R"("error":)") != std::string::npos) {
m_board25 = 0;
}
msg.Printf(wxT("START OK(board25:%d,japanese_rule:%d)"), m_board25, m_japanese_rule);
m_katago_status = KATAGO_IDLE;
return true;
} else if (m_send_msg == "kata-get-rules\n") {
if (msg.StartsWith("=")) {
if (msg.find(R"("scoring":"AREA")") == std::string::npos) {
m_japanese_rule = 1;
}
} else {
m_parent->SetStatusBarText(msg, 1);
}
wxTextOutputStream os(*GetOutputStream());
m_send_msg = "boardsize 25\n";
os.WriteString(m_send_msg);
return false;
} else if (m_send_msg == "boardsize 25\n") {
if (msg.StartsWith("?")) {
m_board25 = 0;
}
msg.Printf(wxT("START OK(board25:%d,japanese_rule:%d)"), m_board25, m_japanese_rule);
m_katago_status = KATAGO_IDLE;
return true;
} else if (m_ini_line_idx < m_ini_line_size) {
wxTextOutputStream os(*GetOutputStream());
m_send_msg = m_ini_line[m_ini_line_idx] + "\n";
os.WriteString(m_send_msg);
m_ini_line_idx++;
return false;
} else {
wxTextOutputStream os(*GetOutputStream());
m_send_msg = "kata-get-rules\n";
os.WriteString(m_send_msg);
return false;
}
return false;
}
void SubProcess::StartUpStdErr(wxString& msg, int offset) {
if (msg.find("Uncaught exception:") != std::string::npos ||
msg.find("PARSE ERROR:") != std::string::npos ||
msg.find("failed with error") != std::string::npos ||
msg.find(": error while loading shared libraries:") != std::string::npos ||
msg.find("what():") != std::string::npos) {
m_parent->SetStatusBarText(msg.substr(offset), 1);
wxLogError(_("Error was returned from KataGo engine: %s"), msg.substr(offset).mb_str());
m_katago_status = INIT;
} else if (m_engine_type == GTP::ANALYSIS &&
msg.find("Started, ready to begin handling requests") != std::string::npos) {
wxTextOutputStream os(*GetOutputStream());
m_send_msg = R"({"boardXSize":25,"boardYSize":25,"id":"dummy",)";
m_send_msg += R"("maxVisits":1,"moves":[],"rules":"chinese"})";
m_send_msg += "\n";
os.WriteString(m_send_msg);
} else if (m_engine_type == GTP::GTP_INTERFACE &&
msg.find("GTP ready, beginning main protocol loop") != std::string::npos) {
wxTextOutputStream os(*GetOutputStream());
if (m_ini_line_size > 1) {
m_send_msg = m_ini_line[1] + "\n";
} else {
m_send_msg = "kata-get-rules\n";
}
os.WriteString(m_send_msg);
}
m_parent->SetStatusBarText(msg.substr(offset), 1);
}
bool SubProcess::KataRawNnRecieve(wxString& msg) {
if (msg == "policy" || msg == "whiteOwnership") {
m_stack_msg += msg + " ";
m_row_getting = true;
m_row_num = 0;
} else if (m_row_getting) {
if (m_col_num == 0) {
bool prev_sp = true;
for (size_t i = 0; i < msg.length(); i++) {
if (msg[i] == (const char)' ') {
prev_sp = true;
} else if (prev_sp) {
prev_sp = false;
m_col_num++;
}
}
if (m_col_num < 7) {
m_col_num = 0;
return (m_stack_row == 0);
}
}
m_stack_msg += msg;
m_row_num++;
if (m_row_num >= m_col_num) {
m_stack_row--;
}
} else if (msg.StartsWith("policyPass ")) {
m_stack_msg += msg;
m_stack_row--;
} else {
m_stack_msg += msg + " ";
m_stack_row--;
}
return (m_stack_row == 0);
}
#endif