]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Transport/SlimJabber/PeerConnection.pm
af93d3616aa1b67366b93503cbd948043121deef
[OpenSRF.git] / src / perlmods / 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 #       my @keys = keys %apps_hash;
33 #       OpenSRF::Utils::Logger->transport( 
34 #                       "Requesting peer for $app and we have @keys", INFO );
35 #       return $apps_hash{$app};
36 }
37
38
39
40 # !! In here we use the bootstrap config ....
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 $domains = $conf->bootstrap->domains;
55         my $h = $conf->env->hostname;
56
57         my $username    = $conf->bootstrap->username;
58         my $password    = $conf->bootstrap->passwd;
59         my $port                        = $conf->bootstrap->port;
60         my $resource    = "${app}_drone_at_$h";
61         my $host                        = $domains->[0]; # XXX for now...
62
63         if( $app eq "client" ) { $resource = "client_at_$h"; }
64
65
66
67         OpenSRF::EX::Config->throw( "JPeer could not load all necesarry values from config" )
68                 unless ( $username and $password and $resource and $host and $port );
69
70         OpenSRF::Utils::Logger->transport( "Built Peer with", INTERNAL );
71
72         my $self = __PACKAGE__->SUPER::new( 
73                 username                => $username,
74                 resource                => $resource,
75                 password                => $password,
76                 host                    => $host,
77                 port                    => $port,
78                 );      
79                                         
80         bless( $self, $class );
81
82         $self->app($app);
83
84         $_singleton_connection = $self;
85         $apps_hash{$app} = $self;
86
87         return $_singleton_connection;
88         return $apps_hash{$app};
89 }
90
91 sub process {
92
93         my $self = shift;
94         my $val = $self->SUPER::process(@_);
95         return 0 unless $val;
96
97         OpenSRF::Utils::Logger->transport( "Calling transport handler for ".$self->app." with: $val", INTERNAL );
98         my $t;
99         $t = OpenSRF::Transport->handler($self->app, $val);
100
101         return $t;
102 }
103
104 sub app {
105         my $self = shift;
106         my $app = shift;
107         if( $app ) {
108                 OpenSRF::Utils::Logger->transport( "PEER changing app to $app: ".$self->jid, INTERNAL );
109         }
110
111         $self->{app} = $app if ($app);
112         return $self->{app};
113 }
114
115 1;
116