Debian Codegraph @

examples/example.pl

raw
1 #!/usr/bin/perl
2
3
4 # example.pl - example script for cwdaemon
5 # Copyright (C) 2012 Jenö Vágó HA5SE
6 # Copyright (C) 2012 - 2015 Kamil Ignacak
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Library General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
22
23 use warnings;
24 use strict;
25
26 use IO::Socket::INET;
27
28 use IO::Handle;
29 STDERR->autoflush(1);
30 STDOUT->autoflush(1);
31
32 # How many times to run a basic set of send/receive calls.
33 my $cycles = 5;
34
35
36
37 my $server_port = 6789;
38
39 my $cwsocket = IO::Socket::INET->new(PeerAddr => "localhost",
40 PeerPort => $server_port,
41 Proto => "udp",
42 Type => SOCK_DGRAM)
43
44 or die "Couldn't setup udp server on port $server_port : $@\n";
45
46
47 sub INT_handler {
48
49 $cwsocket->close();
50
51 exit(0);
52
53 }
54
55 $SIG{'INT'} = 'INT_handler';
56
57 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./;
58
59
60
61
62
63
64 for (my $i = 0; $i < $cycles; $i++) {
65
66 # PTT ON
67 print "PTT ON\n\n";
68 print $cwsocket chr(27).'a1';
69
70 my $word = @words[rand(50)];
71 print " sending \"".$word."\"\t";
72
73 # First define reply expected from daemon (in this example it's
74 # first two letters of a word). This is Escaped request 'h'.
75 print $cwsocket chr(27).'h'.substr($word, 0, 2);
76
77 print $cwsocket $word."";
78
79 # Leading 'h' will be stripped from reply by example_receive().
80 my $reply = example_receive($cwsocket, "h");
81
82 if ($reply ne substr($word, 0, 2)) {
83 print("\t\tWrong reply\n");
84 } else {
85 print("\t\tCorrect reply\n");
86 }
87 }
88
89
90
91
92 print("\nThe end. Asking cwdaemon to exit...\n");
93 # Exit cwdaemon (Escaped request '5').
94 print $cwsocket chr(27).'5';
95
96
97
98
99
100 $cwsocket->close();
101
102
103
104
105
106 sub example_receive
107 {
108 my $cwsocket = shift;
109 my $expected_prefix = shift;
110
111 my $expected_postfix = "\r\n";
112 my $pre_len = length($expected_prefix);
113 my $post_len = length($expected_postfix);
114
115
116 my $reply = <$cwsocket>;
117
118
119 if (substr($reply, 0, $pre_len) ne $expected_prefix) {
120 print("malformed reply, missing leading '$expected_prefix'");
121 return "";
122 }
123
124
125 if (substr($reply, length($reply) - $post_len, $post_len) ne $expected_postfix) {
126 print("malformed reply, missing ending '\\r\\n'");
127 return "";
128 }
129
130
131 $reply = substr($reply, $pre_len, length($reply) - $pre_len - $post_len);
132 print("received ");
133 if ($pre_len) {
134 print("'" . $expected_prefix . "' + ");
135 }
136 print("'" . $reply . "' + '\\r\\n'");
137
138
139 return $reply;
140 }