| 1 | package IPC::Manager::Base::DBI; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000035'; | ||
| 6 | |||
| 7 | use Carp qw/croak/; | ||
| 8 | use Scalar::Util qw/blessed/; | ||
| 9 | use Time::HiRes qw/time/; | ||
| 10 | use File::Temp qw/tempfile/; | ||
| 11 | |||
| 12 | use DBI 1.644; | ||
| 13 | |||
| 14 | use parent 'IPC::Manager::Client'; | ||
| 15 | use Object::HashBase qw{ | ||
| 16 | +dbh | ||
| 17 | <user | ||
| 18 | <pass | ||
| 19 | <attrs | ||
| 20 | }; | ||
| 21 | |||
| 22 | sub dsn { croak "Not Implemented" } | ||
| 23 | sub table_sql { croak "Not Implemented" } | ||
| 24 | |||
| 25 | sub escape { '' } | ||
| 26 | sub blob_type { DBI::SQL_BLOB } | ||
| 27 | |||
| 28 | sub pending_messages { 0 } | ||
| 29 | sub ready_messages { $_[0]->_get_message_ids ? 1 : 0 } | ||
| 30 | |||
| 31 | sub dbh { | ||
| 32 | my $this = shift; | ||
| 33 | my (%params) = @_; | ||
| 34 | |||
| 35 | if (blessed($this) && $this->{+DBH}) { | ||
| 36 | $this->pid_check; | ||
| 37 | |||
| 38 | # During global destruction DBI's tied inner handle may already | ||
| 39 | # be torn down; probing ->{Active} then emits | ||
| 40 | # "Can't call method 'FETCH' on an undefined value". Guard the | ||
| 41 | # probe so we transparently fall through to reconnect instead. | ||
| 42 | my $active; | ||
| 43 | { | ||
| 44 | local $SIG{__WARN__} = sub { }; | ||
| 45 | local $@; | ||
| 46 | $active = eval { $this->{+DBH}->{Active} }; | ||
| 47 | } | ||
| 48 | |||
| 49 | return $this->{+DBH} if $active; | ||
| 50 | delete $this->{+DBH}; | ||
| 51 | } | ||
| 52 | |||
| 53 | my $dsn = $params{dsn}; | ||
| 54 | my $user = $params{user}; | ||
| 55 | my $pass = $params{pass}; | ||
| 56 | my $attrs = $params{attrs}; | ||
| 57 | |||
| 58 | if (blessed($this)) { | ||
| 59 | $this->pid_check; | ||
| 60 | $dsn //= $this->dsn; | ||
| 61 | $user //= $this->user; | ||
| 62 | $pass //= $this->pass; | ||
| 63 | $attrs //= $this->attrs; | ||
| 64 | } | ||
| 65 | |||
| 66 | croak "No DSN" unless $dsn; | ||
| 67 | $attrs //= {}; | ||
| 68 | |||
| 69 | my $dbh = DBI->connect($dsn, $user, $pass, $attrs) or die "Could not connect"; | ||
| 70 | |||
| 71 | $this->{+DBH} = $dbh if blessed($this); | ||
| 72 | |||
| 73 | return $dbh; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub init_db { | ||
| 77 | my $this = shift; | ||
| 78 | |||
| 79 | my $dbh = $this->dbh(@_); | ||
| 80 | |||
| 81 | for my $sql ($this->table_sql) { | ||
| 82 | local $dbh->{PrintWarn} = 0; | ||
| 83 | $dbh->do($sql) or die $dbh->errstr; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub default_attrs {} | ||
| 88 | |||
| 89 | sub init { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | $self->SUPER::init(); | ||
| 93 | |||
| 94 | $self->{+USER} //= delete $self->{username} // ''; | ||
| 95 | $self->{+PASS} //= delete $self->{password} // ''; | ||
| 96 | |||
| 97 | $self->{+ATTRS} //= $self->default_attrs; | ||
| 98 | |||
| 99 | my $id = $self->{+ID}; | ||
| 100 | |||
| 101 | my $row = $self->_get_peer($self->{+ID}); | ||
| 102 | |||
| 103 | if ($self->{+RECONNECT} && !$row) { | ||
| 104 | $self->{disconnected} = 1; | ||
| 105 | croak "The '$id' peer does not exist"; | ||
| 106 | } | ||
| 107 | |||
| 108 | if ($row && $row->{pid} && $self->pid_is_running($row->{pid})) { | ||
| 109 | $self->{disconnected} = 1; | ||
| 110 | croak "Looks like the connection is already running in pid $row->{pid}"; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $dbh = $self->dbh; | ||
| 114 | my $e = $self->escape; | ||
| 115 | |||
| 116 | if ($row) { | ||
| 117 | my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}active${e} = ?, ${e}pid${e} = ? WHERE ${e}id${e} = ?"); | ||
| 118 | $sth->execute(time, $self->{+PID}, $self->{+ID}) or die $dbh->errstr; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | my $sth = $dbh->prepare("INSERT INTO ipcm_peers(${e}id${e}, ${e}pid${e}, ${e}active${e}) VALUES (?, ?, ?)") or die $dbh->errstr; | ||
| 122 | $sth->execute($id, $self->{+PID}, time) or die $dbh->errstr; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | sub all_stats { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | my $out = {}; | ||
| 130 | |||
| 131 | my $dbh = $self->dbh; | ||
| 132 | my $e = $self->escape; | ||
| 133 | my $sth = $dbh->prepare("SELECT ${e}id${e}, ${e}stats${e} FROM ipcm_peers") or die $dbh->errstr; | ||
| 134 | $sth->execute() or die $dbh->errstr; | ||
| 135 | |||
| 136 | while (my $row = $sth->fetchrow_arrayref) { | ||
| 137 | $out->{$row->[0]} = $self->{+SERIALIZER}->deserialize($row->[1]); | ||
| 138 | } | ||
| 139 | |||
| 140 | return $out; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub write_stats { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | my $dbh = $self->dbh; | ||
| 147 | my $e = $self->escape; | ||
| 148 | my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}stats${e} = ? WHERE ${e}id${e} = ?") or die $dbh->errstr; | ||
| 149 | $sth->bind_param(1, $self->{+SERIALIZER}->serialize($self->{+STATS}), $self->blob_type); | ||
| 150 | $sth->bind_param(2, $self->{+ID}); | ||
| 151 | $sth->execute or die $dbh->errstr; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub read_stats { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | my $dbh = $self->dbh; | ||
| 158 | my $e = $self->escape; | ||
| 159 | my $sth = $dbh->prepare("SELECT ${e}stats${e} FROM ipcm_peers WHERE ${e}id${e} = ?") or die $dbh->errstr; | ||
| 160 | $sth->execute($self->{+ID}) or die $dbh->errstr; | ||
| 161 | |||
| 162 | my $row = $sth->fetchrow_arrayref or return undef; | ||
| 163 | return $self->{+SERIALIZER}->deserialize($row->[0]); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub peers { | ||
| 167 | my $self = shift; | ||
| 168 | |||
| 169 | my $dbh = $self->dbh; | ||
| 170 | my $e = $self->escape; | ||
| 171 | my $sth = $dbh->prepare("SELECT ${e}id${e} FROM ipcm_peers WHERE ${e}id${e} != ? AND active IS NOT NULL ORDER BY ${e}id${e} ASC") or die $dbh->errstr; | ||
| 172 | $sth->execute($self->{+ID}); | ||
| 173 | |||
| 174 | return map { $_->[0] } @{$sth->fetchall_arrayref([0])}; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub peer_pid { | ||
| 178 | my $self = shift; | ||
| 179 | my ($id) = @_; | ||
| 180 | |||
| 181 | my $row = $self->_get_peer($id) or return undef; | ||
| 182 | return $row->{pid} // undef if $row->{active}; | ||
| 183 | return undef; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub peer_exists { | ||
| 187 | my $self = shift; | ||
| 188 | my ($id) = @_; | ||
| 189 | return $self->_get_peer($id) ? 1 : 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _get_peer { | ||
| 193 | my $self = shift; | ||
| 194 | my ($id) = @_; | ||
| 195 | |||
| 196 | my $dbh = $self->dbh; | ||
| 197 | my $e = $self->escape; | ||
| 198 | my $sth = $dbh->prepare("SELECT * FROM ipcm_peers WHERE ${e}id${e} = ?") or die $dbh->errstr; | ||
| 199 | $sth->execute($id); | ||
| 200 | return $sth->fetchrow_hashref; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub send_message { | ||
| 204 | my $self = shift; | ||
| 205 | my $msg = $self->build_message(@_); | ||
| 206 | |||
| 207 | my $peer_id = $msg->to or croak "Message has no peer"; | ||
| 208 | croak "Client '$peer_id' does not exist" unless $self->peer_exists($peer_id); | ||
| 209 | |||
| 210 | my $dbh = $self->dbh; | ||
| 211 | my $e = $self->escape; | ||
| 212 | my $sth = $dbh->prepare("INSERT INTO ipcm_messages(${e}id${e}, ${e}from${e}, ${e}to${e}, ${e}stamp${e}, ${e}broadcast${e}, ${e}content${e}) VALUES (?, ?, ?, ?, ?, ?)") | ||
| 213 | or die $dbh->errstr; | ||
| 214 | |||
| 215 | my $content = $self->{+SERIALIZER}->serialize($msg->{content}); | ||
| 216 | $sth->bind_param(1, $msg->{id}); | ||
| 217 | $sth->bind_param(2, $msg->{from}); | ||
| 218 | $sth->bind_param(3, $msg->{to}); | ||
| 219 | $sth->bind_param(4, $msg->{stamp}); | ||
| 220 | $sth->bind_param(5, $msg->{broadcast} ? 1 : 0); | ||
| 221 | $sth->bind_param(6, $content, $self->blob_type); | ||
| 222 | $sth->execute or die $dbh->errstr; | ||
| 223 | |||
| 224 | $self->{+STATS}->{sent}->{$msg->{to}}++; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub _get_message_ids { | ||
| 228 | my $self = shift; | ||
| 229 | my $dbh = $self->dbh; | ||
| 230 | my $e = $self->escape; | ||
| 231 | my $sth = $dbh->prepare("SELECT ${e}id${e} FROM ipcm_messages WHERE ${e}to${e} = ? ORDER BY ${e}stamp${e} ASC") or die $dbh->errstr; | ||
| 232 | $sth->execute($self->{+ID}) or die $dbh->errstr; | ||
| 233 | my $ids = [map { $_->[0] } @{$sth->fetchall_arrayref([0])}]; | ||
| 234 | return $ids if $ids && @$ids; | ||
| 235 | return; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub get_messages { | ||
| 239 | my $self = shift; | ||
| 240 | my $dbh = $self->dbh; | ||
| 241 | |||
| 242 | my $ids = $self->_get_message_ids or return; | ||
| 243 | |||
| 244 | my $e = $self->escape; | ||
| 245 | my $where = "FROM ipcm_messages WHERE ${e}id${e} IN (" . join(', ' => map { '?' } 1 .. scalar(@$ids)) . ")"; | ||
| 246 | my $sth = $dbh->prepare("SELECT * $where ORDER BY ${e}stamp${e} ASC") or die $dbh->errstr; | ||
| 247 | $sth->execute(@$ids) or die $dbh->errstr; | ||
| 248 | my $rows = $sth->fetchall_arrayref({}); | ||
| 249 | |||
| 250 | my @out; | ||
| 251 | |||
| 252 | for my $row (@$rows) { | ||
| 253 | $row->{content} = $self->{+SERIALIZER}->deserialize($row->{content}); | ||
| 254 | $self->{+STATS}->{read}->{$row->{from}}++; | ||
| 255 | push @out => IPC::Manager::Message->new($row); | ||
| 256 | } | ||
| 257 | |||
| 258 | $sth = $dbh->prepare("DELETE $where") or die $dbh->errstr; | ||
| 259 | $sth->execute(@$ids) or die $dbh->errstr; | ||
| 260 | |||
| 261 | |||
| 262 | return @out; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub requeue_message { | ||
| 266 | my $self = shift; | ||
| 267 | $self->send_message($_) for @_; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub pre_disconnect_hook { | ||
| 271 | my $self = shift; | ||
| 272 | |||
| 273 | my $dbh = $self->dbh; | ||
| 274 | my $e = $self->escape; | ||
| 275 | my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}pid${e} = NULL, active = NULL WHERE ${e}id${e} = ?") or die $dbh->errstr; | ||
| 276 | $sth->execute($self->{+ID}) or die $dbh->errstr; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub pre_suspend_hook { | ||
| 280 | my $self = shift; | ||
| 281 | |||
| 282 | my $dbh = $self->dbh; | ||
| 283 | my $e = $self->escape; | ||
| 284 | my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}pid${e} = NULL WHERE ${e}id${e} = ?") or die $dbh->errstr; | ||
| 285 | $sth->execute($self->{+ID}) or die $dbh->errstr; | ||
| 286 | } | ||
| 287 | |||
| 288 | 1; | ||
| 289 | |||
| 290 | __END__ |