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