-
Notifications
You must be signed in to change notification settings - Fork 0
/
ner_srv.pl
62 lines (50 loc) · 1.95 KB
/
ner_srv.pl
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
#!/usr/bin/perl -w
use strict;
use warnings;
use IO::Socket;
use threads;
use Thread::Queue;
use POSIX qw(strftime);
use Time::HiRes;
use POE qw(Component::Server::TCP);
our $THREADS = 20;
my @branch_queues = map {new Thread::Queue} 1 .. $THREADS;
my %names = ();
@ARGV = ("./name_tf.dat");
print strftime ("%Y/%m/%d %H:%M:%S", localtime), " Now start to load dictionary...\n";
while (<>) {
chomp;
next if /\,/;
/^\s*(.+) ==> (\d+)$/s;
my $raw_name_len = length $1;
next if 1 == $raw_name_len;
2 == $raw_name_len and $1 =~ m/^[a-z]{2}$/i and next;
$names{quotemeta ($1)} = $2;
}
my $regex = join '|', sort {length $b <=> length $a} keys %names;
print strftime ("%Y/%m/%d %H:%M:%S", localtime), " load dictionary finished ", scalar keys %names, " entrys in all.\n\n";
POE::Component::Server::TCP->new (
Port => 6666,
ClientConnected => sub {
print "got a connection from $_[HEAP]{remote_ip}\n";
# $_[HEAP]{client}->put("Smile from the server!");
},
ClientInput => sub {
my ($checksum, $client_len, $client_input) = unpack ("a8ia*", $_[ARG0]);
"NER_iSRV" eq $checksum or print "checksum error.\n" and $_[HEAP]{client}->put (pack ("a8i", "NER_iSRV", -1)) and return;
$client_len > 1 or print "client_len $client_len error.\n" and $_[HEAP]{client}->put (pack ("a8i", "NER_iSRV", -2)) and return;
print strftime ("%Y/%m/%d %H:%M:%S", localtime), " Now begain proc:\n$client_input\n";
my @matches = ($client_input =~ /($regex)/g);
(0 + @matches) > 0 or print "$client_input have no name.\n" and $_[HEAP]{client}->put (pack ("a8i", "NER_iSRV", -3)) and return;
{
local $" = '+';
$_[HEAP]{client}->put (pack ("a8ia*", "NER_iSRV", length "@matches\n", "@matches\n"));
}
print strftime ("%Y/%m/%d %H:%M:%S", localtime), " proc finished\n";
},
ClientDisconnected => sub {
print "$_[HEAP]{remote_ip} disconnected\n\n";
},
);
POE::Kernel->run ();
exit (0);