| 1 | # -*- mode: Perl; -*- | ||
| 2 | package NewsClipper::Server; | ||
| 3 | |||
| 4 | # This package contains the Server class. This class will automatically | ||
| 5 | # determine which of its implementations to load and then load it. | ||
| 6 | # Implementations are contained, not inherited. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | use NewsClipper::Globals; | ||
| 12 | |||
| 13 | use vars qw( $VERSION ); | ||
| 14 | |||
| 15 | my $HANDLER_SERVER = 'handlers.newsclipper.com'; | ||
| 16 | #my $HANDLER_SERVER = '192.168.0.1'; | ||
| 17 | |||
| 18 | $VERSION = 0.10; | ||
| 19 | |||
| 20 | # ------------------------------------------------------------------------------ | ||
| 21 | |||
| 22 | sub new | ||
| 23 | { | ||
| 24 | my $implementation = _Load_Implementation(); | ||
| 25 | |||
| 26 | $implementation->{handler_server} = $HANDLER_SERVER; | ||
| 27 | |||
| 28 | return $implementation; | ||
| 29 | } | ||
| 30 | |||
| 31 | # ------------------------------------------------------------------------------ | ||
| 32 | |||
| 33 | sub _Load_Implementation() | ||
| 34 | { | ||
| 35 | my $implementation; | ||
| 36 | |||
| 37 | # Try to load the MySQL implementation, which is a lot faster. | ||
| 38 | if (eval "require NewsClipper::Server::MySQL") | ||
| 39 | { | ||
| 40 | $implementation = NewsClipper::Server::MySQL->new(); | ||
| 41 | |||
| 42 | if (defined $implementation) | ||
| 43 | { | ||
| 44 | dprint "Using MySQL implementation of server class."; | ||
| 45 | } | ||
| 46 | else | ||
| 47 | { | ||
| 48 | dprint "Tried to create MySQL implementation, but failed"; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | # Otherwise fall back on the CGI implementation | ||
| 52 | else | ||
| 53 | { | ||
| 54 | dprint "Tried to load MySQL implementation, but failed: $@"; | ||
| 55 | require NewsClipper::Server::CGI; | ||
| 56 | |||
| 57 | $implementation = new NewsClipper::Server::CGI; | ||
| 58 | dprint "Using CGI implementation of server class." | ||
| 59 | } | ||
| 60 | |||
| 61 | return $implementation; | ||
| 62 | } | ||
| 63 | |||
| 64 | # ------------------------------------------------------------------------------ | ||
| 65 | |||
| 66 | # These should be overridden by the implementation class | ||
| 67 | sub Get_Handler_Type | ||
| 68 | { | ||
| 69 | die "Get_Handler_Type not overridden by implementation!"; | ||
| 70 | } | ||
| 71 | |||
| 72 | # ------------------------------------------------------------------------------ | ||
| 73 | |||
| 74 | sub Get_Handler | ||
| 75 | { | ||
| 76 | die "Get_Handler not overridden by implementation!"; | ||
| 77 | } | ||
| 78 | |||
| 79 | # ------------------------------------------------------------------------------ | ||
| 80 | |||
| 81 | sub _Get_Compatible_Working_Handler_Version | ||
| 82 | { | ||
| 83 | die "_Get_Compatible_Working_Handler_Version not overridden by implementation!"; | ||
| 84 | } | ||
| 85 | |||
| 86 | # ------------------------------------------------------------------------------ | ||
| 87 | |||
| 88 | sub _Get_Latest_Working_Handler_Version | ||
| 89 | { | ||
| 90 | die "_Get_Latest_Working_Handler_Version not overridden by implementation!"; | ||
| 91 | } | ||
| 92 | |||
| 93 | # ------------------------------------------------------------------------------ | ||
| 94 | |||
| 95 | # Checks if a new version of the handler is available, taking consideration of | ||
| 96 | # -n flag into account. | ||
| 97 | # Params: | ||
| 98 | # 1) the handler name | ||
| 99 | # 2) whether you want only a bugfix update, not a functional update too | ||
| 100 | # ('bugfix','functional') | ||
| 101 | # Returns: | ||
| 102 | # 1) status: okay, failed, not found, no update | ||
| 103 | # 2) the version | ||
| 104 | # 3) type of update it is ("bugfix" or "functional") | ||
| 105 | # if $needBugfix == 0, type can be either bugfix or functional. | ||
| 106 | # if $needBugfix == 1, type can be only bugfix. | ||
| 107 | |||
| 108 | sub Get_New_Handler_Version($$$$$) | ||
| 109 | { | ||
| 110 | my $self = shift; | ||
| 111 | my $handler_name = shift; | ||
| 112 | my $needBugfix = shift; | ||
| 113 | my $localVersion = shift; | ||
| 114 | my $ncversion = shift; | ||
| 115 | |||
| 116 | dprint "Checking for a new version for handler \"$handler_name\""; | ||
| 117 | |||
| 118 | my ($status,$newVersion); | ||
| 119 | |||
| 120 | if ($needBugfix eq 'bugfix' && defined $localVersion) | ||
| 121 | { | ||
| 122 | ($status,$newVersion) = | ||
| 123 | $self->_Get_Compatible_Working_Handler_Version($handler_name, | ||
| 124 | $ncversion, $localVersion); | ||
| 125 | } | ||
| 126 | else | ||
| 127 | { | ||
| 128 | ($status,$newVersion) = | ||
| 129 | $self->_Get_Latest_Working_Handler_Version($handler_name,$ncversion); | ||
| 130 | } | ||
| 131 | |||
| 132 | if ($status eq 'no update') | ||
| 133 | { | ||
| 134 | dprint "Server reports that handler \"$handler_name\" doesn't have an update.\n"; | ||
| 135 | return 'no update'; | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($status eq 'not found') | ||
| 139 | { | ||
| 140 | dprint "Server reports that handler \"$handler_name\" doesn't exist.\n"; | ||
| 141 | return 'not found'; | ||
| 142 | } | ||
| 143 | |||
| 144 | return 'failed' if $status eq 'failed'; | ||
| 145 | |||
| 146 | if (defined $localVersion && $newVersion <= $localVersion) | ||
| 147 | { | ||
| 148 | dprint "No new version is available"; | ||
| 149 | return 'no update'; | ||
| 150 | } | ||
| 151 | |||
| 152 | # We actually got a version | ||
| 153 | my $updateType; | ||
| 154 | |||
| 155 | if (defined $localVersion) | ||
| 156 | { | ||
| 157 | if (int($newVersion * 100) == int($localVersion * 100)) | ||
| 158 | { | ||
| 159 | $updateType = 'bugfix'; | ||
| 160 | } | ||
| 161 | else | ||
| 162 | { | ||
| 163 | $updateType = 'functional'; | ||
| 164 | } | ||
| 165 | |||
| 166 | dprint "A new version is available.\n New version:$newVersion " . | ||
| 167 | "Old version: $localVersion Update type: $updateType\n"; | ||
| 168 | } | ||
| 169 | else | ||
| 170 | { | ||
| 171 | $updateType = 'functional'; | ||
| 172 | |||
| 173 | dprint "A new version is available.\n New version:$newVersion " . | ||
| 174 | "Old version: <NONE FOUND> Update type: $updateType\n"; | ||
| 175 | } | ||
| 176 | |||
| 177 | return ('okay',$newVersion,$updateType); | ||
| 178 | } | ||
| 179 | |||
| 180 | # ------------------------------------------------------------------------------ | ||
| 181 | |||
| 182 | 1; |