| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | package Net::Server::Framework; | ||
| 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 | 1 | ▶ | use Data::Serializer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Net::Server::Framework::DB; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Net::Server::Framework::Crypt; | ||
| 11 | use Net::Server::Framework::Auth; | ||
| 12 | use Net::Server::Framework::Format; | ||
| 13 | use base qw/Exporter Net::Server::PreFork/; | ||
| 14 | use vars qw(@EXPORT $VERSION); | ||
| 15 | |||
| 16 | our ($VERSION) = '1.2'; | ||
| 17 | @EXPORT = qw/options encode decode register/; | ||
| 18 | |||
| 19 | sub options { | ||
| 20 | my $self = shift; | ||
| 21 | my $prop = $self->{server}; | ||
| 22 | my $template = shift; | ||
| 23 | |||
| 24 | $self->SUPER::options($template); | ||
| 25 | $prop->{daemon_name} ||= undef; | ||
| 26 | $template->{daemon_name} = \$prop->{daemon_name}; | ||
| 27 | $prop->{node_name} ||= undef; | ||
| 28 | $template->{node_name} = \$prop->{node_name}; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub encode { | ||
| 32 | my ( $self, $data ) = @_; | ||
| 33 | my $s = Data::Serializer->new( compress => '1' ); | ||
| 34 | return $s->serialize($data); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub decode { | ||
| 38 | my ( $self, $data ) = @_; | ||
| 39 | my $s = Data::Serializer->new( compress => '1' ); | ||
| 40 | return $s->deserialize($data); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub register { | ||
| 44 | my $self = shift; | ||
| 45 | my $dereg = shift; | ||
| 46 | my $status; | ||
| 47 | if (defined $dereg) { | ||
| 48 | $status = $dereg; | ||
| 49 | } else { | ||
| 50 | $status = 'running'; | ||
| 51 | } | ||
| 52 | my $dbh = Net::Server::Framework::DB::dbconnect('registry'); | ||
| 53 | my ( $host, $port ); | ||
| 54 | foreach my $p ( @{ $self->{server}->{port} } ) { | ||
| 55 | $port .= $p . ','; | ||
| 56 | } | ||
| 57 | $port =~ s/,$//; | ||
| 58 | foreach my $h ( @{ $self->{server}->{host} } ) { | ||
| 59 | $host .= $h . ','; | ||
| 60 | } | ||
| 61 | $host =~ s/,$//; | ||
| 62 | my $data = { | ||
| 63 | service => $self->{server}->{daemon_name}, | ||
| 64 | port => $port, | ||
| 65 | host => $host, | ||
| 66 | lastcheck => time(), | ||
| 67 | startup => time(), | ||
| 68 | status => $status, | ||
| 69 | }; | ||
| 70 | Net::Server::Framework::DB::put( { dbh => $dbh, data => $data, table => 'services' , replace_into => 'true'} ); | ||
| 71 | $self->log(2,"Registered successfuly\n"); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | =head1 NAME | ||
| 78 | |||
| 79 | Net::Server::Framework - an event driven infrastructure around Net::Server | ||
| 80 | |||
| 81 | |||
| 82 | =head1 VERSION | ||
| 83 | |||
| 84 | This documentation refers to C<Net::Server::Framework> version 1.0. | ||
| 85 | |||
| 86 | |||
| 87 | =head1 SYNOPSIS | ||
| 88 | |||
| 89 | In order to use this codebase you have to subclass this class. To get an | ||
| 90 | idea of how this looks like have a look at the C<Net::Server> | ||
| 91 | documentation. | ||
| 92 | |||
| 93 | A typical invocation looks like this: | ||
| 94 | |||
| 95 | use base qw/Net::Server::Framework/; | ||
| 96 | use strict; | ||
| 97 | use warnings; | ||
| 98 | |||
| 99 | __PACKAGE__->run; | ||
| 100 | exit; | ||
| 101 | |||
| 102 | =head1 DESCRIPTION | ||
| 103 | |||
| 104 | C<Net::Server::Framework> is the result of many iterations of backend | ||
| 105 | daemon programming. I use the C<Net::Server::PreFork> code for some years | ||
| 106 | now and wrote some libs around it. This is an attempt to take those libs | ||
| 107 | and release them. The challenge for me is to isolate all the additions, | ||
| 108 | clean them up and pack them into one framework that installs nicely. | ||
| 109 | |||
| 110 | The purpose of this framework is an easy to use event driven and | ||
| 111 | scalable infrastructure that you can use to run multiple daemons doing | ||
| 112 | specific things. There are some key parts in this setup. | ||
| 113 | |||
| 114 | The central registry is used to register each daemon with its connection | ||
| 115 | info (UNIX socket or IP/port). The client library resolves daemon names | ||
| 116 | with this registry to connection information. The client lib supports | ||
| 117 | both, synchronous and asynchronous connection handling via a cache | ||
| 118 | daemon. | ||
| 119 | |||
| 120 | This version uses SQLite as the standard DB. Most parameters in the | ||
| 121 | system (including the database type) are configurable via INI style | ||
| 122 | config files. | ||
| 123 | |||
| 124 | This framework is used in some quite busy environments and some things | ||
| 125 | might look strange but are the result of optimization or problems we ran | ||
| 126 | into when scaling. One such thing is the DB abstraction which is tuned | ||
| 127 | for the least possible overhead (memory and cpu wise). | ||
| 128 | |||
| 129 | The source code and some working examples can be found on github: | ||
| 130 | http://github.com/norbu09/net--server--framework/tree/master | ||
| 131 | |||
| 132 | =head1 BASIC METHODS | ||
| 133 | |||
| 134 | =head2 options | ||
| 135 | |||
| 136 | This function overrides the standard options function in C<Net::Server> | ||
| 137 | |||
| 138 | =head2 encode | ||
| 139 | |||
| 140 | This is a generic wrapper for transport encodings. This function can be | ||
| 141 | overridden to use transports like JSON or XML. The standard is a | ||
| 142 | compressed Data::Serialiser stream. | ||
| 143 | |||
| 144 | =head2 decode | ||
| 145 | |||
| 146 | The reverse of encode. | ||
| 147 | |||
| 148 | =head2 register | ||
| 149 | |||
| 150 | This registers the daemon in the registry and has to be called in the | ||
| 151 | startup phase. It is also used to unregister the daemon in the tear down | ||
| 152 | phase. | ||
| 153 | |||
| 154 | =head1 DIAGNOSTICS | ||
| 155 | |||
| 156 | The framework normally logs to a central logfile under /var/log but can | ||
| 157 | log directly to syslog as well. Have a look at the C<Net::Server> options. | ||
| 158 | |||
| 159 | =head1 CONFIGURATION AND ENVIRONMENT | ||
| 160 | |||
| 161 | The framework expects a etc/ directory with a config file containing a | ||
| 162 | C<Net::Server> conform structure with some extra fields. Have a look at the | ||
| 163 | github repository for more reference. | ||
| 164 | |||
| 165 | =head1 BUGS AND LIMITATIONS | ||
| 166 | |||
| 167 | There are no known bugs in this module. | ||
| 168 | Please report problems to | ||
| 169 | Lenz Gschwendtner ( <lenz@springtimesoft.com> ) | ||
| 170 | Patches are welcome. | ||
| 171 | |||
| 172 | =head1 AUTHOR | ||
| 173 | |||
| 174 | Lenz Gschwendtner ( <lenz@springtimesoft.com> ) | ||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | =head1 LICENCE AND COPYRIGHT | ||
| 179 | |||
| 180 | Copyright (c) | ||
| 181 | 2007 Lenz Gschwerndtner ( <lenz@springtimesoft.comn> ) | ||
| 182 | All rights reserved. | ||
| 183 | |||
| 184 | This module is free software; you can redistribute it and/or | ||
| 185 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 186 | |||
| 187 | This program is distributed in the hope that it will be useful, | ||
| 188 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 189 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 190 | |||
| 191 | |||