File: | blib/lib/OpenSRF/Transport/SlimJabber/PeerConnection.pm |
Coverage: | 23.7% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package OpenSRF::Transport::SlimJabber::PeerConnection; | ||||||
2 | 9 9 9 | 50 30 70 | use strict; | ||||
3 | 9 9 9 | 60 37 61 | use base qw/OpenSRF::Transport::SlimJabber::Client/; | ||||
4 | 9 9 9 | 76 36 77 | use OpenSRF::Utils::Config; | ||||
5 | 9 9 9 | 63 31 77 | use OpenSRF::Utils::Logger qw(:level); | ||||
6 | 9 9 9 | 64 34 76 | use OpenSRF::EX qw/:try/; | ||||
7 | |||||||
8 - 15 | =head1 Description Represents a single connection to a remote peer. The Jabber values are loaded from the config file. Subclasses OpenSRF::Transport::SlimJabber::Client. =cut | ||||||
16 | |||||||
17 - 24 | =head2 new() new( $appname ); The $appname parameter tells this class how to find the correct Jabber username, password, etc to connect to the server. =cut | ||||||
25 | |||||||
26 | our %apps_hash; | ||||||
27 | our $_singleton_connection; | ||||||
28 | |||||||
29 | sub retrieve { | ||||||
30 | 0 | 0 | my( $class, $app ) = @_; | ||||
31 | 0 | return $_singleton_connection; | |||||
32 | } | ||||||
33 | |||||||
34 | |||||||
35 | sub new { | ||||||
36 | 0 | 1 | my( $class, $app ) = @_; | ||||
37 | |||||||
38 | 0 | my $peer_con = $class->retrieve; | |||||
39 | 0 | return $peer_con if ($peer_con and $peer_con->tcp_connected); | |||||
40 | |||||||
41 | 0 | my $config = OpenSRF::Utils::Config->current; | |||||
42 | |||||||
43 | 0 | if( ! $config ) { | |||||
44 | 0 | throw OpenSRF::EX::Config( "No suitable config found for PeerConnection" ); | |||||
45 | } | ||||||
46 | |||||||
47 | 0 | my $conf = OpenSRF::Utils::Config->current; | |||||
48 | 0 | my $domain = $conf->bootstrap->domain; | |||||
49 | 0 | my $h = $conf->env->hostname; | |||||
50 | 0 | OpenSRF::Utils::Logger->error("use of <domains/> is deprecated") if $conf->bootstrap->domains; | |||||
51 | |||||||
52 | 0 | my $username = $conf->bootstrap->username; | |||||
53 | 0 | my $password = $conf->bootstrap->passwd; | |||||
54 | 0 | my $port = $conf->bootstrap->port; | |||||
55 | 0 | my $resource = "${app}_drone_at_$h"; | |||||
56 | 0 | my $host = $domain; # XXX for now... | |||||
57 | |||||||
58 | 0 0 | if( $app eq "client" ) { $resource = "client_at_$h"; } | |||||
59 | |||||||
60 | 0 | 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 | 0 | OpenSRF::Utils::Logger->transport( "Built Peer with", INTERNAL ); | |||||
64 | |||||||
65 | 0 | my $self = __PACKAGE__->SUPER::new( | |||||
66 | username => $username, | ||||||
67 | resource => $resource, | ||||||
68 | password => $password, | ||||||
69 | host => $host, | ||||||
70 | port => $port, | ||||||
71 | ); | ||||||
72 | |||||||
73 | 0 | bless( $self, $class ); | |||||
74 | |||||||
75 | 0 | $self->app($app); | |||||
76 | |||||||
77 | 0 | $_singleton_connection = $self; | |||||
78 | 0 | $apps_hash{$app} = $self; | |||||
79 | |||||||
80 | 0 | return $_singleton_connection; | |||||
81 | 0 | return $apps_hash{$app}; | |||||
82 | } | ||||||
83 | |||||||
84 | sub process { | ||||||
85 | 0 | 1 | my $self = shift; | ||||
86 | 0 | my $val = $self->SUPER::process(@_); | |||||
87 | 0 | return 0 unless $val; | |||||
88 | 0 | return OpenSRF::Transport->handler($self->app, $val); | |||||
89 | } | ||||||
90 | |||||||
91 | sub app { | ||||||
92 | 0 | 0 | my $self = shift; | ||||
93 | 0 | my $app = shift; | |||||
94 | 0 | $self->{app} = $app if $app; | |||||
95 | 0 | return $self->{app}; | |||||
96 | } | ||||||
97 | |||||||
98 | 1; | ||||||
99 |