/
net-mqtt-pub
208 lines (159 loc) · 4.98 KB
/
net-mqtt-pub
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
#!/usr/bin/perl
use strict;
use warnings;
# ABSTRACT: Perl script for publishing to an MQTT topic
# PODNAME: net-mqtt-pub
use strict;
use Net::MQTT::Constants;
use Net::MQTT::Message;
use IO::Select;
use IO::Socket::INET;
use Time::HiRes;
use Getopt::Long;
use Pod::Usage;
my $help;
my $man;
my $verbose = 0;
my $host = '127.0.0.1';
my $port = 1883;
my $keep_alive_timer = 120;
my $retain;
GetOptions('help|?' => \$help,
'man' => \$man,
'verbose+' => \$verbose,
'host=s' => \$host,
'port=i' => \$port,
'keepalive=i' => \$keep_alive_timer,
'retain' => \$retain,
) or pod2usage(2);
pod2usage(1) if ($help);
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
my $topic = shift || pod2usage(2); # need a topic
my $socket =
IO::Socket::INET->new(PeerAddr => $host.':'.$port,
Timeout => $keep_alive_timer,
) or die "Socket connect failed: $!\n";
my $stdin_buf = '';
my $sock_buf = '';
my $mid = 1;
my $next_ping;
my $got_ping_response = 1;
send_message($socket, message_type => MQTT_CONNECT,
keep_alive_timer => $keep_alive_timer);
my $msg = read_message($socket, $sock_buf) or die "No ConnAck\n";
print 'Received: ', $msg->string, "\n" if ($verbose >= 2);
if (@ARGV) {
$msg = join ' ', @ARGV;
send_message($socket,
message_type => MQTT_PUBLISH,
retain => $retain,
topic => $topic,
message => $msg);
#$msg = read_message($socket, $sock_buf) or die "No PubAck\n";
#print 'Received: ', $msg->string, "\n" if ($verbose >= 2);
exit;
}
while (1) {
$msg = read_message($socket, $sock_buf, $stdin_buf);
if ($msg) {
if (ref $msg) {
if ($msg->message_type == MQTT_PINGRESP) {
$got_ping_response = 1;
print 'Received: ', $msg->string, "\n" if ($verbose >= 3);
} else {
print 'Received: ', $msg->string, "\n" if ($verbose >= 2);
}
} else {
send_message($socket,
message_type => MQTT_PUBLISH,
retain => $retain,
topic => $topic,
message => $msg);
}
}
if (Time::HiRes::time > $next_ping) {
die "Ping Response timeout. Exiting\n" unless ($got_ping_response);
send_message($socket, message_type => MQTT_PINGREQ);
}
}
sub send_message {
my $socket = shift;
my $msg = Net::MQTT::Message->new(@_);
print 'Sending: ', $msg->string, "\n" if ($verbose >= 2);
$msg = $msg->bytes;
syswrite $socket, $msg, length $msg;
print dump_string($msg, 'Sent: '), "\n\n" if ($verbose >= 3);
$next_ping = Time::HiRes::time + $keep_alive_timer;
}
sub read_message {
my $socket = shift;
my $select = IO::Select->new($socket);
$select->add(\*STDIN) if (@_ > 1);
my $timeout = $next_ping - Time::HiRes::time;
do {
my $mqtt = Net::MQTT::Message->new_from_bytes($_[0], 1);
return $mqtt if (defined $mqtt);
return $1 if (@_ > 1 && $_[1] =~ s/^(.*?)\n//);
my @handles = $select->can_read($timeout) or return;
$timeout = $next_ping - Time::HiRes::time;
foreach my $handle (@handles) {
if ($handle eq $socket) {
my $bytes = sysread $socket, $_[0], 2048, length $_[0];
unless ($bytes) {
die "Socket closed ", (defined $bytes ? 'gracefully' : 'error'), "\n";
}
print "Receive buffer: ", dump_string($_[0], ' '), "\n\n"
if ($verbose >= 3);
} else {
my $bytes = sysread STDIN, $_[1], 2048, length $_[1];
exit unless ($bytes);
}
}
} while ($timeout > 0);
return;
}
__END__
=pod
=head1 NAME
net-mqtt-pub - Perl script for publishing to an MQTT topic
=head1 VERSION
version 1.130190
=head1 SYNOPSIS
# messages one per line on stdin
echo message | net-mqtt-pub [options] topic
# message as command line arguments
net-mqtt-pub [options] topic this is a message
=head1 DESCRIPTION
This script publishes each line from stdin as an MQTT message on the
given topic.
=head1 OPTIONS
=over
=item B<-help>
Print a brief help message.
=item B<-man>
Print the manual page.
=item B<-host>
The host running the MQTT service. The default is C<127.0.0.1>.
=item B<-port>
The port of the running MQTT service. The default is 1883.
=item B<-verbose>
Include more verbose output. Without this option the script only
outputs errors
=item B<-keepalive NNN>
The keep alive timer value. Defaults to 120 seconds. For simplicity,
it is also currently used as the connection timeout.
=item B<-retain>
Set the retain flag on the message. Default is not set.
=back
=head1 SEE ALSO
Net::MQTT::Message(3)
=head1 DISCLAIMER
This is B<not> official IBM code. I work for IBM but I'm writing this
in my spare time (with permission) for fun.
=head1 AUTHOR
Mark Hindess <soft-cpan@temporalanomaly.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Mark Hindess.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut