From 3ed0b7217e3728ffb4c8449f6e683366a235cedd Mon Sep 17 00:00:00 2001 From: erickson Date: Tue, 3 Aug 2010 15:50:06 +0000 Subject: [PATCH] new SIP2 client contrib module. currently supports login and item information requests git-svn-id: svn://svn.open-ils.org/ILS-Contrib/constrictor/trunk@945 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- constrictor.properties | 16 +++- contrib/sip2/sip2_client.py | 109 +++++++++++++++++++++++ contrib/sip2/sip2_item_info_endurance.py | 49 ++++++++++ 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 contrib/sip2/sip2_client.py create mode 100644 contrib/sip2/sip2_item_info_endurance.py diff --git a/constrictor.properties b/constrictor.properties index a93c497bd..33a75d860 100644 --- a/constrictor.properties +++ b/constrictor.properties @@ -7,7 +7,7 @@ constrictor.script=sleep.py #constrictor.script=eg_title_hold.py # comma separated list of directories where test scripts are found -constrictor.scriptDirs=samples,contrib/evergreen +constrictor.scriptDirs=samples,contrib/evergreen,contrib/sip2 # local directory to cache test-specific files constrictor.cacheDir=cache @@ -70,3 +70,17 @@ evergreen.batchAPIFile=contrib/evergreen/batch_api.txt #evergreen.patronBarcodes= #evergreen.osrfConfig= #evergreen.osrfConfigContext= + + + +# ---- Properties for the SIP2 contrib module -------------- + + +sip2.username=sip_username +sip2.password=sip_password +sip2.institution=example +sip2.server=sip.example.org +sip2.port=6001 +sip2.copyBarcodes= + + diff --git a/contrib/sip2/sip2_client.py b/contrib/sip2/sip2_client.py new file mode 100644 index 000000000..5d5dc8056 --- /dev/null +++ b/contrib/sip2/sip2_client.py @@ -0,0 +1,109 @@ +# ----------------------------------------------------------------------- +# Copyright (C) 2010 Equinox Software, Inc +# Bill Erickson +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# ----------------------------------------------------------------------- + +import sys, socket +import constrictor.task +import constrictor.script +import constrictor.log as log + +line_terminator = '\r\n' + +class SIP2Client(object): + + def __init__(self, server, port): + self.server = server + self.port = port + self.sock = None + + def init_socket(self): + + try: + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + except socket.error, msg: + log.log_error("%s\n" % msg[1]) + return False + + try: + self.sock.connect((self.server, self.port)) + except socket.error, msg: + log.log_error("%s\n" % msg[1]) + return False + + return True + + def send_msg(self, msg): + log.log_debug('SIP2 socket sending: %s' % msg) + self.sock.send(msg + line_terminator) + + def recv_msg(self): + data = '' + buf = '' + + while True: + buf = self.sock.recv(2048) + data = data + buf + log.log_debug("SIP2 socket read: %s" % buf) + if data[-(len(line_terminator)):] == line_terminator: + break + + return data + + def login(self, username, password, institution): + client = self + + class LoginTask(constrictor.task.Task): + + def __init__(self): + constrictor.task.Task.__init__(self, self.__class__.__name__) + + def run(self, **kw): + + log.log_info("LoginTask: %s %s %s" % (username, password, institution)) + + msg = '9300CN%s|CO%s|CP%s|' % (username, password, institution) + client.send_msg(msg) + data = client.recv_msg() + + if data[:3] == '941': + log.log_info("SIP2 login OK") + return True + else: + log.log_error("SIP2 login failed: %s" % data) + return False + + return LoginTask().start() + + def item_info_request(self, institution, copy_barcode): + + client = self + + class ItemInfoTask(constrictor.task.Task): + + def __init__(self, name=None): + constrictor.task.Task.__init__(self, self.__class__.__name__) + + def run(self, **kw): + # TODO: use a generated date + log.log_info("ItemInfoTask: %s" % copy_barcode) + msg = '1720060110 215612AO%s|AB%s|' % (institution, copy_barcode) + client.send_msg(msg) + data = client.recv_msg() + # TODO: check for valid response + log.log_info('SIP2 item info response: %s' % data) + return data + + return ItemInfoTask().start() + + diff --git a/contrib/sip2/sip2_item_info_endurance.py b/contrib/sip2/sip2_item_info_endurance.py new file mode 100644 index 000000000..e71e88289 --- /dev/null +++ b/contrib/sip2/sip2_item_info_endurance.py @@ -0,0 +1,49 @@ +# ----------------------------------------------------------------------- +# Copyright (C) 2010 Equinox Software, Inc +# Bill Erickson +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# ----------------------------------------------------------------------- + +import constrictor.script +import constrictor.properties +import constrictor.log as log +import sip2_client + + +class SIP2ItemInfoEnduranceScript(constrictor.script.Script): + + def __init__(self): + constrictor.script.Script.__init__(self) + + def run(self): + props = constrictor.properties.Properties.get_properties() + + username = props.get_property('sip2.username') + password = props.get_property('sip2.password') + institution = props.get_property('sip2.institution') + server = props.get_property('sip2.server') + port = int(props.get_property('sip2.port')) + + barcodes = props.get_property('sip2.copyBarcodes').split(',') + copy_barcode = barcodes[constrictor.script.ScriptThread.get_thread_id()] + + client = sip2_client.SIP2Client(server, port) + client.init_socket() + + if client.login(username, password, institution): + for i in range(100): + if not client.item_info_request(institution, copy_barcode): + break + +constrictor.script.ScriptManager.go(SIP2ItemInfoEnduranceScript()) + + -- 2.43.2