]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Transport/SlimJabber/PeerConnection.pm
LP1999823: Bump libtool library version
[OpenSRF.git] / src / perl / lib / OpenSRF / Transport / SlimJabber / PeerConnection.pm
1 package OpenSRF::Transport::SlimJabber::PeerConnection;
2 use strict;
3 use base qw/OpenSRF::Transport::SlimJabber::Client/;
4 use OpenSRF::Utils::Config;
5 use OpenSRF::Utils::Logger qw(:level);
6 use OpenSRF::EX qw/:try/;
7
8 =head1 Description
9
10 Represents a single connection to a remote peer.  The 
11 Jabber values are loaded from the config file.  
12
13 Subclasses OpenSRF::Transport::SlimJabber::Client.
14
15 =cut
16
17 =head2 new()
18
19         new( $appname );
20
21         The $appname parameter tells this class how to find the correct
22         Jabber username, password, etc to connect to the server.
23
24 =cut
25
26 our %apps_hash;
27 our $_singleton_connection;
28
29 sub retrieve { 
30         my( $class, $app ) = @_;
31         return $_singleton_connection;
32 }
33
34 sub reset {
35     return unless $_singleton_connection;
36     $_singleton_connection->disconnect;
37     $_singleton_connection = undef;
38 }
39
40
41 sub new {
42         my( $class, $app ) = @_;
43
44         my $peer_con = $class->retrieve;
45         return $peer_con if ($peer_con and $peer_con->tcp_connected);
46
47         my $config = OpenSRF::Utils::Config->current;
48
49         if( ! $config ) {
50                 throw OpenSRF::EX::Config( "No suitable config found for PeerConnection" );
51         }
52
53         my $conf                        = OpenSRF::Utils::Config->current;
54         my $domain = $conf->bootstrap->domain;
55         my $h = $conf->env->hostname;
56         OpenSRF::Utils::Logger->error("use of <domains/> is deprecated") if $conf->bootstrap->domains;
57
58         my $username    = $conf->bootstrap->username;
59         my $password    = $conf->bootstrap->passwd;
60         my $port        = $conf->bootstrap->port;
61         my $resource    = "${app}_drone_at_$h";
62         my $host        = $domain; # XXX for now...
63
64         if( $app eq "client" ) { $resource = "client_at_$h"; }
65
66         OpenSRF::EX::Config->throw( "JPeer could not load all necessary values from config" )
67                 unless ( $username and $password and $resource and $host and $port );
68
69         my $self = __PACKAGE__->SUPER::new( 
70                 username                => $username,
71                 resource                => $resource,
72                 password                => $password,
73                 host                    => $host,
74                 port                    => $port,
75                 );      
76                                         
77         bless( $self, $class );
78
79         $self->app($app);
80
81         $_singleton_connection = $self;
82         $apps_hash{$app} = $self;
83
84         return $_singleton_connection;
85         return $apps_hash{$app};
86 }
87
88 sub process {
89         my $self = shift;
90         my $val = $self->SUPER::process(@_);
91         return 0 unless $val;
92         return OpenSRF::Transport->handler($self->app, $val);
93 }
94
95 sub app {
96         my $self = shift;
97         my $app = shift;
98         $self->{app} = $app if $app;
99         return $self->{app};
100 }
101
102 1;
103