| 1 | #! /bin/perl | ||
| 2 | ############################################################################# | ||
| 3 | # | ||
| 4 | # NOTE: This file under revision control using RCS | ||
| 5 | # Any changes made without RCS will be lost | ||
| 6 | # | ||
| 7 | # $Source: /usr/local/cvsroot/vbtk/VBTK/InputStream.pm,v $ | ||
| 8 | # $Revision: 1.5 $ | ||
| 9 | # $Date: 2002/03/04 20:53:07 $ | ||
| 10 | # $Author: bhenry $ | ||
| 11 | # $Locker: $ | ||
| 12 | # $State: Exp $ | ||
| 13 | # | ||
| 14 | # Purpose: A perl library used to access data streams in the | ||
| 15 | # form of a file, a list of files, or a unix command. | ||
| 16 | # If the follow mode is used, the read command will | ||
| 17 | # never return EOF, but will always try to get more | ||
| 18 | # data out of the last file in the list, including | ||
| 19 | # checking to see if that file's inode has changed and | ||
| 20 | # if so, restarting the datastream from the beginning | ||
| 21 | # of the new file. | ||
| 22 | # | ||
| 23 | # Description: | ||
| 24 | # | ||
| 25 | # Directions: The call to new VBTK::InputStream should look like: | ||
| 26 | # | ||
| 27 | # $fh = new VBTK::InputStream( | ||
| 28 | # SourceList => [ "file1", "file2" ... ], | ||
| 29 | # Follow => < 1 | 0 >, | ||
| 30 | # ReadTimeout => <Timeout in seconds>, | ||
| 31 | # MaxReadTime => <Max total read time>); | ||
| 32 | # | ||
| 33 | # The read timeout is how long to block waiting for more I/O | ||
| 34 | # on the file being read before returning from the 'read' call. | ||
| 35 | # The MaxReadTime is the total amount of time allowed before | ||
| 36 | # returning from the 'read' call, even if there is more data | ||
| 37 | # coming in. This is used to prevent a slow but steady data | ||
| 38 | # stream from monopolizing the 'read' call and never returning | ||
| 39 | # control. | ||
| 40 | # | ||
| 41 | # Invoked by: | ||
| 42 | # | ||
| 43 | # Depends on: VBTK::Common | ||
| 44 | # | ||
| 45 | # Copyright (C) 1996 - 2002 Brent Henry | ||
| 46 | # | ||
| 47 | # This program is free software; you can redistribute it and/or | ||
| 48 | # modify it under the terms of version 2 of the GNU General Public | ||
| 49 | # License as published by the Free Software Foundation available at: | ||
| 50 | # http://www.gnu.org/copyleft/gpl.html | ||
| 51 | # | ||
| 52 | # This program is distributed in the hope that it will be useful, | ||
| 53 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 54 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 55 | # GNU General Public License for more details. | ||
| 56 | # | ||
| 57 | ############################################################################# | ||
| 58 | # | ||
| 59 | # | ||
| 60 | # REVISION HISTORY: | ||
| 61 | # | ||
| 62 | # $Log: InputStream.pm,v $ | ||
| 63 | # Revision 1.5 2002/03/04 20:53:07 bhenry | ||
| 64 | # *** empty log message *** | ||
| 65 | # | ||
| 66 | # Revision 1.4 2002/03/04 16:49:09 bhenry | ||
| 67 | # Changed requirement back to perl 5.6.0 | ||
| 68 | # | ||
| 69 | # Revision 1.3 2002/03/02 00:53:54 bhenry | ||
| 70 | # Documentation updates | ||
| 71 | # | ||
| 72 | # Revision 1.2 2002/01/21 17:07:40 bhenry | ||
| 73 | # Disabled 'uninitialized' warnings | ||
| 74 | # | ||
| 75 | # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry | ||
| 76 | # VBTK Project | ||
| 77 | # | ||
| 78 | # | ||
| 79 | |||
| 80 | package VBTK::InputStream; | ||
| 81 | |||
| 82 | use 5.6.0; | ||
| 83 | use strict; | ||
| 84 | use warnings; | ||
| 85 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 86 | no warnings qw(uninitialized); | ||
| 87 | |||
| 88 | use VBTK::Common; | ||
| 89 | use FileHandle; | ||
| 90 | |||
| 91 | # commented out to test grapher | ||
| 92 | our $VERBOSE=$ENV{'VERBOSE'}; | ||
| 93 | |||
| 94 | our $DEFAULT_TIMEOUT = 0; | ||
| 95 | our $DEFAULT_MAX_READ_TIME = 7; | ||
| 96 | our $BUFFER_SIZE = 10240; | ||
| 97 | |||
| 98 | our $prog_name = 'VBTK::InputStream.pm'; | ||
| 99 | |||
| 100 | #------------------------------------------------------------------------------- | ||
| 101 | # Function: new | ||
| 102 | # Description: Object constructor. Allocates memory for all class members | ||
| 103 | # Input Parms: Configuration filename | ||
| 104 | # Output Parms: Pointer to class | ||
| 105 | #------------------------------------------------------------------------------- | ||
| 106 | sub new | ||
| 107 | { | ||
| 108 | my $type = shift; | ||
| 109 | my $self = {}; | ||
| 110 | bless $self, $type; | ||
| 111 | |||
| 112 | log("Setting up InputStream object") if ($VERBOSE > 1); | ||
| 113 | |||
| 114 | # Store the passed input variables | ||
| 115 | $self->set(@_); | ||
| 116 | |||
| 117 | # Set defaults if not specified | ||
| 118 | $self->{ReadTimeout} = $DEFAULT_TIMEOUT if ($self->{ReadTimeout} eq ''); | ||
| 119 | $self->{MaxReadTime} = $DEFAULT_MAX_READ_TIME if ($self->{MaxReadTime} eq ''); | ||
| 120 | |||
| 121 | # Make sure something was passed in the SourceList parm. | ||
| 122 | my $SourceList = $self->{SourceList}; | ||
| 123 | return undef if ($SourceList eq ''); | ||
| 124 | |||
| 125 | # If the value passed in the SourceList parm is not a reference to an array | ||
| 126 | # then create an anonymous array and store the string into it. If the string | ||
| 127 | # does not end in '|' (a unix command), then look for whitespace in the string | ||
| 128 | # and split it up, storing the resulting array. | ||
| 129 | if (ref($SourceList) ne 'ARRAY') | ||
| 130 | { | ||
| 131 | if ($SourceList =~ /\|\s*$/) | ||
| 132 | { | ||
| 133 | $self->{SourceList} = [ $SourceList ]; | ||
| 134 | } | ||
| 135 | else | ||
| 136 | { | ||
| 137 | $self->{SourceList} = [ split(/\s+/,$SourceList) ]; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | # If the CopySourceList variable is specified, then make a copy of the | ||
| 141 | # SourceList array so that as list items are shifted off, it doesn't alter | ||
| 142 | # the original array referenced by SourceList. This obviously only applied | ||
| 143 | # if SourceList is a reference to an array to begin with. | ||
| 144 | elsif($self->{CopySourceList}) | ||
| 145 | { | ||
| 146 | &log("Copying source list") if ($VERBOSE > 1); | ||
| 147 | $self->{SourceList} = [ @{$SourceList} ]; | ||
| 148 | } | ||
| 149 | |||
| 150 | # Open the first element in the input stream list | ||
| 151 | $self->open || return undef; | ||
| 152 | |||
| 153 | return $self; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | #------------------------------------------------------------------------------- | ||
| 158 | # Function: open | ||
| 159 | # Description: Open up the next input source in the SourceList array. Close the | ||
| 160 | # previously open filehandle if it's open. If we're already at the | ||
| 161 | # last source file in the list, and we're in Follow mode, then check | ||
| 162 | # to see if the inode has changed of the filesize has decreased and | ||
| 163 | # if so, re-open the file and begin reading from the start. | ||
| 164 | # Input Parms: None | ||
| 165 | # Output Parms: Return Value | ||
| 166 | #------------------------------------------------------------------------------- | ||
| 167 | sub open | ||
| 168 | { | ||
| 169 | my $obj = shift; | ||
| 170 | my $SourceList = $obj->{SourceList}; | ||
| 171 | my $Follow = $obj->{Follow}; | ||
| 172 | my $fh = $obj->{fh}; | ||
| 173 | my $currSource = $obj->{currSource}; | ||
| 174 | my $listSize = @{$SourceList}; | ||
| 175 | my $pid; | ||
| 176 | |||
| 177 | my ($inode,$fsize); | ||
| 178 | |||
| 179 | # Close the filehandle if it's open and store the return code if the | ||
| 180 | # filehandle was really a child process. | ||
| 181 | if (($fh ne '')&&($obj->{pid})) | ||
| 182 | { | ||
| 183 | &log("Closing FH for '$currSource'") if ($VERBOSE > 1); | ||
| 184 | $fh->close; | ||
| 185 | |||
| 186 | # An error code can appear in either the lower or the higher byte of $?. | ||
| 187 | # By or-ing the low byte with the upper byte, we get an accurate error code. | ||
| 188 | $obj->{retval} = ($? & 0xFF) | ($? >> 8); | ||
| 189 | $obj->{fh} = $fh = undef; | ||
| 190 | } | ||
| 191 | |||
| 192 | # If there's nothing on the list, then just return an error | ||
| 193 | return 0 if ($listSize < 1); | ||
| 194 | |||
| 195 | # Pull off the first entry in the source list and return an error if blank | ||
| 196 | $currSource = shift(@{$SourceList}); | ||
| 197 | return 0 if ($currSource eq ''); | ||
| 198 | |||
| 199 | # If the file handle is still open at this point, then we know it's not a | ||
| 200 | # child process, so just close it and clear out the retval value. | ||
| 201 | if ($fh ne '') | ||
| 202 | { | ||
| 203 | &log("Closing FH for '$currSource'") if ($VERBOSE > 1); | ||
| 204 | $fh->close; | ||
| 205 | $obj->{retval} = undef; | ||
| 206 | } | ||
| 207 | |||
| 208 | # Open the source and check for errors. If it's a unix command being | ||
| 209 | # executed, then the 'open' call will return a pid. | ||
| 210 | &log("Opening input stream '$currSource'") if ($VERBOSE); | ||
| 211 | $fh = new FileHandle; | ||
| 212 | $pid = $fh->open("$currSource"); | ||
| 213 | |||
| 214 | unless ($pid) | ||
| 215 | { | ||
| 216 | &error("Cannot open input stream '$currSource'"); | ||
| 217 | $obj->{currSource} = undef; | ||
| 218 | $obj->{rinVec} = undef; | ||
| 219 | $obj->{pid} = undef; | ||
| 220 | $fh->close(); | ||
| 221 | return 0; | ||
| 222 | } | ||
| 223 | |||
| 224 | # If this is really a child process being executed, then store the PID | ||
| 225 | if($currSource =~ /\|\s*$/) | ||
| 226 | { | ||
| 227 | $obj->{pid} = $pid; | ||
| 228 | } | ||
| 229 | else | ||
| 230 | { | ||
| 231 | $obj->{pid} = undef; | ||
| 232 | } | ||
| 233 | |||
| 234 | # If this is the last file, and we're using Follow mode, and the source | ||
| 235 | # is a file, then store the inode and fsize. | ||
| 236 | if(($listSize == 1)&&($Follow)&&(-f $currSource)) | ||
| 237 | { | ||
| 238 | ($inode,$fsize) = (stat $currSource)[1,7]; | ||
| 239 | $obj->{lastInode} = $inode; | ||
| 240 | $obj->{lastFsize} = $fsize; | ||
| 241 | } | ||
| 242 | |||
| 243 | # Store read-ready vector for later use in 'select'ing filehandle status | ||
| 244 | my $rinVec = ""; | ||
| 245 | vec($rinVec,fileno($fh),1) = 1; | ||
| 246 | $obj->{rinVec} = $rinVec; | ||
| 247 | |||
| 248 | # Store the other variables into the object | ||
| 249 | $obj->{fh} = $fh; | ||
| 250 | $obj->{currSource} = $currSource; | ||
| 251 | |||
| 252 | (1); | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | #------------------------------------------------------------------------------- | ||
| 257 | # Function: read | ||
| 258 | # Description: Read as many rows as possible from the input stream. If EOF is | ||
| 259 | # reached, then open the next file in the source list and read | ||
| 260 | # everything from it. When the last file in the source list is | ||
| 261 | # reached, keep waiting for more data if we're in Follow mode. | ||
| 262 | # Otherwise, just return. | ||
| 263 | # Input Parms: None | ||
| 264 | # Output Parms: Return Value | ||
| 265 | #------------------------------------------------------------------------------- | ||
| 266 | sub read | ||
| 267 | { | ||
| 268 | my $obj = shift; | ||
| 269 | my $fh = $obj->{fh}; | ||
| 270 | my $ReadTimeout = $obj->{ReadTimeout}; | ||
| 271 | my $MaxReadTime = $obj->{MaxReadTime}; | ||
| 272 | my $Follow = $obj->{Follow}; | ||
| 273 | my $rinVec = $obj->{rinVec}; | ||
| 274 | my $lastInode = $obj->{lastInode}; | ||
| 275 | my $lastFsize = $obj->{lastFsize}; | ||
| 276 | my $currSource = $obj->{currSource}; | ||
| 277 | my $line = $obj->{leftover}; | ||
| 278 | my $start_time = time; | ||
| 279 | my $rows = []; | ||
| 280 | |||
| 281 | my ($buf,$found_eof,$num_rows,$inode,$fsize); | ||
| 282 | |||
| 283 | return 0 if (($fh eq '')||($rinVec eq '')); | ||
| 284 | |||
| 285 | # Check to see if there's data ready to be read on the filehandle. | ||
| 286 | my ($nfound, $timeleft) = select($rinVec, undef, undef, $ReadTimeout); | ||
| 287 | |||
| 288 | # Keep reading data, as long as there's data to read, until we reach | ||
| 289 | # the MaxReadTime value. | ||
| 290 | while(($nfound > 0)&&(($start_time + $MaxReadTime) > time)) | ||
| 291 | { | ||
| 292 | sysread($fh,$buf,$BUFFER_SIZE); | ||
| 293 | |||
| 294 | # If nothing was returned, there we're at EOF | ||
| 295 | if($buf eq '') | ||
| 296 | { | ||
| 297 | &log("No more data to read from '$currSource'") if ($VERBOSE > 1); | ||
| 298 | |||
| 299 | # Try to open the next element in the source list. If there isn't | ||
| 300 | # another, then you're done. | ||
| 301 | if($obj->open) | ||
| 302 | { | ||
| 303 | $fh = $obj->{fh}; | ||
| 304 | $rinVec = $obj->{rinVec}; | ||
| 305 | $currSource = $obj->{currSource}; | ||
| 306 | } | ||
| 307 | # If we're in follow mode, and there's a lastInode value, then check | ||
| 308 | # the inode and filesize. If they've changed, then push the currSource | ||
| 309 | # file back onto the SourceList array, set the EOF flag, and break out. | ||
| 310 | elsif(($Follow)&&($lastInode ne '')) | ||
| 311 | { | ||
| 312 | ($inode,$fsize) = (stat $currSource)[1,7]; | ||
| 313 | if (($lastInode != $inode)||($lastFsize > $fsize)) | ||
| 314 | { | ||
| 315 | unshift(@{$obj->{SourceList}},$currSource); | ||
| 316 | $found_eof = 1; | ||
| 317 | } | ||
| 318 | |||
| 319 | $obj->{lastInode} = $inode; | ||
| 320 | $obj->{lastFsize} = $fsize; | ||
| 321 | last; | ||
| 322 | } | ||
| 323 | # Otherwise, set EOF if this is a child process or we're not in | ||
| 324 | # follow mode, and then break out. | ||
| 325 | else | ||
| 326 | { | ||
| 327 | $found_eof = 1 if (($obj->{pid})||(! $Follow)); | ||
| 328 | last; | ||
| 329 | } | ||
| 330 | last; | ||
| 331 | } | ||
| 332 | |||
| 333 | $line .= $buf; | ||
| 334 | |||
| 335 | &log("Read from '$currSource':\n$buf") if ($VERBOSE > 3); | ||
| 336 | ($nfound, $timeleft) = select($rinVec, undef, undef, $ReadTimeout); | ||
| 337 | } | ||
| 338 | |||
| 339 | # Split the retrieved data blob into rows | ||
| 340 | @{$rows} = split(/\n/,$line . " "); | ||
| 341 | $num_rows = @{$rows} - 1; | ||
| 342 | |||
| 343 | # If we received only part of a line at the end of the data blob, then | ||
| 344 | # save the rest for next time | ||
| 345 | my $leftover = pop(@{$rows}); | ||
| 346 | $leftover =~ s/\s$//; | ||
| 347 | $obj->{leftover} = $leftover; | ||
| 348 | |||
| 349 | &log("Text leftover: $leftover") if (($VERBOSE > 1) && ($leftover ne '')); | ||
| 350 | |||
| 351 | # Add the \n back onto the end of each line | ||
| 352 | grep(s/$/\n/,@{$rows}); | ||
| 353 | |||
| 354 | ($rows,$found_eof); | ||
| 355 | } | ||
| 356 | |||
| 357 | # Simple get methods | ||
| 358 | sub getPid { $_[0]->{pid}; } | ||
| 359 | sub getRetVal { $_[0]->{retval}; } | ||
| 360 | sub getCurrSource { $_[0]->{currSource}; } | ||
| 361 | |||
| 362 | 1; | ||
| 363 | __END__ |