-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.pl
More file actions
134 lines (100 loc) · 2.72 KB
/
Copy pathserver.pl
File metadata and controls
134 lines (100 loc) · 2.72 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
#!/usr/bin/env perl
use strict;
use warnings;
use Mojolicious::Lite;
use Mojo::JSON qw(decode_json);
use Mojo::IOLoop;
use FindBin;
use lib "$FindBin::Bin/lib";
use Life;
app->defaults(layout => 'default', title => 'Cellular Automaton');
get '/' => sub {
my $self = shift;
$self->redirect_to('/index.html');
};
my $clients = {};
my $client_counter = 0;
my $timer;
my $MAX_CLIENTS = 100;
my $MAX_GENERATIONS = 10_000;
my $LOOP_DELAY = 0.75;
my $INACTIVITY_TIMEOUT = 60;
sub incubate {
app->log->debug("Incubate called.");
foreach my $cid(keys %$clients) {
if($clients->{$cid}->{life}) {
app->log->debug("[$cid]: " . $clients->{$cid}->{life});
my $life = \$clients->{$cid}->{life};
if ($$life->generation < $MAX_GENERATIONS) {
$$life = $$life->spawn();
_send_life($cid);
}
}
}
}
websocket '/ws' => sub {
my $self = shift;
return if ($client_counter >= $MAX_CLIENTS);
$self->inactivity_timeout($INACTIVITY_TIMEOUT);
my $tx = $self->tx;
my $cid = $self->tx->connection;
$clients->{$cid}->{tx} = $tx;
$client_counter++;
my $ip = $tx->remote_address;
app->log->debug("Client '$ip' [$cid] connected [$client_counter]");
$self->on( message => sub {
my ($self, $message) = @_;
$message = eval { decode_json($message) };
return if ( !$message );
return if ( !defined($message->{cmd}));
my $cmd = $message->{cmd};
return if ($cmd !~ /^(start|stop)$/);
my $cid = $self->tx->connection;
app->log->debug("$cid: $cmd");
if ($cmd eq 'start') {
return if ($clients->{$cid}->{life});
my %args = (
universe => [ Cell->new() ]
);
if(defined($message->{rule}) && $message->{rule} =~ /^\d+$/) {
if ($message->{rule} >= 0 && $message->{rule} <= 255) {
$args{rule} = $message->{rule};
}
}
$clients->{$cid}->{life} = Life->new( %args );
_send_life($cid);
if ($client_counter >= 1) {
if(!defined($timer)) {
app->log->debug("Starting Timer");
$timer = Mojo::IOLoop->recurring($LOOP_DELAY => \&incubate);
}
}
} elsif ($cmd eq 'stop') {
return if (!defined($clients->{$cid}->{life}));
$self->finish;
}
});
$self->on( finish => sub {
my ($self, $code, $reason) = @_;
_close_ws($self);
});
};
sub _close_ws {
my $controller = shift;
my $cid = $controller->tx->connection;
$clients->{$cid}->{life} = undef;
delete $clients->{$cid};
$client_counter--;
if ($client_counter == 0) {
app->log->debug("Removing Timer");
Mojo::IOLoop->remove($timer);
$timer = undef;
}
app->log->debug("Client [$cid] disconnected [$client_counter]");
}
sub _send_life {
my $cid = shift;
my $life = $clients->{$cid}->{life};
$clients->{$cid}->{tx}->send({ json => { generation => $life->generation, universe => [ $life->flatten ] }});
}
app->start;