1 # -----------------------------------------------------------------------
2 # Copyright (C) 2010 Equinox Software, Inc
3 # Bill Erickson <erickson@esilibrary.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 3
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 constrictor.task
18 import constrictor.script
19 import constrictor.log as log
21 line_terminator = '\r\n'
23 class SIP2Client(object):
25 def __init__(self, server, port):
30 def init_socket(self):
33 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
34 except socket.error, msg:
35 log.log_error("%s\n" % msg[1])
39 self.sock.connect((self.server, self.port))
40 except socket.error, msg:
41 log.log_error("%s\n" % msg[1])
46 def send_msg(self, msg):
47 log.log_debug('SIP2 socket sending: %s' % msg)
48 self.sock.send(msg + line_terminator)
55 buf = self.sock.recv(2048)
57 log.log_debug("SIP2 socket read: %s" % buf)
58 if data[-(len(line_terminator)):] == line_terminator:
63 def login(self, username, password, institution):
66 class LoginTask(constrictor.task.Task):
69 constrictor.task.Task.__init__(self, self.__class__.__name__)
73 log.log_info("LoginTask: %s %s %s" % (username, password, institution))
75 msg = '9300CN%s|CO%s|CP%s|' % (username, password, institution)
77 data = client.recv_msg()
80 log.log_info("SIP2 login OK")
83 log.log_error("SIP2 login failed: %s" % data)
86 return LoginTask().start()
88 def item_info_request(self, institution, copy_barcode):
92 class ItemInfoTask(constrictor.task.Task):
94 def __init__(self, name=None):
95 constrictor.task.Task.__init__(self, self.__class__.__name__)
98 # TODO: use a generated date
99 log.log_info("ItemInfoTask: %s" % copy_barcode)
100 msg = '1720060110 215612AO%s|AB%s|' % (institution, copy_barcode)
102 data = client.recv_msg()
103 # TODO: check for valid response
104 log.log_info('SIP2 item info response: %s' % data)
107 return ItemInfoTask().start()