summaryrefslogtreecommitdiff
path: root/p6/mixed-server.pl
blob: 7f9b8a897171e33fbcfa25f971413a43039342ad (plain)
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
#!/usr/bin/env perl6 
use v6.d;
 
class Model {
    has Lock::Async $!lock .= new;
    has BagHash $!words .= new;
 
    method add_words(@words --> Promise{
        return $!lock.lock.then: {
            LEAVE $!lock.unlock;
            ++$!words{$_} for @words;
        }
    }
 
    method get_most_common_pairs(Int() $how_many=10 --> Promise{
        return $!lock.lock.then: {
            LEAVE $!lock.unlock;
            reverse $!words.sort(*.value).head($how_many);
        }
    }
}
 
class View {
    method words_from_input(Str() $input --> Seq{
        return $input.split(/\s+/,:skip-empty);
    }
 
    method table_from_ranked_pairs(@ranked_pairs{
        return @ranked_pairs.map({ sprintf '%-15s %3d',$^p.key,$^p.value }).join("\n");
    }
}
 
class HTTPController {
    use Cro::HTTP::Router;
 
    has $.model is required;
    has $.view is required;
 
    sub respond($body{
        response.status = 200;
        response.set-body("{$body}\n");
    }
 
    method routes() {
        return route {
            get -> *@:$n! is query {
                self.get_words($n);
            }
            get -> *@ {
                self.get_words();
            }
            post -> *@ {
                request-body-text -> $body {
                    self.post_words($body);
                }
            }
        }
    }
 
    method get_words($n=10{
        my @most_common_pairs = await $!model.get_most_common_pairs($n);
        my $table = $!view.table_from_ranked_pairs(@most_common_pairs);
        respond($table);
    }
 
    method post_words($body{
        my @words = $!view.words_from_input($body);
        await $!model.add_words(@words);
        respond('ok');
    }
}
 
class LineController {
    has $.model is required;
    has $.view is required;
 
    method on_command(:$stream,:$command,:$args_string{
        if $command eq 'get' {
            self.get_words($stream,$args_string);
        }
        elsif $command eq 'put' {
            self.put_words($stream,$args_string);
        }
        else {
            reply($stream,"bad command, only 'get' and 'put'");
        }
    }
 
    sub reply($stream,$text{
        await $stream.print("$text\n");
    }
 
    method get_words($stream,$args_string{
        my $how_many = $args_string || 10;
 
        my @most_common_pairs = await $!model.get_most_common_pairs($how_many);
        my $table = $!view.table_from_ranked_pairs(@most_common_pairs);
 
        reply($stream,$table);
    }
 
    method put_words($stream,$args_string{
        my @words = $!view.words_from_input($args_string);
        await $!model.add_words(@words);
 
        reply($stream,'ok');
    }
}
 
class TextServer {
 
    has $.controller is required;
    has $.host is required;
    has $.port is required;
 
    has $!stop = Promise.new;
 
    method start() {
        start react {
            whenever IO::Socket::Async.listen($!host,$!port) -> $conn {
                whenever $conn.Supply.lines -> $line {
                    my ($command,$args_string= $line.split(/\s+/,2);
                    $.controller.on_command(
                        stream => $conn,
                        :$command:$args_string
                    );
                }
            }
            whenever $!stop -> {
                last;
            }
        };
    }
 
    method stop() { $!stop.keep }
}
 
use Cro::HTTP::Server;
 
my Model $model .= new;
my View $view .= new;
 
my HTTPController $httpcontroller .= new(:$model,:$view);
my $httpserver = Cro::HTTP::Server.new(
    host => 'localhost',
    port => 8080,
    application => $httpcontroller.routes,
);
$httpserver.start;
 
my LineController $linecontroller .= new(:$model,:$view);
my TextServer $textserver .=new(
    host => 'localhost',
    port => 2020,
    controller => $linecontroller,
);
$textserver.start;
 
react whenever signal(SIGINT{
    $httpserver.stop;
    $textserver.stop;
    exit;
}