]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Transport/SlimJabber/PeerConnection.pm
c41517c5370c34ffc56db3f0f23fc35ebf497ee0
[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
35 sub new {
36         my( $class, $app ) = @_;
37
38         my $peer_con = $class->retrieve;
39         return $peer_con if ($peer_con and $peer_con->tcp_connected);
40
41         my $config = OpenSRF::Utils::Config->current;
42
43         if( ! $config ) {
44                 throw OpenSRF::EX::Config( "No suitable config found for PeerConnection" );
45         }
46
47         my $conf                        = OpenSRF::Utils::Config->current;
48         my $domain = $conf->bootstrap->domain;
49         my $h = $conf->env->hostname;
50         OpenSRF::Utils::Logger->error("use of <domains/> is deprecated") if $conf->bootstrap->domains;
51
52         my $username    = $conf->bootstrap->username;
53         my $password    = $conf->bootstrap->passwd;
54         my $port        = $conf->bootstrap->port;
55         my $resource    = "${app}_drone_at_$h";
56         my $host        = $domain; # XXX for now...
57
58         if( $app eq "client" ) { $resource = "client_at_$h"; }
59
60         OpenSRF::EX::Config->throw( "JPeer could not load all necessary values from config" )
61                 unless ( $username and $password and $resource and $host and $port );
62
63         my $self = __PACKAGE__->SUPER::new( 
64                 username                => $username,
65                 resource                => $resource,
66                 password                => $password,
67                 host                    => $host,
68                 port                    => $port,
69                 );      
70                                         
71         bless( $self, $class );
72
73         $self->app($app);
74
75         $_singleton_connection = $self;
76         $apps_hash{$app} = $self;
77
78         return $_singleton_connection;
79         return $apps_hash{$app};
80 }
81
82 sub process {
83         my $self = shift;
84         my $val = $self->SUPER::process(@_);
85         return 0 unless $val;
86         return OpenSRF::Transport->handler($self->app, $val);
87 }
88
89 sub app {
90         my $self = shift;
91         my $app = shift;
92         $self->{app} = $app if $app;
93         return $self->{app};
94 }
95
96 1;
97