-
Notifications
You must be signed in to change notification settings - Fork 3
/
cy1.pm
310 lines (237 loc) · 5.95 KB
/
cy1.pm
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/perl
#
# package for the protocol of cytrone 0.X .
# the protocol is undocumented. however, it was made over HTTP.
# by k-chinen, CROND, JAIST.
#
package cy1;
use Exporter qw(import);
use strict;
use POSIX qw(strftime);
use LWP::UserAgent;
#use HTTP::Request;
#use Net::SSLeay;
#use Crypt::SSLeay;
#use IO::Socket::SSL;
use URI::Escape;
use JSON;
use Sys::Syslog;
use Sys::Syslog qw(:standard :macros);
use threads;
use threads::shared;
use Thread::Queue;
use Encode qw(decode_utf8);
our @EXPORT = qw(cy1_trlist cy1_aclist cy1_example cy1_create cy1_end cy1_nodebug cy1_debug cy1_openlog cy1_useHTTP cy1_useHTTPS $cy1_eq);
my $debug = 0;
my $cy1_seq = int(rand(100));
my $proto = "http://";
sub cy1_useHTTP {
$proto = "http://";
}
sub cy1_useHTTPS {
$proto = "https://";
}
our $cy1_eq = new Thread::Queue;
# debug message procedures
sub _info {
my($msg) = @_;
syslog(LOG_INFO, $msg);
print ";".$msg."\n";
}
sub _notice {
my($msg) = @_;
syslog(LOG_NOTICE, $msg);
print $msg."\n";
}
sub cy1_trlist {
my ($host, $port, $lang, $user, $passwd) = @_;
my $url;
my $ua;
my $res;
$url = $proto.$host.":".$port."/";
#print STDERR "cy1_trlist: $url\n";
my %form;
$form{"action"} = "fetch_content";
$form{"lang"} = $lang;
$form{"user"} = $user;
$form{"password"} = $passwd;
$ua = LWP::UserAgent->new;
$res = $ua->post($url, \%form);
if($debug) {
print "DEBUG: cy1_trlist(): Response content: " . $res->content . "\n";
}
return $res;
}
sub cy1_aclist {
my ($host, $port, $lang, $user, $passwd) = @_;
my $url;
my $ua;
my $res;
$url = $proto.$host.":".$port."/";
my %form;
$form{"action"} = "get_sessions";
$form{"lang"} = $lang;
$form{"user"} = $user;
$form{"password"} = $passwd;
$ua = LWP::UserAgent->new;
$res = $ua->post($url, \%form);
return $res;
}
sub cy1_example {
my ($host, $port, $lang, $user, $passwd) = @_;
my $url;
my $ua;
my $res;
$url = $proto.$host.":".$port."/";
my %form;
$form{"action"} = "get_configurations";
$form{"lang"} = $lang;
$form{"user"} = $user;
$form{"password"} = $passwd;
$ua = LWP::UserAgent->new;
$res = $ua->post($url, \%form);
return $res;
}
sub _base {
my ($pmode, $conn, $lmsg, $majorid, $minorid, $xparam, $host, $port, $xform) = @_;
my $cparam;
$cparam = join(":", @{ $xparam } );
my %form = %{ $xform };
my $url;
my $ua;
my $res;
my $ts;
my $te;
my $tk;
my $rid;
my $sid;
$rid = -1;
$sid = -1;
if($cparam eq '') {
$cparam = ":";
}
if($pmode eq '') {
$pmode = "_";
}
$url = $proto.$host.":".$port."/";
if($debug) {
printf " %-8s %s\n", "#name", "value";
foreach my $k (keys %form) {
printf " %-8s %s\n", $k, $form{$k};
}
}
foreach my $k (keys %form) {
if($k eq 'range_id') {
$rid = $form{$k};
}
}
$ua = LWP::UserAgent->new;
$ua->timeout(3600); # 1 hour
_info("INFO _base $pmode START $majorid $minorid $cparam");
$ts = time;
# Special handling for fields that could contain Japanese characters,
# since otherwise Python side doesn't handle them correctly
$form{'type'}=decode_utf8($form{'type'});
$form{'scenario'}=decode_utf8($form{'scenario'});
$form{'level'}=decode_utf8($form{'level'});
$res = $ua->post($url, \%form);
$te = time;
_info("INFO _base $pmode END $majorid $minorid");
$tk = $te - $ts;
# _notice("TAKES _base $pmode elap $majorid $minorid $cparam $tk");
#print STDERR "---B\n";
#print STDERR $res->content;
#print STDERR "---M\n";
#print STDERR uri_unescape($res->content);
#print STDERR "---E\n";
#### seek session-id
# ...Training Session #1...
#
my $cal = from_json($res->content);
foreach my $x ( @{$cal} ) {
# print "x |$x|\n";
my %y;
%y = %{ $x };
# foreach my $k (keys %y) {
# print "$k |".$y{$k}."|\n";
# }
if(defined $y{"message"}) {
my $rmsg = $y{"message"};
#print "rmsg 0 |$rmsg|\n";
if($rmsg ne '') {
#print "rmsg 1 |$rmsg|\n";
my $cmsg = uri_unescape($rmsg);
#print "cmsg 0 |$cmsg|\n";
if($cmsg =~ /Training Session #(\d+)\b/i) {
$sid = $1;
#print "sid |$sid|\n";
}
}
}
}
_notice("INFO _base $pmode $sid $rid\n");
if($pmode eq 'C') {
_notice("TAKES _base $pmode elap $majorid $minorid $cparam $tk $sid");
}
if($pmode eq 'E') {
_notice("TAKES _base $pmode elap $majorid $minorid $cparam $tk $rid");
}
#sleep rand(17)+3
$conn->send_utf8($lmsg." ".$res->content);
$cy1_eq->enqueue("CHANGE $majorid $minorid");
threads->detach();
return $res;
}
sub cy1_create {
my ($conn, $lmsg, $qid,
$host, $port, $lang, $user, $passwd,
$type, $scenario, $level, $count) = @_;
$cy1_seq++;
my @param;
@param = ($count);
my %form;
$form{"action"} = "create_training";
$form{"lang"} = $lang;
$form{"user"} = $user;
$form{"password"} = $passwd;
$form{"type"} = $type;
$form{"scenario"} = $scenario;
$form{"level"} = $level;
$form{"count"} = $count;
my $thr = threads->create(\&_base, 'C',
$conn, $lmsg, $qid, $cy1_seq, \@param, $host, $port, \%form);
$conn->send_utf8("NOP 2 create");
return 0;
}
sub cy1_end {
my ($conn, $lmsg, $qid,
$host, $port, $lang, $user, $passwd,
$rangeid) = @_;
my $url;
my $ua;
my $res;
$cy1_seq++;
my @param;
@param = ();
my %form;
$form{"action"} = "end_training";
$form{"lang"} = $lang;
$form{"user"} = $user;
$form{"password"} = $passwd;
$form{"range_id"} = $rangeid;
my $thr = threads->create(\&_base, 'E',
$conn, $lmsg, $qid, $cy1_seq, \@param, $host, $port, \%form);
$conn->send_utf8("NOP 2 end");
return 0;
}
sub cy1_nodebug {
$debug = 0;
}
sub cy1_debug {
$debug = 1;
}
sub cy1_openlog {
# openlog('cy1', '', 'users');
syslog('info', "START");
}
1;