« blib/lib/Lemonldap/NG/Common/CliSessions.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Conf/Backends/YAMLFile.pm »
| 1 | package Lemonldap::NG::Common::MessageBroker::MQTT; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '2.23.0'; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ( $class, $conf, $logger ) = @_; | ||
| 9 | |||
| 10 | require Net::MQTT::Simple; | ||
| 11 | |||
| 12 | my $args = $conf->{messageBrokerOptions}; | ||
| 13 | $args //= {}; | ||
| 14 | $args->{server} ||= 'localhost:1883'; | ||
| 15 | my $mqtt; | ||
| 16 | if ( $args->{ssl} ) { | ||
| 17 | require Net::MQTT::Simple::SSL; | ||
| 18 | my $h = {}; | ||
| 19 | $h->{$_} = $args->{$_} | ||
| 20 | foreach (qw(SSL_ca_file SSL_cert_file SSL_key_file)); | ||
| 21 | $mqtt = Net::MQTT::Simple::SSL->new( $args->{server}, $h ); | ||
| 22 | if ( $args->{user} ) { | ||
| 23 | $mqtt->login( $args->{user}, $args->{password} ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | $mqtt = Net::MQTT::Simple->new( $args->{server} ); | ||
| 28 | } | ||
| 29 | unless ($mqtt) { | ||
| 30 | $logger->error("Unable to connect to MQTT server $@$!"); | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | my $self = bless { mqtt => $mqtt, _ch => [], logger => $logger }, $class; | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub publish { | ||
| 38 | my ( $self, $channel, $msg ) = @_; | ||
| 39 | die 'Not a hash msg' unless ref $msg eq 'HASH'; | ||
| 40 | my $j = eval { JSON::to_json($msg) }; | ||
| 41 | die "MessageBroker publish only hashes! $@" if $@; | ||
| 42 | $self->{mqtt}->publish( "llng/$channel", $j ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub subscribe { | ||
| 46 | my ( $self, $channel ) = @_; | ||
| 47 | $self->{messages}{$channel} = []; | ||
| 48 | $self->{mqtt}->subscribe( | ||
| 49 | "llng/$channel", | ||
| 50 | sub { | ||
| 51 | return unless $_[1]; | ||
| 52 | $_[0] =~ s#llng/##; | ||
| 53 | my $tmp = eval { JSON::from_json( $_[1] ) }; | ||
| 54 | if ($@) { | ||
| 55 | $self->{logger}->error("Bad message from MQTT server: $@"); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | push @{ $self->{messages}{ $_[0] } }, $tmp; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | ); | ||
| 62 | push @{ $self->{_ch} }, "llng/$channel"; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub DESTROY { | ||
| 66 | my ($self) = @_; | ||
| 67 | eval { | ||
| 68 | ( $self->{mqtt} && $self->{mqtt}->unsubscribe($_) ) | ||
| 69 | foreach ( @{ $self->{_ch} } ); | ||
| 70 | }; | ||
| 71 | $self->{logger}->error($@) if $@; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub getNextMessage { | ||
| 75 | my ( $self, $channel, $delay ) = @_; | ||
| 76 | return undef unless $self->{messages}{$channel}; | ||
| 77 | return shift( @{ $self->{messages}{$channel} } ) | ||
| 78 | if @{ $self->{messages}{$channel} }; | ||
| 79 | $self->{mqtt}->tick( $delay // 0 ); | ||
| 80 | return shift( @{ $self->{messages}{$channel} } ) | ||
| 81 | if @{ $self->{messages}{$channel} }; | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub waitForNextMessage { | ||
| 86 | my ( $self, $channel ) = @_; | ||
| 87 | return undef | ||
| 88 | unless $self->{messages}{$channel}; | ||
| 89 | |||
| 90 | # Infinite loop until one message is seen | ||
| 91 | my $res; | ||
| 92 | while ( !$res ) { | ||
| 93 | $res = $self->{redis}->getNextMessage( $channel, 1 ); | ||
| 94 | } | ||
| 95 | return $res; | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; |