| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Queue::Server.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: UNIX-socket based queue: server for command-line analyzer | ||
| 6 | |||
| 7 | package DTA::CAB::Queue::Server; | ||
| 8 | use DTA::CAB::Socket ':flags'; | ||
| 9 | use DTA::CAB::Socket::UNIX; | ||
| 10 | use DTA::CAB::Queue::Client; | ||
| 11 | use DTA::CAB::Format; | ||
| 12 | use DTA::CAB::Format::Builtin; | ||
| 13 | use DTA::CAB::Utils ':temp', ':files'; | ||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | ##============================================================================== | ||
| 18 | ## Globals | ||
| 19 | ##============================================================================== | ||
| 20 | our @ISA = qw(DTA::CAB::Socket::UNIX); | ||
| 21 | |||
| 22 | ##============================================================================== | ||
| 23 | ## Constructors etc. | ||
| 24 | ##============================================================================== | ||
| 25 | |||
| 26 | ## $qs = DTA::CAB::Queue::Server->new(%args) | ||
| 27 | ## + %$qs, %args: | ||
| 28 | ## ( | ||
| 29 | ## ##-- NEW in DTA::CAB::Queue::Server | ||
| 30 | ## queue => \@queue, ##-- actual queue data | ||
| 31 | ## status => $str, ##-- queue status (defualt: 'active') | ||
| 32 | ## ntok => $ntok, ##-- total number of tokens processed by clients | ||
| 33 | ## nchr => $nchr, ##-- total number of characters processed by clients | ||
| 34 | ## blocks => \%blocks, ##-- output tracker: %blocks = ($outfile => $po={cur=>$max_input_byte_written, pending=>\@pending}, ...) | ||
| 35 | ## ## + @pending is a list of pending blocks ($pb={off=>$ioffset, len=>$ilength, data=>\$data, ...}, ...) | ||
| 36 | ## logBlock => $level, ##-- log-level for block merge operations (default=undef (none)) | ||
| 37 | ## ## | ||
| 38 | ## ##-- INHERITED from DTA::CAB::Socket::UNIX | ||
| 39 | ## local => $path, ##-- path to local UNIX socket (server only; set to empty string to use a tempfile) | ||
| 40 | ## #peer => $path, ##-- path to peer socket (client only) | ||
| 41 | ## listen => $n, ##-- queue size for listen (default=SOMAXCONN) | ||
| 42 | ## unlink => $bool, ##-- if true, server socket will be unlink()ed on DESTROY() (default=true) | ||
| 43 | ## perms => $perms, ##-- file create permissions for server socket (default=0600) | ||
| 44 | ## pid => $pid, ##-- pid of creating process (for auto-unlink) | ||
| 45 | ## ## | ||
| 46 | ## ##-- INHERITED from DTA::CAB::Socket | ||
| 47 | ## fh => $sockfh, ##-- an IO::Socket::UNIX object for the socket | ||
| 48 | ## timeout => $secs, ##-- default timeout for select() (default=undef: none) | ||
| 49 | ## nonblocking => $bool, ##-- set O_NONBLOCK on open? (override default=true) | ||
| 50 | ## logSocket => $level, ##-- log level for full socket trace (default=undef (none)) | ||
| 51 | ## logRequest => $level, ##-- log level for client requests (server only; default=undef (none)) | ||
| 52 | ## ) | ||
| 53 | sub new { | ||
| 54 | my $that = shift; | ||
| 55 | return $that->SUPER::new( | ||
| 56 | local => '', ##-- use temp socket if unspecified | ||
| 57 | |||
| 58 | ##-- local data | ||
| 59 | queue=>[], | ||
| 60 | status => 'active', | ||
| 61 | ntok => 0, | ||
| 62 | nchr => 0, | ||
| 63 | blocks => {}, | ||
| 64 | |||
| 65 | ##-- logging | ||
| 66 | logBlock => undef, | ||
| 67 | logRequest => undef, | ||
| 68 | logSocket => undef, | ||
| 69 | |||
| 70 | ##-- overrides | ||
| 71 | nonblocking => 1, | ||
| 72 | |||
| 73 | ##-- user args | ||
| 74 | @_, | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | |||
| 78 | ##============================================================================== | ||
| 79 | ## Open/Close | ||
| 80 | |||
| 81 | ## $bool = $qs->opened() | ||
| 82 | ## $qs = $qs->close() | ||
| 83 | ## $qs = $qs->open(%args) | ||
| 84 | ## + all INHERITED from CAB::Socket::UNIX | ||
| 85 | |||
| 86 | ##============================================================================== | ||
| 87 | ## Socket Protocol | ||
| 88 | ## + all INHERITED from CAB::Socket | ||
| 89 | |||
| 90 | ##============================================================================== | ||
| 91 | ## Queue Maintenance | ||
| 92 | ## + for use from main thread | ||
| 93 | |||
| 94 | ## $n_items = $q->size() | ||
| 95 | sub size { | ||
| 96 | return scalar(@{$_[0]{queue}}); | ||
| 97 | } | ||
| 98 | |||
| 99 | ## $n_items = $q->enq($item) | ||
| 100 | ## + enqueue an item; returns new number of items in queue | ||
| 101 | sub enq { | ||
| 102 | push(@{$_[0]{queue}},$_[1]); | ||
| 103 | } | ||
| 104 | |||
| 105 | ## $item_or_undef = $q->deq() | ||
| 106 | ## + de-queue a single item; undef at end-of-queue | ||
| 107 | sub deq { | ||
| 108 | shift(@{$_[0]{queue}}); | ||
| 109 | } | ||
| 110 | |||
| 111 | ## $item = $q->peek() | ||
| 112 | ## + peek at the top of the queue; undef if queue is empty | ||
| 113 | sub peek { | ||
| 114 | return $_[0]{queue}[0]; | ||
| 115 | } | ||
| 116 | |||
| 117 | ## $q = $q->clear() | ||
| 118 | ## + clear the queue | ||
| 119 | sub clear { | ||
| 120 | @{$_[0]{queue}} = qw(); | ||
| 121 | return $_[0]; | ||
| 122 | } | ||
| 123 | |||
| 124 | ## ($ntok,$nchr) = $qs->addcounts($ntok,$nchr) | ||
| 125 | ## + get or add to total number of (tokens,character) processed | ||
| 126 | BEGIN { | ||
| 127 | *counts = \&addcounts; | ||
| 128 | } | ||
| 129 | sub addcounts { | ||
| 130 | $_[0]{ntok} += $_[1] if (defined($_[1])); | ||
| 131 | $_[0]{nchr} += $_[2] if (defined($_[2])); | ||
| 132 | return @{$_[0]}{qw(ntok nchr)}; | ||
| 133 | } | ||
| 134 | |||
| 135 | ## $qs = $qs->addblock(\%blk) | ||
| 136 | ## + append block \%blk to appropriate output file if possible, or save it for later | ||
| 137 | ## + %blk should have keys: (off=>$nbytes, len=>$nbytes, ofile=>$ofilename, fmt=>$class, data=>\$data, ...) | ||
| 138 | sub addblock { | ||
| 139 | my ($qs,$blk) = @_; | ||
| 140 | |||
| 141 | ##-- push block to block-tracker's ($bt) pending list | ||
| 142 | my ($bt); | ||
| 143 | $bt = $qs->{blocks}{$blk->{ofile}} = {cur=>0,pending=>[]} if (!defined($bt=$qs->{blocks}{$blk->{ofile}})); | ||
| 144 | push(@{$bt->{pending}}, $blk); | ||
| 145 | |||
| 146 | ##-- greedy append | ||
| 147 | if ($blk->{id}[0] == $bt->{cur}) { | ||
| 148 | my $fmt = DTA::CAB::Format->newFormat($blk->{ofmt} || $DTA::CAB::Format::CLASS_DEFAULT); | ||
| 149 | @{$bt->{pending}} = sort {$a->{id}[0]<=>$b->{id}[0]} @{$bt->{pending}}; | ||
| 150 | while (@{$bt->{pending}} && $bt->{pending}[0]{id}[0]==$bt->{cur}) { | ||
| 151 | $blk=shift(@{$bt->{pending}}); | ||
| 152 | |||
| 153 | $qs->vlog($qs->{logBlock}, "BLOCK_APPEND(ofile=$blk->{ofile}, id=$blk->{id}[0]/$blk->{id}[1], ioff=$blk->{ioff}, ilen=$blk->{ilen}, iend=".($blk->{ioff}+$blk->{ilen}).")"); | ||
| 154 | $fmt->blockAppend($blk); | ||
| 155 | $bt->{cur}++; | ||
| 156 | } | ||
| 157 | } else { | ||
| 158 | $qs->vlog($qs->{logBlock}, "BLOCK_DELAY(ofile=$blk->{ofile}, id=$blk->{id}[0]/$blk->{id}[1])"); | ||
| 159 | } | ||
| 160 | |||
| 161 | return $qs; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | ##============================================================================== | ||
| 166 | ## Server Methods | ||
| 167 | |||
| 168 | ## $class = $CLASS_OR_OBJECT->clientClass() | ||
| 169 | ## + default client class, used by newClient() | ||
| 170 | sub clientClass { | ||
| 171 | return 'DTA::CAB::Queue::ClientConn'; | ||
| 172 | } | ||
| 173 | |||
| 174 | ## $client = $CLASS_OR_OBJECT->newClient(%args) | ||
| 175 | ## + wrapper for clients, called by $s->accept() | ||
| 176 | ## + default just calls $CLASS_OR_OBJECT->clientClass->new(%args) | ||
| 177 | sub newClient { | ||
| 178 | my $that = shift; | ||
| 179 | return $that->clientClass->new(@_, logSocket=>$that->{logSocket}); | ||
| 180 | } | ||
| 181 | |||
| 182 | ## $cli_or_undef = $qs->accept() | ||
| 183 | ## $cli_or_undef = $qs->accept($timeout_secs) | ||
| 184 | ## + accept incoming client connections with optional timeout | ||
| 185 | ## + INHERITED from DTA::CAB::Socket | ||
| 186 | |||
| 187 | ## $rc = $qs->handleClient($cli) | ||
| 188 | ## $rc = $qs->handleClient($cli, %callbacks) | ||
| 189 | ## + handle a single client request | ||
| 190 | ## + INHERITED from DTA::CAB::Socket | ||
| 191 | |||
| 192 | ##-------------------------------------------------------------- | ||
| 193 | ## Server Methods: Request Handling | ||
| 194 | ## | ||
| 195 | ## + request commands (case-insensitive) handled here: | ||
| 196 | ## ADDCOUNTS $NN : add to total number of (tokens,characters) processed; arg (string) $NN=pack('NN',$ntok,$chr); no response | ||
| 197 | ## ADDBLOCK $blk : block output; $blk is a HASH-ref passed to $qs->block($blk); no response | ||
| 198 | ## DEQ : dequeue the first item in the queue; response: $cli->put($item) | ||
| 199 | ## DEQ_STR : dequeue a string reference; response: $cli->put_str(\$item) | ||
| 200 | ## DEQ_REF : dequeue a reference; response: $cli->put_ref($item) | ||
| 201 | ## ENQ $item : enqueue an item; no response | ||
| 202 | ## ENQ_STR $str : enqueue a string-reference; no response | ||
| 203 | ## ENQ_REF $ref : enqueue a reference; no response | ||
| 204 | ## SIZE : get current queue size; response=STRING $qs->size() | ||
| 205 | ## STATUS : get queue status response: STRING $qs->{status} | ||
| 206 | ## CLEAR : clear queue; no response | ||
| 207 | ## QUIT : close client connection; no response | ||
| 208 | ## ... : other messages are passed to $callback->(\$request,$cli) or produce an error | ||
| 209 | ## + returns: same as $callback->() if called, otherwise $qs | ||
| 210 | |||
| 211 | |||
| 212 | ## $qs = $qs->handle_deq($cli,\$cmd) | ||
| 213 | ## $qs = $qs->handle_deq_str($cli,\$cmd) | ||
| 214 | ## $qs = $qs->handle_deq_ref($cli,\$cmd) | ||
| 215 | ## + implements "$item = DEQ", "\$str = DEQ_STR", "$ref = DEQ_REF" | ||
| 216 | BEGIN { | ||
| 217 | *handle_deq_str = *handle_deq_ref = \&handle_deq; | ||
| 218 | } | ||
| 219 | sub handle_deq { | ||
| 220 | my ($qs,$cli,$creq) = @_; | ||
| 221 | my $cmd = lc($$creq); | ||
| 222 | my $qu = $qs->{queue}; | ||
| 223 | if ($cmd =~ /^deq(?:_ref|_str)?$/) { | ||
| 224 | ##-- DEQ: dequeue an item | ||
| 225 | if (!@{$qs->{queue}}) { $cli->put_eoq(); } | ||
| 226 | elsif ($cmd eq 'deq') { $cli->put( $qu->[0] ); } | ||
| 227 | elsif ($cmd eq 'deq_str') { $cli->put_str( ref($qu->[0]) ? $qu->[0] : \$qu->[0] ); } | ||
| 228 | elsif ($cmd eq 'deq_ref') { $cli->put_ref( ref($qu->[0]) ? $qu->[0] : \$qu->[0] ); } | ||
| 229 | shift(@$qu); | ||
| 230 | } | ||
| 231 | return $qs; | ||
| 232 | } | ||
| 233 | |||
| 234 | ## $qs = $qs->handle_enq($cli,\$cmd) | ||
| 235 | ## + implements "ENQ $item" | ||
| 236 | sub handle_enq { | ||
| 237 | my ($qs,$cli,$creq) = @_; | ||
| 238 | my $buf = undef; | ||
| 239 | my $ref = $cli->get(\$buf); | ||
| 240 | push(@{$qs->{queue}}, ($ref eq \$buf ? $buf : $ref)); | ||
| 241 | return $qs; | ||
| 242 | } | ||
| 243 | |||
| 244 | ## $qs = $qs->handle_enq_str($cli,\$cmd) | ||
| 245 | ## $qs = $qs->handle_enq_ref($cli,\$cmd) | ||
| 246 | ## + implements "ENQ_STR \$str", "ENQ_REF $ref" | ||
| 247 | BEGIN { | ||
| 248 | *handle_enq_str = *handle_enq_ref; | ||
| 249 | } | ||
| 250 | sub handle_enq_ref { | ||
| 251 | my ($qs,$cli,$creq) = @_; | ||
| 252 | my $ref = $cli->get(); | ||
| 253 | push(@{$qs->{queue}}, $ref); | ||
| 254 | return $qs; | ||
| 255 | } | ||
| 256 | |||
| 257 | ## $qs = $qs->handle_size($cli,$creq) | ||
| 258 | ## + implements "$size = SIZE" | ||
| 259 | sub handle_size { | ||
| 260 | #my ($qs,$cli,$creq) = @_; | ||
| 261 | #my $size = $_[0]->size; | ||
| 262 | $_[1]->put_str($_[0]->size); | ||
| 263 | return $_[0]; | ||
| 264 | } | ||
| 265 | |||
| 266 | ## $qs = $qs->handle_status($cli,$creq) | ||
| 267 | ## + implements "$status = STATUS" | ||
| 268 | sub handle_status { | ||
| 269 | #my ($qs,$cli,$creq) = @_; | ||
| 270 | $_[1]->put_str($_[0]{status}); | ||
| 271 | return $_[0]; | ||
| 272 | } | ||
| 273 | |||
| 274 | ## $qs = $qs->handle_clear($cli,$creq) | ||
| 275 | ## + implements "CLEAR" | ||
| 276 | sub handle_clear { | ||
| 277 | @{$_[0]{queue}} = qw(); | ||
| 278 | return $_[0]; | ||
| 279 | } | ||
| 280 | |||
| 281 | ## $qs = $qs->handle_quit($cli,$creq) | ||
| 282 | ## + implements "QUIT" | ||
| 283 | BEGIN { | ||
| 284 | *handle_bye = *handle_close = *handle_exit = \&handle_quit; | ||
| 285 | } | ||
| 286 | sub handle_quit { | ||
| 287 | $_[1]->close(); | ||
| 288 | return $_[0]; | ||
| 289 | } | ||
| 290 | |||
| 291 | ## $qs = $qs->handle_addcounts($cli,\$cmd) | ||
| 292 | ## + implements "ADDCOUNTS pack('NN',$ntok,$nchr)" | ||
| 293 | sub handle_addcounts { | ||
| 294 | my ($qs,$cli,$creq) = @_; | ||
| 295 | my $buf = $cli->get(); | ||
| 296 | $qs->addcounts(unpack('NN',$$buf)); | ||
| 297 | return $qs; | ||
| 298 | } | ||
| 299 | |||
| 300 | ## $qs = $qs->handle_addblock($cli,\$cmd) | ||
| 301 | ## + implements "ADDBLOCK \%blk" | ||
| 302 | sub handle_addblock { | ||
| 303 | my ($qs,$cli,$creq) = @_; | ||
| 304 | my $blk = $cli->get(); | ||
| 305 | $qs->addblock($blk); | ||
| 306 | return $qs; | ||
| 307 | } | ||
| 308 | |||
| 309 | ##============================================================================== | ||
| 310 | ## Client Connections | ||
| 311 | package DTA::CAB::Queue::ClientConn; | ||
| 312 | use strict; | ||
| 313 | our @ISA = qw(DTA::CAB::Socket::UNIX); | ||
| 314 | |||
| 315 | |||
| 316 | 1; | ||
| 317 | |||
| 318 | __END__ |