grandfather clock
[uccvend-vendserver.git] / sql-edition / servers / DoorClient.py
1 #!/usr/bin/python
2
3 from LATClient import LATClient
4 from select import select
5 import signal
6 import sys
7
8 def check_door_service(service, test_string="got wombles?"):
9         latclient = LATClient(service=sys.argv[1])
10         latclient.sock.send(test_string)
11         rfh, wfh = latclient.get_fh()
12         wfh.write(test_string)
13         wfh.flush()
14         rr, wr, er = select([rfh], [], [], 10.0)
15         if rfh not in rr: return "open"
16         recv = rfh.read(len(test_string))
17         if recv <> test_string: return "error"
18         return "closed"
19
20 if __name__ == '__main__':
21         result_codes = { 'open' : 0, 'closed' : 1, 'error' : 2, 'invalid args' : 3}
22         def return_result(result):
23                 print result
24                 sys.exit(result_codes[result])
25         def timeout(signum, frame):
26                 return_result("error")
27         if len(sys.argv) <> 2: return_result('invalid args')
28         signal.signal(signal.SIGALRM, timeout)
29         signal.alarm(15)
30         return_result(check_door_service(sys.argv[1]))

UCC git Repository :: git.ucc.asn.au