blib/lib/UDPServersAndClients.pm

filter: Statementstmt 7.3% 12 / 163 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 20.0% 1 / 5 Podpod 0.0% 0 / 4 totaltotal 5.2% 13 / 248 CC 31 SCAR 57.1
CC
31
Cov
5%
client_filestore
462.0
server_filestore
90.0
server_onlinecheck
6.0
CRAP 301.1
↑ Summary
1 #!/usr/bin/perl
2  
3 package UDPServersAndClients;
4  
5 1 require Exporter;
6  
7 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our @ISA = qw (Exporter);
10 1 our @EXPORT = qw (server_onlinecheck client_onlinecheck server_filestore client_filestore $ioserver $ioclient $ip);
11 1 our @EXPORT_OK = (@EXPORT);
12  
13 1 our $VERSION = '1.0a';
14  
15 1 our $ip = 'localhost';
16 1 our $ioserver = new IO::Socket::INET -> new (Proto=>'udp', LocalPort=>'1224');
17 1 our $ioclient = new IO::Socket::INET -> new (Proto=>'udp', PeerPort=>'1224', PeerAddr=>"$ip");
18  
19 0 ▶sub server_onlinecheck {
Subroutine server_onlinecheck: not called CC=2 SCAR=17.9
Pod undocumented
20 0 print "Server running. Waiting for connections. Press Control-C to exit server.\n";
21 0 my $clientdata;
22 0 while () {
23
24 0 ▶ if ($ioserver -> recv ($clientdata, 3000)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ioserver->recv($clientdata, 3000)) 0 0 0
25 0 print "A client has connected. Sending the client data...\n";
26 0 $ioserver -> send ("Request Received. Server online.\n");
27 }
28 }
29 }
30  
31 0 ▶sub client_onlinecheck {
Subroutine client_onlinecheck: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my $input;
33 my $serverdata;
34 0 print 'If after typing message this program hangs, you will have to exit it with Control-C and will know that the server is down. Enter a message to send the server to check if server is online: ' ;
35 0 chomp ($input = <STDIN>);
36 0 $ioclient -> send ($input);
37 0 $ioclient -> recv ($serverdata, 200);
38 0 ▶ if ($serverdata)
Branch: 1 branch
BranchTrueFalseTotal
if ($serverdata) 0 0 0
39 {
40 0 print "Server is running.\n";
41 0 exit 1;
42 }
43 }
44 sub server_filestore
45 0 ▶ {
Subroutine server_filestore: not called CC=9 SCAR=45.0
Pod undocumented
46 0 my $clientdata;
47 my $filedata;
48 0 my $dirdata;
49 0 my $filetosend;
50 0 my @dir;
51 0 my @fdataspl;
52 0 my @sentfdata;
53 0 my $joinedfdata;
54 0 my $in;
55 0 print "Server running. Waiting for connections. Press Control-C to exit server.\n";
56 0 while ()
57 {
58 0 ▶ if ($ioserver -> recv ($clientdata, 3000) )
Branch: 1 branch
BranchTrueFalseTotal
if ($ioserver->recv($clientdata, 3000)) 0 0 0
59 {
60 0 print "[[[INCOMING DATA]]]\n";
61 }
62 0 ▶ if ($clientdata eq "|sendfile|")
Branch: 1 branch
BranchTrueFalseTotal
if ($clientdata eq "|sendfile|") 0 0 0
63 {
64 0 print "A client is trying to send a file.\n";
65 0 $ioserver -> recv ($filedata, 1000000000);
66 0 @fdataspl = split (/\|\|\|/, $filedata);
67 0 ▶ if (! @fdataspl)
Branch: 1 branch
BranchTrueFalseTotal
unless (@fdataspl) 0 0 0
68 {
69 0 print "Invalid file sent\n";
70 0 $ioserver -> send ("|fileerror|");
71 0 next;
72 }
73 0 ▶ if (-e $fdataspl[0])
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fdataspl[0]) { } 0 0 0
74 {
75 0 print "File sending to server canceled. File exists on server.\n";
76 0 $ioserver -> send ("|fileexists|");
77 }
78 else
79 {
80 0 open (F, ">$fdataspl[0]");
81 0 print F $fdataspl[1];
82 0 close (F);
83 0 print "File $fdataspl[0] saved to server directory.\n";
84 0 $ioserver -> send ("|sendsuccess|");
85  
86 }
87 }
88 0 ▶ if ($clientdata eq "|listfiles|")
Branch: 1 branch
BranchTrueFalseTotal
if ($clientdata eq "|listfiles|") 0 0 0
89 {
90 0 print "Client requested list of files.\n";
91 0 opendir (D, "./");
92 0 @dir = readdir (D);
93 0 closedir (D);
94 0 $dirdata = join ("\n", @dir);
95 0 $ioserver -> send ($dirdata);
96 }
97 0 ▶ if ($clientdata eq "|getfile|")
Branch: 1 branch
BranchTrueFalseTotal
if ($clientdata eq "|getfile|") 0 0 0
98 {
99 0 print "A client requested a file.\n";
100 0 $ioserver -> recv ($filetosend, 1000);
101 0 ▶ if (-e $filetosend and ! -d $filetosend)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filetosend and not -d $filetosend) { } 0 0 0
Condition0%0/3
-e $filetosend and not -d $filetosend
!ll&&!rl&&r
000
MC/DC0%0/2
-e $filetosend and not -d $filetosend
-e $filetosend not -d $filetosend
Truth table0%0/3
A and not(B)
A= -e $filetosendB= not -d $filetosend
ABresult
0X0
100
111
102 {
103 0 open (FTS, $filetosend);
104 0 @sentfdata = <FTS>;
105 0 close (FTS);
106 0 $joinedfdata = join ('', @sentfdata);
107 0 print "Sending file...\n";
108 0 $ioserver -> send ($joinedfdata);
109 }
110 else
111 {
112 0 print "Client specified invalid file name to get.\n";
113 0 $ioserver -> send ("|filenoexist|");
114 0 next;
115 }
116 }
117 }
118 }
119  
120 sub client_filestore
121 0 ▶ {
Subroutine client_filestore: not called CC=21 SCAR=61.4
Pod undocumented
122 0 my $recv1;
123 my $recv2;
124 0 my $recv3;
125 0 my $filein;
126 0 my $filein2;
127 0 my $filein3;
128 0 my $overwritetogg;
129 0 my $filedata;
130 0 my $filedata2;
131 0 my $genin;
132 0 my @filedata;
133 0 my $joinedfdata;
134 0 my $sendrec;
135 0 print "Welcome...\n";
136 0 while ()
137 {
138 0 print "Enter '/get' to get a file or '/list' for a list of files on the server.\nEnter '/send' to send a file to the server.\nEnter '/ls' to list local files.\nEnter a blank line to exit. \n";
139 0 chomp ($genin = <STDIN>);
140 0 ▶ if (! $genin)
Branch: 5 branches
BranchTrueFalseTotal
if (not $genin) { } 0 0 0
elsif ($genin eq '/list') { } 0 0 0
elsif ($genin eq '/get') { } 0 0 0
elsif ($genin eq '/send') { } 0 0 0
elsif ($genin eq '/ls') { } 0 0 0
141 {
142 0 last;
143 }
144 elsif ($genin eq '/list')
145 {
146 0 $ioclient -> send ("|listfiles|");
147 0 $ioclient -> recv ($recv1, 4000);
148 0 print "Files on the server:\n";
149 0 print "$recv1\n";
150 }
151 elsif ($genin eq '/get')
152 {
153 0 print "Enter the name of a file on the remote server to get: ";
154 0 chomp ($filein = <STDIN>);
155 0 ▶ if ($filein eq "." or $filein eq "..")
Branch: 1 branch
BranchTrueFalseTotal
if ($filein eq "." or $filein eq "..") 0 0 0
Condition0%0/3
$filein eq "." or $filein eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$filein eq "." or $filein eq ".."
$filein eq "." $filein eq ".."
Truth table0%0/3
A or B
A= $filein eq "."B= $filein eq ".."
ABresult
1X1
011
000
156 {
157 0 print "Those are special files and cannot be gotten.\n";
158 0 next;
159 }
160 0 ▶ if ($filein)
Branch: 1 branch
BranchTrueFalseTotal
if ($filein) 0 0 0
161 {
162 0 $ioclient -> send ("|getfile|");
163 0 $ioclient -> send ($filein);
164 0 print "Receiving file...\n";
165 0 $ioclient -> recv ($filedata, 1000000000);
166 0 ▶ if ($filedata eq "|filenoexist|")
Branch: 1 branch
BranchTrueFalseTotal
if ($filedata eq '|filenoexist|') { } 0 0 0
167 {
168 0 print "File does not exist on the remote server or file is a directory. Try again.\n";
169 0 print "Operation canceled.\n";
170 0 next;
171 }
172 else
173 {
174 0 print "Enter filename for your file from the server: ";
175 0 chomp ($filein = <STDIN>);
176 0 ▶ if ($filein eq "." or $filein eq ".." or -d $filein)
Branch: 1 branch
BranchTrueFalseTotal
if ($filein eq "." or $filein eq ".." or -d $filein) 0 0 0
Condition0%0/6
$filein eq "." or $filein eq ".." or -d $filein
l!l&&r!l&&!r
000
$filein eq "." or $filein eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/3
$filein eq "." or $filein eq ".." or -d $filein
$filein eq "." $filein eq ".." -d $filein
Truth table0%0/4
A or B or C
A= $filein eq "."B= $filein eq ".."C= -d $filein
ABCresult
1XX1
01X1
0011
0000
177 {
178 0 print "File is a directory. You cannot overwrite it.\n";
179 0 print "Operation canceled.\n";
180 0 next;
181 }
182 0 ▶ if (-e $filein)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filein) { } 0 0 0
183 {
184 0 print "File exists. Overwrite? (y/n) ";
185 0 chomp ($overwritetogg = <STDIN>);
186 0 ▶ if ($overwritetogg eq 'y')
Branch: 1 branch
BranchTrueFalseTotal
if ($overwritetogg eq 'y') { } 0 0 0
187 {
188 0 open (F, ">$filein");
189 0 print F $filedata;
190 0 close (F);
191 0 print "File saved as $filein.\n";
192 }
193 else
194 {
195 0 print "Operation canceled.\n";
196 0 next;
197 }
198 }
199 else
200 {
201 0 open (F, ">$filein");
202 0 print F $filedata;
203 0 close (F);
204 0 print "File saved as $filein.\n";
205 }
206  
207 }
208  
209 }
210  
211 }
212 elsif ($genin eq '/send')
213 {
214 0 print "Enter the file name of the file you wish to send: ";
215 0 chomp ($filein2 = <STDIN>);
216 0 ▶ if (! -e $filein2 or -d $filein2 or ! $filein2)
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $filein2 or -d $filein2 or not $filein2) 0 0 0
Condition0%0/6
not -e $filein2 or -d $filein2 or not $filein2
l!l&&r!l&&!r
000
not -e $filein2 or -d $filein2
l!l&&r!l&&!r
000
MC/DC0%0/3
not -e $filein2 or -d $filein2 or not $filein2
not -e $filein2 -d $filein2 not $filein2
Truth table0%0/4
not(A) or B or not(C)
A= not -e $filein2B= -d $filein2C= not $filein2
ABCresult
1XX1
01X1
0011
0000
217 {
218 0 print "File does not exist or is a directory. Try again.\n";
219 0 print "Operation canceled.\n";
220 0 next;
221 }
222 0 print "Enter what you want to call the file on the remote server: ";
223 0 chomp ($filein3 = <STDIN>);
224 0 ▶ if (! $filein3)
Branch: 1 branch
BranchTrueFalseTotal
unless ($filein3) 0 0 0
225 {
226 0 print "Please specify a file name. Try again.\n";
227 0 print "Operation canceled.\n";
228 0 next;
229 }
230 0 $ioclient -> send ("|sendfile|");
231 0 open (F, $filein2);
232 0 @filedata = <F>;
233 0 close (F);
234 0 $joinedfdata = join ('', @filedata);
235 0 $ioclient -> send ("$filein3|||$joinedfdata");
236 0 $ioclient -> recv ($sendrec, 1000);
237 0 ▶ if ($sendrec eq "|sendsuccess|")
Branch: 2 branches
BranchTrueFalseTotal
if ($sendrec eq '|sendsuccess|') { } 0 0 0
elsif ($sendrec eq '|fileerror|') { } 0 0 0
238 {
239 0 print "File saved to server successfully.\n";
240 }
241 elsif ($sendrec eq "|fileerror|")
242 {
243 0 print "A general error occured. Try again.\n";
244 0 print "Operation canceled.\n";
245 }
246 else
247 {
248 0 print "File exists on server. Try a different file name.\n";
249 0 print "Operation canceled.\n";
250 }
251 }
252 elsif ($genin eq '/ls')
253 {
254 0 my @dir;
255 my $j;
256 0 opendir (D, "./");
257 0 @dir = readdir (D);
258 0 closedir (D);
259 0 $j = join ("\n", @dir);
260 0 print "Local files:\n";
261 0 print "$j\n";
262 }
263 else
264 {
265 0 print "Invalid command.\n";
266 }
267  
268 }
269 }
270 1 1;
271  
272 __END__
↑ Summary