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
|
#!/usr/bin/perl
# example.pl - example script for cwdaemon
# Copyright (C) 2012 Jenö Vágó HA5SE
# Copyright (C) 2012 - 2015 Kamil Ignacak
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
use warnings;
use strict;
use IO::Socket::INET;
use IO::Handle;
STDERR->autoflush(1);
STDOUT->autoflush(1);
# How many times to run a basic set of send/receive calls.
my $cycles = 5;
my $server_port = 6789;
my $cwsocket = IO::Socket::INET->new(PeerAddr => "localhost",
PeerPort => $server_port,
Proto => "udp",
Type => SOCK_DGRAM)
or die "Couldn't setup udp server on port $server_port : $@\n";
sub INT_handler {
$cwsocket->close();
exit(0);
}
$SIG{'INT'} = 'INT_handler';
my @words = qw/Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum./;
for (my $i = 0; $i < $cycles; $i++) {
# PTT ON
print "PTT ON\n\n";
print $cwsocket chr(27).'a1';
my $word = @words[rand(50)];
print " sending \"".$word."\"\t";
# First define reply expected from daemon (in this example it's
# first two letters of a word). This is Escaped request 'h'.
print $cwsocket chr(27).'h'.substr($word, 0, 2);
print $cwsocket $word."";
# Leading 'h' will be stripped from reply by example_receive().
my $reply = example_receive($cwsocket, "h");
if ($reply ne substr($word, 0, 2)) {
print("\t\tWrong reply\n");
} else {
print("\t\tCorrect reply\n");
}
}
print("\nThe end. Asking cwdaemon to exit...\n");
# Exit cwdaemon (Escaped request '5').
print $cwsocket chr(27).'5';
$cwsocket->close();
sub example_receive
{
my $cwsocket = shift;
my $expected_prefix = shift;
my $expected_postfix = "\r\n";
my $pre_len = length($expected_prefix);
my $post_len = length($expected_postfix);
my $reply = <$cwsocket>;
if (substr($reply, 0, $pre_len) ne $expected_prefix) {
print("malformed reply, missing leading '$expected_prefix'");
return "";
}
if (substr($reply, length($reply) - $post_len, $post_len) ne $expected_postfix) {
print("malformed reply, missing ending '\\r\\n'");
return "";
}
$reply = substr($reply, $pre_len, length($reply) - $pre_len - $post_len);
print("received ");
if ($pre_len) {
print("'" . $expected_prefix . "' + ");
}
print("'" . $reply . "' + '\\r\\n'");
return $reply;
}
|