| 1 | #!/usr/bin/perl -w -Ilib | ||
| 2 | |||
| 3 | package Net::Server::Framework::Spooler; | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Net::Server::Framework::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | our ($VERSION) = '1.0'; | ||
| 11 | |||
| 12 | sub put { | ||
| 13 | my $data = shift; | ||
| 14 | my $hash = { | ||
| 15 | command => 'put', | ||
| 16 | user => $data->{user}, | ||
| 17 | body => $data->{body}, | ||
| 18 | }; | ||
| 19 | return(_chat($hash)); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub get { | ||
| 23 | my $data = shift; | ||
| 24 | my $hash = { | ||
| 25 | command => 'get', | ||
| 26 | user => $data->{user}, | ||
| 27 | ID => $data->{ID}, | ||
| 28 | }; | ||
| 29 | my $res = _chat($hash); | ||
| 30 | return(Net::Server::Framework::Client::decode($res->{hash})); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub virgin { | ||
| 34 | my $data = shift; | ||
| 35 | my $hash = { | ||
| 36 | command => 'virgin', | ||
| 37 | user => $data->{user}, | ||
| 38 | ID => $data->{ID}, | ||
| 39 | }; | ||
| 40 | my $res = _chat($hash); | ||
| 41 | my $r = Net::Server::Framework::Client::decode($res->{$data->{ID}}->{hash}); | ||
| 42 | return $r->{body}; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub mod { | ||
| 46 | my $data = shift; | ||
| 47 | my $hash = { | ||
| 48 | command => 'mod', | ||
| 49 | user => $data->{user}, | ||
| 50 | ID => $data->{ID}, | ||
| 51 | body => $data, | ||
| 52 | status => "modified", | ||
| 53 | }; | ||
| 54 | return(_chat($hash)); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub del { | ||
| 58 | my $data = shift; | ||
| 59 | my $hash = { | ||
| 60 | command => 'del', | ||
| 61 | user => $data->{user}, | ||
| 62 | ID => $data->{ID}, | ||
| 63 | }; | ||
| 64 | return(_chat($hash)); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub archive { | ||
| 68 | my $data = shift; | ||
| 69 | my $hash = { | ||
| 70 | command => 'archive', | ||
| 71 | user => $data->{user}, | ||
| 72 | ID => $data->{ID}, | ||
| 73 | }; | ||
| 74 | return(_chat($hash)); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _chat { | ||
| 78 | my $data = shift; | ||
| 79 | # TODO: can we use UDP datagrams to speed things up here? | ||
| 80 | my $remote = Net::Server::Framework::Client::c_connect('queue') | ||
| 81 | or carp( "cannot connect to spooler, check the config section in your program"); | ||
| 82 | |||
| 83 | # send the hash to the daemon | ||
| 84 | print $remote Net::Server::Framework::Client::encode($data); | ||
| 85 | shutdown $remote, 1; | ||
| 86 | my $resp = <$remote>; | ||
| 87 | return Net::Server::Framework::Client::decode($resp); | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | Net::Server::Framework::Spooler - asynchronous interface for Net::Server::Framework | ||
| 95 | based daemons | ||
| 96 | |||
| 97 | |||
| 98 | =head1 VERSION | ||
| 99 | |||
| 100 | This documentation refers to Net::Server::Framework::Spooler version 1.0. | ||
| 101 | |||
| 102 | |||
| 103 | =head1 SYNOPSIS | ||
| 104 | |||
| 105 | A typical invocation looks like this: | ||
| 106 | |||
| 107 | my $put = {body => $c, user => $c->{user}}; | ||
| 108 | $c->{ID} = Net::Server::Framework::Spooler::put($put); | ||
| 109 | |||
| 110 | =head1 DESCRIPTION | ||
| 111 | |||
| 112 | This interface is used to process asynchronous requests for daemons. It | ||
| 113 | relies on a spooler daemon and a spool database. If present this lib | ||
| 114 | handles the interaction of clients and server that need to process | ||
| 115 | things in a asynchronous way. | ||
| 116 | |||
| 117 | =head1 BASIC METHODS | ||
| 118 | |||
| 119 | The commands accepted by the lib are: | ||
| 120 | |||
| 121 | =head2 put | ||
| 122 | |||
| 123 | Insert a hash into the spooler | ||
| 124 | |||
| 125 | =head2 get | ||
| 126 | |||
| 127 | Retrieve a hash from the spooler | ||
| 128 | |||
| 129 | =head2 mod | ||
| 130 | |||
| 131 | Update a hash in the spooler (normally this is done when a response is | ||
| 132 | processed) | ||
| 133 | |||
| 134 | =head2 del | ||
| 135 | |||
| 136 | Remove a hash from the spooler | ||
| 137 | |||
| 138 | =head2 virgin | ||
| 139 | |||
| 140 | Test if a hash is updated in the spooler. This function is called in | ||
| 141 | async mode to test if we have a response or not (basic polling) | ||
| 142 | |||
| 143 | =head2 archive | ||
| 144 | |||
| 145 | This archives a hash into a archive location. | ||
| 146 | |||
| 147 | =head1 BUGS AND LIMITATIONS | ||
| 148 | |||
| 149 | There are no known bugs in this module. | ||
| 150 | Please report problems to | ||
| 151 | Lenz Gschwendtner ( <lenz@springtimesoft.com> ) | ||
| 152 | Patches are welcome. | ||
| 153 | |||
| 154 | =head1 AUTHOR | ||
| 155 | |||
| 156 | Lenz Gschwendtner ( <lenz@springtimesoft.com> ) | ||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | =head1 LICENCE AND COPYRIGHT | ||
| 161 | |||
| 162 | Copyright (c) | ||
| 163 | 2007 Lenz Gschwerndtner ( <lenz@springtimesoft.comn> ) | ||
| 164 | All rights reserved. | ||
| 165 | |||
| 166 | This module is free software; you can redistribute it and/or | ||
| 167 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 168 | |||
| 169 | This program is distributed in the hope that it will be useful, | ||
| 170 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 171 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||