e6f67056875e831e2a535bdd3a602ee85636094e
[OpenSRF.git] / src / perl / lib / OpenSRF / Transport / SlimJabber / Client.pm
1 package OpenSRF::Transport::SlimJabber::Client;
2
3 use strict;
4 use warnings;
5
6 use OpenSRF::EX;
7 use OpenSRF::Utils::Config;
8 use OpenSRF::Utils::Logger qw/$logger/;
9 use OpenSRF::Transport::SlimJabber::XMPPReader;
10 use OpenSRF::Transport::SlimJabber::XMPPMessage;
11 use IO::Socket::UNIX;
12 use FreezeThaw qw/freeze/;
13
14 sub DESTROY{
15     shift()->disconnect;
16 }
17
18 =head1 NAME
19
20 OpenSRF::Transport::SlimJabber::Client
21
22 =head1 SYNOPSIS
23
24
25
26 =head1 DESCRIPTION
27
28
29
30 =cut
31
32 =head1 METHODS
33
34 =head2 new
35
36 =cut
37
38 sub new {
39         my( $class, %params ) = @_;
40     my $self = bless({}, ref($class) || $class);
41     $self->params(\%params);
42         return $self;
43 }
44
45 =head2 reader
46
47 =cut
48
49 sub reader {
50     my($self, $reader) = @_;
51     $self->{reader} = $reader if $reader;
52     return $self->{reader};
53 }
54
55 =head2 params
56
57 =cut
58
59 sub params {
60     my($self, $params) = @_;
61     $self->{params} = $params if $params;
62     return $self->{params};
63 }
64
65 =head2 socket
66
67 =cut
68
69 sub socket {
70     my($self, $socket) = @_;
71     $self->{socket} = $socket if $socket;
72     return $self->{socket};
73 }
74
75 =head2 disconnect
76
77 =cut
78
79 sub disconnect {
80     my $self = shift;
81         $self->reader->disconnect if $self->reader;
82 }
83
84
85 =head2 gather
86
87 =cut
88
89 sub gather { 
90     my $self = shift; 
91     $self->process( 0 ); 
92 }
93
94 # -------------------------------------------------
95
96 =head2 tcp_connected
97
98 =cut
99
100 sub tcp_connected {
101         my $self = shift;
102     return $self->reader->tcp_connected if $self->reader;
103     return 0;
104 }
105
106
107
108 =head2 send
109
110 =cut
111
112 sub send {
113         my $self = shift;
114     my $msg = OpenSRF::Transport::SlimJabber::XMPPMessage->new(@_);
115     $msg->osrf_xid($logger->get_osrf_xid);
116     $self->reader->send($msg->to_xml);
117 }
118
119 =head2 initialize
120
121 =cut
122
123 sub initialize {
124
125         my $self = shift;
126
127         my $host        = $self->params->{host}; 
128         my $port        = $self->params->{port}; 
129         my $username    = $self->params->{username};
130         my $resource    = $self->params->{resource};
131         my $password    = $self->params->{password};
132
133     my $jid = "$username\@$host/$resource";
134
135         my $conf = OpenSRF::Utils::Config->current;
136
137         my $tail = "_$$";
138         $tail = "" if !$conf->bootstrap->router_name and $username eq "router";
139     $resource = "$resource$tail";
140
141     my $socket = IO::Socket::INET->new(
142         PeerHost => $host,
143         PeerPort => int($port),
144         Proto  => 'tcp' );
145
146     throw OpenSRF::EX::Jabber("Could not open TCP socket to Jabber server: $@")
147             unless ( $socket and $socket->connected );
148
149     $self->socket($socket);
150     $self->reader(OpenSRF::Transport::SlimJabber::XMPPReader->new($socket));
151     $self->reader->connect($host, $username, $password, $resource);
152
153     throw OpenSRF::EX::Jabber("Could not authenticate with Jabber server: $@")
154             unless ( $self->reader->connected );
155
156         return $self;
157 }
158
159
160 =head2 construct
161
162 =cut
163
164 sub construct {
165         my( $class, $app ) = @_;
166         $class->peer_handle($class->new( $app )->initialize());
167 }
168
169
170 =head2 process
171
172 =cut
173
174 sub process {
175         my($self, $timeout) = @_;
176
177         $timeout ||= 0;
178     $timeout = int($timeout);
179
180         unless( $self->reader and $self->reader->connected ) {
181         throw OpenSRF::EX::JabberDisconnected 
182             ("This JabberClient instance is no longer connected to the server ");
183         }
184
185     return $self->reader->wait_msg($timeout);
186 }
187
188
189 =head2 flush_socket
190
191 Sets the socket to O_NONBLOCK, reads all of the data off of the
192 socket, the restores the sockets flags.  Returns 1 on success, 0 if
193 the socket isn't connected.
194
195 =cut
196
197 sub flush_socket {
198         my $self = shift;
199     return $self->reader->flush_socket;
200 }
201
202 1;
203
204