From 37123733790f1525161256cfabd1da877bf37e74 Mon Sep 17 00:00:00 2001 From: Cameron Patrick Date: Sun, 1 Aug 2004 07:00:15 +0000 Subject: [PATCH] fix bugs in last few commits --- sql-edition/servers/LATClient.py | 64 +++++++++++++++------------ sql-edition/servers/VendingMachine.py | 1 + 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/sql-edition/servers/LATClient.py b/sql-edition/servers/LATClient.py index 12cc47c..ce77970 100644 --- a/sql-edition/servers/LATClient.py +++ b/sql-edition/servers/LATClient.py @@ -17,27 +17,27 @@ def read_for_a_bit(rfh): while 1: r = select([rfh], [], [], 5.0)[0] if r: - message = message + rfh.read(1) + try: + ch = rfh.read(1) + except socket.error: + ch = '' + if ch == '': + break + message = message + ch else: break print "Received message: ", repr(message) return message -def write_expecting_echo(rfh, wfh, message): +def write_and_get_response(rfh, wfh, message, expect_echo=True): print "Writing message:", repr(message) - while len(message) > 0: - wfh.write(message[0]) - wfh.flush() - if message[0] == '\n': - echo = rfh.read(2) - expected_echo = '\r\n' - else: - echo = rfh.read(1) - expected_echo = message[0] - if echo != expected_echo: - print "Gah, expected echo when writing", repr(message[0]), "but got", repr(echo) - message = message[1:] - print " --> Sent and echoed." + wfh.write(message+'\r\n') + wfh.flush() + print " --> Sent" + response = read_for_a_bit(rfh) + if response.find(message) == -1 and expect_echo: + raise LATClientException("Talking to DEC server, expected to find original message in echo but didn't") + return response class LATClient: def __init__(self, service = None, node = None, port = None, @@ -98,19 +98,27 @@ class LATClient: self.sock.shutdown(2) self.sock.close() mopw, mopr = popen4('/usr/sbin/moprc '+self.server_name) - read_for_a_bit(mopr) - mopw.write('\n') - read_for_a_bit(mopr) - mopw.write(self.connect_password+'\n') - read_for_a_bit(mopr) - write_expecting_echo(mopr, mopw, 'grim reaper\n') - read_for_a_bit(mopr) - write_expecting_echo(mopr, mopw, 'set priv\n') - read_for_a_bit(mopr) - write_expecting_echo(mopr, mopw, self.priv_password+'\n') - read_for_a_bit(mopr) - write_expecting_echo(mopr, mopw, 'init del 0\n') - read_for_a_bit(mopr) + write_and_get_response(mopr, mopw, '') + r = write_and_get_response(mopr, mopw, self.connect_password, False) + if r.find('Enter username> ') == -1: + print "Expected username prompt, got ", repr(r) + raise LATClientException('failed to reboot server') + r = write_and_get_response(mopr, mopw, 'grim reaper') + if r.find('Local> ') == -1: + print "Expected DEC server prompt, got ", repr(r) + raise LATClientException('failed to reboot server') + r = write_and_get_response(mopr, mopw, 'set priv') + if r.find('Password> ') == -1: + print "Expected priv password prompt, got ", repr(r) + raise LATClientException('failed to reboot server') + r = write_and_get_response(mopr, mopw, self.priv_password, False) + if r.find('Local> ') == -1: + print "Expected DEC server prompt, got ", repr(r) + raise LATClientException('failed to reboot server') + r = write_and_get_response(mopr, mopw, 'init del 0') + if r.find('Target does not respond') == -1: + print "Expected DEC server to die, got ", repr(r) + raise LATClientException('failed to reboot server') mopr.close() mopw.close() print diff --git a/sql-edition/servers/VendingMachine.py b/sql-edition/servers/VendingMachine.py index d86cc3f..9506daf 100644 --- a/sql-edition/servers/VendingMachine.py +++ b/sql-edition/servers/VendingMachine.py @@ -2,6 +2,7 @@ import re from CRC import do_crc from select import select +import socket asynchronous_responses = [ '400', '401', # door open/closed '610', # switches changed -- 2.20.1