/
IPC.pm
238 lines (208 loc) · 7.06 KB
/
IPC.pm
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
229
230
231
232
233
234
235
236
237
238
# Copyright (C) 2015 SUSE Linux GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
package OpenQA::IPC;
use strict;
use warnings;
use Net::DBus;
use Net::DBus::Callback;
use Net::DBus::Binding::Watch;
use Mojo::IOLoop;
use Data::Dump qw/pp/;
use Try::Tiny;
use Carp;
use Scalar::Util qw/weaken/;
use OpenQA::Utils qw/log_debug/;
my $openqa_prefix = 'org.opensuse.openqa';
my %services = (
scheduler => 'Scheduler',
websockets => 'WebSockets',
webapi => 'WebAPI',
);
my %handles;
sub new {
my ($class, $reactor, $test) = @_;
$class = ref $class || $class;
my $self = {};
bless $self, $class;
my $bus;
if (!$test) {
# for WebAPI and WebSockets we use Mojo with its own event loop, so use external reactor if supplied
$bus = Net::DBus->find(nomainloop => $reactor ? 1 : 0);
}
else {
$bus = Net::DBus->test;
}
return unless $bus;
$self->{bus} = $bus;
return $self;
}
sub ipc {
CORE::state $ipc = shift->new(@_);
}
sub register_service {
my ($self, $service) = @_;
die "Unsupported service \"$service\"" unless $service && grep { /$service/ } keys %services;
my $s = $self->{bus}->export_service(join('.', $openqa_prefix, $services{$service}));
die "Failed to export service \"$service\"" unless $s;
$self->{service}{$service} = $s;
return $s;
}
sub manage_events {
my ($self, $reactor, $object) = @_;
return unless $reactor;
if (ref($reactor) =~ /Mojo::Reactor/) {
# Hook DBus events to Mojo::Reactor
my $c = $self->{bus}->get_connection;
$c->set_watch_callbacks(
sub {
my ($object, $watch) = @_;
$self->_manage_watch_on($reactor, $watch);
},
sub {
my ($object, $watch) = @_;
$self->_manage_watch_off($reactor, $watch);
},
sub {
my ($object, $watch) = @_;
$self->_manage_watch_toggle($reactor, $watch);
},
);
# TODO add timeout
# $c->set_timeout_callbacks(sub{
# my ($object, $watch) = @_;
# $self->_manage_timeout_on($reactor, $watch);
# },
# sub {return},
# sub {return},
# );
if ($c->can("dispatch")) {
my $cb = Net::DBus::Callback->new(object => $c, method => "dispatch", args => []);
$reactor->on('dbus-dispatch' => sub { $cb->invoke });
}
if ($c->can("flush")) {
my $cb = Net::DBus::Callback->new(object => $c, method => "flush", args => []);
$reactor->on('dbus-flush' => sub { $cb->invoke });
}
}
else {
die 'Unsupported reactor';
}
}
sub _manage_watch_on {
my ($self, $reactor, $watch) = @_;
my $flags = $watch->get_flags;
if ($flags & &Net::DBus::Binding::Watch::READABLE) {
my $fh = $self->_get_fh_from_fd($watch->get_fileno);
my $cb = Net::DBus::Callback->new(object => $watch, method => "handle", args => [&Net::DBus::Binding::Watch::READABLE]);
# Net::DBus calls dispatch each time some event wakes it up, Mojo::Reactor does not support this kind of hooks
$reactor->io($fh => sub { my ($self, $writable) = @_; $cb->invoke; $self->emit('dbus-dispatch') });
$reactor->watch($fh, $watch->is_enabled, 0);
}
if ($flags & &Net::DBus::Binding::Watch::WRITABLE) {
my $fh = $self->_get_fh_from_fd($watch->get_fileno, 1);
my $cb = Net::DBus::Callback->new(object => $watch, method => "handle", args => [&Net::DBus::Binding::Watch::WRITABLE]);
# Net::DBus calls flush event each time some event wakes it up, Mojo::Reactor does not support this kind of hooks
$reactor->io($fh => sub { my ($self, $writable) = @_; $cb->invoke; $self->emit('dbus-flush') });
$reactor->watch($fh, 0, $watch->is_enabled);
}
}
sub _manage_watch_off {
my ($self, $reactor, $watch) = @_;
my $flags = $watch->get_flags;
my ($fh, $rw);
if ($flags & &Net::DBus::Binding::Watch::READABLE) {
$fh = $self->_get_fh_from_fd($watch->get_fileno);
$rw = '<';
$reactor->remove($fh);
}
if ($flags & &Net::DBus::Binding::Watch::WRITABLE) {
$fh = $self->_get_fh_from_fd($watch->get_fileno, 1);
$rw = '>';
$reactor->remove($fh);
}
close $fh;
delete $self->{handles}{$rw}{$watch->get_fileno};
}
sub _manage_watch_toggle {
my ($self, $reactor, $watch) = @_;
my $flags = $watch->get_flags;
if ($flags & &Net::DBus::Binding::Watch::READABLE) {
my $fh = $self->{handles}{'<'}{$watch->get_fileno};
if (defined $fh) {
$reactor->watch($fh, $watch->is_enabled, 0);
}
}
if ($flags & &Net::DBus::Binding::Watch::WRITABLE) {
my $fh = $self->{handles}{'>'}{$watch->get_fileno};
if (defined $fh) {
$reactor->watch($fh, 0, $watch->is_enabled);
}
}
}
sub _manage_timeout_on {
}
sub _manage_timeout_off {
}
# Mojo::Reactor works with filehandles instead of descriptors
sub _get_fh_from_fd {
my ($self, $fd, $write) = @_;
if ($write) {
$write = '>';
}
else {
$write = '<';
}
if (defined $self->{handles}{$write}{$fd}) {
return $self->{handles}{$write}{$fd};
}
my $fh;
open($fh, $write . '&', $fd) || die "unable to open fd $fd for $write";
$self->{handles}{$write}{$fd} = $fh;
return $fh;
}
sub _dispatch {
my ($self, $target, $command, @data) = @_;
my $service_name = join('.', $openqa_prefix, $services{$target});
my $service;
try {
$service = $self->{bus}->get_service($service_name);
}
catch {
confess "error getting ipc service: $_";
};
my $object = $service->get_object('/' . $services{$target}, join('.', $openqa_prefix, $services{$target}));
log_debug("dispatching IPC $command to $target: " . pp(\@data));
my $ret = $object->$command(@data);
log_debug("IPC retrieved " . pp($ret));
return $ret;
}
## Helpers
# scheduler - send message to scheduler
sub scheduler {
my ($self, @param) = @_;
return $self->_dispatch('scheduler', @param);
}
# websockets - send message to websockets
sub websockets {
my ($self, @param) = @_;
return $self->_dispatch('websockets', @param);
}
# webapi - send message to WebUI/API
sub webapi {
my ($self, @param) = @_;
return $self->_dispatch('webapi', @param);
}
1;
# vim: set sw=4 et: