line |
stmt |
code |
1
|
|
# Copyright 2018-2019 SUSE LLC |
2
|
|
# SPDX-License-Identifier: GPL-2.0-or-later |
3
|
|
|
4
|
|
use Mojo::Base 'Mojolicious::Controller', -signatures; |
5
|
4
|
|
|
4
|
|
|
4
|
|
6
|
|
use commands; |
7
|
4
|
use Try::Tiny; |
|
4
|
|
|
4
|
|
8
|
4
|
use Mojo::JSON qw(decode_json to_json); |
|
4
|
|
|
4
|
|
9
|
4
|
|
|
4
|
|
|
4
|
|
10
|
|
my $app = $self->app; |
11
|
1
|
my $isotovideo = $app->defaults('isotovideo'); |
|
1
|
|
|
1
|
|
|
1
|
|
|
1
|
|
12
|
1
|
return $app->log->debug('cmdsrv: not passing command from client to isotovideo; connection to isotovideo has already been stopped') |
13
|
1
|
unless defined $isotovideo; |
14
|
1
|
|
15
|
|
$app->log->debug("cmdsrv: passing command from client to isotovideo $isotovideo: $msg"); |
16
|
|
|
17
|
1
|
my $decoded_message; |
18
|
|
try { |
19
|
1
|
$decoded_message = decode_json($msg); |
20
|
|
} |
21
|
1
|
catch { |
22
|
|
$app->log->warn('cmdsrv: failed to decode message'); |
23
|
|
return undef; |
24
|
0
|
}; |
25
|
0
|
return undef unless defined $decoded_message; |
26
|
1
|
|
27
|
1
|
myjsonrpc::send_json($isotovideo, $decoded_message); |
28
|
|
|
29
|
1
|
# note: no myjsonrpc::read_json() here - response is broadcasted to all clients in commands.pm |
30
|
|
} |
31
|
|
|
32
|
|
$self->app->log->debug('cmdsrv: client disconnected: ' . $id); |
33
|
|
delete $self->app->defaults('clients')->{$id}; |
34
|
1
|
} |
|
1
|
|
|
1
|
|
|
1
|
|
35
|
1
|
|
36
|
1
|
my $id = sprintf "%s", $self->tx; |
37
|
|
$self->app->log->debug('cmdsrv: client connected: ' . $id); |
38
|
|
$self->app->defaults('clients')->{$id} = $self->tx; |
39
|
1
|
|
|
1
|
|
|
1
|
|
40
|
1
|
$self->on( |
41
|
1
|
message => sub ($self, $msg) { |
42
|
1
|
$self->pass_message_from_ws_client_to_isotovideo($id, $msg); |
43
|
|
}); |
44
|
1
|
$self->on(finish => sub { |
45
|
1
|
$self->handle_ws_client_disconnects($id); |
|
1
|
|
|
1
|
|
46
|
1
|
}); |
47
|
1
|
} |
48
|
|
|
49
|
1
|
my $app = $self->app; |
50
|
1
|
my $clients = $app->defaults('clients'); |
51
|
|
my $message = $self->req->json; |
52
|
|
|
53
|
4
|
$app->log->debug('cmdsrv: broadcasting message from API call to all ws clients'); |
|
4
|
|
|
4
|
|
54
|
4
|
return $self->render( |
55
|
4
|
json => { |
56
|
4
|
error => 'JSON message to be boradcasted missing or invalid', |
57
|
|
status => 'boradcast failed', |
58
|
4
|
}, |
59
|
4
|
status => 400, |
60
|
|
) unless ($message); |
61
|
|
|
62
|
|
$app->log->debug('cmdsrv: broadcasting message from API call to all ws clients: ' . to_json($message)); |
63
|
|
|
64
|
|
my $outstanding_transactions = scalar keys %$clients; |
65
|
|
return $self->render(json => {status => 'boradcast done'}) unless $outstanding_transactions; |
66
|
|
|
67
|
4
|
for (keys %$clients) { |
68
|
|
$clients->{$_}->send({json => $message}, sub { |
69
|
4
|
return undef if (($outstanding_transactions -= 1) > 0); |
70
|
4
|
return $self->render(json => {status => 'boradcast done'}); |
71
|
|
}); |
72
|
1
|
} |
73
|
|
|
74
|
1
|
return $self; |
75
|
1
|
} |
76
|
1
|
|
77
|
|
1; |