-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmothership2
More file actions
executable file
·228 lines (199 loc) · 6.17 KB
/
mothership2
File metadata and controls
executable file
·228 lines (199 loc) · 6.17 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/perl
# http://poe.perl.org/?POE_Cookbook/TCP_Servers
use warnings;
use strict;
use POE qw(Component::Server::TCP Wheel::ReadLine);
use Data::Dumper;
use Linux::Inotify2;
my %known = (
"18:fe:34:9c:8d:d0"=>"clock2",
"18:fe:34:98:65:cc"=>"esp01-def",
"18:fe:34:99:ec:ed"=>"clock1"
);
my $selected;
my %users;
my $watchdir = "/var/www/heeen.de/htdocs/esp8266/";
POE::Session->create(
inline_states => {
_start => \&mystart,
got_input => \&got_input_handler,
output_text => sub {
my( $heap, $text ) = @_[HEAP,ARG0];
$heap->{readline_wheel}->put($text);
},
update_prompt => sub {
my( $heap) = @_[HEAP,ARG0];
if($selected) {
$heap->{readline_wheel}->get( "$users{$selected}->{id}> ");
} else {
$heap->{readline_wheel}->get( "* > ");
}
},
inotify_poll => sub { $_[HEAP]{inotify}->poll; },
watch_hdlr => \&watch_hdlr,
_stop => \&mystop,
trigger_ota => \&trigger_ota,
},
);
POE::Component::Server::TCP->new(
Alias => "mothership",
Port => 7778,
InlineStates => {send => \&handle_send},
ClientConnected => \&client_connected,
ClientError => \&client_error,
ClientDisconnected => \&client_disconnected,
ClientInput => \&client_input,
);
$poe_kernel->run();
exit 0;
sub mystart {
my ($kernel, $heap) = @_[KERNEL, HEAP];
$kernel->alias_set("console");
$heap->{readline_wheel} = POE::Wheel::ReadLine->new( InputEvent => 'got_input' );
$heap->{readline_wheel}->get(":> ");
$heap->{inotify} = new Linux::Inotify2
or die "Unable to create new inotify object: $!";
$heap->{inotify}->watch($watchdir, IN_CLOSE_WRITE, $_[SESSION]->postback("watch_hdlr"))
or die "Unable to watch dir: $!";
my $inotify_FH;
open $inotify_FH, "< &=" . $_[HEAP]{inotify}->fileno or die "Can't fdopen: $!\n";
$kernel->select_read( $inotify_FH, "inotify_poll" );
}
sub mystop {
delete $_[HEAP]->{readline_wheel};
}
sub got_input_handler {
my ( $heap, $kernel, $input, $exception ) =
@_[ HEAP, KERNEL, ARG0, ARG1 ];
if ( defined $input && $input ne "") {
$heap->{readline_wheel}->addhistory($input);
if($input eq "ls") {
list_users();
} elsif($input =~ /^cd/) {
cmd_cd($input);
} else {
if($selected) {
$poe_kernel->post($selected => send => $input);
} else {
broadcast($input);
}
}
}
elsif ( $exception eq 'interrupt' ) {
$heap->{readline_wheel}->put("Goodbye.");
delete $heap->{readline_wheel};
# close sockets
exit(0);
}
elsif($exception != 1) {
$heap->{readline_wheel}->put("\tException: $exception");
}
$heap->{readline_wheel}->get();
}
sub broadcast {
my ($message) = @_;
foreach my $user (keys %users) {
$poe_kernel->post($user => send => $message);
}
}
sub list_users {
foreach my $session (keys %users) {
my $c = $users{$session};
myprint("$session: $c->{id}: $c->{MAC} rom: $c->{ROM} alive: $c->{alive}");
}
}
sub cmd_cd {
my $input = shift;
if($input =~ /^cd *$/) {
myprint("selecting all.");
$selected = 0;
} elsif($input =~/^cd (\w+)$/) {
foreach my $session (keys %users) { #FIXME o(n)
my $c = $users{$session};
if($c->{id} eq $1 || $session == $1) {
$selected = $session;
myprint("selected: $c->{id}: $c->{MAC} rom: $c->{ROM} alive: $c->{alive}");
last;
}
}
$selected = 0;
} else {
myprint("cd: error in arguments.");
}
$poe_kernel->post( console => "update_prompt");
}
sub trigger_ota {
foreach my $session (keys %users) {
my $c = $users{$session};
$poe_kernel->post($session => send => "OTA");
myprint("sending OTA to $c->{id}");
}
}
sub myprint {
$poe_kernel->post( console => output_text => join(" ", @_) );
}
# Handle an outgoing message by sending it to the client.
sub handle_send {
my ($heap, $session, $message) = @_[HEAP, SESSION, ARG0];
my $id = $users{$session->ID}->{id};
myprint(">> $id: <$message>");
$heap->{client}->put($message);
}
# Handle a connection. Register the new user, and broadcast a message
# to whoever is already connected.
sub client_connected {
my $session_id = $_[SESSION]->ID;
$users{$session_id} = {};
myprint($session_id, "connected.");
}
# The client disconnected. Remove them from the chat room and
# broadcast a message to whoever is left.
sub client_disconnected {
my $session_id = $_[SESSION]->ID;
delete $users{$session_id};
myprint($session_id, "disconnected.");
}
# The client socket has had an error. Remove them from the chat room
# and broadcast a message to whoever is left.
sub client_error {
my $session_id = $_[SESSION]->ID;
delete $users{$session_id};
myprint($session_id, "disconnected.");
$_[KERNEL]->yield("shutdown");
}
# Broadcast client input to everyone in the chat room.
sub client_input {
my ($kernel, $session, $heap, $input) = @_[KERNEL, SESSION, HEAP, ARG0];
my $c = $users{$session->ID};
if($input =~ /^(.+)=(.+)$/) {
$c->{$1} = $2;
if($1 eq "MAC") {
if($known{$c->{MAC}}) {
$c->{id} = $known{$c->{MAC}};
} else {
$c->{id} = substr $2,-8;
}
}
$c->{alive} = "?";
}
if($input =~ /^alive (.+)$/) {
$c->{alive} = $1;
$kernel->post( console => "update_prompt");
} else {
$kernel->post( console => output_text => "<$c->{id}> $input" );
}
}
sub watch_hdlr {
my $event = $_[ARG1][0];
my $name = $event->fullname;
my $msg;
$msg .= "$name was accessed\n" if $event->IN_ACCESS;
$msg .= "$name is no longer mounted\n" if $event->IN_UNMOUNT;
$msg .= "$name is gone\n" if $event->IN_IGNORED;
$msg .= "$name is new\n" if $event->IN_CLOSE_WRITE;
$msg .= "events for $name have been lost\n" if $event->IN_Q_OVERFLOW;
$poe_kernel->post( console => output_text => $msg );
if($event->IN_CLOSE_WRITE ) {
$poe_kernel->delay( trigger_ota => 1.0 );
}
}