]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Transport/SlimJabber/PeerConnection.pm
using new config settings
[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
28 sub retrieve { 
29         my( $class, $app ) = @_;
30         my @keys = keys %apps_hash;
31         OpenSRF::Utils::Logger->transport( 
32                         "Requesting peer for $app and we have @keys", INFO );
33         return $apps_hash{$app};
34 }
35
36
37
38 # !! In here we use the bootstrap config ....
39 sub new {
40         my( $class, $app ) = @_;
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 $trans_list = $config->bootstrap->transport;
48         unless( $trans_list && $trans_list->[0] ) {
49                 throw OpenSRF::EX::Config ("Peer Connection needs transport info");
50         }
51
52         # For now we just use the first in the list...
53         my $trans               = $trans_list->[0];
54
55         my $username;
56         if( $app eq "system_client" ) {
57                 $username       = $config->$trans->username;
58         } else {
59                 $username = $app;
60         }
61
62
63
64         my $password    = $config->$trans->password;
65         OpenSRF::Utils::Logger->transport( "Building Peer with " .$config->$trans->password, INTERNAL );
66         my $h = $config->env->hostname;
67         my $resource    = "$h" . "_$$";
68         my $server              = $config->$trans->server;
69         OpenSRF::Utils::Logger->transport( "Building Peer with " .$config->$trans->server, INTERNAL );
70         my $port                        = $config->$trans->port;
71         OpenSRF::Utils::Logger->transport( "Building Peer with " .$config->$trans->port, INTERNAL );
72
73
74         OpenSRF::EX::Config->throw( "JPeer could not load all necesarry values from config" )
75                 unless ( $username and $password and $resource and $server and $port );
76
77         OpenSRF::Utils::Logger->transport( "Built Peer with", INTERNAL );
78
79         my $self = __PACKAGE__->SUPER::new( 
80                 username                => $username,
81                 resource                => $resource,
82                 password                => $password,
83                 host                    => $server,
84                 port                    => $port,
85                 );      
86                                         
87         bless( $self, $class );
88
89         $self->app($app);
90
91         $apps_hash{$app} = $self;
92         return $apps_hash{$app};
93 }
94
95 sub process {
96         my $self = shift;
97         my $val = $self->SUPER::process(@_);
98         return 0 unless $val;
99         OpenSRF::Utils::Logger->transport( "Calling transport handler for ".$self->app." with: $val", INTERNAL );
100         my $t;
101 #try {
102         $t = OpenSRF::Transport->handler($self->app, $val);
103
104 #       } catch OpenSRF::EX with {
105 #               my $e = shift;
106 #               $e->throw();
107
108 #       } catch Error with { return undef; }
109
110         return $t;
111 }
112
113 sub app {
114         my $self = shift;
115         my $app = shift;
116         if( $app ) {
117                 OpenSRF::Utils::Logger->transport( "PEER changing app to $app: ".$self->jid, INTERNAL );
118         }
119
120         $self->{app} = $app if ($app);
121         return $self->{app};
122 }
123
124 1;
125