1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
17 import os, time, threading
18 from pyxmpp.jabber.client import JabberClient
19 from pyxmpp.message import Message
20 from pyxmpp.jid import JID
21 from socket import gethostname
27 # - log jabber activity (for future reference)
29 #logger=logging.getLogger()
30 #logger.addHandler(logging.StreamHandler())
31 #logger.addHandler(logging.FileHandler('j.log'))
32 #logger.setLevel(logging.DEBUG)
34 def set_network_handle(handle):
35 """ Sets the thread-specific network handle"""
36 THREAD_SESSIONS[threading.currentThread().getName()] = handle
38 def get_network_handle():
39 """ Returns the thread-specific network connection handle."""
40 return THREAD_SESSIONS.get(threading.currentThread().getName())
42 def clear_network_handle():
43 ''' Disconnects the thread-specific handle and discards it '''
44 handle = THREAD_SESSIONS.get(threading.currentThread().getName())
47 del THREAD_SESSIONS[threading.currentThread().getName()]
49 class NetworkMessage(object):
54 sender - message sender
55 recipient - message recipient
56 body - the body of the message
57 thread - the message thread
58 locale - locale of the message
61 def __init__(self, message=None, **args):
63 self.body = message.get_body()
64 self.thread = message.get_thread()
65 self.recipient = message.get_to()
66 self.router_command = None
67 self.router_class = None
68 if message.xmlnode.hasProp('router_from') and \
69 message.xmlnode.prop('router_from') != '':
70 self.sender = message.xmlnode.prop('router_from')
72 self.sender = message.get_from().as_utf8()
74 self.sender = args.get('sender')
75 self.recipient = args.get('recipient')
76 self.body = args.get('body')
77 self.thread = args.get('thread')
78 self.router_command = args.get('router_command')
79 self.router_class = args.get('router_class')
83 doc = libxml2.parseDoc(xml)
84 msg = Message(doc.getRootElement())
85 return NetworkMessage(msg)
88 def make_xmpp_msg(self):
89 ''' Creates a pyxmpp.message.Message and adds custom attributes '''
91 msg = Message(None, self.sender, self.recipient, None, None, None, \
92 self.body, self.thread)
93 if self.router_command:
94 msg.xmlnode.newProp('router_command', self.router_command)
96 msg.xmlnode.newProp('router_class', self.router_class)
100 ''' Turns this message into XML '''
101 return self.make_xmpp_msg().serialize()
104 class Network(JabberClient):
105 def __init__(self, **args):
106 self.isconnected = False
108 # Create a unique jabber resource
109 resource = args.get('resource') or 'python_client'
110 resource += '_' + gethostname() + ':' + str(os.getpid()) + '_' + \
111 threading.currentThread().getName().lower()
112 self.jid = JID(args['username'], args['host'], resource)
114 osrf.log.log_debug("initializing network with JID %s and host=%s, "
115 "port=%s, username=%s" % (self.jid.as_utf8(), args['host'], \
116 args['port'], args['username']))
118 #initialize the superclass
119 JabberClient.__init__(self, self.jid, args['password'], args['host'])
122 self.receive_callback = None
125 JabberClient.connect(self)
126 while not self.isconnected:
127 stream = self.get_stream()
128 act = stream.loop_iter(10)
132 def set_receive_callback(self, func):
133 """The callback provided is called when a message is received.
135 The only argument to the function is the received message. """
136 self.receive_callback = func
138 def session_started(self):
139 osrf.log.log_info("Successfully connected to the opensrf network")
141 self.stream.set_message_handler("normal", self.message_received)
142 self.isconnected = True
144 def send(self, message):
145 """Sends the provided network message."""
146 osrf.log.log_internal("jabber sending to %s: %s" % (message.recipient, message.body))
147 message.sender = self.jid.as_utf8()
148 msg = message.make_xmpp_msg()
149 self.stream.send(msg)
151 def message_received(self, stanza):
152 """Handler for received messages."""
153 if stanza.get_type()=="headline":
156 osrf.log.log_internal("jabber received message from %s : %s"
157 % (stanza.get_from().as_utf8(), stanza.get_body()))
158 self.queue.append(NetworkMessage(stanza))
161 def recv(self, timeout=120):
162 """Attempts to receive a message from the network.
164 timeout - max number of seconds to wait for a message.
165 If a message is received in 'timeout' seconds, the message is passed to
166 the receive_callback is called and True is returned. Otherwise, false is
175 if len(self.queue) == 0:
176 while (forever or timeout >= 0) and len(self.queue) == 0:
177 starttime = time.time()
178 act = self.get_stream().loop_iter(timeout)
179 endtime = time.time() - starttime
182 osrf.log.log_internal("exiting stream loop after %s seconds. "
183 "act=%s, queue size=%d" % (str(endtime), act, len(self.queue)))
187 # if we've acquired a message, handle it
189 if len(self.queue) > 0:
190 msg = self.queue.pop(0)
191 if self.receive_callback:
192 self.receive_callback(msg)