Framework/Client.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Framework.pm ↑ Summary
1 #!/usr/bin/perl -w -Ilib
2 #
3 # a General client library using the Net::Server::Framework framework
4 #
5  
6 package Net::Server::Framework::Client;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use Net::Server::Framework::DB;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Net::Server::Framework::Spooler;
14 use Time::HiRes;
15 use Data::Serializer;
16  
17 our ($VERSION) = '1.0';
18 our $DB = 'registry';
19  
20 sub c_connect {
21 my $service = shift;
22  
23 my @hosts = _find($service);
24 foreach my $host (@hosts) {
25 if ( $host->{host} eq 'unix' ) {
26 return IO::Socket::UNIX->new( Peer => $host->{port}, )
27 or next;
28 }
29 else {
30 return IO::Socket::INET->new(
31 Proto => "tcp",
32 PeerAddr => $host->{host},
33 PeerPort => $host->{port},
34 ) or next;
35 }
36 }
37 carp("Could not find a valid connection method!");
38 }
39  
40 sub encode {
41 my $data = shift;
42 my $s = Data::Serializer->new( compress => '1' );
43 return $s->serialize($data);
44 }
45  
46 sub decode {
47 my $data = shift;
48 my $s = Data::Serializer->new( compress => '1' );
49 return $s->deserialize($data);
50 }
51  
52 sub talk {
53 my ( $mech, $data ) = @_;
54  
55 my $start = time();
56 my $timeout = 15;
57 my $remote = c_connect($mech)
58 or carp( "cannot connect to $mech Daemon, check the config section in your program");
59  
60 # send the hash to the daemon
61 print $remote encode($data);
62 shutdown $remote, 1;
63 my $resp = <$remote>;
64 # we work in asyc mode and have to poll the queue
65 if ($resp eq 'accepted')
66 {
67 while (1) {
68 my $res = Net::Server::Framework::Spooler::virgin($data);
69 return $res if defined $res;
70 if ( time > $start + $timeout ) {
71 return 1001;
72 }
73 Time::HiRes::usleep 100_000;
74 }
75 } else {
76 return decode($resp);
77 }
78 }
79  
80 #TODO make logging in couchDB
81 sub logging {
82 my $h = shift;
83 return $h;
84 }
85  
86 sub log {
87 my $h = shift;
88 $h->{command} = 'put';
89 my $id = talk('logD', $h);
90 return $h;
91 }
92  
93 sub _find {
94 my $service = shift;
95 my $dbh = Net::Server::Framework::DB::dbconnect($DB);
96 my @ret;
97 my $res =
98 Net::Server::Framework::DB::get( { dbh => $dbh, key => 'host', term => $service } );
99 foreach my $l ( keys %{$res} ) {
100 my $ret;
101 if ( $l eq '*' ) {
102 if ( $res->{$l}->{port} =~ m{\d+} ) {
103 $ret->{host} = 'localhost';
104 $ret->{port} = $res->{$l}->{port};
105 }
106 else {
107 $ret->{host} = 'unix';
108 $ret->{port} = $res->{$l}->{port};
109 }
110 }
111 else {
112 $ret->{host} = $l;
113 $ret->{port} = $res->{$l}->{port};
114 }
115 push( @ret, $ret );
116 }
117 return @ret;
118 }
119  
120 1;
121  
122 =head1 NAME
123  
124 Net::Server::Framework::Client - a client library with auto discovery for
125 daemons
126  
127  
128 =head1 VERSION
129  
130 This documentation refers to Net::Server::Framework::Client version 1.0.
131  
132  
133 =head1 SYNOPSIS
134  
135 A typical invocation looks like this:
136  
137 my $data = Net::Server::Framework::Client::talk('DAEMON_TO_TALK_TO',$c);
138  
139 =head1 DESCRIPTION
140  
141 This is a lib that is used to interface with daemons. The interface uses
142 by default a C<Data::Serializer> compressed string to exchange information
143 and finds the appropriate daemon based on the name. The name is looked
144 up in the central registry configured with the $DB variable. The
145 database based registry holds connection data like UNIX sockets or TCP
146 sockets. If there is more than one daemon with the same name the lib
147 does a basic round robin.
148  
149  
150 =head1 BASIC METHODS
151  
152 =head2 c_connect
153  
154 The connection logic.
155  
156 =head2 encode
157  
158 The analog to the C<Net::Server::Framework::encode> function only client
159 sides.
160  
161 =head2 decode
162  
163 The analog to the C<Net::Server::Framework::decode> function only client
164 sides.
165  
166 =head2 talk
167  
168 This method abstracts the connection logic and the syn/async connection
169 handling. Use this function to talk to a daemon within the
170 C<Net::Server::Framework>.
171  
172 =head2 log
173  
174 Deprecated. Has to be consolidated to one general function that is
175 backend neutral
176  
177 =head2 logging
178  
179 See log
180  
181 =head1 CONFIGURATION AND ENVIRONMENT
182  
183 The library needs a working etc/db.conf file and a configured $DB
184 variable. If asynchronous connections are used then a spooler process is
185 needed.
186  
187 =head1 BUGS AND LIMITATIONS
188  
189 There are no known bugs in this module.
190 Please report problems to
191 Lenz Gschwendtner ( <lenz@springtimesoft.com> )
192 Patches are welcome.
193  
194 =head1 AUTHOR
195  
196 Lenz Gschwendtner ( <lenz@springtimesoft.com> )
197  
198  
199  
200 =head1 LICENCE AND COPYRIGHT
201  
202 Copyright (c)
203 2007 Lenz Gschwerndtner ( <lenz@springtimesoft.comn> )
204 All rights reserved.
205  
206 This module is free software; you can redistribute it and/or
207 modify it under the same terms as Perl itself. See L<perlartistic>.
208  
209 This program is distributed in the hope that it will be useful,
210 but WITHOUT ANY WARRANTY; without even the implied warranty of
211 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
« Framework.pm ↑ Summary